Class: ForemanProxmox::Proxmoxserver
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ForemanProxmox::Proxmoxserver
- Defined in:
- app/models/foreman_proxmox/proxmoxserver.rb
Instance Method Summary collapse
- #authenticate_client ⇒ Object
- #check_ip_connectivity ⇒ Object
-
#create_ide(vmid, size) ⇒ Object
manage kvms.
- #create_kvm(vmid, sockets, cores, memory, mac) ⇒ Object
- #delete_kvm(vmid) ⇒ Object
- #edit_kvm(vmid) ⇒ Object
- #find_node_for_vmid(vmid) ⇒ Object
- #get_first_node_in_cluster ⇒ Object
- #get_next_free_vmid ⇒ Object
-
#reboot ⇒ Object
manage node.
- #reboot_kvm(vmid) ⇒ Object
- #setup_httpclient ⇒ Object
- #shutdown ⇒ Object
- #start_all_vms ⇒ Object
- #start_kvm(vmid) ⇒ Object
- #stop_all_vms ⇒ Object
- #stop_kvm(vmid) ⇒ Object
Instance Method Details
#authenticate_client ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 31 def authenticate_client if !check_ip_connectivity then return nil end $LOG.error("authenticating") domain= "https://#{self.ip}:8006/" url= URI.parse(domain) credentials= {:username => "#{self.username}@pam", :password => self.password} auth_response= @client.post("https://#{self.ip}:8006/api2/json/access/ticket", credentials) $LOG.error(auth_response.body) auth= JSON.parse(auth_response.body) ticket= auth["data"]["ticket"] token= auth["data"]["CSRFPreventionToken"] @header= {:CSRFPreventionToken => token} $LOG.error("#{ticket} #{token}") = WebAgent::Cookie.new .name= 'PVEAuthCookie' .value= ticket .url= url @client..add() $LOG.error("authenticated") end |
#check_ip_connectivity ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 16 def check_ip_connectivity $LOG.error("checking connect") if @client == nil then setup_httpclient end code_response = @client.get("https://#{self.ip}:8006/api2/json/access/ticket") $LOG.error(code_response) if code_response.code != 200 then $LOG.error("connection fail") flash[:error] = "Proxmoxserver seems down, try again or change Server" return false else $LOG.error("connection there") return true end end |
#create_ide(vmid, size) ⇒ Object
manage kvms
124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 124 def create_ide(vmid, size) if !check_ip_connectivity then return nil end authenticate_client body= { :filename => "vm-#{vmid}-disk-0.qcow2", :format => "qcow2", :size => size, :vmid => vmid} testres= @client.post("https://#{self.ip}:8006/api2/json/nodes/#{@node}/storage/local/content",body,@header) $LOG.error("Body: #{testres.body}") $LOG.error("Header: #{testres.header}") end |
#create_kvm(vmid, sockets, cores, memory, mac) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 135 def create_kvm(vmid, sockets, cores ,memory,mac) if !check_ip_connectivity then return nil end authenticate_client body= { :vmid => vmid, :sockets => sockets, :cores => cores, :memory => memory, :net0 => "e1000=#{mac},bridge=vmbr1", :ide0 => "volume=local:#{vmid}/vm-#{vmid}-disk-0.qcow2,media=disk"} testres= @client.post("https://#{self.ip}:8006/api2/json/nodes/#{@node}/qemu",body,@header) $LOG.error("Body: #{testres.body}") $LOG.error("Header: #{testres.header}") end |
#delete_kvm(vmid) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 149 def delete_kvm(vmid) if !check_ip_connectivity then return nil end find_node_for_vmid(vmid) testres= @client.delete("https://#{self.ip}:8006/api2/json/nodes/proxmox/qemu/#{vmid}",{},@header) $LOG.error("Body: #{testres.body}") $LOG.error("Header: #{testres.header}") end |
#edit_kvm(vmid) ⇒ Object
146 147 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 146 def edit_kvm(vmid) end |
#find_node_for_vmid(vmid) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 64 def find_node_for_vmid(vmid) authenticate_client nodes_response = @client.get("https://#{self.ip}:8006/api2/json/nodes") nodes = JSON.parse(nodes_response.body) current_node_id = 0 current_node = nodes["data"][current_node_id] while current_node != nil node_name = nodes["data"][current_node_id]["node"] current_vm_id = 0 vms_response = @client.get("https://#{self.ip}:8006/api2/json/nodes/#{node_name}/qemu") vms = JSON.parse(vms_response.body) current_vm = vms["data"][current_vm_id] while current_vm != nil do if vms["data"][current_vm_id]["vmid"] == vmid @node = node_name end current_vm_id+=1 current_vm = vms["data"][current_vm_id] end current_node_id+=1 current_node = nodes["data"][current_node_id] end end |
#get_first_node_in_cluster ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 54 def get_first_node_in_cluster if !check_ip_connectivity then return nil end authenticate_client nodes_response = @client.get("https://#{self.ip}:8006/api2/json/nodes") nodes = JSON.parse(nodes_response.body) return nodes["data"][0]["node"] end |
#get_next_free_vmid ⇒ Object
88 89 90 91 92 93 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 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 88 def get_next_free_vmid $LOG= Logger.new("/tmp/proxmox_debug.log") $LOG.error("start here") authenticate_client nodes_response = @client.get("https://#{self.ip}:8006/api2/json/nodes") nodes = JSON.parse(nodes_response.body) $LOG.error(nodes) current_node_id = 0 highest_vmid = 0 current_node = nodes["data"][current_node_id] $LOG.error(current_node) while current_node != nil node_name = nodes["data"][current_node_id]["node"] $LOG.error(node_name) current_vm_id = 0 vms_response = @client.get("https://#{self.ip}:8006/api2/json/nodes/#{node_name}/qemu") vms = JSON.parse(vms_response.body) $LOG.error(vms) current_vm = vms["data"][current_vm_id] while current_vm != nil do if vms["data"][current_vm_id]["vmid"].to_i > highest_vmid highest_vmid = vms["data"][current_vm_id]["vmid"].to_i $LOG.error(highest_vmid) end current_vm_id+=1 current_vm = vms["data"][current_vm_id] end current_node_id+=1 current_node = nodes["data"][current_node_id] end $LOG.error(highest_vmid+1).to_s return highest_vmid+1 end |
#reboot ⇒ Object
manage node
191 192 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 191 def reboot end |
#reboot_kvm(vmid) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 179 def reboot_kvm(vmid) if !check_ip_connectivity then return nil end find_node_for_vmid(vmid) testres= @client.post("https://#{self.ip}:8006/api2/json/nodes/proxmox/qemu/#{vmid}/status/reset",{},@header) $LOG.error("Body: #{testres.body}") $LOG.error("Header: #{testres.header}") end |
#setup_httpclient ⇒ Object
8 9 10 11 12 13 14 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 8 def setup_httpclient @client= HTTPClient.new @client.ssl_config.verify_mode= OpenSSL::SSL::VERIFY_NONE @node = get_first_node_in_cluster $LOG.error("Created HttpClient") end |
#shutdown ⇒ Object
194 195 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 194 def shutdown end |
#start_all_vms ⇒ Object
197 198 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 197 def start_all_vms end |
#start_kvm(vmid) ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 159 def start_kvm(vmid) if !check_ip_connectivity then return nil end find_node_for_vmid(vmid) testres= @client.post("https://#{self.ip}:8006/api2/json/nodes/proxmox/qemu/#{vmid}/status/start",{},@header) $LOG.error("Body: #{testres.body}") $LOG.error("Header: #{testres.header}") end |
#stop_all_vms ⇒ Object
200 201 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 200 def stop_all_vms end |
#stop_kvm(vmid) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'app/models/foreman_proxmox/proxmoxserver.rb', line 169 def stop_kvm(vmid) if !check_ip_connectivity then return nil end find_node_for_vmid(vmid) testres= @client.post("https://#{self.ip}:8006/api2/json/nodes/proxmox/qemu/#{vmid}/status/stop",{},@header) $LOG.error("Body: #{testres.body}") $LOG.error("Header: #{testres.header}") end |