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, 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(options = {}) ⇒ 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.
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 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/cloudstack_client/commands/server.rb', line 131 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.
305 306 307 308 309 310 311 |
# File 'lib/cloudstack_client/commands/server.rb', line 305 def destroy_server(id, async = true) params = { 'command' => 'destroyVirtualMachine', 'id' => id } async ? send_async_request(params)['virtualmachine'] : send_request(params) 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 |
# File 'lib/cloudstack_client/commands/server.rb', line 8 def get_server(name, args = {}) params = { 'command' => 'listVirtualMachines', 'listAll' => true, 'name' => name } params['projectid'] = args[:project_id] if args[:project_id] if args[:account] if account = list_accounts({name: args[:account]}).first params['domainid'] = account["domainid"] params['account'] = args[:account] end 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
90 91 92 93 94 |
# File 'lib/cloudstack_client/commands/server.rb', line 90 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cloudstack_client/commands/server.rb', line 75 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
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cloudstack_client/commands/server.rb', line 59 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cloudstack_client/commands/server.rb', line 33 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 servers.
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 126 |
# File 'lib/cloudstack_client/commands/server.rb', line 99 def list_servers( = {}) params = { 'command' => 'listVirtualMachines', 'listAll' => true } params['projectid'] = [:project_id] if [:project_id] params['state'] = [:status] if [:status] params['groupid'] = [:group_id] if [:group_id] if [:zone] zone = get_zone([:zone]) unless zone puts "Error: Zone #{options[: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, args = {}) ⇒ Object
Reboot the server with the specified name.
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/cloudstack_client/commands/server.rb', line 287 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.
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/cloudstack_client/commands/server.rb', line 269 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.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/cloudstack_client/commands/server.rb', line 250 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
48 49 50 51 52 53 54 |
# File 'lib/cloudstack_client/commands/server.rb', line 48 def wait_for_server_state(id, state) while get_server_state(id) != state print '..' sleep 5 end state end |