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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



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

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



101
102
103
# File 'lib/fog/hp/models/compute_v2/server.rb', line 101

def network_name
  @network_name ||= get_first_network_with_public_ip
end

#passwordObject (readonly)

Returns the value of attribute password.



38
39
40
# File 'lib/fog/hp/models/compute_v2/server.rb', line 38

def password
  @password
end

#private_keyObject



129
130
131
# File 'lib/fog/hp/models/compute_v2/server.rb', line 129

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

#private_key_pathObject



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

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

#public_keyObject



157
158
159
# File 'lib/fog/hp/models/compute_v2/server.rb', line 157

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

#public_key_pathObject



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

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

#usernameObject



310
311
312
# File 'lib/fog/hp/models/compute_v2/server.rb', line 310

def username
  @username ||= 'ubuntu'
end

Instance Method Details

#add_security_group(sg_name) ⇒ Object



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

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



179
180
181
# File 'lib/fog/hp/models/compute_v2/server.rb', line 179

def block_device_mapping=(new_block_device_mapping)
  @block_device_mapping = new_block_device_mapping
end

#console_output(num_lines) ⇒ Object



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

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



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

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



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

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

#flavorObject



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

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

#imageObject



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

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

#key_pairObject



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

def key_pair
  requires :key_name

  service.key_pairs.get(key_name)
end

#key_pair=(new_keypair) ⇒ Object



97
98
99
# File 'lib/fog/hp/models/compute_v2/server.rb', line 97

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

#max_count=(new_max_count) ⇒ Object



175
176
177
# File 'lib/fog/hp/models/compute_v2/server.rb', line 175

def max_count=(new_max_count)
  @max_count = new_max_count
end

#metadataObject



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

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

#metadata=(new_metadata = {}) ⇒ Object



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

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

#min_count=(new_min_count) ⇒ Object



171
172
173
# File 'lib/fog/hp/models/compute_v2/server.rb', line 171

def min_count=(new_min_count)
  @min_count = new_min_count
end

#network_namesObject

derive the network names from the addresses hash



106
107
108
# File 'lib/fog/hp/models/compute_v2/server.rb', line 106

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

#networks=(new_networks) ⇒ Object



183
184
185
# File 'lib/fog/hp/models/compute_v2/server.rb', line 183

def networks=(new_networks)
  @networks = new_networks
end

#private_ip_addressObject



120
121
122
# File 'lib/fog/hp/models/compute_v2/server.rb', line 120

def private_ip_address
  private_ip_addresses.first if private_ip_addresses
end

#private_ip_addressesObject



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

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



148
149
150
# File 'lib/fog/hp/models/compute_v2/server.rb', line 148

def public_ip_address
  public_ip_addresses.first if public_ip_addresses
end

#public_ip_addressesObject



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

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)


187
188
189
# File 'lib/fog/hp/models/compute_v2/server.rb', line 187

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

#reboot(type = 'SOFT') ⇒ Object



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

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

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



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

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

#remove_security_group(sg_name) ⇒ Object



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

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

#saveObject

Raises:

  • (Fog::Errors::Error)


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

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



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

def setup(credentials = {})
  requires :ssh_ip_address, :identity, :public_key, :username
  Fog::SSH.new(ssh_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



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

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

#user_data=(ascii_userdata) ⇒ Object



67
68
69
# File 'lib/fog/hp/models/compute_v2/server.rb', line 67

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

#vnc_console_url(type = 'novnc') ⇒ Object



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

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



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

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

#windows_passwordObject



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

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