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



146
147
148
# File 'lib/cloudstack-cli/commands/server.rb', line 146

def bootstrap
  bootstrap_server_interactive
end

#create(*names) ⇒ Object



81
82
83
84
85
86
87
88
89
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
116
117
# File 'lib/cloudstack-cli/commands/server.rb', line 81

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cloudstack-cli/commands/server.rb', line 123

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"], {
            sync: false,
            expunge: options[:expunge]
          }
        )
        puts
      end
    end
  end
end

#listObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloudstack-cli/commands/server.rb', line 18

def list
  if options[:project]
    options[:project_id] = find_project['id']
    options[:project] = nil
  end
  options[:custom] = { 'storageid' => options[:storage_id] } if options[:storage_id]
  client.verbose = true
  servers = client.list_servers(options)
  if servers.size < 1
    puts "No servers found."
  else
    print_servers(servers)
    execute_server_commands(servers) if options[:command]
  end
end

#list_from_file(file) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/cloudstack-cli/commands/server.rb', line 41

def list_from_file(file)
  servers = parse_file(file)["servers"]
  if servers.size < 1
    puts "No servers found."
  else
    print_servers(servers)
    execute_server_commands(servers) if options[:command]
  end
end

#reboot(name) ⇒ Object



175
176
177
178
179
180
# File 'lib/cloudstack-cli/commands/server.rb', line 175

def reboot(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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloudstack-cli/commands/server.rb', line 53

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

#start(name) ⇒ Object



164
165
166
167
168
169
# File 'lib/cloudstack-cli/commands/server.rb', line 164

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

#stop(name) ⇒ Object



154
155
156
157
158
159
# File 'lib/cloudstack-cli/commands/server.rb', line 154

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