module DopCommon::Cli

Public Class Methods

global_options(base) click to toggle source
# File lib/dop_common/cli/global_options.rb, line 8
def self.global_options(base)
  base.class_eval do
    desc 'Verbosity of the command line tool'
    default_value 'INFO'
    arg_name 'Verbosity'
    flag [:verbosity, :v]

    desc 'Show stacktrace on crash'
    default_value DopCommon.config.trace
    switch [:trace, :t]

    desc 'Specify the directory where the plans and their state will be stored'
    default_value DopCommon.config.plan_store_dir
    arg_name 'DIR'
    flag [:plan_store_dir, :s]

    desc 'Directory for the log files'
    default_value DopCommon.config.log_dir
    arg_name 'LOGDIR'
    flag [:log_dir]

    desc 'Log level for the logfiles'
    default_value DopCommon.config.log_level
    arg_name 'LOGLEVEL'
    flag [:log_level, :l]
  end
end
initialize_logger(name, log_level, verbosity, trace) click to toggle source
# File lib/dop_common/cli/log.rb, line 24
def self.initialize_logger(name, log_level, verbosity, trace)
  # create a dummy logger and use the lowest log level configured
  DopCommon.logger = Logger.new('/dev/null')
  file_log_level = ::Logger.const_get(log_level.upcase)
  cli_log_level = ::Logger.const_get(verbosity.upcase)
  min_log_level = file_log_level < cli_log_level ? file_log_level : cli_log_level
  DopCommon.log.level = min_log_level

  # create the cli console logger
  logger = Logger.new(STDOUT)
  logger.level = cli_log_level
  if trace
    logger.formatter = DopCommon::Cli::TraceFormatter.new
  else
    logger.formatter = DopCommon::Cli::DefaultFormatter.new
  end
  DopCommon.add_log_junction(logger)

  # create the cli file logger
  FileUtils.mkdir_p(DopCommon.config.log_dir)
  log_file = File.join(DopCommon.config.log_dir, name)
  logger = Logger.new(log_file , 10, 1024000)
  logger.level = ::Logger.const_get(DopCommon.config.log_level.upcase)
  DopCommon.add_log_junction(logger)
end
node_select_options(command) click to toggle source
# File lib/dop_common/cli/node_selection.rb, line 6
def node_select_options(command)
  command.desc 'Run plans for this nodes only'
  command.default_value ""
  command.arg_name 'node01.example.com,node02.example.com,/example\.com$/'
  command.flag [:nodes]

  command.desc 'Run plans for this roles only'
  command.default_value ""
  command.arg_name 'role01,role01,/^rolepattern/'
  command.flag [:roles]

  command.desc 'Exclude this nodes from the run'
  command.default_value ""
  command.arg_name 'node01.example.com,node02.example.com,/example\.com$/'
  command.flag [:exclude_nodes]

  command.desc 'Exclude this roles from the run'
  command.default_value ""
  command.arg_name 'role01,role01,/^rolepattern/'
  command.flag [:exclude_roles]

  command.desc 'Run plans for this nodes with this config only (You have to specify a JSON hash here)'
  command.default_value "{}"
  command.arg_name '\'{"var1": ["val1", "/val2/"], "var2": "val2"}\''
  command.flag [:nodes_by_config]

  command.desc 'Exclude nodes with this config from the run (You have to specify a JSON hash here)'
  command.default_value "{}"
  command.arg_name '\'{"var1": ["val1", "/val2/"], "var2": "val2"}\''
  command.flag [:exclude_nodes_by_config]
end
parse_node_select_options(options) click to toggle source
# File lib/dop_common/cli/node_selection.rb, line 39
def parse_node_select_options(options)
  pattern_hash = {}
  [:nodes, :roles, :exclude_nodes, :exclude_roles].each do |key|
    hash = { key => options[key].split(',')}
    pattern_hash[key] = DopCommon::HashParser.pattern_list_valid?(hash, key) ?
      DopCommon::HashParser.parse_pattern_list(hash, key) : []
  end
  [:nodes_by_config, :exclude_nodes_by_config].each do |key|
    hash = {key => JSON.parse(options[key])}
    pattern_hash[key] = DopCommon::HashParser.hash_of_pattern_lists_valid?(hash, key) ?
      DopCommon::HashParser.parse_hash_of_pattern_lists(hash, key) : {}
  end
  # Select all nodes if nothing is included
  if [:nodes, :roles, :nodes_by_config].all?{|k| pattern_hash[k].empty?}
    pattern_hash[:nodes] = :all
  end
  OpenStruct.new(pattern_hash)
rescue DopCommon::PlanParsingError => e
  raise StandardError, "Error while parsing the node selection options: #{e.message}"
end

Private Instance Methods

node_select_options(command) click to toggle source
# File lib/dop_common/cli/node_selection.rb, line 6
def node_select_options(command)
  command.desc 'Run plans for this nodes only'
  command.default_value ""
  command.arg_name 'node01.example.com,node02.example.com,/example\.com$/'
  command.flag [:nodes]

  command.desc 'Run plans for this roles only'
  command.default_value ""
  command.arg_name 'role01,role01,/^rolepattern/'
  command.flag [:roles]

  command.desc 'Exclude this nodes from the run'
  command.default_value ""
  command.arg_name 'node01.example.com,node02.example.com,/example\.com$/'
  command.flag [:exclude_nodes]

  command.desc 'Exclude this roles from the run'
  command.default_value ""
  command.arg_name 'role01,role01,/^rolepattern/'
  command.flag [:exclude_roles]

  command.desc 'Run plans for this nodes with this config only (You have to specify a JSON hash here)'
  command.default_value "{}"
  command.arg_name '\'{"var1": ["val1", "/val2/"], "var2": "val2"}\''
  command.flag [:nodes_by_config]

  command.desc 'Exclude nodes with this config from the run (You have to specify a JSON hash here)'
  command.default_value "{}"
  command.arg_name '\'{"var1": ["val1", "/val2/"], "var2": "val2"}\''
  command.flag [:exclude_nodes_by_config]
end
parse_node_select_options(options) click to toggle source
# File lib/dop_common/cli/node_selection.rb, line 39
def parse_node_select_options(options)
  pattern_hash = {}
  [:nodes, :roles, :exclude_nodes, :exclude_roles].each do |key|
    hash = { key => options[key].split(',')}
    pattern_hash[key] = DopCommon::HashParser.pattern_list_valid?(hash, key) ?
      DopCommon::HashParser.parse_pattern_list(hash, key) : []
  end
  [:nodes_by_config, :exclude_nodes_by_config].each do |key|
    hash = {key => JSON.parse(options[key])}
    pattern_hash[key] = DopCommon::HashParser.hash_of_pattern_lists_valid?(hash, key) ?
      DopCommon::HashParser.parse_hash_of_pattern_lists(hash, key) : {}
  end
  # Select all nodes if nothing is included
  if [:nodes, :roles, :nodes_by_config].all?{|k| pattern_hash[k].empty?}
    pattern_hash[:nodes] = :all
  end
  OpenStruct.new(pattern_hash)
rescue DopCommon::PlanParsingError => e
  raise StandardError, "Error while parsing the node selection options: #{e.message}"
end