Class: KnifeCloudstack::CsServerReboot

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

Instance Method Summary collapse

Instance Method Details

#connectionObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/knife/cs_server_reboot.rb', line 80

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



97
98
99
100
# File 'lib/chef/knife/cs_server_reboot.rb', line 97

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

#msg(label, value) ⇒ Object



91
92
93
94
95
# File 'lib/chef/knife/cs_server_reboot.rb', line 91

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

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/cs_server_reboot.rb', line 50

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"
    ui.confirm("Do you really want to reboot this server")
    print "#{ui.color("Rebooting", :magenta)}"

    connection.reboot_server(hostname)
    puts "\n"
    ui.msg("Rebooted server #{hostname}")
  end

end