72 lines
1.7 KiB
Ruby
72 lines
1.7 KiB
Ruby
require_relative 'rest_connection'
|
|
|
|
module Proxmox
|
|
def self.cnfstr2hash str
|
|
str.
|
|
split( ',').
|
|
map do |o|
|
|
k, v = o.split( '=', 2)
|
|
[k.to_sym, v]
|
|
end.
|
|
to_h
|
|
end
|
|
|
|
def self.hash2cnfstr **opts
|
|
opts.
|
|
map do |k, v|
|
|
v =
|
|
case v
|
|
when true then 1
|
|
when false then 0
|
|
else v.to_s
|
|
end
|
|
"#{k}=#{v}"
|
|
end.
|
|
join( ',')
|
|
end
|
|
|
|
def self.connect username, password, realm: nil, verify_tls: nil, uri: nil
|
|
uri ||= 'https://localhost:8006/api2/json'
|
|
cred =
|
|
{
|
|
username: username,
|
|
password: password,
|
|
realm: realm || 'pve'
|
|
}
|
|
@@connection =
|
|
RestClient::Resource.new( uri, verify_ssl: ! ! verify_tls).tap do |rs|
|
|
resp = rs['/access/ticket'].post cred
|
|
case resp.code
|
|
when 200
|
|
data = JSON.parse resp.body, symbolize_names: true
|
|
@@api_ticket =
|
|
{
|
|
CSRFPreventionToken: data[:data][:CSRFPreventionToken],
|
|
cookie: "PVEAuthCookie=#{CGI.escape data[:data][:ticket]}"
|
|
}
|
|
else
|
|
raise Exception, "Authentication failed"
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.connection
|
|
@@connection
|
|
end
|
|
|
|
def self.api_ticket
|
|
@@api_ticket
|
|
end
|
|
|
|
def self.find_by_name name
|
|
Proxmox::LXC.find_by_name( name) || Proxmox::Qemu.find_by_name( name) || Proxmox::Node.find_by_name( name)
|
|
end
|
|
|
|
def self.find_by_vmid vmid
|
|
Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid)
|
|
end
|
|
|
|
def self.find name_or_id
|
|
Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find( name_or_id) || Proxmox::Node.find( name_or_id)
|
|
end
|
|
end
|