Class: Chef::Knife::ProxmoxServerCreate

Inherits:
Chef::Knife show all
Includes:
ProxmoxBase
Defined in:
lib/chef/knife/proxmox_server_create.rb

Instance Method Summary collapse

Methods included from ProxmoxBase

#action_response, #check_config_parameter, #check_global_parameter, #connection, #destroy_item, included, #locate_config_value, #new_vmid, #server_create, #server_destroy, #server_get_data, #server_name_to_vmid, #server_start, #server_stop, #template_number_to_name, #vmid_to_node, #waitfor

Instance Method Details

#bootstrap_for_node(bootstrap_ip_address) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/chef/knife/proxmox_server_create.rb', line 204

def bootstrap_for_node(bootstrap_ip_address)
  require 'chef/knife/bootstrap'
  Chef::Knife::Bootstrap.load_deps
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [bootstrap_ip_address]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:environment] = locate_config_value(:environment)
  # bootstrap.config[:first_boot_attributes] = config[:first_boot_attributes]
  bootstrap.config[:ssh_user] = "root"
  bootstrap.config[:ssh_password] = config[:vm_password]
  # bootstrap.config[:identity_file] = config[:identity_file]
  # bootstrap.config[:host_key_verify] = config[:host_key_verify]
  bootstrap.config[:chef_node_name] = config[:vm_hostname]
  # bootstrap.config[:prerelease] = config[:prerelease]
  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] = false
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  
  pp bootstrap.config
  bootstrap
end

#runObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
# File 'lib/chef/knife/proxmox_server_create.rb', line 124

def run
  # Needed
  connection
  
  vm_id       = config[:vm_vmid]     || new_vmid
  vm_hostname = config[:vm_hostname] || 'proxmox'
  vm_storage  = config[:vm_storage]  || 'local'
  vm_password = config[:vm_password] || 'pve123'
  vm_cpus     = config[:vm_cpus]     || 1
  vm_memory   = config[:vm_memory]   || 512
  vm_disk     = config[:vm_disk]     || 4
  vm_swap     = config[:vm_swap]     || 512
  vm_ipaddress= config[:vm_ipaddress]|| nil
  vm_netif    = config[:vm_netif]    || 'ifname%3Deth0%2Cbridge%3Dvmbr0'
  vm_template = template_number_to_name(config[:vm_template],vm_storage) || 'local%3Avztmpl%2Fubuntu-11.10-x86_64-jorge2-.tar.gz'
  
  vm_definition = "vmid=#{vm_id}&hostname=#{vm_hostname}&storage=#{vm_storage}&password=#{vm_password}&ostemplate=#{vm_template}&memory=#{vm_memory}&swap=#{vm_swap}&disk=#{vm_disk}&cpus=#{vm_cpus}"
  
  # Add ip_address parameter to vm_definition if it's provided by CLI
  if (config[:vm_ipaddress]) then 
    vm_definition += "&ip_address=" + vm_ipaddress
  elsif (config[:vm_netif] || vm_netif) then
    vm_definition += "&netif=" + vm_netif
  end
  
  Chef::Log.debug(vm_definition)
  
  server_create(vm_id,vm_definition)
  ui.msg("Preparing the server to start")
  sleep(5)
  server_start(vm_id)
  sleep(5)
  
  # which IP address to bootstrap
  bootstrap_ip_address = server_get_data(vm_id,'ip')
  ui.msg("New Server #{vm_id} has IP Address: #{bootstrap_ip_address}")
  
  if bootstrap_ip_address.nil?
    ui.error("No IP address available for bootstrapping.")
    exit 1
  end

  print(".") until tcp_test_ssh(bootstrap_ip_address) {
    sleep @initial_sleep_delay ||= 10
    puts("done")
  }

  # bootstrapping the node
  if config[:bootstrap]
    bootstrap_for_node(bootstrap_ip_address).run
  else
    ui.msg("Skipping bootstrap of the server because --no-bootstrap used as argument.")   
  end 

end

#tcp_test_ssh(hostname) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/chef/knife/proxmox_server_create.rb', line 180

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
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end