Class: Fog::Compute::HPV2::Server

Inherits:
Server show all
Defined in:
lib/fog/hp/models/compute_v2/server.rb

Instance Attribute Summary collapse

Attributes inherited from Server

#ssh_options, #ssh_port

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #sshable?

Methods inherited from Model

#inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



45
46
47
48
49
50
51
52
# File 'lib/fog/hp/models/compute_v2/server.rb', line 45

def initialize(attributes = {})
  # assign these attributes first to prevent race condition with new_record?
  self.min_count = attributes.delete(:min_count)
  self.max_count = attributes.delete(:max_count)
  self.block_device_mapping = attributes.delete(:block_device_mapping)
  self.networks = attributes.delete(:networks)
  super
end

Instance Attribute Details

#network_nameObject



103
104
105
# File 'lib/fog/hp/models/compute_v2/server.rb', line 103

def network_name
  @network_name ||= get_first_network_with_public_ip
end

#passwordObject (readonly)

Returns the value of attribute password.



40
41
42
# File 'lib/fog/hp/models/compute_v2/server.rb', line 40

def password
  @password
end

#private_keyObject



131
132
133
# File 'lib/fog/hp/models/compute_v2/server.rb', line 131

def private_key
  @private_key ||= private_key_path && File.read(private_key_path)
end

#private_key_pathObject



126
127
128
129
# File 'lib/fog/hp/models/compute_v2/server.rb', line 126

def private_key_path
  @private_key_path ||= Fog.credentials[:private_key_path]
  @private_key_path &&= File.expand_path(@private_key_path)
end

#public_keyObject



159
160
161
# File 'lib/fog/hp/models/compute_v2/server.rb', line 159

def public_key
  @public_key ||= public_key_path && File.read(public_key_path)
end

#public_key_pathObject



154
155
156
157
# File 'lib/fog/hp/models/compute_v2/server.rb', line 154

def public_key_path
  @public_key_path ||= Fog.credentials[:public_key_path]
  @public_key_path &&= File.expand_path(@public_key_path)
end

#usernameObject



312
313
314
# File 'lib/fog/hp/models/compute_v2/server.rb', line 312

def username
  @username ||= 'ubuntu'
end

Instance Method Details

#add_security_group(sg_name) ⇒ Object



254
255
256
257
258
# File 'lib/fog/hp/models/compute_v2/server.rb', line 254

def add_security_group(sg_name)
  requires :id
  service.add_security_group(id, sg_name)
  true
end

#block_device_mapping=(new_block_device_mapping) ⇒ Object



181
182
183
# File 'lib/fog/hp/models/compute_v2/server.rb', line 181

def block_device_mapping=(new_block_device_mapping)
  @block_device_mapping = new_block_device_mapping
end

#console_output(num_lines) ⇒ Object



73
74
75
76
# File 'lib/fog/hp/models/compute_v2/server.rb', line 73

def console_output(num_lines)
  requires :id
  service.get_console_output(id, num_lines)
end

#create_image(name, metadata = {}) ⇒ Object

def revert_resize

requires :id
service.revert_resized_server(id)
true

end

def confirm_resize

requires :id
service.confirm_resized_server(id)
true

end



228
229
230
231
232
233
234
235
236
237
# File 'lib/fog/hp/models/compute_v2/server.rb', line 228

def create_image(name,  = {})
  requires :id
  response = service.create_image(id, name, )
  begin
    image_id = response.headers["Location"].match(/\/([^\/]+$)/)[1]
    Fog::Compute::HPV2::Image.new(:collection => service.images, :service => service, :id => image_id)
  rescue
    nil
  end
end

#destroyObject



87
88
89
90
91
# File 'lib/fog/hp/models/compute_v2/server.rb', line 87

def destroy
  requires :id
  service.delete_server(id)
  true
end

#flavorObject



163
164
165
166
# File 'lib/fog/hp/models/compute_v2/server.rb', line 163

def flavor
  requires :flavor_id
  @flavor ||= service.flavors.get(flavor_id)
end

#imageObject



168
169
170
171
# File 'lib/fog/hp/models/compute_v2/server.rb', line 168

def image
  requires :image_id
  @image ||= service.images.get(image_id)
end

#key_pairObject



93
94
95
96
97
# File 'lib/fog/hp/models/compute_v2/server.rb', line 93

def key_pair
  requires :key_name

  service.key_pairs.get(key_name)
end

#key_pair=(new_keypair) ⇒ Object



99
100
101
# File 'lib/fog/hp/models/compute_v2/server.rb', line 99

def key_pair=(new_keypair)
  self.key_name = new_keypair && new_keypair.name
end

#max_count=(new_max_count) ⇒ Object



177
178
179
# File 'lib/fog/hp/models/compute_v2/server.rb', line 177

def max_count=(new_max_count)
  @max_count = new_max_count
end

#metadataObject



54
55
56
57
58
59
60
61
# File 'lib/fog/hp/models/compute_v2/server.rb', line 54

def 
  @metadata ||= begin
    Fog::Compute::HPV2::Metadata.new({
      :service => service,
      :parent => self
    })
  end
end

#metadata=(new_metadata = {}) ⇒ Object



63
64
65
66
67
# File 'lib/fog/hp/models/compute_v2/server.rb', line 63

def metadata=(={})
  metas = []
  .each_pair {|k,v| metas << {'key' => k, 'value' => v} }
  .load(metas)
end

#min_count=(new_min_count) ⇒ Object



173
174
175
# File 'lib/fog/hp/models/compute_v2/server.rb', line 173

def min_count=(new_min_count)
  @min_count = new_min_count
end

#network_namesObject

derive the network names from the addresses hash



108
109
110
# File 'lib/fog/hp/models/compute_v2/server.rb', line 108

def network_names
  self.addresses.keys unless self.addresses.nil?
end

#networks=(new_networks) ⇒ Object



185
186
187
# File 'lib/fog/hp/models/compute_v2/server.rb', line 185

def networks=(new_networks)
  @networks = new_networks
end

#private_ip_addressObject



122
123
124
# File 'lib/fog/hp/models/compute_v2/server.rb', line 122

def private_ip_address
  private_ip_addresses.first if private_ip_addresses
end

#private_ip_addressesObject



112
113
114
115
116
117
118
119
120
# File 'lib/fog/hp/models/compute_v2/server.rb', line 112

def private_ip_addresses
  return nil if addresses.nil?
  addr = []
  addresses.each { |key, value|
    ipaddr = value.first
    addr << ipaddr["addr"] unless ipaddr.nil?
  }
  addr
end

#public_ip_addressObject



150
151
152
# File 'lib/fog/hp/models/compute_v2/server.rb', line 150

def public_ip_address
  public_ip_addresses.first if public_ip_addresses
end

#public_ip_addressesObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fog/hp/models/compute_v2/server.rb', line 135

def public_ip_addresses
  return nil if addresses.nil?
  addr = []
  addresses.each { |key, value|
    if value.count > 1
      value = value.dup
      value.delete_at(0)
      value.each { |ipaddr|
        addr << ipaddr["addr"]
      }
    end
  }
  addr
end

#ready?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/fog/hp/models/compute_v2/server.rb', line 189

def ready?
  self.state == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



198
199
200
201
202
# File 'lib/fog/hp/models/compute_v2/server.rb', line 198

def reboot(type = 'SOFT')
  requires :id
  service.reboot_server(id, type)
  true
end

#rebuild(image_id, name, options = {}) ⇒ Object



204
205
206
207
208
# File 'lib/fog/hp/models/compute_v2/server.rb', line 204

def rebuild(image_id, name, options={})
  requires :id
  service.rebuild_server(id, image_id, name, options)
  true
end

#remove_security_group(sg_name) ⇒ Object



260
261
262
263
264
# File 'lib/fog/hp/models/compute_v2/server.rb', line 260

def remove_security_group(sg_name)
  requires :id
  service.remove_security_group(id, sg_name)
  true
end

#saveObject

Raises:



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
# File 'lib/fog/hp/models/compute_v2/server.rb', line 266

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
  requires :flavor_id, :name
  meta_hash = {}
  .each { |meta| meta_hash.store(meta.key, meta.value) }
  options = {
    'metadata'        => meta_hash,
    'personality'     => personality,
    'accessIPv4'      => accessIPv4,
    'accessIPv6'      => accessIPv6,
    'min_count'       => @min_count,
    'max_count'       => @max_count,
    'key_name'        => key_name,
    'security_groups' => security_groups,
    'config_drive'    => config_drive,
    'user_data'       => user_data_encoded,
    'availability_zone'  => availability_zone,
    'networks'        => @networks
  }
  options = options.reject {|key, value| value.nil?}
  # either create a regular server or a persistent server based on input
  if image_id
    # create a regular server using the image
    data = service.create_server(name, flavor_id, image_id, options)
  elsif image_id.nil? && !@block_device_mapping.nil? && !@block_device_mapping.empty?
    # create a persistent server using the bootable volume in the block_device_mapping
    data = service.create_persistent_server(name, flavor_id, @block_device_mapping, options)
  end
  merge_attributes(data.body['server'])
  true
end

#setup(credentials = {}) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/fog/hp/models/compute_v2/server.rb', line 298

def setup(credentials = {})
  requires :public_ip_address, :identity, :public_key, :username
  Fog::SSH.new(public_ip_address, username, credentials).run([
    %{mkdir .ssh},
    %{echo "#{public_key}" >> ~/.ssh/authorized_keys},
    %{passwd -l #{username}},
    %{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
    %{echo "#{Fog::JSON.encode()}" >> ~/metadata.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#update_name(new_name) ⇒ Object



248
249
250
251
252
# File 'lib/fog/hp/models/compute_v2/server.rb', line 248

def update_name(new_name)
  requires :id
  service.update_server(id, :name => new_name)
  true
end

#user_data=(ascii_userdata) ⇒ Object



69
70
71
# File 'lib/fog/hp/models/compute_v2/server.rb', line 69

def user_data=(ascii_userdata)
  self.user_data_encoded = [ascii_userdata].pack('m')  # same as Base64.encode64
end

#vnc_console_url(type = 'novnc') ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/fog/hp/models/compute_v2/server.rb', line 78

def vnc_console_url(type='novnc')
  requires :id
  if resp = service.get_vnc_console(id, type).body
    resp['console']['url']
  else
    nil
  end
end

#volume_attachmentsObject



239
240
241
242
243
244
245
246
# File 'lib/fog/hp/models/compute_v2/server.rb', line 239

def volume_attachments
  @volume_attachments ||= begin
    Fog::Compute::HPV2::VolumeAttachments.new({
      :service => service,
      :server => self
    })
  end
end

#windows_passwordObject



193
194
195
196
# File 'lib/fog/hp/models/compute_v2/server.rb', line 193

def windows_password
  requires :id
  service.get_windows_password(id)
end