Class: Knifecosmic::CosmicServerDelete

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

Instance Method Summary collapse

Methods included from Chef::Knife::KnifecosmicBase

included

Instance Method Details

#confirm_action(question) ⇒ Object



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

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

#delete_client(name) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/chef/knife/cosmic_server_delete.rb', line 130

def delete_client(name)
  begin
    client = Chef::ApiClient.load(name)
  rescue Net::HTTPServerException
    return
  end

  client.destroy
  ui.msg "Deleted client #{name}"
end

#delete_node(name) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/knife/cosmic_server_delete.rb', line 141

def delete_node(name)
  begin
    node = Chef::Node.load(name)
  rescue Net::HTTPServerException
    return
  end

  node.destroy
  ui.msg "Deleted node #{name}"
end

#disassociate_virtual_ip_address(server) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef/knife/cosmic_server_delete.rb', line 118

def disassociate_virtual_ip_address(server)
  ip_addr = connection.get_server_public_ip(server)
  return unless ip_addr
  ip_addr_info = connection.get_public_ip_address(ip_addr)
  #Check if Public IP has been allocated and is not Source NAT
  if ip_addr_info
    if not ip_addr_info['issourcenat']
      connection.disassociate_ip_address(ip_addr_info['id'])
    end
  end
end

#runObject



43
44
45
46
47
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
82
# File 'lib/chef/knife/cosmic_server_delete.rb', line 43

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

    if server['state'] == 'Destroyed' then
      ui.warn("Server '#{hostname}' already destroyed")
      connection.delete_server(hostname, true) if confirm_action("Server '#{hostname}' already destroyed, do you want to expunge it?")
      next
    end

    rules = connection.list_port_forwarding_rules

    show_object_details(server, connection, rules)

    result = confirm_action("Do you really want to delete this server")
    if result
      print "#{ui.color("Waiting for deletion", :magenta)}"
      disassociate_virtual_ip_address server
      connection.delete_server(hostname, false)
      puts "\n"
      ui.msg("Deleted server #{hostname}")

      # delete chef client and node
      node_name = hostname  # connection.get_server_fqdn server ## server create doesn't add fqdn!
      delete_chef = locate_config_value(:chefserver) ? confirm_action("Do you want to delete the chef node and client '#{node_name}'") : false
      if delete_chef
        delete_node node_name
        delete_client node_name
      end

    end
  end
end

#show_object_details(s, connection, rules) ⇒ Object



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

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