Class: Server
- Inherits:
-
CloudstackCli::Base
- Object
- Thor
- CloudstackCli::Base
- Server
- Defined in:
- lib/cloudstack-cli/commands/server.rb
Constant Summary
Constants included from CloudstackCli::Helper
CloudstackCli::Helper::ASYNC_STATES
Instance Attribute Summary
Attributes inherited from CloudstackCli::Base
Instance Method Summary collapse
- #bootstrap ⇒ Object
- #create(*names) ⇒ Object
- #destroy(*names) ⇒ Object
- #list ⇒ Object
- #restart(name) ⇒ Object
- #show(name) ⇒ Object
- #start(name) ⇒ Object
- #stop(name) ⇒ Object
Methods inherited from CloudstackCli::Base
Methods included from CloudstackCli::Helper
#ask_number, #bootstrap_server, #bootstrap_server_interactive, #create_port_rules, #create_server, #print_job_status, #print_options, #update_job_status, #watch_jobs
Instance Method Details
#bootstrap ⇒ Object
163 164 165 |
# File 'lib/cloudstack-cli/commands/server.rb', line 163 def bootstrap bootstrap_server_interactive end |
#create(*names) ⇒ Object
104 105 106 107 108 109 110 111 112 113 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 |
# File 'lib/cloudstack-cli/commands/server.rb', line 104 def create(*names) projectid = find_project['id'] if [:project] say "Start deploying servers...", :green jobs = names.map do |name| server = client(quiet: true).get_server(name, project_id: projectid) if server say "Server #{name} (#{server["state"]}) already exists.", :yellow job = { id: 0, name: "Create server #{name}", status: 1 } else job = { id: client.create_server(.merge({name: name, sync: true}))['jobid'], name: "Create server #{name}" } end job end watch_jobs(jobs) if [:port_rules].size > 0 say "Create port forwarding rules...", :green jobs = [] names.each do |name| server = client(quiet: true).get_server(name, project_id: projectid) create_port_rules(server, [:port_rules], false).each_with_index do |job_id, index| jobs << { id: job_id, name: "Create port forwarding ##{index + 1} rules for server #{server['name']}" } end end watch_jobs(jobs) end say "Finished.", :green end |
#destroy(*names) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cloudstack-cli/commands/server.rb', line 145 def destroy(*names) projectid = find_project['id'] if [:project] names.each do |name| server = client.get_server(name, project_id: projectid) unless server say "Server #{name} not found.", :red else ask = "Destroy #{name} (#{server['state']})? [y/N]:" if [:force] || yes?(ask, :yellow) say "destroying #{name} " client.destroy_server(server["id"]) puts end end end end |
#list ⇒ Object
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cloudstack-cli/commands/server.rb', line 10 def list if [:project] if %w(all -1).include? [:project].downcase [:project_id] = -1 project_id = -1 else project = find_project [:project_id] = project['id'] project_id = project['id'] end end servers = client.list_servers() if servers.size < 1 puts "No servers found." else table = [["Name", "Status", "Offering", "Zone", project_id ? "Project" : "Account", "IP's"]] servers.each do |server| table << [ server['name'], server['state'], server['serviceofferingname'], server['zonename'], project_id ? server['project'] : server['account'], server['nic'].map { |nic| nic['ipaddress']}.join(' ') ] end print_table table say "Total number of servers: #{servers.count}" if [:command] args = { project_id: project_id, sync: true, account: [:account] } case [:command].downcase when "start" exit unless yes?("\nStart the server(s) above? [y/N]:", :magenta) jobs = servers.map do |server| {id: client.start_server(server['name'], args)['jobid'], name: "Start server #{server['name']}"} end when "stop" exit unless yes?("\nStop the server(s) above? [y/N]:", :magenta) jobs = servers.map do |server| {id: client.stop_server(server['name'], args)['jobid'], name: "Stop server #{server['name']}"} end when "restart" exit unless yes?("\nRestart the server(s) above? [y/N]:", :magenta) jobs = servers.map do |server| {id: client.reboot_server(server['name'], args)['jobid'], name: "Restart server #{server['name']}"} end else say "\nCommand #{options[:command]} not supported.", :red exit 1 end puts watch_jobs(jobs) end end end |
#restart(name) ⇒ Object
192 193 194 195 196 197 |
# File 'lib/cloudstack-cli/commands/server.rb', line 192 def restart(name) [project_id] = find_project['id'] if [:project] exit unless [:force] || yes?("Reboot server #{name}? [y/N]:", :magenta) client.reboot_server(name, ) puts end |
#show(name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cloudstack-cli/commands/server.rb', line 69 def show(name) if [:project] if [:project].downcase == "all" [:project_id] = -1 else project = find_project [:project_id] = project['id'] end end unless server = client.get_server(name, ) puts "No server found." else server.each do |key, value| say "#{key}: ", :yellow say "#{value}" end end end |
#start(name) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/cloudstack-cli/commands/server.rb', line 181 def start(name) [project_id] = find_project['id'] if [:project] say("Start server #{name}", :magenta) client.start_server(name, ) puts end |
#stop(name) ⇒ Object
171 172 173 174 175 176 |
# File 'lib/cloudstack-cli/commands/server.rb', line 171 def stop(name) [project_id] = find_project['id'] if [:project] exit unless [:force] || yes?("Stop server #{name}? [y/N]:", :magenta) client.stop_server(name, ) puts end |