Class: Kytoon::Providers::CloudServersVPC::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kytoon/providers/cloud_servers_vpc/client.rb

Constant Summary collapse

@@data_dir =
File.join(KYTOON_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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 26

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.



24
25
26
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 24

def cache_file
  @cache_file
end

#descriptionObject

Returns the value of attribute description.



21
22
23
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 21

def description
  @description
end

#idObject

Returns the value of attribute id.



19
20
21
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 19

def id
  @id
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 20

def name
  @name
end

#server_group_idObject

Returns the value of attribute server_group_id.



23
24
25
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 23

def server_group_id
  @server_group_id
end

#statusObject

Returns the value of attribute status.



22
23
24
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 22

def status
  @status
end

Class Method Details

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 151

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



11
12
13
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 11

def self.data_dir
  @@data_dir
end

.data_dir=(dir) ⇒ Object



15
16
17
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 15

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

.from_xml(xml) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 64

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

.get(options = {}) ⇒ Object

Get a client. The following options are available:

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 172

def self.get(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 get :source specified."
  end

end

Instance Method Details

#cache_to_diskObject



49
50
51
52
53
54
55
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 49

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



57
58
59
60
61
62
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 57

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


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
148
149
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 122

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.get(: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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 91

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



45
46
47
# File 'lib/kytoon/providers/cloud_servers_vpc/client.rb', line 45

def vpn_network_interfaces
  @vpn_network_interfaces
end