Class: Knifecosmic::CosmicServerStop

Inherits:
Chef::Knife show all
Includes:
Chef::Knife::KnifecosmicBase
Defined in:
lib/chef/knife/cosmic_server_stop.rb

Instance Method Summary collapse

Methods included from Chef::Knife::KnifecosmicBase

included

Instance Method Details

#confirm_action(question) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/chef/knife/cosmic_server_stop.rb', line 107

def confirm_action(question)
  return true if locate_config_value(:yes)
  result = ui.ask_question(question, :default => "Y" )
  if result == "Y" || result == "y" then
    return true
  else
    return false
  end
end

#runObject



48
49
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
79
80
81
# File 'lib/chef/knife/cosmic_server_stop.rb', line 48

def run
  validate_base_options

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

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

    rules = connection.list_port_forwarding_rules
   
    show_object_details(server, connection, rules) 

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

#show_object_details(s, connection, rules) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/cosmic_server_stop.rb', line 83

def show_object_details(s, connection, rules)
  return if locate_config_value(:yes)
  
  object_fields = []
  object_fields << ui.color("Name:", :cyan)
  object_fields << s['name'].to_s
  object_fields << ui.color("Public IP:", :cyan)
  object_fields << (connection.get_server_public_ip(s, rules) || '')
  object_fields << ui.color("Service:", :cyan)
  object_fields << s['serviceofferingname'].to_s
  object_fields << ui.color("Template:", :cyan)
  object_fields << s['templatename']
  object_fields << ui.color("Domain:", :cyan)
  object_fields << s['domain']
  object_fields << ui.color("Zone:", :cyan)
  object_fields << s['zonename']
  object_fields << ui.color("State:", :cyan)
  object_fields << s['state']

  puts "\n"
  puts ui.list(object_fields, :uneven_columns_across, 2)
  puts "\n"
end