Class: Fog::Compute::HP::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



40
41
42
43
44
45
46
# File 'lib/fog/hp/models/compute/server.rb', line 40

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)
  super
end

Instance Attribute Details

#flavor_idObject



160
161
162
# File 'lib/fog/hp/models/compute/server.rb', line 160

def flavor_id
  @flavor_id ||= (flavor.nil? ? nil : flavor["id"])
end

#image_idObject



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

def image_id
  @image_id ||= (image.nil? ? nil : image["id"])
end

#network_nameObject



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

def network_name
  @network_name ||= "private"
end

#passwordObject (readonly)

Returns the value of attribute password.



35
36
37
# File 'lib/fog/hp/models/compute/server.rb', line 35

def password
  @password
end

#private_keyObject



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

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

#private_key_pathObject



115
116
117
118
# File 'lib/fog/hp/models/compute/server.rb', line 115

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

#public_keyObject



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

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

#public_key_pathObject



143
144
145
146
# File 'lib/fog/hp/models/compute/server.rb', line 143

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

#usernameObject



281
282
283
# File 'lib/fog/hp/models/compute/server.rb', line 281

def username
  @username ||= 'root'
end

Instance Method Details

#block_device_mapping=(new_block_device_mapping) ⇒ Object



176
177
178
# File 'lib/fog/hp/models/compute/server.rb', line 176

def block_device_mapping=(new_block_device_mapping)
  @block_device_mapping = new_block_device_mapping
end

#change_password(admin_password) ⇒ Object



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

def change_password(admin_password)
  requires :id
  service.change_password_server(id, admin_password)
  true
end

#confirm_resizeObject



219
220
221
222
223
# File 'lib/fog/hp/models/compute/server.rb', line 219

def confirm_resize
  requires :id
  service.confirm_resized_server(id)
  true
end

#console_output(num_lines) ⇒ Object



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

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

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



225
226
227
228
# File 'lib/fog/hp/models/compute/server.rb', line 225

def create_image(name, ={})
  requires :id
  service.create_image(id, name, )
end

#destroyObject



81
82
83
84
85
# File 'lib/fog/hp/models/compute/server.rb', line 81

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

#key_pairObject



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

def key_pair
  requires :key_name

  service.key_pairs.get(key_name)
end

#key_pair=(new_keypair) ⇒ Object



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

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

#max_count=(new_max_count) ⇒ Object



172
173
174
# File 'lib/fog/hp/models/compute/server.rb', line 172

def max_count=(new_max_count)
  @max_count = new_max_count
end

#metadataObject



48
49
50
51
52
53
54
55
# File 'lib/fog/hp/models/compute/server.rb', line 48

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

#metadata=(new_metadata = {}) ⇒ Object



57
58
59
60
61
# File 'lib/fog/hp/models/compute/server.rb', line 57

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

#min_count=(new_min_count) ⇒ Object



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

def min_count=(new_min_count)
  @min_count = new_min_count
end

#private_ip_addressObject



111
112
113
# File 'lib/fog/hp/models/compute/server.rb', line 111

def private_ip_address
  private_ip_addresses.first
end

#private_ip_addressesObject



101
102
103
104
105
106
107
108
109
# File 'lib/fog/hp/models/compute/server.rb', line 101

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



139
140
141
# File 'lib/fog/hp/models/compute/server.rb', line 139

def public_ip_address
  public_ip_addresses.first
end

#public_ip_addressesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fog/hp/models/compute/server.rb', line 124

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)


180
181
182
# File 'lib/fog/hp/models/compute/server.rb', line 180

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

#reboot(type = 'SOFT') ⇒ Object



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

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

#rebuild(image_id, name, admin_pass = nil, metadata = nil, personality = nil) ⇒ Object



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

def rebuild(image_id, name, admin_pass=nil, =nil, personality=nil)
  requires :id
  service.rebuild_server(id, image_id, name, admin_pass, , personality)
  true
end

#resize(flavor_id) ⇒ Object



207
208
209
210
211
# File 'lib/fog/hp/models/compute/server.rb', line 207

def resize(flavor_id)
  requires :id
  service.resize_server(id, flavor_id)
  true
end

#revert_resizeObject



213
214
215
216
217
# File 'lib/fog/hp/models/compute/server.rb', line 213

def revert_resize
  requires :id
  service.revert_resized_server(id)
  true
end

#saveObject

Raises:

  • (Fog::Errors::Error)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fog/hp/models/compute/server.rb', line 237

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
  }
  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



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/fog/hp/models/compute/server.rb', line 267

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

#user_data=(ascii_userdata) ⇒ Object



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

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

#vnc_console_url(type = 'novnc') ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/fog/hp/models/compute/server.rb', line 72

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



230
231
232
233
234
235
# File 'lib/fog/hp/models/compute/server.rb', line 230

def volume_attachments
  requires :id
  if vols = service.list_server_volumes(id).body
    vols["volumeAttachments"]
  end
end

#windows_passwordObject



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

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