Class: ForemanMaintain::Cli::Base

Inherits:
Clamp::Command
  • Object
show all
Extended by:
ForemanMaintain::Concerns::Logger
Includes:
ForemanMaintain::Concerns::Finders
Defined in:
lib/foreman_maintain/cli/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ForemanMaintain::Concerns::Logger

logger

Methods included from ForemanMaintain::Concerns::Finders

#check, #detector, #feature, #find_all_scenarios, #find_checks, #find_procedures, #find_scenarios, #procedure

Instance Attribute Details

#runnerObject (readonly)

Returns the value of attribute runner.



8
9
10
# File 'lib/foreman_maintain/cli/base.rb', line 8

def runner
  @runner
end

Class Method Details

.available_tags(collection) ⇒ Object



88
89
90
# File 'lib/foreman_maintain/cli/base.rb', line 88

def self.available_tags(collection)
  collection.inject([]) { |array, item| array.concat(item.tags).uniq }.sort_by(&:to_s)
end

.completion_mapObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/foreman_maintain/cli/base.rb', line 92

def self.completion_map
  completion = {}
  # collect options
  recognised_options.each do |opt|
    opt.switches.each do |switch|
      completion[switch] = completion_types.fetch(switch, {})
    end
  end
  # collect subcommands recursively
  recognised_subcommands.each do |cmd|
    completion[cmd.names.first] = cmd.subcommand_class.completion_map
  end
  # collect params
  completion[:params] = completion_types[:params] unless completion_types[:params].empty?
  completion
end

.completion_typesObject



109
110
111
# File 'lib/foreman_maintain/cli/base.rb', line 109

def self.completion_types
  @completion_types ||= { :params => [] }
end

.dashize(string) ⇒ Object



27
28
29
# File 'lib/foreman_maintain/cli/base.rb', line 27

def self.dashize(string)
  string.to_s.tr('_', '-')
end

.delete_duplicate_assumeyes_if_anyObject



178
179
180
# File 'lib/foreman_maintain/cli/base.rb', line 178

def self.delete_duplicate_assumeyes_if_any
  declared_options.delete_if { |opt| opt.handles?('--assumeyes') }
end

.interactive_optionObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/foreman_maintain/cli/base.rb', line 155

def self.interactive_option
  delete_duplicate_assumeyes_if_any

  option ['-y', '--assumeyes'], :flag,
         'Automatically answer yes for all questions' do |assume|
    ForemanMaintain.reporter.assumeyes = assume
  end

  option(['-w', '--whitelist'], 'whitelist',
         'Comma-separated list of labels of steps to be skipped') do |whitelist|
    raise ArgumentError, 'value not specified' if whitelist.nil? || whitelist.empty?
    whitelist.split(',').map(&:strip)
  end

  option ['-f', '--force'], :flag,
         'Force steps that would be skipped as they were already run'
end

.label_optionObject



136
137
138
139
140
141
142
143
# File 'lib/foreman_maintain/cli/base.rb', line 136

def self.label_option
  option '--label', 'label',
         'Run only a specific check with a label. ' \
           '(Use "list" command to see available labels)' do |label|
    raise ArgumentError, 'value not specified' if label.nil? || label.empty?
    underscorize(label).to_sym
  end
end

.option(switches, type, description, opts = {}, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/foreman_maintain/cli/base.rb', line 113

def self.option(switches, type, description, opts = {}, &block)
  multivalued = opts.delete(:multivalued)
  completion_type = opts.delete(:completion)
  completion_type = { :type => :flag } if completion_type.nil? && type == :flag
  completion_type ||= { :type => :value }
  [switches].flatten(1).each { |s| completion_types[s] = completion_type }
  description += ' (comma-separated list)' if multivalued
  super(switches, type, description, opts) do |value|
    value = CSVParser.new.parse(value) if multivalued
    value = instance_exec(value, &block) if block
    value
  end
end

.parameter(name, description, opts = {}, &block) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/foreman_maintain/cli/base.rb', line 127

def self.parameter(name, description, opts = {}, &block)
  unless [:subcommand_name, :subcommand_arguments].include?(opts[:attribute_name])
    completion_type = opts.delete(:completion)
    completion_type ||= { :type => :value }
    completion_types[:params] << completion_type
  end
  super(name, description, opts, &block)
end

.service_optionsObject



173
174
175
176
# File 'lib/foreman_maintain/cli/base.rb', line 173

def self.service_options
  option '--exclude', 'EXCLUDE', 'A comma-separated list of services to skip'
  option '--only', 'ONLY', 'A comma-separated list of services to include'
end

.subcommand(name, description, subcommand_class = self, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/foreman_maintain/cli/base.rb', line 13

def subcommand(name, description, subcommand_class = self, &block)
  add_command = true
  if subcommand_class.superclass == ForemanMaintain::Cli::Base
    sc = subcommand_class.to_s
    sc.slice!('ForemanMaintain::Cli::')
    if ForemanMaintain.config.disable_commands.include? sc
      logger.info("Disable command #{sc}")
      add_command = false
    end
  end
  super if add_command
end

.tags_optionObject



145
146
147
148
149
150
151
152
153
# File 'lib/foreman_maintain/cli/base.rb', line 145

def self.tags_option
  option('--tags', 'tags',
         'Run only those with all specific set of tags. ' \
           '(Use list-tags command to see available tags)',
         :multivalued => true) do |tags|
    raise ArgumentError, 'value not specified' if tags.nil? || tags.empty?
    tags.map { |tag| underscorize(tag).to_sym }
  end
end

Instance Method Details

#available_checksObject



72
73
74
75
76
# File 'lib/foreman_maintain/cli/base.rb', line 72

def available_checks
  filter = {}
  filter[:tags] = tags if respond_to?(:tags)
  ForemanMaintain.available_checks(filter)
end

#available_proceduresObject



78
79
80
81
82
# File 'lib/foreman_maintain/cli/base.rb', line 78

def available_procedures
  filter = {}
  filter[:tags] = tags if respond_to?(:tags)
  ForemanMaintain.available_procedures(filter)
end

#available_tags(collection) ⇒ Object



84
85
86
# File 'lib/foreman_maintain/cli/base.rb', line 84

def available_tags(collection)
  self.class.available_tags(collection)
end

#dashize(string) ⇒ Object



31
32
33
# File 'lib/foreman_maintain/cli/base.rb', line 31

def dashize(string)
  self.class.dashize(string)
end

#label_string(string) ⇒ Object



39
40
41
# File 'lib/foreman_maintain/cli/base.rb', line 39

def label_string(string)
  HighLine.color("[#{dashize(string)}]", :yellow)
end

#option_wrapper(option) ⇒ Object



182
183
184
# File 'lib/foreman_maintain/cli/base.rb', line 182

def option_wrapper(option)
  respond_to?(option.to_sym) ? send(option) : false
end


47
48
49
50
51
# File 'lib/foreman_maintain/cli/base.rb', line 47

def print_check_info(check)
  desc = "#{label_string(check.label)} #{check.description}".ljust(80)
  tags = check.tags.map { |t| tag_string(t) }.join(' ').to_s
  puts "#{desc} #{tags}".strip
end

#reporterObject



53
54
55
# File 'lib/foreman_maintain/cli/base.rb', line 53

def reporter
  @reporter ||= ForemanMaintain.reporter
end

#run_scenario(scenarios, rescue_scenario = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/foreman_maintain/cli/base.rb', line 57

def run_scenario(scenarios, rescue_scenario = nil)
  @runner ||=
    ForemanMaintain::Runner.new(reporter, scenarios,
                                :assumeyes => option_wrapper('assumeyes?'),
                                :whitelist => option_wrapper('whitelist') || [],
                                :force => option_wrapper('force?'),
                                :rescue_scenario => rescue_scenario)
  runner.run
end

#run_scenarios_and_exit(scenarios, rescue_scenario: nil) ⇒ Object



67
68
69
70
# File 'lib/foreman_maintain/cli/base.rb', line 67

def run_scenarios_and_exit(scenarios, rescue_scenario: nil)
  run_scenario(scenarios, rescue_scenario)
  exit runner.exit_code
end

#tag_string(string) ⇒ Object



43
44
45
# File 'lib/foreman_maintain/cli/base.rb', line 43

def tag_string(string)
  HighLine.color("[#{dashize(string)}]", :cyan)
end

#underscorize(string) ⇒ Object



35
36
37
# File 'lib/foreman_maintain/cli/base.rb', line 35

def underscorize(string)
  string.to_s.tr('-', '_')
end