Class: VagrantPlugins::Proxmox::Action::CreateVm

Inherits:
ProxmoxAction show all
Defined in:
lib/vagrant-proxmox/action/create_vm.rb

Overview

This action creates a new virtual machine on the Proxmox server and stores its node and vm_id env.id

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateVm

Returns a new instance of CreateVm.



9
10
11
12
# File 'lib/vagrant-proxmox/action/create_vm.rb', line 9

def initialize app, env
	@app = app
	@logger = Log4r::Logger.new 'vagrant_proxmox::action::create_vm'
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-proxmox/action/create_vm.rb', line 14

def call env
	env[:ui].info I18n.t('vagrant_proxmox.creating_vm')
	config = env[:machine].provider_config
	node = env[:proxmox_nodes].sample[:node]
	vm_id = nil

	begin
		retryable(tries: config.task_timeout,
		          sleep: config.task_status_check_interval) do
			vm_id = get_free_vm_id env
			params = {vmid: vm_id,
			          ostemplate: config.os_template,
			          hostname: env[:machine].config.vm.hostname || env[:machine].name.to_s,
			          password: 'vagrant',
			          memory: config.vm_memory,
			          description: "#{config.vm_name_prefix}#{env[:machine].name}"}
			get_machine_ip_address(env).try { |ip_address| params[:ip_address] = ip_address }

			response = RestClient.post "#{config.endpoint}/nodes/#{node}/openvz", params,
			                           {CSRFPreventionToken: env[:proxmox_csrf_prevention_token],
			                            cookies: {PVEAuthCookie: env[:proxmox_ticket]}}
			exit_status = wait_for_completion parse_task_id(response), node, env, 'vagrant_proxmox.errors.create_vm_timeout'
			exit_status == 'OK' ? exit_status : raise(VagrantPlugins::Proxmox::Errors::ProxmoxTaskFailed, proxmox_exit_status: exit_status)
		end
	rescue StandardError => e
		raise VagrantPlugins::Proxmox::Errors::VMCreationError, proxmox_exit_status: e.message
	end

	env[:machine].id = "#{node}/#{vm_id}"

	env[:ui].info I18n.t('vagrant_proxmox.done')
	next_action env
end