Class: Chef::Knife::Cloud::Command

Inherits:
Chef::Knife show all
Includes:
Helpers
Defined in:
lib/chef/knife/cloud/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#locate_config_value, #msg_pair

Instance Attribute Details

#custom_argumentsObject

Returns the value of attribute custom_arguments.



29
30
31
# File 'lib/chef/knife/cloud/command.rb', line 29

def custom_arguments
  @custom_arguments
end

#serviceObject

Returns the value of attribute service.



29
30
31
# File 'lib/chef/knife/cloud/command.rb', line 29

def service
  @service
end

Instance Method Details

#after_exec_commandObject



71
72
# File 'lib/chef/knife/cloud/command.rb', line 71

def after_exec_command
end

#before_exec_commandObject

Derived classes can override before_exec_command and after_exec_command



68
69
# File 'lib/chef/knife/cloud/command.rb', line 68

def before_exec_command
end

#create_service_instanceObject

Raises:

  • (Chef::Exceptions::Override)


59
60
61
# File 'lib/chef/knife/cloud/command.rb', line 59

def create_service_instance
  raise Chef::Exceptions::Override, "You must override create_service_instance in #{self} to create cloud specific service"
end

#execute_commandObject

Raises:

  • (Chef::Exceptions::Override)


63
64
65
# File 'lib/chef/knife/cloud/command.rb', line 63

def execute_command
  raise Chef::Exceptions::Override, "You must override execute_command in #{self}"
end

#pretty_key(key) ⇒ Object



91
92
93
# File 'lib/chef/knife/cloud/command.rb', line 91

def pretty_key(key)
  key.to_s.tr("_", " ").gsub(/\w+/) { |w| (w =~ /(ssh)|(aws)/i) ? w.upcase : w.capitalize }
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/knife/cloud/command.rb', line 31

def run
    # Set dafult config
  set_default_config

  # validate compulsory params
  validate!

  # validate command pre-requisites (cli options)
  validate_params!

  # setup the service
  @service = create_service_instance

  service.ui = ui # for interactive user prompts/messages

  # Perform any steps before handling the command
  before_exec_command

  # exec the actual cmd
  execute_command

  # Perform any steps after handling the command
  after_exec_command
rescue CloudExceptions::KnifeCloudError => e
  Chef::Log.debug(e.message)
  exit 1
end

#set_default_configObject



74
75
# File 'lib/chef/knife/cloud/command.rb', line 74

def set_default_config
end

#validate!(*keys) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/chef/knife/cloud/command.rb', line 77

def validate!(*keys)
  # validates necessary options/params to carry out the command.
  # subclasses to implement this.
  errors = []
  keys.each do |k|
    errors << "You did not provide a valid '#{pretty_key(k)}' value." if locate_config_value(k).nil?
  end
  error_message = ""
  raise CloudExceptions::ValidationError, error_message if errors.each { |e| ui.error(e); error_message = "#{error_message} #{e}." }.any?
end

#validate_params!Object



88
89
# File 'lib/chef/knife/cloud/command.rb', line 88

def validate_params!
end