142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
218
219
220
|
# File 'lib/chef/knife/kvm_vm_create.rb', line 142
def run
$stdout.sync = true
unless config[:vm_disk]
ui.error("You have not provided a valid QCOW2 file. (--vm-disk)")
exit 1
end
if not File.exist?(config[:vm_disk])
ui.error("Invalid QCOW2 disk file (--vm-disk)")
exit 1
end
vm_name = config[:vm_name]
if not vm_name
ui.error("Invalid Virtual Machine name (--vm-name)")
exit 1
end
pool = config[:pool]
memory = config[:memory]
vm_disk = config[:vm_disk]
os_type =config[:os_type]
destination_path = "/var/lib/libvirt/images/"
puts "#{ui.color("Creating VM... ", :magenta)}"
net_type, net_if = config[:network_interface].split(':')
vm = connection.servers.create :name => vm_name,
:volume_allocation => "#{File.size(vm_disk)/1024/1024}M",
:volume_capacity => '10G',
:volume_format_type => 'qcow2',
:volume_pool_name => pool,
:network_interface_type => net_type,
:memory_size => memory.to_i * 1024,
:network_bridge_name => net_if
puts "#{ui.color("Importing VM disk... ", :magenta)}"
upload_file(vm_disk, "#{destination_path}/#{vm_name}.qcow2")
vm.start
puts "#{ui.color("VM Name", :cyan)}: #{vm.name}"
puts "#{ui.color("VM Memory", :cyan)}: #{vm.memory_size.to_i.kilobytes.to.megabytes.round} MB"
print "\n#{ui.color("Waiting server... ", :magenta)}"
timeout = 100
found = connection.servers.all.find { |v| v.name == vm.name }
loop do
begin
if not vm.addresses.nil? and not vm.addresses.empty?
puts
puts "\n#{ui.color("VM IP Address: #{vm.public_ip_address}", :cyan)}"
break
end
rescue Fog::Errors::Error
print "\r#{ui.color('Waiting a valid IP', :magenta)}..." + "." * (100 - timeout)
end
timeout -= 1
if timeout == 0
ui.error "Timeout trying to reach the VM. Couldn't find the IP address."
exit 1
end
sleep 1
found = connection.servers.all.find { |v| v.name == vm.name }
end
print "\n#{ui.color("Waiting for sshd... ", :magenta)}"
print(".") until tcp_test_ssh(vm.public_ip_address) { sleep @initial_sleep_delay ||= 10; puts(" done") }
bootstrap_for_node(vm).run
puts "\n"
puts "#{ui.color("Name", :cyan)}: #{vm.name}"
puts "#{ui.color("IP Address", :cyan)}: #{vm.public_ip_address}"
puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
puts "#{ui.color("Done!", :green)}"
end
|