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)


211
212
213
# File 'lib/chef/knife/cloud/fog/service.rb', line 211

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

#allocate_address(pool = nil) ⇒ Object



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

def allocate_address(pool = nil)
  response = connection.allocate_address(pool)
  response.body
rescue Fog::Compute::OpenStack::NotFound => e
  error_message = 'Floating ip pool not found.'
  ui.error(error_message)
  raise CloudExceptions::NotFoundError, "#{e.message}"
rescue Excon::Errors::Forbidden => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
end

#associate_address(*args) ⇒ Object



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

def associate_address(*args)
  connection.associate_address(*args)
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
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



207
208
209
# File 'lib/chef/knife/cloud/fog/service.rb', line 207

def delete_server_on_failure(server = nil)
  server.destroy unless server.nil?
end

#disassociate_address(*args) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/chef/knife/cloud/fog/service.rb', line 196

def disassociate_address(*args)
  connection.disassociate_address(*args)
rescue Fog::Compute::OpenStack::NotFound
  error_message = 'Floating ip not found.'
  ui.error(error_message)
rescue Excon::Errors::UnprocessableEntity => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
end

#get_address(address_id) ⇒ Object



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

def get_address(address_id)
  connection.get_address(address_id)
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
end

#get_address_ip(response) ⇒ Object



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

def get_address_ip(response)
  response.body['floating_ip']['ip'] if response.body['floating_ip']
end

#get_flavor(name_or_id) ⇒ Object



229
230
231
# File 'lib/chef/knife/cloud/fog/service.rb', line 229

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



225
226
227
# File 'lib/chef/knife/cloud/fog/service.rb', line 225

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



219
220
221
222
223
# File 'lib/chef/knife/cloud/fog/service.rb', line 219

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

#get_server_name(server) ⇒ Object



215
216
217
# File 'lib/chef/knife/cloud/fog/service.rb', line 215

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[response.keys[0]]['code']}): #{response[response.keys[0]]['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)


250
251
252
253
# File 'lib/chef/knife/cloud/fog/service.rb', line 250

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

#list_addressesObject



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

def list_addresses
  connection.addresses.all
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::CloudAPIException, e)
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
    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

#release_address(address_id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/chef/knife/cloud/fog/service.rb', line 153

def release_address(address_id)
  response = get_address(address_id)
  msg_pair('IP address', get_address_ip(response))
  puts
  ui.confirm('Do you really want to delete this ip')
  connection.release_address(address_id)
rescue Fog::Compute::OpenStack::NotFound => e
  error_message = 'Floating ip not found.'
  ui.error(error_message)
  raise CloudExceptions::NotFoundError, "#{e.message}"
rescue Excon::Errors::BadRequest => e
  handle_excon_exception(CloudExceptions::KnifeCloudError, e)
end

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



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/chef/knife/cloud/fog/service.rb', line 233

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