Class: Chef::Knife::Cloud::FogService

Inherits:
Service
  • Object
show all
Defined in:
lib/chef/knife/cloud/fog/service.rb

Instance Attribute Summary

Attributes inherited from Service

#ui

Instance Method Summary collapse

Methods inherited from Service

#add_custom_attributes, #create_server_dependencies, #delete_server_dependencies, #list_images, #list_resource_allocations, #list_servers

Methods included from Helpers

#locate_config_value, #msg_pair

Constructor Details

#initialize(options = {}) ⇒ FogService

Returns a new instance of FogService.



15
16
17
18
# File 'lib/chef/knife/cloud/fog/service.rb', line 15

def initialize(options = {})
  load_fog_gem
  super
end

Instance Method Details

#add_api_endpointObject

Raises:

  • (Chef::Exceptions::Override)


151
152
153
# File 'lib/chef/knife/cloud/fog/service.rb', line 151

def add_api_endpoint
  raise Chef::Exceptions::Override, "You must override add_api_endpoint in #{self.to_s} to add endpoint in auth_params for connection"
end

#connectionObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/knife/cloud/fog/service.rb', line 32

def connection
  add_api_endpoint
  @connection ||= begin
    connection  = Fog::Compute.new(@auth_params)
                  rescue Excon::Errors::Unauthorized => e
                    error_message = "Connection failure, please check your username and password."
                    ui.fatal(error_message)
                    raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
                  rescue Excon::Errors::SocketError => e
                    error_message = "Connection failure, please check your authentication URL."
                    ui.fatal(error_message)
                    raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
                  end
end

#create_server(options = {}) ⇒ Object

cloud server specific implementation methods for commands.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/knife/cloud/fog/service.rb', line 66

def create_server(options = {})
  begin
    add_custom_attributes(options[:server_def])
    server = connection.servers.create(options[:server_def])
  rescue Excon::Errors::BadRequest => e
    response = Chef::JSONCompat.from_json(e.response.body)
    if response['badRequest']['code'] == 400
      message = "Bad request (400): #{response['badRequest']['message']}"
      ui.fatal(message)
    else
      message = "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
      ui.fatal(message)
    end
    raise CloudExceptions::ServerCreateError, message
  rescue Fog::Errors::Error => e
    raise CloudExceptions::ServerCreateError, e.message
  end

  print "\n#{ui.color("Waiting for server [wait time = #{options[:server_create_timeout]}]", :magenta)}"

  # wait for it to be ready to do stuff
  server.wait_for(Integer(options[:server_create_timeout])) { print "."; ready? }

  puts("\n")
  server
end

#delete_server(server_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/knife/cloud/fog/service.rb', line 93

def delete_server(server_name)
  begin
    server = get_server(server_name)
    msg_pair("Instance Name", get_server_name(server))
    msg_pair("Instance ID", server.id)

    puts "\n"
    ui.confirm("Do you really want to delete this server")

    # delete the server
    server.destroy
  rescue NoMethodError
    error_message = "Could not locate server '#{server_name}'."
    ui.error(error_message)
    raise CloudExceptions::ServerDeleteError, error_message
  rescue Excon::Errors::BadRequest => e
    handle_excon_exception(CloudExceptions::ServerDeleteError, e)
  end
end

#delete_server_on_failure(server = nil) ⇒ Object



147
148
149
# File 'lib/chef/knife/cloud/fog/service.rb', line 147

def delete_server_on_failure(server = nil)
  server.destroy if ! server.nil?
end

#get_flavor(name_or_id) ⇒ Object



171
172
173
# File 'lib/chef/knife/cloud/fog/service.rb', line 171

def get_flavor(name_or_id)
  connection.flavors.find{|f| f.name == name_or_id || f.id == name_or_id }
end

#get_image(name_or_id) ⇒ Object



167
168
169
# File 'lib/chef/knife/cloud/fog/service.rb', line 167

def get_image(name_or_id)
  connection.images.find{|img| img.name =~ /#{name_or_id}/ || img.id == name_or_id }
end

#get_server(instance_id) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/chef/knife/cloud/fog/service.rb', line 159

def get_server(instance_id)
  begin
    server = connection.servers.get(instance_id)
  rescue Excon::Errors::BadRequest => e
    handle_excon_exception(CloudExceptions::KnifeCloudError, e)
  end
end

#get_server_name(server) ⇒ Object



155
156
157
# File 'lib/chef/knife/cloud/fog/service.rb', line 155

def get_server_name(server)
  server.name
end

#handle_excon_exception(exception_class, e) ⇒ Object

Raises:

  • (exception_class)


128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/knife/cloud/fog/service.rb', line 128

def handle_excon_exception(exception_class, e)
  error_message = if e.response
                    response = Chef::JSONCompat.from_json(e.response.body)
                    "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
                  else
                    "Unknown server error : #{e.message}"
                  end
  ui.fatal(error_message)
  raise exception_class, error_message
end

#is_image_windows?(image) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
# File 'lib/chef/knife/cloud/fog/service.rb', line 192

def is_image_windows?(image)
  image_info = connection.images.get(image)
  !image_info.nil? ? image_info.platform == 'windows' : false
end

#list_resource_configurationsObject



139
140
141
142
143
144
145
# File 'lib/chef/knife/cloud/fog/service.rb', line 139

def list_resource_configurations
  begin
    flavors = connection.flavors.all
  rescue Excon::Errors::BadRequest => e
    handle_excon_exception(CloudExceptions::CloudAPIException, e)
  end
end

#load_fog_gemObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chef/knife/cloud/fog/service.rb', line 20

def load_fog_gem
  begin
    # Load specific version of fog. Any other classes/modules using fog are loaded after this.
    gem "fog", Chef::Config[:knife][:cloud_fog_version]
    require 'fog'
    Chef::Log.debug("Using fog version: #{Gem.loaded_specs["fog"].version}")
  rescue Exception => e
    Chef::Log.error "Error loading fog gem."
    exit 1
  end
end

#networkObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/knife/cloud/fog/service.rb', line 47

def network
  @network ||= begin
    network = Fog::Network.new(@auth_params)
              rescue Excon::Errors::Unauthorized => e
                error_message = "Connection failure, please check your username and password."
                ui.fatal(error_message)
                raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
              rescue Excon::Errors::SocketError => e
                error_message = "Connection failure, please check your authentication URL."
                ui.fatal(error_message)
                raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
              rescue Fog::Errors::NotFound => e
                error_message = "No Network service found. This command is unavailable with current cloud."
                ui.fatal(error_message)
                raise CloudExceptions::NetworkNotFoundError, "#{e.message}. #{error_message}"
              end
end

#server_summary(server, columns_with_info = []) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chef/knife/cloud/fog/service.rb', line 175

def server_summary(server, columns_with_info = [])
  # columns_with_info is array of hash with label, key and attribute extraction callback, ex [{:label => "Label text", :key => 'key', value => 'the_actual_value', value_callback => callback_method to extract/format the required value}, ...]
  list = []
  columns_with_info.each do |col_info|
    value = if col_info[:value].nil?
              (col_info[:value_callback].nil? ? server.send(col_info[:key]).to_s : col_info[:value_callback].call(server.send(col_info[:key])))
            else
              col_info[:value]
            end
    if !(value.nil? || value.empty?)
      list << ui.color(col_info[:label], :bold)
      list << value
    end
  end
  puts ui.list(list, :uneven_columns_across, 2) if columns_with_info.length > 0
end