Class: Fog::Libvirt::Compute::Server
- Inherits:
-
Compute::Server
- Object
- Compute::Server
- Fog::Libvirt::Compute::Server
show all
- Includes:
- Util
- Defined in:
- lib/fog/libvirt/models/compute/server.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#randomized_name, #to_xml, #xml_element, #xml_elements
Constructor Details
#initialize(attributes = {}) ⇒ Server
Can be created by passing in :xml => “<xml to create domain/server>” or by providing :template_options => {
:name => "", :cpus => 1, :memory_size => 256 , :volume_template
}
49
50
51
52
53
54
55
56
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 49
def initialize(attributes={} )
@xml = attributes.delete(:xml)
verify_boot_order(attributes[:boot_order])
super defaults.merge(attributes)
initialize_nics
initialize_volumes
@user_data = attributes.delete(:user_data)
end
|
Instance Attribute Details
#iso_dir ⇒ Object
The following attributes are only needed when creating a new vm TODO: Add depreciation warning
38
39
40
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 38
def iso_dir
@iso_dir
end
|
#iso_file ⇒ Object
The following attributes are only needed when creating a new vm TODO: Add depreciation warning
38
39
40
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 38
def iso_file
@iso_file
end
|
#network_bridge_name ⇒ Object
Returns the value of attribute network_bridge_name.
39
40
41
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 39
def network_bridge_name
@network_bridge_name
end
|
#network_interface_type ⇒ Object
Returns the value of attribute network_interface_type.
39
40
41
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 39
def network_interface_type
@network_interface_type
end
|
#network_nat_network ⇒ Object
Returns the value of attribute network_nat_network.
39
40
41
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 39
def network_nat_network
@network_nat_network
end
|
#password ⇒ Object
Returns the value of attribute password.
41
42
43
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 41
def password
@password
end
|
#user_data ⇒ Object
Returns the value of attribute user_data.
42
43
44
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 42
def user_data
@user_data
end
|
#volume_allocation ⇒ Object
Returns the value of attribute volume_allocation.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_allocation
@volume_allocation
end
|
#volume_capacity ⇒ Object
Returns the value of attribute volume_capacity.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_capacity
@volume_capacity
end
|
Returns the value of attribute volume_format_type.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_format_type
@volume_format_type
end
|
#volume_name ⇒ Object
Returns the value of attribute volume_name.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_name
@volume_name
end
|
#volume_path ⇒ Object
Returns the value of attribute volume_path.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_path
@volume_path
end
|
#volume_pool_name ⇒ Object
Returns the value of attribute volume_pool_name.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_pool_name
@volume_pool_name
end
|
#volume_template_name ⇒ Object
Returns the value of attribute volume_template_name.
40
41
42
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 40
def volume_template_name
@volume_template_name
end
|
#xml ⇒ Object
Returns the value of attribute xml.
10
11
12
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 10
def xml
@xml
end
|
Instance Method Details
#cloud_init_volume_name ⇒ Object
256
257
258
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 256
def cloud_init_volume_name
"#{name}-cloud-init.iso"
end
|
#create_user_data_iso ⇒ Object
247
248
249
250
251
252
253
254
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 247
def create_user_data_iso
generate_config_iso(user_data) do |iso|
vol = service.volumes.create(:name => cloud_init_volume_name, :capacity => "#{File.size(iso)}b", :allocation => "0G")
vol.upload_image(iso)
@iso_file = cloud_init_volume_name
@iso_dir = File.dirname(vol.path) if vol.path
end
end
|
#destroy(options = { :destroy_volumes => false}) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 88
def destroy(options={ :destroy_volumes => false})
poweroff unless stopped?
service.vm_action(uuid, :undefine)
volumes.each { |vol| vol.destroy } if options[:destroy_volumes]
true
end
|
#disk_path ⇒ Object
84
85
86
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 84
def disk_path
volumes.first.path if volumes and volumes.first
end
|
#generate_config_iso(user_data, &blk) ⇒ Object
230
231
232
233
234
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 230
def generate_config_iso(user_data, &blk)
Dir.mktmpdir('config') do |wd|
generate_config_iso_in_dir(wd, user_data, &blk)
end
end
|
#generate_config_iso_in_dir(dir_path, user_data, &blk) ⇒ Object
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 236
def generate_config_iso_in_dir(dir_path, user_data, &blk)
FileUtils.touch(File.join(dir_path, "meta-data"))
File.open(File.join(dir_path, 'user-data'), 'w') { |f| f.write user_data }
isofile = Tempfile.new(['init', '.iso']).path
unless system("genisoimage -output #{isofile} -volid cidata -joliet -rock #{File.join(dir_path, 'user-data')} #{File.join(dir_path, 'meta-data')}")
raise Fog::Errors::Error.new("Couldn't generate cloud-init iso disk with genisoimage.")
end
blk.call(isofile)
end
|
#mac ⇒ Object
80
81
82
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 80
def mac
nics.first.mac if nics && nics.first
end
|
#new? ⇒ Boolean
58
59
60
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 58
def new?
uuid.nil?
end
|
#poweroff ⇒ Object
Also known as:
halt
101
102
103
104
105
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 101
def poweroff
action_status = service.vm_action(uuid, :destroy)
reload
action_status
end
|
#private_ip_address ⇒ Object
143
144
145
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 143
def private_ip_address
ip_address(:private)
end
|
#public_ip_address ⇒ Object
147
148
149
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 147
def public_ip_address
ip_address(:public)
end
|
#ready? ⇒ Boolean
129
130
131
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 129
def ready?
state == "running"
end
|
#reboot ⇒ Object
95
96
97
98
99
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 95
def reboot
action_status = service.vm_action(uuid, :reboot)
reload
action_status
end
|
#resume ⇒ Object
113
114
115
116
117
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 113
def resume
action_status = service.vm_action(uuid, :resume)
reload
action_status
end
|
#save ⇒ Object
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 62
def save
raise Fog::Errors::Error.new('Saving an existing server may create a duplicate') unless new?
create_or_clone_volume unless xml or @volumes
create_user_data_iso if user_data
@xml ||= to_xml
self.id = (persistent ? service.define_domain(xml) : service.create_domain(xml)).uuid
reload
rescue => e
raise Fog::Errors::Error.new("Error saving the server: #{e}")
end
|
#scp(local_path, remote_path, upload_options = {}) ⇒ Object
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 175
def scp(local_path, remote_path, upload_options = {})
requires :ssh_ip_address, :username
scp_options = {}
scp_options[:password] = password unless self.password.nil?
scp_options[:key_data] = [private_key] if self.private_key
scp_options[:proxy]= ssh_proxy unless self.ssh_proxy.nil?
Fog::SCP.new(ssh_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end
|
#setup(credentials = {}) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 187
def setup(credentials = {})
requires :public_key, :ssh_ip_address, :username
credentials[:proxy]= ssh_proxy unless ssh_proxy.nil?
credentials[:password] = password unless self.password.nil?
credentials[:key_data] = [private_key] if self.private_key
commands = [
%{mkdir .ssh},
]
if public_key
commands << %{echo "#{public_key}" >> ~/.ssh/authorized_keys}
end
Timeout::timeout(360) do
begin
Timeout::timeout(8) do
Fog::SSH.new(ssh_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
end
rescue Errno::ECONNREFUSED
sleep(2)
retry
rescue Net::SSH::AuthenticationFailed, Timeout::Error
retry
end
end
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
end
|
#shutdown ⇒ Object
Also known as:
stop
107
108
109
110
111
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 107
def shutdown
action_status = service.vm_action(uuid, :shutdown)
reload
action_status
end
|
#ssh(commands) ⇒ Object
151
152
153
154
155
156
157
158
159
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 151
def ssh(commands)
requires :ssh_ip_address, :username
ssh_options={}
ssh_options[:password] = password unless password.nil?
ssh_options[:proxy]= ssh_proxy unless ssh_proxy.nil?
super(commands, ssh_options)
end
|
#ssh_proxy ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 161
def ssh_proxy
begin
require 'net/ssh/proxy/command'
rescue LoadError
Fog::Logger.warning("'net/ssh' missing, please install and try again.")
exit(1)
end
return nil unless connection.uri.ssh_enabled?
user_string= service.uri.user ? "-l #{service.uri.user}" : ""
Net::SSH::Proxy::Command.new("ssh #{user_string} #{service.uri.host} nc %h %p")
end
|
#start ⇒ Object
73
74
75
76
77
78
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 73
def start
return true if active?
action_status = service.vm_action(uuid, :create)
reload
action_status
end
|
#stopped? ⇒ Boolean
125
126
127
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 125
def stopped?
state == "shutoff"
end
|
#suspend ⇒ Object
119
120
121
122
123
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 119
def suspend
action_status = service.vm_action(uuid, :suspend)
reload
action_status
end
|
#update_display(attrs = {}) ⇒ Object
219
220
221
222
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 219
def update_display attrs = {}
service.update_display attrs.merge(:uuid => uuid)
reload
end
|
#vnc_port ⇒ Object
can’t use deprecate method, as the value is part of the display hash
225
226
227
228
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 225
def vnc_port
Fog::Logger.deprecation("#{self.class} => #vnc_port is deprecated, use #display[:port] instead [light_black](#{caller.first})[/]")
display[:port]
end
|
#volumes ⇒ Object
138
139
140
141
|
# File 'lib/fog/libvirt/models/compute/server.rb', line 138
def volumes
@volumes ||= (@volumes_path || []).map{|path| service.volumes.all(:path => path).first }
end
|