Class: Aws::Cfn::Stacker::StackerBase

Inherits:
Object
  • Object
show all
Extended by:
ConvertToClassName, Mixlib::CLI::ClassMethods
Includes:
Mixlib::CLI
Defined in:
lib/aws/cfn/stacker/base.rb

Direct Known Subclasses

StackerApplication

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConvertToClassName

convert_to_class_name, convert_to_snake_case, filename_to_qualified_string, snake_case_basename

Class Attribute Details

.stacker_cmdObject

Returns the value of attribute stacker_cmd.



187
188
189
# File 'lib/aws/cfn/stacker/base.rb', line 187

def stacker_cmd
  @stacker_cmd
end

Class Method Details

.category(new_category) ⇒ Object

Explicitly set the category for the current command to new_category The category is normally determined from the first word of the command name, but some commands make more sense using two or more words

Arguments

new_category:

A String to set the category to (see examples)

Examples:

Data bag commands would be in the ‘data’ category by default. To put them in the ‘data bag’ category:

category('data bag')


62
63
64
# File 'lib/aws/cfn/stacker/base.rb', line 62

def category(new_category)
  @category = new_category
end

.cmd=(cmd) ⇒ Object



188
189
190
# File 'lib/aws/cfn/stacker/base.rb', line 188

def cmd=(cmd)
  @stacker_cmd = cmd
end

.common_nameObject



74
75
76
# File 'lib/aws/cfn/stacker/base.rb', line 74

def common_name
  snake_case_name.split('_').join(' ')
end

.dependency_loadersObject



129
130
131
# File 'lib/aws/cfn/stacker/base.rb', line 129

def dependency_loaders
  @dependency_loaders ||= []
end

.guess_category(args) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/aws/cfn/stacker/base.rb', line 175

def guess_category(args)
  category_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }
  category_words.map! {|w| w.split('-')}.flatten!
  matching_category = nil
  while (!matching_category) && (!category_words.empty?)
    candidate_category = category_words.join(' ')
    matching_category = candidate_category if subcommands_by_category.key?(candidate_category)
    matching_category || category_words.pop
  end
  matching_category
end

.inherited(subclass) ⇒ Object



47
48
49
50
51
# File 'lib/aws/cfn/stacker/base.rb', line 47

def inherited(subclass)
  unless subclass.unnamed?
    subcommands[subclass.snake_case_name] = subclass
  end
end

.list_commands(preferred_category = nil) ⇒ Object

Print the list of subcommands knife knows about. If preferred_category is given, only subcommands in that category are shown



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/aws/cfn/stacker/base.rb', line 107

def list_commands(preferred_category=nil)
  load_commands

  category_desc = preferred_category ? preferred_category + " " : ''
  puts "Available #{category_desc}subcommands: (for details, #{@stacker_cmd} SUB-COMMAND --help)\n\n"

  if preferred_category && subcommands_by_category.key?(preferred_category)
    commands_to_show = {preferred_category => subcommands_by_category[preferred_category]}
  else
    commands_to_show = subcommands_by_category
  end

  commands_to_show.sort.each do |category, commands|
    next if category =~ /deprecated/i
    puts "** #{category.upcase} COMMANDS **"
    commands.sort.each do |command|
      puts subcommands[command].banner if subcommands[command]
    end
    puts
  end
end

.load_commandsObject



87
88
89
# File 'lib/aws/cfn/stacker/base.rb', line 87

def load_commands
  @commands_loaded ||= subcommand_loader.load_commands
end

.load_depsObject



137
138
139
140
141
# File 'lib/aws/cfn/stacker/base.rb', line 137

def load_deps
  dependency_loaders.each do |dep_loader|
    dep_loader.call
  end
end

.requires(&block) ⇒ Object



133
134
135
# File 'lib/aws/cfn/stacker/base.rb', line 133

def requires(&block)
  dependency_loaders << block
end

.reset_config_path!Object

noinspection RubyClassVariableUsageInspection



20
21
22
# File 'lib/aws/cfn/stacker/base.rb', line 20

def self.reset_config_path!
  @@stacker_config_dir = nil
end

.reset_subcommands!Object



42
43
44
45
# File 'lib/aws/cfn/stacker/base.rb', line 42

def reset_subcommands!
  @subcommands = {}
  @subcommands_by_category = nil
end

.snake_case_nameObject



70
71
72
# File 'lib/aws/cfn/stacker/base.rb', line 70

def snake_case_name
  convert_to_snake_case(name.split('::').last) unless unnamed?
end

.stacker_config_dirObject

search upward from current_dir until .chef directory is found noinspection RubyClassVariableUsageInspection



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aws/cfn/stacker/base.rb', line 27

def stacker_config_dir
  if @@stacker_config_dir.nil? # share this with subclasses
    @@stacker_config_dir = false
    full_path = Dir.pwd.split(File::SEPARATOR)
    (full_path.length - 1).downto(0) do |i|
      candidate_directory = File.join(full_path[0..i] + ["config" ])
      if File.exist?(candidate_directory) && File.directory?(candidate_directory)
        @@stacker_config_dir = candidate_directory
        break
      end
    end
  end
  @@stacker_config_dir
end

.subcommand_categoryObject



66
67
68
# File 'lib/aws/cfn/stacker/base.rb', line 66

def subcommand_category
  @category || snake_case_name.split('_').first unless unnamed?
end

.subcommand_class_from(args) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/aws/cfn/stacker/base.rb', line 143

def subcommand_class_from(args)
  command_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }

  subcommand_class = nil

  while ( !subcommand_class ) && ( !command_words.empty? )
    snake_case_class_name = command_words.join("_")
    subcommand_class = subcommands[snake_case_class_name]
    unless subcommand_class
      command_words.pop
    end
  end
  # see if we got the command as e.g., knife node-list
  subcommand_class ||= subcommands[args.first.gsub('-', '_')]
  subcommand_class || subcommand_not_found!(args)
end

.subcommand_loaderObject



83
84
85
# File 'lib/aws/cfn/stacker/base.rb', line 83

def subcommand_loader
  @subcommand_loader ||= SubcommandLoader.new(stacker_config_dir)
end

.subcommand_not_found!(args) ⇒ Object

:nodoc: Error out and print usage. probably becuase the arguments given by the user could not be resolved to a subcommand.



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/aws/cfn/stacker/base.rb', line 163

def subcommand_not_found!(args)
  puts "Cannot find sub command for: '#{args.join(' ')}'"

  if category_commands = guess_category(args)
    list_commands(category_commands)
  else
    list_commands
  end

  exit 10
end

.subcommandsObject



91
92
93
# File 'lib/aws/cfn/stacker/base.rb', line 91

def subcommands
  @@subcommands ||= {}
end

.subcommands_by_categoryObject



95
96
97
98
99
100
101
102
103
# File 'lib/aws/cfn/stacker/base.rb', line 95

def subcommands_by_category
  unless @subcommands_by_category
    @subcommands_by_category = Hash.new { |hash, key| hash[key] = [] }
    subcommands.each do |snake_cased, klass|
      @subcommands_by_category[klass.subcommand_category] << snake_cased
    end
  end
  @subcommands_by_category
end

.unnamed?Boolean

Does this class have a name? (Classes created via Class.new don’t)

Returns:

  • (Boolean)


79
80
81
# File 'lib/aws/cfn/stacker/base.rb', line 79

def unnamed?
  name.nil? || name.empty?
end

Instance Method Details

#run_applicationObject

Actually run the application



200
201
202
# File 'lib/aws/cfn/stacker/base.rb', line 200

def run_application
  logger.warn "#{self.to_s}: you must override run_application"
end

#setup_applicationObject

Called prior to starting the application, by the run method



195
196
197
# File 'lib/aws/cfn/stacker/base.rb', line 195

def setup_application
  logger.warn "#{self.to_s}: you must override setup_application"
end