Class: ProxmoxRb::QemuKvm

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

Instance Method Summary collapse

Constructor Details

#initialize(ticket, node, id) ⇒ QemuKvm

Returns a new instance of QemuKvm.



3
4
5
6
7
8
9
10
# File 'lib/proxmox-rb/qemu_kvm.rb', line 3

def initialize(ticket, node, id)
  @node = node
  @csrf = ticket.csrf
  @id = id
  @ip = ticket.host
  @ticket = ticket.ticket
  @headers = { 'Cookie' => "PVEAuthCookie=#{@ticket}", "CSRFPreventionToken" => @csrf }
end

Instance Method Details

#configObject



12
13
14
15
16
17
18
# File 'lib/proxmox-rb/qemu_kvm.rb', line 12

def config
  http = Net::HTTP.new(@ip, '8006')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @qemu ||= http.get('/api2/json/nodes/' + @node + '/' + @id + '/config', @headers).body
  JSON.parse( @qemu )['data']
end

#deleteObject



36
37
38
39
40
41
42
43
44
# File 'lib/proxmox-rb/qemu_kvm.rb', line 36

def delete
  puts "[unlinking] #{@id}"
  http = Net::HTTP.new(@ip, '8006')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Delete.new('/api2/json/nodes/' + @node + '/' + @id, @headers)
  resp = http.request(req)
  puts resp.body
end

#delete_snapshot(name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/proxmox-rb/qemu_kvm.rb', line 77

def delete_snapshot(name)
  puts "[deleting snapshot #{name}] #{@id}"
  begin
    params = { snapname: name }
    http = Net::HTTP.new(@ip, '8006')
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Delete.new('/api2/json/nodes/' + @node + '/' + @id + '/snapshot/' + name, @headers)
    resp = http.request(request)
    puts resp
  rescue => e
    puts "retrying #{e}"
    sleep 1
    retry
  end
end

#macObject



28
29
30
31
32
33
34
# File 'lib/proxmox-rb/qemu_kvm.rb', line 28

def mac
  begin
    config['net0'].split('=')[1].split(',')[0]
  rescue
    ''
  end
end

#nameObject



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

def name
  config['name'] rescue nil
end

#nodeObject



20
21
22
# File 'lib/proxmox-rb/qemu_kvm.rb', line 20

def node
  @node
end

#shutdownObject



46
47
48
49
50
51
52
# File 'lib/proxmox-rb/qemu_kvm.rb', line 46

def shutdown
  puts "[stopping] #{@id}"
  http = Net::HTTP.new(@ip, '8006')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  resp = http.post('/api2/json/nodes/' + @node + '/' + @id + '/status/shutdown', nil, @headers)
end

#snapshot(name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/proxmox-rb/qemu_kvm.rb', line 63

def snapshot(name)
  puts "[snapshotting] #{@id}"
  params = { snapname: name, vmstate: 1 }
  http = Net::HTTP.new(@ip, '8006')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new('/api2/json/nodes/' + @node + '/' + @id + '/snapshot')
  request.add_field("Cookie", "PVEAuthCookie=#{@ticket}")
  request.add_field("CSRFPreventionToken", @csrf)
  request.set_form_data(params)
  resp = http.request(request)
  puts resp
end

#startObject



95
96
97
98
99
100
101
# File 'lib/proxmox-rb/qemu_kvm.rb', line 95

def start
  puts "[starting] #{@id}"
  http = Net::HTTP.new(@ip, '8006')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  resp = http.post('/api2/json/nodes/' + @node + '/' + @id + '/status/start', nil, @headers)
end

#stopObject



54
55
56
57
58
59
60
# File 'lib/proxmox-rb/qemu_kvm.rb', line 54

def stop
  puts "[stopping] #{@id}"
  http = Net::HTTP.new(@ip, '8006')
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  resp = http.post('/api2/json/nodes/' + @node + '/' + @id + '/status/stop', nil, @headers)
end