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, async = true) ⇒ Object
Destroy the server with the specified name.
-
#get_server(name, project_id = nil) ⇒ 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(options = {}) ⇒ Object
Lists all the servers in your account.
-
#reboot_server(name) ⇒ Object
Reboot the server with the specified name.
-
#start_server(name) ⇒ Object
Start the server with the specified name.
-
#stop_server(name, forced = nil) ⇒ 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.
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 160 161 162 163 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 |
# File 'lib/cloudstack-client/commands/server.rb', line 120 def create_server(args = {}) if args[:name] if get_server(args[:name]) puts "Error: Server '#{args[:name]}' already exists." exit 1 end end service = get_service_offering(args[:offering]) if !service puts "Error: Service offering '#{args[:offering]}' is invalid" exit 1 end 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 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 end if !template && !iso puts "Error: Iso or Template is required" exit 1 end 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 if args[:project] project = get_project(args[:project]) if !project msg = "Project '#{args[:project]}' is invalid" puts "Error: #{msg}" exit 1 end end networks = [] if args[:networks] args[:networks].each do |name| network = project ? get_network(name, project['id']) : get_network(name) if !network puts "Error: Network '#{name}' not found" exit 1 end networks << network end end if networks.empty? networks << get_default_network end if networks.empty? puts "No default network found" exit 1 end network_ids = networks.map { |network| network['id'] } params = { 'command' => 'deployVirtualMachine', 'serviceOfferingId' => service['id'], 'templateId' => template ? template['id'] : iso['id'], 'zoneId' => zone['id'], 'networkids' => network_ids.join(',') } params['name'] = args[:name] if args[:name] params['projectid'] = project['id'] if project params['diskofferingid'] = disk_offering['id'] if disk_offering params['hypervisor'] = (args[:hypervisor] || 'vmware') if iso 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 args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine'] end |
#destroy_server(id, async = true) ⇒ Object
Destroy the server with the specified name.
300 301 302 303 304 305 306 307 |
# File 'lib/cloudstack-client/commands/server.rb', line 300 def destroy_server(id, async = true) params = { 'command' => 'destroyVirtualMachine', 'id' => id } async ? send_async_request(params)['virtualmachine'] : send_request(params) end |
#get_server(name, project_id = nil) ⇒ Object
Finds the server with the specified name.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cloudstack-client/commands/server.rb', line 8 def get_server(name, project_id=nil) params = { 'command' => 'listVirtualMachines', 'name' => name } params['projectid'] = project_id if project_id 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
81 82 83 84 85 |
# File 'lib/cloudstack-client/commands/server.rb', line 81 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cloudstack-client/commands/server.rb', line 66 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
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cloudstack-client/commands/server.rb', line 50 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cloudstack-client/commands/server.rb', line 24 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(options = {}) ⇒ Object
Lists all the servers in your account.
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 |
# File 'lib/cloudstack-client/commands/server.rb', line 90 def list_servers( = {}) params = { 'command' => 'listVirtualMachines', 'listAll' => true } params['projectid'] = [:project_id] if [:project_id] if [:zone] zone = get_zone([:zone]) unless zone puts "Error: Zone #{[:zone]} not found" exit 1 end params['zoneid'] = zone['id'] end if [:account] if account = list_accounts({name: [:account]}).first params['domainid'] = account["domainid"] params['account'] = [:account] end end json = send_request(params) json['virtualmachine'] || [] end |
#reboot_server(name) ⇒ Object
Reboot the server with the specified name.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/cloudstack-client/commands/server.rb', line 280 def reboot_server(name) server = get_server(name) if !server || !server['id'] puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'rebootVirtualMachine', 'id' => server['id'] } json = send_async_request(params) json['virtualmachine'] end |
#start_server(name) ⇒ Object
Start the server with the specified name.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/cloudstack-client/commands/server.rb', line 260 def start_server(name) server = get_server(name) if !server || !server['id'] puts "Error: Virtual machine '#{name}' does not exist" exit 1 end params = { 'command' => 'startVirtualMachine', 'id' => server['id'] } json = send_async_request(params) json['virtualmachine'] end |
#stop_server(name, forced = nil) ⇒ Object
Stops the server with the specified name.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/cloudstack-client/commands/server.rb', line 239 def stop_server(name, forced=nil) server = get_server(name) 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 forced json = send_async_request(params) json['virtualmachine'] end |
#wait_for_server_state(id, state) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/cloudstack-client/commands/server.rb', line 39 def wait_for_server_state(id, state) while get_server_state(id) != state print '..' sleep 5 end state end |