Module: CloudstackClient::Server
- Defined in:
- lib/cloudstack_client/commands/server.rb
Instance Method Summary collapse
-
#create_server(args = {}) ⇒ Object
Deploys a new server using the specified parameters.
-
#destroy_server(id, args = {}) ⇒ Object
Destroy the server with the specified name.
-
#get_server(name, args = {}) ⇒ Object
Finds the server with the specified name.
- #get_server_default_nic(server) ⇒ Object
-
#get_server_fqdn(server) ⇒ Object
Returns the fully qualified domain name for a server.
-
#get_server_public_ip(server, cached_rules = nil) ⇒ Object
Finds the public ip for a server.
- #get_server_state(id) ⇒ Object
-
#list_servers(args = {}) ⇒ Object
Lists servers.
-
#reboot_server(name, args = {}) ⇒ Object
Reboot the server with the specified name.
-
#start_server(name, args = {}) ⇒ Object
Start the server with the specified name.
-
#stop_server(name, args = {}) ⇒ Object
Stops the server with the specified name.
- #wait_for_server_state(id, state) ⇒ Object
Instance Method Details
#create_server(args = {}) ⇒ Object
Deploys a new server using the specified parameters.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/cloudstack_client/commands/server.rb', line 164 def create_server(args = {}) params = {'command' => 'deployVirtualMachine'} params['keypair'] = args[:keypair] if args[:keypair] params['size'] = args[:disk_size] if args[:disk_size] params['group'] = args[:group] if args[:group] params['displayname'] = args[:displayname] if args[:displayname] if args[:account] account = list_accounts({name: args[:account]}).first unless account puts "Error: Account #{args[:account]} not found." exit 1 end params['domainid'] = account["domainid"] params['account'] = args[:account] end if args[:project] project = get_project(args[:project]) if !project msg = "Project '#{args[:project]}' is invalid" puts "Error: #{msg}" exit 1 end params['projectid'] = project['id'] elsif args[:project_id] params['projectid'] = args[:project_id] end params['name'] = args[:name] if args[:name] if args[:name] server = get_server(args[:name], project_id: params['projectid']) if server puts "Error: Server '#{args[:name]}' already exists." exit 1 end end networks = [] if args[:networks] args[:networks].each do |name| network = get_network(name, params['projectid']) if !network puts "Error: Network '#{name}' not found" exit 1 end networks << network end end if networks.empty? unless default_network = get_default_network puts "Error: No default network found" exit 1 end networks << default_network end network_ids = networks.map { |network| network['id'] } params['networkids'] = network_ids.join(',') service = get_service_offering(args[:offering]) if !service puts "Error: Service offering '#{args[:offering]}' is invalid" exit 1 end params['serviceOfferingId'] = service['id'] if args[:template] template = get_template(args[:template]) if !template puts "Error: Template '#{args[:template]}' is invalid" exit 1 end end if args[:disk_offering] disk_offering = get_disk_offering(args[:disk_offering]) unless disk_offering msg = "Disk offering '#{args[:disk_offering]}' is invalid" puts "Error: #{msg}" exit 1 end params['diskofferingid'] = disk_offering['id'] end if args[:iso] iso = get_iso(args[:iso]) unless iso puts "Error: Iso '#{args[:iso]}' is invalid" exit 1 end unless disk_offering puts "Error: a disk offering is required when using iso" exit 1 end params['hypervisor'] = (args[:hypervisor] || 'vmware') end if !template && !iso puts "Error: Iso or Template is required" exit 1 end params['templateId'] = template ? template['id'] : iso['id'] zone = args[:zone] ? get_zone(args[:zone]) : get_default_zone if !zone msg = args[:zone] ? "Zone '#{args[:zone]}' is invalid" : "No default zone found" puts "Error: #{msg}" exit 1 end params['zoneid'] = zone['id'] args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine'] end |
#destroy_server(id, args = {}) ⇒ Object
Destroy the server with the specified name.
339 340 341 342 343 344 345 346 |
# File 'lib/cloudstack_client/commands/server.rb', line 339 def destroy_server(id, args = {}) params = { 'command' => 'destroyVirtualMachine', 'id' => id } params['expunge'] = true if args[:expunge] args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine'] end |
#get_server(name, args = {}) ⇒ Object
Finds the server with the specified name.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cloudstack_client/commands/server.rb', line 8 def get_server(name, args = {}) params = { 'command' => 'listVirtualMachines', 'listAll' => true, 'name' => name } params['domainid'] = args[:domain_id] if args[:domain_id] if args[:account] account = list_accounts({name: args[:account]}).first unless account puts "Error: Account #{args[:account]} not found." exit 1 end params['domainid'] = account["domainid"] params['account'] = args[:account] end if args[:project] project = get_project(args[:project]) if !project msg = "Project '#{args[:project]}' is invalid" puts "Error: #{msg}" exit 1 end params['projectid'] = project['id'] elsif args[:project_id] params['projectid'] = args[:project_id] end json = send_request(params) machines = json['virtualmachine'] if !machines || machines.empty? then return nil end machines.select {|m| m['name'] == name }.first end |
#get_server_default_nic(server) ⇒ Object
105 106 107 108 109 |
# File 'lib/cloudstack_client/commands/server.rb', line 105 def get_server_default_nic(server) server['nic'].each do |nic| return nic if nic['isdefault'] end end |
#get_server_fqdn(server) ⇒ Object
Returns the fully qualified domain name for a server.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cloudstack_client/commands/server.rb', line 90 def get_server_fqdn(server) return nil unless server nic = get_server_default_nic(server) || {} networks = list_networks(project_id: server['projectid']) || {} id = nic['networkid'] network = networks.select { |net| net['id'] == id }.first return nil unless network "#{server['name']}.#{network['networkdomain']}" end |
#get_server_public_ip(server, cached_rules = nil) ⇒ Object
Finds the public ip for a server
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cloudstack_client/commands/server.rb', line 74 def get_server_public_ip(server, cached_rules=nil) return nil unless server # find the public ip nic = get_server_default_nic(server) || {} if nic['type'] == 'Virtual' ssh_rule = get_ssh_port_forwarding_rule(server, cached_rules) ssh_rule ? ssh_rule['ipaddress'] : nil else nic['ipaddress'] end end |
#get_server_state(id) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cloudstack_client/commands/server.rb', line 48 def get_server_state(id) params = { 'command' => 'listVirtualMachines', 'id' => id } json = send_request(params) machine_state = json['virtualmachine'][0]['state'] if !machine_state || machine_state.empty? return nil end machine_state end |
#list_servers(args = {}) ⇒ Object
Lists servers.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/cloudstack_client/commands/server.rb', line 114 def list_servers(args = {}) params = { 'command' => 'listVirtualMachines', 'listAll' => true } params.merge!(args[:custom]) params['state'] = args[:state] if args[:state] params['state'] = args[:status] if args[:status] params['groupid'] = args[:group_id] if args[:group_id] if args[:zone] zone = get_zone(args[:zone]) unless zone puts "Error: Zone #{args[:zone]} not found" exit 1 end params['zoneid'] = zone['id'] end if args[:account] account = list_accounts({name: args[:account]}).first unless account puts "Error: Account #{args[:account]} not found." exit 1 end params['domainid'] = account["domainid"] params['account'] = args[:account] end if args[:project] project = get_project(args[:project]) if !project msg = "Project '#{args[:project]}' is invalid" puts "Error: #{msg}" exit 1 end params['projectid'] = project['id'] elsif args[:project_id] params['projectid'] = args[:project_id] end json = send_request(params) json['virtualmachine'] || [] end |
#reboot_server(name, args = {}) ⇒ Object
Reboot the server with the specified name.
321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/cloudstack_client/commands/server.rb', line 321 def reboot_server(name, args = {}) server = get_server(name, args) if !server || !server['id'] puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'rebootVirtualMachine', 'id' => server['id'] } args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine'] end |
#start_server(name, args = {}) ⇒ Object
Start the server with the specified name.
303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/cloudstack_client/commands/server.rb', line 303 def start_server(name, args = {}) server = get_server(name, args) if !server || !server['id'] puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'startVirtualMachine', 'id' => server['id'] } args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine'] end |
#stop_server(name, args = {}) ⇒ Object
Stops the server with the specified name.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/cloudstack_client/commands/server.rb', line 284 def stop_server(name, args = {}) server = get_server(name, args) if !server || !server['id'] puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'stopVirtualMachine', 'id' => server['id'] } params['forced'] = true if args[:forced] args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine'] end |
#wait_for_server_state(id, state) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/cloudstack_client/commands/server.rb', line 63 def wait_for_server_state(id, state) while get_server_state(id) != state print '..' sleep 5 end state end |