Class: Proxmox::Proxmox

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

Overview

Object to manage Proxmox server

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pve_cluster, node, username, password, realm, ssl_options = {}) ⇒ Proxmox

Create a object to manage a Proxmox server through API

:call-seq:

new(pve_cluster, node, username, password, realm, ssl_options) -> Proxmox

Example:

Proxmox::Proxmox.new('https://the-proxmox-server:8006/api2/json/', 'node', 'root', 'secret', 'pam', {verify_ssl: false})


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/proxmox.rb', line 25

def initialize(pve_cluster, node, username, password, realm, ssl_options = {})
  @pve_cluster = pve_cluster
  @node = node
  @username = username
  @password = password
  @realm = realm
  @ssl_options = ssl_options
  @connection_status = 'error'
  @site = RestClient::Resource.new(@pve_cluster, @ssl_options)
  @auth_params = create_ticket
end

Instance Attribute Details

#connection_statusObject (readonly)

Return connection status

  • connected

  • error



14
15
16
# File 'lib/proxmox.rb', line 14

def connection_status
  @connection_status
end

Instance Method Details

#delete(path) ⇒ Object



49
50
51
# File 'lib/proxmox.rb', line 49

def delete(path)
  http_action_delete(path)
end

#get(path, args = {}) ⇒ Object



37
38
39
# File 'lib/proxmox.rb', line 37

def get(path, args = {})
  http_action_get(path, args)
end

#openvz_config(vmid) ⇒ Object

Get CT config

:call-seq:

openvz_config(vmid) -> String

Return a string as task ID

Example:

openvz_config(200)

Example return:

{
  'quotaugidlimit' => 0,
  'disk' => 0,
  'ostemplate' => 'ubuntu-10.04-standard_10.04-4_i386.tar.gz',
  'hostname' => 'test.test.com',
  'nameserver' => '127.0.0.1 192.168.1.1',
  'memory' => 256,
  'searchdomain' => 'domain.com',
  'onboot' => 0,
  'cpuunits' => 1000,
  'swap' => 256,
  'quotatime' => 0,
  'digest' => 'e7e6e21a215af6b9da87a8ecb934956b8983f960',
  'cpus' => 1,
  'storage' => 'local'
}


314
315
316
# File 'lib/proxmox.rb', line 314

def openvz_config(vmid)
  http_action_get "nodes/#{@node}/openvz/#{vmid}/config"
end

#openvz_config_set(vmid, data) ⇒ Object

Set CT config

:call-seq:

openvz_config_set(vmid, parameters) -> Nil

Return nil

Example:

openvz_config(200, { 'swap' => 2048 })

Example return:

nil


333
334
335
# File 'lib/proxmox.rb', line 333

def openvz_config_set(vmid, data)
  http_action_put("nodes/#{@node}/openvz/#{vmid}/config", data)
end

#openvz_delete(vmid) ⇒ Object

Delete CT

:call-seq:

openvz_delete(vmid) -> String

Return a string as task ID

Example:

openvz_delete(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzdelete:200:root@pam:


204
205
206
# File 'lib/proxmox.rb', line 204

def openvz_delete(vmid)
  http_action_delete "nodes/#{@node}/openvz/#{vmid}"
end

#openvz_getObject

Get CT list

:call-seq:

openvz_get -> Hash

Return a Hash of all openvz container

Example:

openvz_get

Example return:

{
  '101' => {
        'maxswap' => 536870912,
        'disk' => 405168128,
        'ip' => '192.168.1.5',
        'status' => 'running',
        'netout' => 272,
        'maxdisk' => 4294967296,
        'maxmem' => 536870912,
        'uptime' => 3068073,
        'swap' => 0,
        'vmid' => '101',
        'nproc' => '10',
        'diskread' => 0,
        'cpu' => 0.00031670581100007,
        'netin' => 0,
        'name' => 'test2.domain.com',
        'failcnt' => 0,
        'diskwrite' => 0,
        'mem' => 22487040,
        'type' => 'openvz',
        'cpus' => 1
  },
  [...]
}


155
156
157
158
159
160
161
162
# File 'lib/proxmox.rb', line 155

def openvz_get
  data = http_action_get "nodes/#{@node}/openvz"
  ve_list = {}
  data.each do |ve|
    ve_list[ve['vmid']] = ve
  end
  ve_list
end

#openvz_post(ostemplate, vmid, config = {}) ⇒ Object

Create CT container

:call-seq:

openvz_post(ostemplate, vmid) -> String
openvz_post(ostemplate, vmid, options) -> String

Return a String as task ID

Examples:

openvz_post('ubuntu-10.04-standard_10.04-4_i386', 200)
openvz_post('ubuntu-10.04-standard_10.04-4_i386', 200, {'hostname' => 'test.test.com', 'password' => 'testt' })

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzcreate:200:root@pam:


181
182
183
184
185
186
187
# File 'lib/proxmox.rb', line 181

def openvz_post(ostemplate, vmid, config = {})
  config['vmid'] = vmid
  config['ostemplate'] = "local%3Avztmpl%2F#{ostemplate}.tar.gz"
  vm_definition = config.to_a.map { |v| v.join '=' }.join '&'

  http_action_post("nodes/#{@node}/openvz", vm_definition)
end

#openvz_shutdown(vmid) ⇒ Object

Shutdown CT

:call-seq:

openvz_shutdown(vmid) -> String

Return a string as task ID

Example:

openvz_shutdown(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzshutdown:200:root@pam:


280
281
282
# File 'lib/proxmox.rb', line 280

def openvz_shutdown(vmid)
  http_action_post "nodes/#{@node}/openvz/#{vmid}/status/shutdown"
end

#openvz_start(vmid) ⇒ Object

Start CT

:call-seq:

openvz_start(vmid) -> String

Return a string as task ID

Example:

openvz_start(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzstart:200:root@pam:


242
243
244
# File 'lib/proxmox.rb', line 242

def openvz_start(vmid)
  http_action_post "nodes/#{@node}/openvz/#{vmid}/status/start"
end

#openvz_status(vmid) ⇒ Object

Get CT status

:call-seq:

openvz_delete(vmid) -> String

Return a string as task ID

Example:

openvz_delete(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzdelete:200:root@pam:


223
224
225
# File 'lib/proxmox.rb', line 223

def openvz_status(vmid)
  http_action_get "nodes/#{@node}/openvz/#{vmid}/status/current"
end

#openvz_stop(vmid) ⇒ Object

Stop CT

:call-seq:

openvz_stop(vmid) -> String

Return a string as task ID

Example:

openvz_stop(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzstop:200:root@pam:


261
262
263
# File 'lib/proxmox.rb', line 261

def openvz_stop(vmid)
  http_action_post "nodes/#{@node}/openvz/#{vmid}/status/stop"
end

#post(path, args = {}) ⇒ Object



41
42
43
# File 'lib/proxmox.rb', line 41

def post(path, args = {})
  http_action_post(path, args)
end

#put(path, args = {}) ⇒ Object



45
46
47
# File 'lib/proxmox.rb', line 45

def put(path, args = {})
  http_action_put(path, args)
end

#task_status(upid) ⇒ Object

Get task status

:call-seq:

task_status(task-id) -> String
  • taksstatus

  • taskstatus:exitstatus

Example:

taskstatus 'UPID:localhost:00051DA0:119EAABC:521CCB19:vzcreate:203:root@pam:'

Examples return:

- running
- stopped:OK


69
70
71
72
73
74
75
76
77
78
# File 'lib/proxmox.rb', line 69

def task_status(upid)
  data = http_action_get "nodes/#{@node}/tasks/#{URI.encode upid}/status"
  status = data['status']
  exitstatus = data['exitstatus']
  if exitstatus
    "#{status}:#{exitstatus}"
  else
    "#{status}"
  end
end

#templatesObject

Get template list

:call-seq:

templates -> Hash

Return a Hash of all templates

Example:

templates

Example return:

 {
   'ubuntu-10.04-standard_10.04-4_i386' => {
       'format' => 'tgz',
       'content' => 'vztmpl',
       'volid' => 'local:vztmpl/ubuntu-10.04-standard_10.04-4_i386.tar.gz',
       'size' => 142126884
   },
   'ubuntu-12.04-standard_12.04-1_i386' => {
       'format' => 'tgz',
       'content' => 'vztmpl',
       'volid' => 'local:vztmpl/ubuntu-12.04-standard_12.04-1_i386.tar.gz',
        'size' => 130040792
   }
}


108
109
110
111
112
113
114
115
116
# File 'lib/proxmox.rb', line 108

def templates
  data = http_action_get "nodes/#{@node}/storage/local/content"
  template_list = {}
  data.each do |ve|
    name = ve['volid'].gsub(%r{local:vztmpl\/(.*).tar.gz}, '\1')
    template_list[name] = ve
  end
  template_list
end