Class: Server

Inherits:
CloudstackCli::Base show all
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

#config

Instance Method Summary collapse

Methods inherited from CloudstackCli::Base

exit_on_failure?

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

#bootstrapObject



150
151
152
# File 'lib/cloudstack-cli/commands/server.rb', line 150

def bootstrap
  bootstrap_server_interactive
end

#create(*names) ⇒ Object



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
121
122
123
124
125
126
127
# File 'lib/cloudstack-cli/commands/server.rb', line 91

def create(*names)
  projectid = find_project['id'] if options[: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(options.merge({name: name, sync: true}))['jobid'],
        name: "Create server #{name}"
      }
    end
    job
  end
  watch_jobs(jobs)
  if options[: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, options[: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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloudstack-cli/commands/server.rb', line 132

def destroy(*names)
  projectid = find_project['id'] if options[: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 options[:force] || yes?(ask, :yellow)
        say "destroying #{name} "
        client.destroy_server(server["id"])
        puts  
      end
    end
  end
end

#listObject



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
# File 'lib/cloudstack-cli/commands/server.rb', line 10

def list
  if options[:project]
    project_id = find_project['id']
    options[:project_id] = project_id
  end
  servers = client.list_servers(options)
  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 options[:command]
      args = { project_id: project_id, sync: true, account: options[:account] }
      case options[: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



179
180
181
182
183
184
# File 'lib/cloudstack-cli/commands/server.rb', line 179

def restart(name)
  options[project_id] = find_project['id'] if options[:project]
  exit unless options[:force] || yes?("Reboot server #{name}? [y/N]:", :magenta)
  client.reboot_server(name, options)
  puts
end

#show(name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cloudstack-cli/commands/server.rb', line 63

def show(name)
  options[:project_id] = find_project['id']
  unless server = client.get_server(name, options)
    puts "No server found."
  else
    server.each do |key, value|
      say "#{key}: ", :yellow
      say "#{value}"
    end
  end
end

#start(name) ⇒ Object



168
169
170
171
172
173
# File 'lib/cloudstack-cli/commands/server.rb', line 168

def start(name)
  options[project_id] = find_project['id'] if options[:project]
  say("Start server #{name}", :magenta)
  client.start_server(name, options)
  puts
end

#stop(name) ⇒ Object



158
159
160
161
162
163
# File 'lib/cloudstack-cli/commands/server.rb', line 158

def stop(name)
  options[project_id] = find_project['id'] if options[:project]
  exit unless options[:force] || yes?("Stop server #{name}? [y/N]:", :magenta)
  client.stop_server(name, options)
  puts
end