Class: Kafo::KafoConfigure

Inherits:
Clamp::Command
  • Object
show all
Includes:
StringHelper
Defined in:
lib/kafo/kafo_configure.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StringHelper

#dashize, #parametrize, #underscore, #with_prefix

Constructor Details

#initialize(*args) ⇒ KafoConfigure

Returns a new instance of KafoConfigure.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
92
93
94
95
96
97
98
99
100
101
# File 'lib/kafo/kafo_configure.rb', line 46

def initialize(*args)
  self.class.preset_color_scheme
  self.class.logger           = Logger.new
  self.class.exit_handler     = ExitHandler.new
  @progress_bar               = nil
  @config_reload_requested    = false

  scenario_manager = setup_scenario_manager
  self.class.scenario_manager = scenario_manager

  # Handle --list-scenarios before we need them
  scenario_manager.list_available_scenarios if ARGV.include?('--list-scenarios')
  scenario_manager.check_enable_scenario
  scenario_manager.check_disable_scenario
  setup_config(config_file)

  self.class.hooking.execute(:pre_migrations)
  reload_config
  applied_total = self.class.config.run_migrations
  request_config_reload if applied_total > 0

  if ARGV.include?('--migrations-only')
    self.class.verbose = (ARGV.include?('--verbose') || ARGV.include?('-v'))
    Logger.setup
    self.class.logger.info('Log buffers flushed')
    self.class.exit(0)
  end

  reload_config

  if scenario_manager.configured?
    scenario_manager.check_scenario_change(self.class.config_file)
    if scenario_manager.scenario_changed?(self.class.config_file) && !self.class.in_help_mode?
      prev_config = scenario_manager.load_configuration(scenario_manager.previous_scenario)
      prev_config.run_migrations
      self.class.config.migrate_configuration(prev_config, :skip => [:log_name])
      setup_config(self.class.config_file)
      self.class.logger.info("Due to scenario change the configuration (#{self.class.config_file}) was updated with #{scenario_manager.previous_scenario} and reloaded.")
    end
  end

  super

  self.class.hooking.execute(:boot)
  set_app_options # define args for installer
  # we need to parse app config params using clamp even before run method does it
  # so we limit parsing only to app config options (because of --help and later defined params)
  parse clamp_app_arguments
  parse_app_arguments # set values from ARGS to config.app
  Logger.setup
  self.class.set_color_scheme

  self.class.hooking.execute(:init)
  set_parameters # here the params gets parsed and we need app config populated
  set_options
end

Class Attribute Details

.app_optionsObject

Returns the value of attribute app_options.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def app_options
  @app_options
end

.check_dirsObject

Returns the value of attribute check_dirs.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def check_dirs
  @check_dirs
end

.configObject

Returns the value of attribute config.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def config
  @config
end

.config_fileObject

Returns the value of attribute config_file.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def config_file
  @config_file
end

.exit_handlerObject

Returns the value of attribute exit_handler.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def exit_handler
  @exit_handler
end

.gem_rootObject

Returns the value of attribute gem_root.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def gem_root
  @gem_root
end

.hookingObject



41
42
43
# File 'lib/kafo/kafo_configure.rb', line 41

def hooking
  @hooking ||= Hooking.new
end

.kafo_modules_dirObject

Returns the value of attribute kafo_modules_dir.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def kafo_modules_dir
  @kafo_modules_dir
end

.loggerObject

Returns the value of attribute logger.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def logger
  @logger
end

.module_dirsObject

Returns the value of attribute module_dirs.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def module_dirs
  @module_dirs
end

.root_dirObject

Returns the value of attribute root_dir.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def root_dir
  @root_dir
end

.scenario_managerObject

Returns the value of attribute scenario_manager.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def scenario_manager
  @scenario_manager
end

.storeObject

Returns the value of attribute store.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def store
  @store
end

.verboseObject

Returns the value of attribute verbose.



36
37
38
# File 'lib/kafo/kafo_configure.rb', line 36

def verbose
  @verbose
end

Class Method Details

.app_option(*args, &block) ⇒ Object



187
188
189
190
191
# File 'lib/kafo/kafo_configure.rb', line 187

def self.app_option(*args, &block)
  self.app_options ||= []
  self.app_options.push self.option(*args, &block)
  self.app_options.last
end

.exit(code, &block) ⇒ Object



159
160
161
# File 'lib/kafo/kafo_configure.rb', line 159

def self.exit(code, &block)
  exit_handler.exit(code, &block)
end

.exit_codeObject



163
164
165
# File 'lib/kafo/kafo_configure.rb', line 163

def self.exit_code
  self.exit_handler.exit_code
end

.help(*args) ⇒ Object



180
181
182
183
184
185
# File 'lib/kafo/kafo_configure.rb', line 180

def self.help(*args)
  kafo          = args.pop
  builder_class = kafo.full_help? ? HelpBuilders::Advanced : HelpBuilders::Basic
  args.push builder_class.new(kafo.params)
  super(*args)
end

.in_help_mode?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/kafo/kafo_configure.rb', line 167

def self.in_help_mode?
  ARGV.include?('--help') || ARGV.include?('--full-help') || ARGV.include?('-h')
end

.runObject



153
154
155
156
157
# File 'lib/kafo/kafo_configure.rb', line 153

def self.run
  return super
rescue SystemExit => e
  self.exit_handler.exit(self.exit_code) # fail in initialize
end

Instance Method Details

#add_module(name) ⇒ Object



205
206
207
208
209
# File 'lib/kafo/kafo_configure.rb', line 205

def add_module(name)
  config.add_module(name)
  reset_params_cache
  self.module(name)
end

#configObject



103
104
105
# File 'lib/kafo/kafo_configure.rb', line 103

def config
  self.class.config
end

#executeObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kafo/kafo_configure.rb', line 115

def execute
  parse_cli_arguments

  if (self.class.verbose = !!verbose?)
    Logger.setup_verbose
  else
    @progress_bar = self.class.config.app[:colors] ? ProgressBars::Colored.new : ProgressBars::BlackWhite.new
  end

  unless skip_checks_i_know_better?
    unless SystemChecker.check
      puts "Your system does not meet configuration criteria"
      self.class.exit(:invalid_system)
    end
  end

  self.class.hooking.execute(:pre_validations)
  if interactive?
    wizard = Wizard.new(self)
    wizard.run
  else
    unless validate_all
      puts "Error during configuration, exiting"
      self.class.exit(:invalid_values)
    end
  end

  self.class.hooking.execute(:pre_commit)
  unless dont_save_answers? || noop?
    store_params
    self.class.scenario_manager.link_last_scenario(self.class.config_file) if self.class.scenario_manager.configured?
  end
  run_installation
  return self
rescue SystemExit
  return self
end

#exit_codeObject



171
172
173
# File 'lib/kafo/kafo_configure.rb', line 171

def exit_code
  self.class.exit_code
end

#helpObject



176
177
178
# File 'lib/kafo/kafo_configure.rb', line 176

def help
  self.class.help(invocation_path, self)
end

#loggerObject



111
112
113
# File 'lib/kafo/kafo_configure.rb', line 111

def logger
  self.class.logger
end

#module(name) ⇒ Object



215
216
217
# File 'lib/kafo/kafo_configure.rb', line 215

def module(name)
  modules.detect { |m| m.name == name }
end

#modulesObject



211
212
213
# File 'lib/kafo/kafo_configure.rb', line 211

def modules
  config.modules.sort
end

#param(mod, name) ⇒ Object



219
220
221
# File 'lib/kafo/kafo_configure.rb', line 219

def param(mod, name)
  config.param(mod, name)
end

#paramsObject



193
194
195
196
197
198
# File 'lib/kafo/kafo_configure.rb', line 193

def params
  @params ||= modules.map(&:params).flatten
rescue KafoParsers::ModuleName => e
  puts e
  self.class.exit(:unknown_module)
end

#request_config_reloadObject



223
224
225
# File 'lib/kafo/kafo_configure.rb', line 223

def request_config_reload
  @config_reload_requested = true
end

#reset_params_cacheObject



200
201
202
203
# File 'lib/kafo/kafo_configure.rb', line 200

def reset_params_cache
  @params = nil
  params
end

#storeObject



107
108
109
# File 'lib/kafo/kafo_configure.rb', line 107

def store
  self.class.store
end