Class: Chef::Knife::KvmVmCreate

Inherits:
Chef::Knife show all
Includes:
KVMBase
Defined in:
lib/chef/knife/kvm_vm_create.rb

Instance Method Summary collapse

Methods included from KVMBase

#connection, included, #locate_config_value, #upload_file

Instance Method Details

#bootstrap_for_node(vm) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/chef/knife/kvm_vm_create.rb', line 222

def bootstrap_for_node(vm)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [vm.public_ip_address]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = config[:ssh_user] 
  bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:chef_node_name] = config[:chef_node_name] || vm.name
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  # bootstrap will run as root...sudo (by default) also messes up Ohai on CentOS boxes
  bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = config[:environment]
  bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
  bootstrap.config[:ssh_password] = config[:ssh_password]
  bootstrap
end

#runObject



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/"

  #connection.remote_command "mkdir #{destination_path}"
  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',
                    #:autostart => true, # Starting guest automatically
                    :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"

  # wait for it to be ready to do stuff
  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

#tcp_test_ssh(hostname) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/knife/kvm_vm_create.rb', line 123

def tcp_test_ssh(hostname)
  tcp_socket = TCPSocket.new(hostname, 22)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
    yield
    true
  else
    false
  end
rescue Errno::ETIMEDOUT, Errno::EPERM
  false
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end