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

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

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #ssh_port, #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 Attributes::InstanceMethods

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

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



36
37
38
39
40
41
42
# File 'lib/fog/hp/models/compute/server.rb', line 36

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

Instance Attribute Details

#flavor_idObject



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

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

#image_idObject



104
105
106
# File 'lib/fog/hp/models/compute/server.rb', line 104

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

#passwordObject (readonly)

Returns the value of attribute password.



33
34
35
# File 'lib/fog/hp/models/compute/server.rb', line 33

def password
  @password
end

#private_keyObject



75
76
77
# File 'lib/fog/hp/models/compute/server.rb', line 75

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

#private_key_pathObject



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

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

#public_keyObject



100
101
102
# File 'lib/fog/hp/models/compute/server.rb', line 100

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

#public_key_pathObject



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

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

#usernameObject



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

def username
  @username ||= 'root'
end

Instance Method Details

#change_password(admin_password) ⇒ Object



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

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

#confirm_resizeObject



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

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

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



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

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

#destroyObject



44
45
46
47
48
# File 'lib/fog/hp/models/compute/server.rb', line 44

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

#imagesObject



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

def images
  requires :id
  connection.images(:server => self)
end

#key_pairObject



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

def key_pair
  requires :key_name

  connection.key_pairs.get(key_name)
end

#key_pair=(new_keypair) ⇒ Object



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

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

#max_count=(new_max_count) ⇒ Object



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

def max_count=(new_max_count)
  @max_count = new_max_count
end

#min_count=(new_min_count) ⇒ Object



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

def min_count=(new_min_count)
  @min_count = new_min_count
end

#private_ip_addressObject



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

def private_ip_address
  addr = addresses.nil? ? nil : addresses.fetch('private', []).first
  addr["addr"] if addr
end

#public_ip_addressObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fog/hp/models/compute/server.rb', line 79

def public_ip_address
  # FIX: Both the private and public ips are bundled under "private" network name
  # So hack to get to the public ip address
  if !addresses.nil?
    addr = addresses.fetch('private', [])
    # if we have more than 1 address, then the return the second address which is public
    if addr.count > 1
      addr[1]["addr"]
    else
      nil
    end
  else
    nil
  end
end

#ready?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/fog/hp/models/compute/server.rb', line 136

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

#reboot(type = 'SOFT') ⇒ Object



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

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

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



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

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

#resize(flavor_id) ⇒ Object



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

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

#revert_resizeObject



164
165
166
167
168
# File 'lib/fog/hp/models/compute/server.rb', line 164

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

#saveObject

Raises:



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fog/hp/models/compute/server.rb', line 181

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_id, :image_id, :name
  options = {
    'metadata'    => ,
    'personality' => personality,
    'accessIPv4'  => accessIPv4,
    'accessIPv6'  => accessIPv6,
    'min_count'   => @min_count,
    'max_count'   => @max_count,
    'key_name'    => key_name,
    'security_groups' => @security_groups
  }
  options = options.reject {|key, value| value.nil?}
  data = connection.create_server(name, flavor_id, image_id, options)
  merge_attributes(data.body['server'])
  true
end

#security_groupsObject



132
133
134
# File 'lib/fog/hp/models/compute/server.rb', line 132

def security_groups   
  @security_groups
end

#security_groups=(new_security_groups) ⇒ Object



128
129
130
# File 'lib/fog/hp/models/compute/server.rb', line 128

def security_groups=(new_security_groups)
  @security_groups = new_security_groups
end

#setup(credentials = {}) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fog/hp/models/compute/server.rb', line 200

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