Class: KnifeCloudstack::CsServerStop

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/cs_server_stop.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/cs_server_stop.rb', line 91

def connection
  unless @connection
    @connection = CloudstackClient::Connection.new(
        locate_config_value(:cloudstack_url),
        locate_config_value(:cloudstack_api_key),
        locate_config_value(:cloudstack_secret_key)
    )
  end
  @connection
end

#locate_config_value(key) ⇒ Object



108
109
110
111
# File 'lib/chef/knife/cs_server_stop.rb', line 108

def locate_config_value(key)
  key = key.to_sym
  Chef::Config[:knife][key] || config[key]
end

#msg(label, value) ⇒ Object



102
103
104
105
106
# File 'lib/chef/knife/cs_server_stop.rb', line 102

def msg(label, value)
  if value && !value.empty?
    puts "#{ui.color(label, :cyan)}: #{value}"
  end
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/knife/cs_server_stop.rb', line 55

def run

  @name_args.each do |hostname|
    server = connection.get_server(hostname)

    if !server then
      ui.error("Server '#{hostname}' not found")
      next
    end

    puts "\n"
    msg("Name", server['name'])
    msg("Public IP", connection.get_server_public_ip(server) || '?')
    msg("Service", server['serviceofferingname'])
    msg("Template", server['templatename'])
    msg("Domain", server['domain'])
    msg("Zone", server['zonename'])
    msg("State", server['state'])

    puts "\n"
    if config[:cloudstack_force_stop]
      ui.confirm("Do you really want to force stop this server")
      print "#{ui.color("Forcefully stopping", :magenta)}"
      connection.stop_server(hostname,config[:cloudstack_force_stop])
    else
      ui.confirm("Do you really want to stop this server")
      print "#{ui.color("Stopping", :magenta)}"
      connection.stop_server(hostname)
    end

    puts "\n"
    ui.msg("Stopped server #{hostname}")
  end

end