Class: VagrantPlugins::GANETI::Util::GanetiClient
- Inherits:
-
Object
- Object
- VagrantPlugins::GANETI::Util::GanetiClient
- Defined in:
- lib/vagrant-ganeti/util/ganeti_client.rb
Instance Attribute Summary collapse
-
#cluster ⇒ Object
Returns the value of attribute cluster.
-
#info ⇒ Object
Returns the value of attribute info.
-
#rapi_pass ⇒ Object
Returns the value of attribute rapi_pass.
-
#rapi_user ⇒ Object
Returns the value of attribute rapi_user.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #authenticate(rapi_user, rapi_pass) ⇒ Object
- #get_url(path, params = nil) ⇒ Object
-
#initialize(cluster, rapi_user, rapi_pass) ⇒ GanetiClient
constructor
A new instance of GanetiClient.
- #instance_create(dry_run = 0) ⇒ Object
- #instance_terminate(dry_run = 0) ⇒ Object
- #is_job_ready(jobno) ⇒ Object
- #parse_response(response_body) ⇒ Object
- #send_request(method, url, body = nil, headers = {}) ⇒ Object
- #set_config(info) ⇒ Object
- #set_default_iallocator ⇒ Object
- #start_instance ⇒ Object
- #version_get ⇒ Object
Constructor Details
#initialize(cluster, rapi_user, rapi_pass) ⇒ GanetiClient
Returns a new instance of GanetiClient.
7 8 9 10 11 12 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 7 def initialize(cluster, rapi_user, rapi_pass) self.cluster = cluster self.rapi_user = rapi_user self.rapi_pass = rapi_pass self.version = self.version_get end |
Instance Attribute Details
#cluster ⇒ Object
Returns the value of attribute cluster.
5 6 7 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 5 def cluster @cluster end |
#info ⇒ Object
Returns the value of attribute info.
5 6 7 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 5 def info @info end |
#rapi_pass ⇒ Object
Returns the value of attribute rapi_pass.
5 6 7 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 5 def rapi_pass @rapi_pass end |
#rapi_user ⇒ Object
Returns the value of attribute rapi_user.
5 6 7 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 5 def rapi_user @rapi_user end |
#version ⇒ Object
Returns the value of attribute version.
5 6 7 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 5 def version @version end |
Instance Method Details
#authenticate(rapi_user, rapi_pass) ⇒ Object
67 68 69 70 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 67 def authenticate(rapi_user, rapi_pass) basic = Base64.encode64("#{rapi_user}:#{rapi_pass}").strip return "Basic #{basic}" end |
#get_url(path, params = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 73 def get_url(path, params = nil) param_string = "" if params params.each do |key, value| if value.kind_of?(Array) value.each do |svalue| param_string += "#{key}=#{svalue}&" end else param_string += "#{key}=#{value}&" end end end url = (self.version)? "/#{self.version}/#{path}?#{param_string}" : "/#{path}?#{param_string}" return url.chop end |
#instance_create(dry_run = 0) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 18 def instance_create( dry_run = 0) url = get_url("instances") body = info.to_json response_body = send_request("POST", url, body) return response_body end |
#instance_terminate(dry_run = 0) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 25 def instance_terminate(dry_run = 0) url = get_url("instances/#{info['instance_name']}") body = info.to_json response_body = send_request("DELETE", url) return response_body end |
#is_job_ready(jobno) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 33 def is_job_ready(jobno) url = get_url("jobs/#{jobno}") response_body = send_request("GET", url) if response_body["status"] =="error" if response_body["opresult"][0][1][1] == "already_exists" return "already_exists" else return "error" end else response_body["status"] end end |
#parse_response(response_body) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 128 def parse_response(response_body) # adding workaround becouse Google seems to operate on 'non-strict' JSON format # http://code.google.com/p/ganeti/issues/detail?id=117 begin response_body = JSON.parse(response_body) rescue response_body = JSON.parse('['+response_body+']').first end return response_body end |
#send_request(method, url, body = nil, headers = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 94 def send_request(method, url, body = nil, headers = {}) uri = URI.parse(cluster) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE headers['User-Agent'] = 'Ruby Ganeti RAPI Client' headers['Content-Type'] = 'application/json' headers['Authorization']= authenticate(self.rapi_user, self.rapi_pass) begin response = http.send_request(method, url, body, headers) # response = http.send_request("GET",url) rescue => e puts "Error sending request" # puts "#{e.message}" else case response when Net::HTTPSuccess parse_response(response.body.strip) else response.instance_eval { class << self; attr_accessor :body_parsed; end } begin response.body_parsed = parse_response(response.body) rescue # raises exception corresponding to http error Net::XXX puts "#{response.error!}" end end end end |
#set_config(info) ⇒ Object
14 15 16 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 14 def set_config(info) self.info = info end |
#set_default_iallocator ⇒ Object
47 48 49 50 51 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 47 def set_default_iallocator() url = get_url("info") response_body = send_request("GET", url) return response_body["default_iallocator"] end |
#start_instance ⇒ Object
53 54 55 56 57 58 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 53 def start_instance() url = get_url("instances/#{info['instance_name']}/startup") response_body = send_request("PUT", url) return response_body end |
#version_get ⇒ Object
60 61 62 63 64 65 |
# File 'lib/vagrant-ganeti/util/ganeti_client.rb', line 60 def version_get url = get_url("version") response_body = send_request("GET", url) return response_body end |