Class: CemAcpt::Bolt::Cmd::Base

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_acpt/bolt/cmd/base.rb

Overview

Base class for all Bolt command classes

Direct Known Subclasses

TaskBase

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, #current_log_level, current_log_level, included, #logger, logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, #new_log_level, new_log_level, #new_logger, new_logger, verbose?, #verbose?

Constructor Details

#initializeBase

Returns a new instance of Base.



52
53
54
55
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 52

def initialize
  @cmd_env = { 'BOLT_GEM' => 'TRUE' }
  @cmd_params = {}
end

Instance Attribute Details

#cmd_envObject (readonly)

Returns the value of attribute cmd_env.



50
51
52
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 50

def cmd_env
  @cmd_env
end

#cmd_paramsObject (readonly)

Returns the value of attribute cmd_params.



50
51
52
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 50

def cmd_params
  @cmd_params
end

Class Method Details

.option(name, flag, config_path: nil, default: nil, bool_flag: false) ⇒ Object

Class method that defines an option for the command

Parameters:

  • name (String, Symbol)

    The name of the option

  • flag (String)

    The flag to use for the option. Ex: ‘–modulepath’

  • config_path (String) (defaults to: nil)

    The path to the config value for the option. Ex: ‘bolt.module_path’

  • default (String) (defaults to: nil)

    The default value for the option. Ex: ‘/home/user/.puppetlabs/bolt/modules’

  • bool_flag (Boolean) (defaults to: false)

    Whether the option is a boolean flag or not. If this is true, only the flag will be used for the option. For example, if the flag is ‘–clear-cache’ and bool_flag is set to true, and the value is ‘true’, then just ‘–clear-cache’ will be added to the command options. This differs from the default behavior of adding ‘–clear-cache <value>’ to the command options.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 30

def self.option(name, flag, config_path: nil, default: nil, bool_flag: false)
  name = name.to_sym
  option_names << name

  define_method(name) do
    if bool_flag
      find_val(name, flag: flag, config_path: config_path, default: default) ? flag : nil
    else
      val = find_val(name, flag: flag, config_path: config_path, default: default)
      val ? "#{flag} #{val}" : nil
    end
  end
end

.option_namesObject

Class method that holds the names of all options for the command



16
17
18
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 16

def self.option_names
  @option_names ||= []
end

.supports_paramsObject

Denotes that the command supports parameters. Calling this method will define a supports_params? method on the command class that returns true.



46
47
48
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 46

def self.supports_params
  define_method(:supports_params?) { true }
end

Instance Method Details

#add_cmd_env(name, value) ⇒ Object

Adds an environment variable to the command

Parameters:

  • name (String)

    The name of the environment variable

  • value (String)

    The value of the environment variable



74
75
76
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 74

def add_cmd_env(name, value)
  @cmd_env[name] = value
end

#add_cmd_param(name, value) ⇒ Object

Adds a parameter to the command. If passing a complex value, such as a hash, use the JSON representation of the value.

Parameters:

  • name (String)

    The name of the parameter

  • value (String)

    The value of the parameter



82
83
84
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 82

def add_cmd_param(name, value)
  @cmd_params[name] = value
end

#bolt_binObject



133
134
135
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 133

def bolt_bin
  @bolt_bin ||= CemAcpt::Utils::Shell.which('bolt', raise_if_not_found: true)
end

#cmdString

Returns the command to run as a string

Returns:

  • (String)

    The command to run as a string

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 67

def cmd
  raise NotImplementedError, 'cmd method must be implemented in subclass'
end

#command_familyString

Returns the family of the command. This is used to determine which command to run when multiple commands are available for the same action. For example, the ‘task’ command has multiple subcommands, such as ‘task show’ and ‘task run’. The family of the ‘task’ command is ‘task’, and the family of the ‘task show’ and ‘task run’ subcommands is ‘task’.

Returns:

  • (String)

    The family of the command

Raises:

  • (NotImplementedError)


92
93
94
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 92

def command_family
  raise NotImplementedError, 'command_family method must be implemented in subclass'
end

#inspectObject



57
58
59
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 57

def inspect
  "#{self.class}:\"#{cmd}\""
end

#optionsString

Returns the options for the command as a string

Returns:

  • (String)

    The options for the command as a string



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 121

def options
  opts = ['--format json']
  recursive_option_names.each do |opt|
    val = send(opt)
    opts << val if val
  end
  if respond_to?(:supports_params?) && supports_params? && !cmd_params.empty?
    opts << "--params '#{JSON.generate(cmd_params)}'"
  end
  join_array(opts)
end

#run(strict: true, **params) ⇒ CemAcpt::Bolt::Cmd::Output

Runs the command with the given options and environment variables

Parameters:

  • strict (Boolean) (defaults to: true)

    Whether to raise an error if the command returns output that does not implement items. Used for commands where the raw JSON output does not include an ‘items’ key. Defaults to true.

  • params (Hash)

    Parameters to pass to the Bolt command. Only used for some command types.

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 102

def run(strict: true, **params)
  unless params.empty?
    logger.debug('CemAcpt::Bolt') { "Setting command parameters: #{params}" }
    params.each { |k, v| add_cmd_param(k, v) }
  end
  logger.debug('CemAcpt::Bolt') { "Running Bolt command: #{cmd}" }
  logger.debug('CemAcpt::Bolt') { "Bolt command environment: #{cmd_env}" }
  begin
    out = CemAcpt::Utils::Shell.run_cmd(cmd, cmd_env, output: nil, raise_on_fail: false)
    logger.debug('CemAcpt::Bolt') { "Bolt command raw output:\n#{out}" }
    CemAcpt::Bolt::Cmd::Output.new(out, strict: strict, **(@item_defaults || {}))
  rescue StandardError => e
    logger.debug('CemAcpt::Bolt') { "Bolt command error:\n#{e}" }
    CemAcpt::Bolt::Cmd::Output.new(e, **(@item_defaults || {}))
  end
end

#to_sObject



61
62
63
# File 'lib/cem_acpt/bolt/cmd/base.rb', line 61

def to_s
  cmd
end