Class: Fog::Compute::OpenStack::Server

Inherits:
Server show all
Defined in:
lib/fog/openstack/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

Methods inherited from Model

#inspect, #reload, #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.



30
31
32
33
34
# File 'lib/fog/openstack/models/compute/server.rb', line 30

def initialize(attributes={})
  @connection = attributes[:connection]
  attributes[:metadata] = {}
  super
end

Instance Attribute Details

#flavor_refObject



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

def flavor_ref
  @flavor_ref
end

#image_refObject



92
93
94
# File 'lib/fog/openstack/models/compute/server.rb', line 92

def image_ref
  @image_ref
end

#passwordObject (readonly)

Returns the value of attribute password.



27
28
29
# File 'lib/fog/openstack/models/compute/server.rb', line 27

def password
  @password
end

#private_keyObject



75
76
77
# File 'lib/fog/openstack/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/openstack/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



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

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

#public_key_pathObject



83
84
85
86
# File 'lib/fog/openstack/models/compute/server.rb', line 83

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

#usernameObject



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

def username
  @username ||= 'root'
end

Instance Method Details

#change_password(admin_password) ⇒ Object



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

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

#confirm_resizeObject



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

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

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



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

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

#destroyObject



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

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

#imagesObject



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

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

#metadataObject



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

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

#metadata=(new_metadata = {}) ⇒ Object



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

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

#private_ip_addressObject



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

def private_ip_address
  nil
end

#public_ip_addressObject



79
80
81
# File 'lib/fog/openstack/models/compute/server.rb', line 79

def public_ip_address
  addresses['public'].first
end

#ready?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/fog/openstack/models/compute/server.rb', line 108

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

#reboot(type = 'SOFT') ⇒ Object



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

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

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



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

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

#resize(flavor_ref) ⇒ Object



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

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

#revert_resizeObject



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

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

#saveObject

Raises:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fog/openstack/models/compute/server.rb', line 153

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_ref, :image_ref, :name
  meta_hash = {}
  .each { |meta| meta_hash.store(meta.key, meta.value) }
  options = {
    'metadata'    => meta_hash,
    'personality' => personality,
    'accessIPv4' => accessIPv4,
    'accessIPv6' => accessIPv6,
    'availability_zone' => availability_zone,
    'user_data' => user_data_encoded
  }
  options = options.reject {|key, value| value.nil?}
  data = connection.create_server(name, image_ref, flavor_ref, options)
  merge_attributes(data.body['server'])
  true
end

#setup(credentials = {}) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fog/openstack/models/compute/server.rb', line 172

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 "#{MultiJson.encode(attributes)}" >> ~/attributes.json},
    %{echo "#{MultiJson.encode()}" >> ~/metadata.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#user_data=(ascii_userdata) ⇒ Object



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

def user_data=(ascii_userdata)
  self.user_data_encoded = [ascii_userdata].pack('m')
end