Class: HP::Cloud::ServerHelper
- Inherits:
-
BaseHelper
- Object
- BaseHelper
- HP::Cloud::ServerHelper
- Defined in:
- lib/hpcloud/server_helper.rb
Instance Attribute Summary collapse
-
#created ⇒ Object
Returns the value of attribute created.
-
#flavor ⇒ Object
Returns the value of attribute flavor.
-
#id ⇒ Object
Returns the value of attribute id.
-
#image ⇒ Object
Returns the value of attribute image.
-
#ips ⇒ Object
Returns the value of attribute ips.
-
#keyname ⇒ Object
Returns the value of attribute keyname.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#name ⇒ Object
Returns the value of attribute name.
-
#network_name ⇒ Object
Returns the value of attribute network_name.
-
#networks ⇒ Object
Returns the value of attribute networks.
-
#personality ⇒ Object
readonly
Returns the value of attribute personality.
-
#private_ip ⇒ Object
Returns the value of attribute private_ip.
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
-
#public_ip ⇒ Object
Returns the value of attribute public_ip.
-
#region ⇒ Object
Returns the value of attribute region.
-
#security ⇒ Object
Returns the value of attribute security.
-
#security_groups ⇒ Object
Returns the value of attribute security_groups.
-
#state ⇒ Object
Returns the value of attribute state.
-
#tenantid ⇒ Object
Returns the value of attribute tenantid.
-
#volume ⇒ Object
Returns the value of attribute volume.
-
#windows ⇒ Object
readonly
Returns the value of attribute windows.
Attributes inherited from BaseHelper
Class Method Summary collapse
Instance Method Summary collapse
- #add_security_groups(value) ⇒ Object
- #create_image(name, hash) ⇒ Object
- #destroy ⇒ Object
-
#initialize(connection, foggy = nil) ⇒ ServerHelper
constructor
A new instance of ServerHelper.
- #is_private_image? ⇒ Boolean
- #is_windows? ⇒ Boolean
- #remove_security_groups(value) ⇒ Object
- #save ⇒ Object
- #set_flavor(value) ⇒ Object
- #set_image(value) ⇒ Object
- #set_keypair(value) ⇒ Object
- #set_network(value) ⇒ Object
- #set_personality(directory) ⇒ Object
- #set_private_key(value) ⇒ Object
- #set_security_groups(value) ⇒ Object
- #set_user_data(filename) ⇒ Object
- #set_volume(value) ⇒ Object
- #windows_password(retries = 10) ⇒ Object
Methods inherited from BaseHelper
#is_valid?, #set_error, #to_hash
Constructor Details
#initialize(connection, foggy = nil) ⇒ ServerHelper
Returns a new instance of ServerHelper.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hpcloud/server_helper.rb', line 36 def initialize(connection, foggy = nil) super(connection, foggy) @windows = false @private_image = false if foggy.nil? @meta = HP::Cloud::Metadata.new(nil) return end @id = foggy.id @name = foggy.name @flavor = foggy.flavor_id @image = foggy.image_id @public_ip = foggy.public_ip_address @private_ip = foggy.private_ip_address @keyname = foggy.key_name unless foggy.security_groups.nil? @security_groups = foggy.security_groups.map {|sg| sg["name"]}.join(', ') end @created = foggy.created_at @state = foggy.state @tenantid = foggy.tenant_id @network_name = foggy.network_name @networks = "" @ips = "" unless foggy.addresses.nil? begin foggy.addresses.keys.each { |x| @networks += "," unless @networks.empty? @networks += x.to_s foggy.addresses[x].each { |y| unless y["addr"].nil? @ips += "," unless @ips.empty? @ips += y["addr"].to_s end } } rescue Exception => e warn "Error parsing addresses: " + e.to_s end end @meta = HP::Cloud::Metadata.new(foggy.) end |
Instance Attribute Details
#created ⇒ Object
Returns the value of attribute created.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def created @created end |
#flavor ⇒ Object
Returns the value of attribute flavor.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def flavor @flavor end |
#id ⇒ Object
Returns the value of attribute id.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def id @id end |
#image ⇒ Object
Returns the value of attribute image.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def image @image end |
#ips ⇒ Object
Returns the value of attribute ips.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def ips @ips end |
#keyname ⇒ Object
Returns the value of attribute keyname.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def keyname @keyname end |
#meta ⇒ Object
Returns the value of attribute meta.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def @meta end |
#name ⇒ Object
Returns the value of attribute name.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def name @name end |
#network_name ⇒ Object
Returns the value of attribute network_name.
30 31 32 |
# File 'lib/hpcloud/server_helper.rb', line 30 def network_name @network_name end |
#networks ⇒ Object
Returns the value of attribute networks.
30 31 32 |
# File 'lib/hpcloud/server_helper.rb', line 30 def networks @networks end |
#personality ⇒ Object (readonly)
Returns the value of attribute personality.
28 29 30 |
# File 'lib/hpcloud/server_helper.rb', line 28 def personality @personality end |
#private_ip ⇒ Object
Returns the value of attribute private_ip.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def private_ip @private_ip end |
#private_key ⇒ Object (readonly)
Returns the value of attribute private_key.
28 29 30 |
# File 'lib/hpcloud/server_helper.rb', line 28 def private_key @private_key end |
#public_ip ⇒ Object
Returns the value of attribute public_ip.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def public_ip @public_ip end |
#region ⇒ Object
Returns the value of attribute region.
30 31 32 |
# File 'lib/hpcloud/server_helper.rb', line 30 def region @region end |
#security ⇒ Object
Returns the value of attribute security.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def security @security end |
#security_groups ⇒ Object
Returns the value of attribute security_groups.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def security_groups @security_groups end |
#state ⇒ Object
Returns the value of attribute state.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def state @state end |
#tenantid ⇒ Object
Returns the value of attribute tenantid.
30 31 32 |
# File 'lib/hpcloud/server_helper.rb', line 30 def tenantid @tenantid end |
#volume ⇒ Object
Returns the value of attribute volume.
29 30 31 |
# File 'lib/hpcloud/server_helper.rb', line 29 def volume @volume end |
#windows ⇒ Object (readonly)
Returns the value of attribute windows.
28 29 30 |
# File 'lib/hpcloud/server_helper.rb', line 28 def windows @windows end |
Class Method Details
.get_keys ⇒ Object
32 33 34 |
# File 'lib/hpcloud/server_helper.rb', line 32 def self.get_keys() return [ "id", "name", "flavor", "image", "ips", "keyname", "security_groups", "created", "state" ] end |
Instance Method Details
#add_security_groups(value) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/hpcloud/server_helper.rb', line 154 def add_security_groups(value) @connection.request( :body => Fog::JSON.encode({ 'addSecurityGroup' => { 'name' => value}}), :expects => 202, :method => 'POST', :path => "servers/#{@id}/action" ) return true end |
#create_image(name, hash) ⇒ Object
243 244 245 246 247 248 249 250 |
# File 'lib/hpcloud/server_helper.rb', line 243 def create_image(name, hash) resp = @fog.create_image(name , hash) if resp.nil? set_error("Error creating image '#{name}'") return nil end return resp end |
#destroy ⇒ Object
311 312 313 314 315 316 317 318 319 |
# File 'lib/hpcloud/server_helper.rb', line 311 def destroy @fog.destroy unless @fog.nil? begin keypair = KeypairHelper.new(nil) keypair.name = "#{@id}" keypair.private_remove rescue Exception => e end end |
#is_private_image? ⇒ Boolean
307 308 309 |
# File 'lib/hpcloud/server_helper.rb', line 307 def is_private_image? return @private_image end |
#is_windows? ⇒ Boolean
303 304 305 |
# File 'lib/hpcloud/server_helper.rb', line 303 def is_windows? return @windows end |
#remove_security_groups(value) ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'lib/hpcloud/server_helper.rb', line 164 def remove_security_groups(value) @connection.request( :body => Fog::JSON.encode({ 'removeSecurityGroup' => { 'name' => value}}), :expects => 202, :method => 'POST', :path => "servers/#{@id}/action" ) return true end |
#save ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/hpcloud/server_helper.rb', line 252 def save if is_valid? set_error(@meta.cstatus) end return false if is_valid? == false acct = Accounts.new.get(Connection.instance.get_account) @flavor = acct[:options][:preferred_flavor] if @flavor.nil? @image = acct[:options][:preferred_image] if @image.nil? && @volume.nil? if @image.nil? && @volume.nil? set_error("You must specify either an image or a volume to create a server.", :incorrect_usage) return false end if @fog.nil? hsh = {:flavor_id => @flavor, :name => @name, :key_name => @keyname, :security_groups => @security, :metadata => @meta.hsh} hsh[:image_id] = @image unless @image.nil? hsh['networks'] = @networks unless @networks.nil? hsh['user_data'] = @user_data unless @user_data.nil? hsh['personality'] = @personality unless @personality.nil? unless @volume.nil? hsh[:block_device_mapping] = [{ 'volume_size' => '', 'volume_id' => "#{@volume}", 'delete_on_termination' => '0', 'device_name' => 'vda' }] end server = @connection.servers.create(hsh) if server.nil? set_error("Error creating server '#{@name}'") return false end @id = server.id @fog = server begin unless @keypair.nil? @keypair.private_read if @keypair.private_key.nil? @keypair.name = "#{@id}" @keypair.private_add end rescue end return true else raise "Update not implemented" end end |
#set_flavor(value) ⇒ Object
79 80 81 82 83 |
# File 'lib/hpcloud/server_helper.rb', line 79 def set_flavor(value) flav = Flavors.new.get(value, false) @flavor = flav.id return true end |
#set_image(value) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/hpcloud/server_helper.rb', line 85 def set_image(value) return true if value.nil? @windows = false @private_image = false image = Images.new().get(value, false) unless image.is_valid? set_error(image.cstatus) return false end @windows = image.is_windows? @private_image = image.is_private? @image = image.id return true end |
#set_keypair(value) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/hpcloud/server_helper.rb', line 112 def set_keypair(value) if value.nil? return true end @keypair = Keypairs.new.get(value, false) unless @keypair.is_valid? set_error(@keypair.cstatus) return false end @keyname = @keypair.name if @windows begin @private_key = @keypair.private_read rescue end end return true end |
#set_network(value) ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/hpcloud/server_helper.rb', line 190 def set_network(value) return true if value.nil? network = Networks.new.get(value, false) unless network.is_valid? set_error(network.cstatus) return false end @networks = [ network.id.to_s ] return true end |
#set_personality(directory) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/hpcloud/server_helper.rb', line 206 def set_personality(directory) return if directory.nil? @personality = [] unless File.exists?(directory) set_error("Directory does not exists '#{directory}'") return false end Dir.glob("#{directory}/**/*").each { |file| unless File.directory?(file) rfile = file.gsub(directory, '') hsh = { "path" => rfile, "contents" => File.read(file) } @personality << hsh end } end |
#set_private_key(value) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/hpcloud/server_helper.rb', line 174 def set_private_key(value) if value.nil? return true unless @windows return true unless @private_key.nil? set_error("You must specify the private key file if you want to create a windows instance.", :incorrect_usage) return false end begin @private_key = File.read(File.(value)) rescue Exception => e set_error("Error reading private key file '#{value}': " + e.to_s, :incorrect_usage) return false end return true end |
#set_security_groups(value) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/hpcloud/server_helper.rb', line 131 def set_security_groups(value) if value.nil? return true end if value.empty? @security = [] @security_groups = '' return true end begin ray = eval("[\"" + value.gsub(',', '","') + "\"]") if (ray.kind_of? Array) @security = ray @security_groups = value return true end rescue SyntaxError => se rescue NameError => ne end set_error("Invalid security group '#{value}' should be comma separated list", :incorrect_usage) return false end |
#set_user_data(filename) ⇒ Object
201 202 203 204 |
# File 'lib/hpcloud/server_helper.rb', line 201 def set_user_data(filename) return if filename.nil? @user_data = File.new(filename).read end |
#set_volume(value) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hpcloud/server_helper.rb', line 100 def set_volume(value) return true if value.nil? @windows = false # windows not supported right now volume = Volumes.new().get(value, false) unless volume.is_valid? set_error(volume.cstatus) return false end @volume = volume.id return true end |
#windows_password(retries = 10) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/hpcloud/server_helper.rb', line 223 def windows_password(retries=10) begin (0..retries).each { |x| @epass = @fog.windows_password() break unless @epass.empty? sleep (x*10) unless retries == 1 } return "Failed to get password" if @epass.empty? begin private_key = OpenSSL::PKey::RSA.new(@private_key) from_base64 = Base64.decode64(@epass) return private_key.private_decrypt(from_base64).strip rescue Exception => error end return "Failed to decrypt: " + @epass rescue Exception => e return "Error getting windows password: " + e.to_s end end |