class Proxmox::Proxmox

Object to manage Proxmox server

Attributes

connection_status[R]

Return connection status

  • connected

  • error

Public Class Methods

new(pve_cluster, node, username, password, realm, ssl_options) → Proxmox click to toggle source

Create a object to manage a Proxmox server through API

Example:

Proxmox::Proxmox.new('https://the-proxmox-server:8006/api2/json/', 'node', 'root', 'secret', 'pam', {verify_ssl: false})
# 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

Public Instance Methods

delete(path) click to toggle source
# File lib/proxmox.rb, line 49
def delete(path)
  http_action_delete(path)
end
get(path, args = {}) click to toggle source
# File lib/proxmox.rb, line 37
def get(path, args = {})
  http_action_get(path, args)
end
openvz_config(vmid) → String click to toggle source

Get CT config

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'
}
# File lib/proxmox.rb, line 314
def openvz_config(vmid)
  http_action_get "nodes/#{@node}/openvz/#{vmid}/config"
end
openvz_config_set(vmid, parameters) → Nil click to toggle source

Set CT config

Return nil

Example:

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

Example return:

nil
# 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) → String click to toggle source

Delete CT

Return a string as task ID

Example:

openvz_delete(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzdelete:200:root@pam:
# File lib/proxmox.rb, line 204
def openvz_delete(vmid)
  http_action_delete "nodes/#{@node}/openvz/#{vmid}"
end
openvz_get → Hash click to toggle source

Get CT list

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
  },
  [...]
}
# 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) → String click to toggle source
openvz_post(ostemplate, vmid, options) → String

Create CT container

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:
# 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) → String click to toggle source

Shutdown CT

Return a string as task ID

Example:

openvz_shutdown(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzshutdown:200:root@pam:
# File lib/proxmox.rb, line 280
def openvz_shutdown(vmid)
  http_action_post "nodes/#{@node}/openvz/#{vmid}/status/shutdown"
end
openvz_start(vmid) → String click to toggle source

Start CT

Return a string as task ID

Example:

openvz_start(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzstart:200:root@pam:
# File lib/proxmox.rb, line 242
def openvz_start(vmid)
  http_action_post "nodes/#{@node}/openvz/#{vmid}/status/start"
end
openvz_delete(vmid) → String click to toggle source

Get CT status

Return a string as task ID

Example:

openvz_delete(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzdelete:200:root@pam:
# File lib/proxmox.rb, line 223
def openvz_status(vmid)
  http_action_get "nodes/#{@node}/openvz/#{vmid}/status/current"
end
openvz_stop(vmid) → String click to toggle source

Stop CT

Return a string as task ID

Example:

openvz_stop(200)

Example return:

UPID:localhost:000BC66A:1279E395:521EFC4E:vzstop:200:root@pam:
# File lib/proxmox.rb, line 261
def openvz_stop(vmid)
  http_action_post "nodes/#{@node}/openvz/#{vmid}/status/stop"
end
post(path, args = {}) click to toggle source
# File lib/proxmox.rb, line 41
def post(path, args = {})
  http_action_post(path, args)
end
put(path, args = {}) click to toggle source
# File lib/proxmox.rb, line 45
def put(path, args = {})
  http_action_put(path, args)
end
qemu_agent_exec(vmid, data) click to toggle source
# File lib/proxmox.rb, line 373
def qemu_agent_exec(vmid, data)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/exec", data)
end
qemu_agent_exec_status(vmid, pid) click to toggle source
# File lib/proxmox.rb, line 377
def qemu_agent_exec_status(vmid, pid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/exec-status?pid=#{pid}")
end
qemu_agent_file_read(vmid, file) click to toggle source
# File lib/proxmox.rb, line 381
def qemu_agent_file_read(vmid, file)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/file-read?file=#{file}")
end
qemu_agent_file_write(vmid, file, content) click to toggle source
# File lib/proxmox.rb, line 385
def qemu_agent_file_write(vmid, file, content)
  data = {
    file: file,
    content: content,
  }
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/file-write", data)
end
qemu_agent_fsfreeze_freeze(vmid) click to toggle source
# File lib/proxmox.rb, line 397
def qemu_agent_fsfreeze_freeze(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/fsfreeze-freeze")
end
qemu_agent_fsfreeze_status(vmid) click to toggle source
# File lib/proxmox.rb, line 393
def qemu_agent_fsfreeze_status(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/fsfreeze-status")
end
qemu_agent_fsfreeze_thaw(vmid) click to toggle source
# File lib/proxmox.rb, line 401
def qemu_agent_fsfreeze_thaw(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/fsfreeze-thaw")
end
qemu_agent_fstrim(vmid) click to toggle source
# File lib/proxmox.rb, line 405
def qemu_agent_fstrim(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/fstrim")
end
qemu_agent_get_fsinfo(vmid) click to toggle source
# File lib/proxmox.rb, line 409
def qemu_agent_get_fsinfo(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-fsinfo")
end
qemu_agent_get_host_name(vmid) click to toggle source
# File lib/proxmox.rb, line 413
def qemu_agent_get_host_name(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-host-name")
end
qemu_agent_get_memory_block_info(vmid) click to toggle source
# File lib/proxmox.rb, line 417
def qemu_agent_get_memory_block_info(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-memory-block-info")
end
qemu_agent_get_memory_blocks(vmid) click to toggle source
# File lib/proxmox.rb, line 421
def qemu_agent_get_memory_blocks(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-memory-blocks")
end
qemu_agent_get_osinfo(vmid) click to toggle source
# File lib/proxmox.rb, line 425
def qemu_agent_get_osinfo(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-osinfo")
end
qemu_agent_get_time(vmid) click to toggle source
# File lib/proxmox.rb, line 429
def qemu_agent_get_time(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-time")
end
qemu_agent_get_timezone(vmid) click to toggle source
# File lib/proxmox.rb, line 433
def qemu_agent_get_timezone(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-timezone")
end
qemu_agent_get_users(vmid) click to toggle source
# File lib/proxmox.rb, line 437
def qemu_agent_get_users(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-users")
end
qemu_agent_get_vcpus(vmid) click to toggle source
# File lib/proxmox.rb, line 441
def qemu_agent_get_vcpus(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/get-vcpus")
end
qemu_agent_info(vmid) click to toggle source
# File lib/proxmox.rb, line 445
def qemu_agent_info(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/info")
end
qemu_agent_network_get_interfaces(vmid) click to toggle source
# File lib/proxmox.rb, line 449
def qemu_agent_network_get_interfaces(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/agent/network-get-interfaces")
end
qemu_agent_ping(vmid) click to toggle source
# File lib/proxmox.rb, line 453
def qemu_agent_ping(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/ping")
end
qemu_agent_set_user_password(vmid, username, password, crypt=0) click to toggle source
# File lib/proxmox.rb, line 457
def qemu_agent_set_user_password(vmid, username, password, crypt=0)
  data = {
    username: username,
    password: password,
  }
  http_action_post("nodes/#{@node}/qemu/#{vmid}/agent/set-user-password", data)
end
qemu_agent_shutdown(vmid) click to toggle source
# File lib/proxmox.rb, line 465
def qemu_agent_shutdown(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/shutdown")
end
qemu_agent_suspend_disk(vmid) click to toggle source
# File lib/proxmox.rb, line 469
def qemu_agent_suspend_disk(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/suspend-disk")
end
qemu_agent_suspend_hybrid(vmid) click to toggle source
# File lib/proxmox.rb, line 473
def qemu_agent_suspend_hybrid(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/suspend-hybrid")
end
qemu_agent_suspend_ram(vmid) click to toggle source
# File lib/proxmox.rb, line 477
def qemu_agent_suspend_ram(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/suspend-ram")
end
qemu_current(vmid) click to toggle source

Current Qemu VM

# File lib/proxmox.rb, line 338
def qemu_current(vmid)
  http_action_get("nodes/#{@node}/qemu/#{vmid}/status/current")
end
qemu_reset(vmid) click to toggle source

Reset Qemu VM

# File lib/proxmox.rb, line 368
def qemu_reset(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/reset")
end
qemu_resume(vmid) click to toggle source

Resume Qemu VM

# File lib/proxmox.rb, line 353
def qemu_resume(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/resume")
end
qemu_shutdown(vmid) click to toggle source

Shutdown Qemu VM

# File lib/proxmox.rb, line 363
def qemu_shutdown(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/shutdown")
end
qemu_start(vmid) click to toggle source

Start Qemu VM

# File lib/proxmox.rb, line 343
def qemu_start(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/start")
end
qemu_stop(vmid) click to toggle source

Stop Qemu VM

# File lib/proxmox.rb, line 348
def qemu_stop(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/stop")
end
qemu_suspend(vmid) click to toggle source

Suspend Qemu VM

# File lib/proxmox.rb, line 358
def qemu_suspend(vmid)
  http_action_post("nodes/#{@node}/qemu/#{vmid}/status/suspend")
end
task_status(task-id) → String click to toggle source

Get task status

  • taksstatus

  • taskstatus:exitstatus

Example:

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

Examples return:

- running
- stopped:OK
# 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
templates → Hash click to toggle source

Get template list

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
   }
}
# 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

Private Instance Methods

check_response(response) click to toggle source

Extract data or return error

# File lib/proxmox.rb, line 511
def check_response(response)
  if response.code == 200
    JSON.parse(response.body)['data']
  else
    'NOK: error code = ' + response.code.to_s
  end
end
create_ticket() click to toggle source

Methods manages auth

# File lib/proxmox.rb, line 484
def create_ticket
  post_param = { username: @username, realm: @realm, password: @password }
  @site['access/ticket'].post post_param do |response, _request, _result, &_block|
    if response.code == 200
      extract_ticket response
    else
      @connection_status = 'error'
    end
  end
end
extract_ticket(response) click to toggle source

Method create ticket

# File lib/proxmox.rb, line 496
def extract_ticket(response)
  data = JSON.parse(response.body)
  ticket = data['data']['ticket']
  csrf_prevention_token = data['data']['CSRFPreventionToken']
  unless ticket.nil?
    token = 'PVEAuthCookie=' + ticket.gsub!(/:/, '%3A').gsub!(/=/, '%3D')
  end
  @connection_status = 'connected'
  {
    CSRFPreventionToken: csrf_prevention_token,
    cookie: token
  }
end
http_action_delete(url) click to toggle source
# File lib/proxmox.rb, line 538
def http_action_delete(url)
  @site[url].delete @auth_params do |response, _request, _result, &_block|
    check_response response
  end
end
http_action_get(url, data = {}) click to toggle source
# File lib/proxmox.rb, line 532
def http_action_get(url, data = {})
  @site[url].get @auth_params.merge(data) do |response, _request, _result, &_block|
    check_response response
  end
end
http_action_post(url, data = {}) click to toggle source

Methods manage http dialogs

# File lib/proxmox.rb, line 520
def http_action_post(url, data = {})
  @site[url].post data, @auth_params do |response, _request, _result, &_block|
    check_response response
  end
end
http_action_put(url, data = {}) click to toggle source
# File lib/proxmox.rb, line 526
def http_action_put(url, data = {})
  @site[url].put data, @auth_params do |response, _request, _result, &_block|
    check_response response
  end
end