Class: ChefVPCToolkit::CloudServersVPC::Client
- Inherits:
-
Object
- Object
- ChefVPCToolkit::CloudServersVPC::Client
- 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
-
#cache_file ⇒ Object
Returns the value of attribute cache_file.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#server_group_id ⇒ Object
Returns the value of attribute server_group_id.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
- .create(server_group, client_name, cache_to_disk = true) ⇒ Object
- .data_dir ⇒ Object
- .data_dir=(dir) ⇒ Object
-
.fetch(options = {}) ⇒ Object
Fetch a client.
- .from_xml(xml) ⇒ Object
Instance Method Summary collapse
- #cache_to_disk ⇒ Object
- #delete ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#poll_until_online(options = {}) ⇒ Object
Poll the server group until it is online.
- #to_xml ⇒ Object
- #vpn_network_interfaces ⇒ Object
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(={}) @id=[:id].to_i @name=[:name] @description=[:description] if [:status] @status=[:status] else @status = "Pending" end @status=[:status] or @status = "Pending" @server_group_id=[:server_group_id] if [:cache_file] then @cache_file=[:cache_file] else @cache_file=[:server_group_id] end @vpn_network_interfaces=[] end |
Instance Attribute Details
#cache_file ⇒ Object
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 |
#description ⇒ Object
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 |
#id ⇒ Object
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 |
#name ⇒ Object
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_id ⇒ Object
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 |
#status ⇒ Object
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_dir ⇒ Object
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( = {}) source = [:source] or source = "remote" if source == "remote" then id=[:id] or raise "Please specify a Client ID." xml=Connection.get("/clients/#{id}.xml") Client.from_xml(xml) elsif source == "cache" then id=[: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_disk ⇒ Object
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 |
#delete ⇒ Object
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(={}) timeout=[: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_xml ⇒ Object
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_interfaces ⇒ Object
43 44 45 |
# File 'lib/chef-vpc-toolkit/cloud-servers-vpc/client.rb', line 43 def vpn_network_interfaces @vpn_network_interfaces end |