lib/proxmox.rb in proxmox-0.0.3 vs lib/proxmox.rb in proxmox-0.0.4

- old
+ new

@@ -1,5 +1,7 @@ +# encoding: utf-8 + require 'proxmox/version' require 'rest_client' require 'json' # This module encapsulates ability to manage Proxmox server @@ -12,24 +14,25 @@ attr_reader :connection_status # Create a object to manage a Proxmox server through API # # :call-seq: - # new(pve_cluster, node, username, password, realm) -> Proxmox + # 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") + # Proxmox::Proxmox.new('https://the-proxmox-server:8006/api2/json/', 'node', 'root', 'secret', 'pam', {verify_ssl: false}) # - def initialize(pve_cluster, node, username, password, realm) + 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) + @site = RestClient::Resource.new(@pve_cluster, @ssl_options) @auth_params = create_ticket end # Get task status # @@ -39,11 +42,11 @@ # - taksstatus # - taskstatus:exitstatus # # Example: # - # taskstatus "UPID:localhost:00051DA0:119EAABC:521CCB19:vzcreate:203:root@pam:" + # taskstatus 'UPID:localhost:00051DA0:119EAABC:521CCB19:vzcreate:203:root@pam:' # # Examples return: # - running # - stopped:OK # @@ -70,21 +73,21 @@ # 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-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 + # '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 # } # } # def templates data = http_action_get "nodes/#{@node}/storage/local/content" @@ -107,31 +110,31 @@ # # 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 + # '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 # }, # [...] # } def openvz_get data = http_action_get "nodes/#{@node}/openvz" @@ -150,12 +153,12 @@ # # 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' }) + # 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: # @@ -274,79 +277,94 @@ # 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" + # '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' # } # def openvz_config(vmid) http_action_get "nodes/#{@node}/openvz/#{vmid}/config" end + # Set CT config + # + # :call-seq: + # openvz_config_set(vmid, parameters) -> Nil + # + # Return nil + # + # Example: + # + # openvz_config(200, { 'swap' => 2048 }) + # + # Example return: + # + # nil + # def openvz_config_set(vmid, data) http_action_put("nodes/#{@node}/openvz/#{vmid}/config", data) end private # Methods manages auth def create_ticket - post_param = { :username => @username, :realm => @realm, :password => @password } + 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" + @connection_status = 'error' end end end # Method create ticket 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') + token = 'PVEAuthCookie=' + ticket.gsub!(/:/, '%3A').gsub!(/=/, '%3D') end - @connection_status = "connected" + @connection_status = 'connected' { - :CSRFPreventionToken => csrf_prevention_token, - :cookie => token + CSRFPreventionToken: csrf_prevention_token, + cookie: token } end # Extract data or return error def check_response(response) - if (response.code == 200) then + if response.code == 200 JSON.parse(response.body)['data'] else - "NOK: error code = " + response.code.to_s + 'NOK: error code = ' + response.code.to_s end end # Methods manage http dialogs - def http_action_post(url, data = "") + def http_action_post(url, data = '') @site[url].post data, @auth_params do |response, request, result, &block| check_response response end end - def http_action_put(url, data = "") + def http_action_put(url, data = '') @site[url].put data, @auth_params do |response, request, result, &block| check_response response end end