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



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

def after_exec_command
end

#before_exec_commandObject

Derived classes can override before_exec_command and after_exec_command



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

def before_exec_command
end

#create_service_instanceObject

Raises:

  • (Chef::Exceptions::Override)


61
62
63
# File 'lib/chef/knife/cloud/command.rb', line 61

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

#execute_commandObject

Raises:

  • (Chef::Exceptions::Override)


65
66
67
# File 'lib/chef/knife/cloud/command.rb', line 65

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

#pretty_key(key) ⇒ Object



93
94
95
# File 'lib/chef/knife/cloud/command.rb', line 93

def pretty_key(key)
  key.to_s.gsub(/_/, ' ').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
58
59
# File 'lib/chef/knife/cloud/command.rb', line 31

def run
  begin
    # 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
end

#set_default_configObject



76
77
# File 'lib/chef/knife/cloud/command.rb', line 76

def set_default_config
end

#validate!(*keys) ⇒ Object



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

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



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

def validate_params!
end