Class: VagrantPlugins::Proxmox::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-proxmox/proxmox/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url, opts = {}) ⇒ Connection

Returns a new instance of Connection.



31
32
33
34
35
36
37
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 31

def initialize api_url, opts = {}
	@api_url = api_url
	@vm_id_range = opts[:vm_id_range] || (900..999)
	@task_timeout = opts[:task_timeout] || 60
	@task_status_check_interval = opts[:task_status_check_interval] || 2
	@imgcopy_timeout = opts[:imgcopy_timeout] || 120
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



23
24
25
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 23

def api_url
  @api_url
end

#csrf_tokenObject (readonly)

Returns the value of attribute csrf_token.



25
26
27
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 25

def csrf_token
  @csrf_token
end

#imgcopy_timeoutObject

Returns the value of attribute imgcopy_timeout.



29
30
31
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 29

def imgcopy_timeout
  @imgcopy_timeout
end

#task_status_check_intervalObject

Returns the value of attribute task_status_check_interval.



28
29
30
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 28

def task_status_check_interval
  @task_status_check_interval
end

#task_timeoutObject

Returns the value of attribute task_timeout.



27
28
29
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 27

def task_timeout
  @task_timeout
end

#ticketObject (readonly)

Returns the value of attribute ticket.



24
25
26
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 24

def ticket
  @ticket
end

#vm_id_rangeObject

Returns the value of attribute vm_id_range.



26
27
28
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 26

def vm_id_range
  @vm_id_range
end

Instance Method Details

#create_vm(node:, vm_type:, params:) ⇒ Object



95
96
97
98
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 95

def create_vm(node:, vm_type:, params:)
	response = post "/nodes/#{node}/#{vm_type}", params
	wait_for_completion task_response: response, timeout_message: 'vagrant_proxmox.errors.create_vm_timeout'
end

#delete_vm(vm_id) ⇒ Object



89
90
91
92
93
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 89

def delete_vm vm_id
	vm_info = get_vm_info vm_id
	response = delete "/nodes/#{vm_info[:node]}/#{vm_info[:type]}/#{vm_id}"
	wait_for_completion task_response: response, timeout_message: 'vagrant_proxmox.errors.destroy_vm_timeout'
end

#get_free_vm_idObject



118
119
120
121
122
123
124
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 118

def get_free_vm_id
	response = get "/cluster/resources?type=vm"
	allowed_vm_ids = vm_id_range.to_set
	used_vm_ids = response[:data].map { |vm| vm[:vmid] }
	free_vm_ids = (allowed_vm_ids - used_vm_ids).sort
	free_vm_ids.empty? ? raise(VagrantPlugins::Proxmox::Errors::NoVmIdAvailable) : free_vm_ids.first
end

#get_node_listObject



51
52
53
54
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 51

def get_node_list
	nodelist = get '/nodes'
	nodelist[:data].map { |n| n[:node] }
end

#get_vm_state(vm_id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 56

def get_vm_state vm_id
	vm_info = get_vm_info vm_id
	if vm_info
		begin
			response = get "/nodes/#{vm_info[:node]}/#{vm_info[:type]}/#{vm_id}/status/current"
			states = {'running' => :running,
								'stopped' => :stopped}
			states[response[:data][:status]]
		rescue ApiError::ServerError
			:not_created
		end
	else
		:not_created
	end
end

#list_storage_files(node:, storage:) ⇒ Object



134
135
136
137
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 134

def list_storage_files(node:, storage:)
	res = get "/nodes/#{node}/storage/#{storage}/content"
	res[:data].map { |e| e[:volid] }
end

#login(username:, password:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 39

def (username:, password:)
	begin
		response = post "/access/ticket", username: username, password: password
		@ticket = response[:data][:ticket]
		@csrf_token = response[:data][:CSRFPreventionToken]
	rescue ApiError::ServerError
		raise ApiError::InvalidCredentials
	rescue => x
		raise ApiError::ConnectionError, x.message
	end
end

#shutdown_vm(vm_id) ⇒ Object



112
113
114
115
116
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 112

def shutdown_vm vm_id
	vm_info = get_vm_info vm_id
	response = post "/nodes/#{vm_info[:node]}/#{vm_info[:type]}/#{vm_id}/status/shutdown", nil
	wait_for_completion task_response: response, timeout_message: 'vagrant_proxmox.errors.shutdown_vm_timeout'
end

#start_vm(vm_id) ⇒ Object



100
101
102
103
104
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 100

def start_vm vm_id
	vm_info = get_vm_info vm_id
	response = post "/nodes/#{vm_info[:node]}/#{vm_info[:type]}/#{vm_id}/status/start", nil
	wait_for_completion task_response: response, timeout_message: 'vagrant_proxmox.errors.start_vm_timeout'
end

#stop_vm(vm_id) ⇒ Object



106
107
108
109
110
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 106

def stop_vm vm_id
	vm_info = get_vm_info vm_id
	response = post "/nodes/#{vm_info[:node]}/#{vm_info[:type]}/#{vm_id}/status/stop", nil
	wait_for_completion task_response: response, timeout_message: 'vagrant_proxmox.errors.stop_vm_timeout'
end

#upload_file(file, content_type:, node:, storage:) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 126

def upload_file(file, content_type:, node:, storage:)
	unless is_file_in_storage? filename: file, node: node, storage: storage
		res = post "/nodes/#{node}/storage/#{storage}/upload", content: content_type,
							 filename: File.new(file, 'rb'), node: node, storage: storage
		wait_for_completion task_response: res, timeout_message: 'vagrant_proxmox.errors.upload_timeout'
	end
end

#wait_for_completion(task_response: task_response, timeout_message: timeout_message) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-proxmox/proxmox/connection.rb', line 72

def wait_for_completion task_response: task_response, timeout_message: timeout_message
	task_upid = task_response[:data]
	timeout = task_timeout
	task_type = /UPID:.*?:.*?:.*?:.*?:(.*)?:.*?:.*?:/.match(task_upid)[1]
	timeout = imgcopy_timeout if task_type == 'imgcopy'
	begin
		retryable(on: VagrantPlugins::Proxmox::ProxmoxTaskNotFinished,
							tries: timeout / task_status_check_interval + 1,
							sleep: task_status_check_interval) do
			exit_status = get_task_exitstatus task_upid
			exit_status.nil? ? raise(VagrantPlugins::Proxmox::ProxmoxTaskNotFinished) : exit_status
		end
	rescue VagrantPlugins::Proxmox::ProxmoxTaskNotFinished
		raise VagrantPlugins::Proxmox::Errors::Timeout.new timeout_message
	end
end