Class: ChefVPCToolkit::CloudServersVPC::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb

Constant Summary collapse

@@data_dir =
File.join(CHEF_VPC_PROJECT, "tmp", "clients")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 24

def initialize(options={})
	@id=options[:id].to_i
	@name=options[:name]
	@description=options[:description]
	if options[:status]
		@status=options[:status]
	else
		@status = "Pending"
	end
	@status=options[:status] or @status = "Pending"
	@server_group_id=options[:server_group_id]
	if options[:cache_file] then
		@cache_file=options[:cache_file]
	else
		@cache_file=options[:server_group_id]
	end
	@vpn_network_interfaces=[]
end

Instance Attribute Details

#cache_fileObject

Returns the value of attribute cache_file.



22
23
24
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 22

def cache_file
  @cache_file
end

#descriptionObject

Returns the value of attribute description.



19
20
21
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 19

def description
  @description
end

#idObject

Returns the value of attribute id.



17
18
19
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 17

def id
  @id
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 18

def name
  @name
end

#server_group_idObject

Returns the value of attribute server_group_id.



21
22
23
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 21

def server_group_id
  @server_group_id
end

#statusObject

Returns the value of attribute status.



20
21
22
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 20

def status
  @status
end

Class Method Details

.create(server_group, client_name, cache_to_disk = true) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 149

def self.create(server_group, client_name, cache_to_disk=true)

	xml = Builder::XmlMarkup.new
	xml.client do |client|
		client.name(client_name)
		client.description("Toolkit Client: #{client_name}")
		client.tag! "server-group-id", server_group.id
	end

	xml=Connection.post("/clients.xml", xml.target!)
	client=Client.from_xml(xml)
	client.cache_to_disk if cache_to_disk
	client

end

.data_dirObject



9
10
11
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 9

def self.data_dir
	@@data_dir
end

.data_dir=(dir) ⇒ Object



13
14
15
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 13

def self.data_dir=(dir)
	@@data_dir=dir
end

.fetch(options = {}) ⇒ Object

Fetch a client. The following options are available:

:id - The ID of the client to fetch. :source - valid options are ‘remote’ and ‘cache’



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 170

def self.fetch(options = {})

       source = options[:source] or source = "remote"

       if source == "remote" then
		id=options[:id] or raise "Please specify a Client ID."
		xml=Connection.get("/clients/#{id}.xml")
		Client.from_xml(xml)
	elsif source == "cache" then
		id=options[:id] or id = ENV['GROUP_ID']
		client_xml_file=File.join(@@data_dir, "#{id}.xml")
		raise "No client files exist." if not File.exists?(client_xml_file)
		Client.from_xml(IO.read(client_xml_file))
	else
		raise "Invalid fetch :source specified."
	end

end

.from_xml(xml) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 62

def self.from_xml(xml)
	client=nil
	dom = REXML::Document.new(xml)
	REXML::XPath.each(dom, "/client") do |cxml|

		client=Client.new(
			:id => XMLUtil.element_text(cxml,"id").to_i,
			:name => XMLUtil.element_text(cxml, "name"),
			:description => XMLUtil.element_text(cxml,"description"),
			:status => XMLUtil.element_text(cxml,"status"),
			:server_group_id => XMLUtil.element_text(cxml, "server-group-id").to_i
		)
		REXML::XPath.each(dom, "//vpn-network-interface") do |vni|
			vni = VpnNetworkInterface.new(
				:id => XMLUtil.element_text(vni, "id"),
				:vpn_ip_addr => XMLUtil.element_text(vni, "vpn-ip-addr"),
				:ptp_ip_addr => XMLUtil.element_text(vni, "ptp-ip-addr"),
				:client_key => XMLUtil.element_text(vni, "client-key"),
				:client_cert => XMLUtil.element_text(vni, "client-cert"),
				:ca_cert => XMLUtil.element_text(vni, "ca-cert")
			)
			client.vpn_network_interfaces << vni
		end
	end
	client
end

Instance Method Details

#cache_to_diskObject



47
48
49
50
51
52
53
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 47

def cache_to_disk
    FileUtils.mkdir_p(@@data_dir)
    File.open(File.join(@@data_dir, "#{@cache_file}.xml"), 'w') do |f|
        f.chmod(0600)
        f.write(self.to_xml)
    end
end

#deleteObject



55
56
57
58
59
60
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 55

def delete
	client_xml_file=File.join(@@data_dir, "#{@cache_file}.xml")
       if File.exists?(client_xml_file) then
           File.delete(client_xml_file)
       end
end

#poll_until_online(options = {}) ⇒ Object

Poll the server group until it is online. :timeout - max number of seconds to wait before raising an exception.

Defaults to 1500


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 120

def poll_until_online(options={})

       timeout=options[:timeout] or timeout = ENV['VPN_CLIENT_TIMEOUT']
       if timeout.nil? or timeout.empty? then
           timeout=300 # defaults to 5 minutes
       end 

	online = false
	count=0
	until online or (count*5) >= timeout.to_i do
		count+=1
		begin
			client=Client.fetch(:id => @id, :source => "remote")

			if client.status == "Online" then
				online = true
			else
				yield client if block_given?
				sleep 5
			end
		rescue EOFError
		end
	end
	if (count*20) >= timeout.to_i then
		raise "Timeout waiting for client to come online."
	end

end

#to_xmlObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 89

def to_xml

       xml = Builder::XmlMarkup.new
       xml.tag! "client" do |sg|
           sg.id(@id)
           sg.name(@name)
           sg.description(@description)
           sg.status(@status)
           sg.tag! "server-group-id", @server_group_id
           sg.tag! "vpn-network-interfaces", {"type" => "array"} do |interfaces|
			@vpn_network_interfaces.each do |vni|
				interfaces.tag! "vpn-network-interface" do |xml_vni|
					xml_vni.id(vni.id)
					xml_vni.tag! "vpn-ip-addr", vni.vpn_ip_addr
					xml_vni.tag! "ptp-ip-addr", vni.ptp_ip_addr
					xml_vni.tag! "client-key", vni.client_key
					xml_vni.tag! "client-cert", vni.client_cert
					xml_vni.tag! "ca-cert", vni.ca_cert
				end	
			end
		end

	end
       xml.target!

end

#vpn_network_interfacesObject



43
44
45
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 43

def vpn_network_interfaces
	@vpn_network_interfaces
end