Class: Bolt::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/shell.rb,
lib/bolt/shell/bash.rb,
lib/bolt/shell/powershell.rb,
lib/bolt/shell/bash/tmpdir.rb,
lib/bolt/shell/powershell/snippets.rb

Direct Known Subclasses

Bash, Powershell

Defined Under Namespace

Classes: Bash, Powershell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, conn) ⇒ Shell

Returns a new instance of Shell.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bolt/shell.rb', line 7

def initialize(target, conn)
  @target = target
  @conn = conn
  @logger = Bolt::Logger.logger(@target.safe_name)

  if Bolt::Logger.stream
    Bolt::Logger.warn_once("stream_experimental",
                           "The 'stream' option is experimental, and might "\
                           "include breaking changes between minor versions.")
    @stream_logger = Bolt::Logger.logger(:stream)
    # Don't send stream messages to the parent logger
    @stream_logger.additive = false

    # Log stream messages without any other data or color
    pattern = Logging.layouts.pattern(pattern: '%m\n')
    @stream_logger.appenders = Logging.appenders.stdout(
      'console',
      layout: pattern
    )
  end
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/bolt/shell.rb', line 5

def conn
  @conn
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/bolt/shell.rb', line 5

def logger
  @logger
end

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/bolt/shell.rb', line 5

def target
  @target
end

Instance Method Details

#default_input_method(_executable) ⇒ Object



49
50
51
# File 'lib/bolt/shell.rb', line 49

def default_input_method(_executable)
  'both'
end

#envify_params(params) ⇒ Object

Transform a parameter map to an environment variable map, with parameter names prefixed with ‘PT_’ and values transformed to JSON unless they’re strings.



68
69
70
71
72
73
# File 'lib/bolt/shell.rb', line 68

def envify_params(params)
  params.each_with_object({}) do |(k, v), h|
    v = v.to_json unless v.is_a?(String)
    h["PT_#{k}"] = v
  end
end

#provided_featuresObject



45
46
47
# File 'lib/bolt/shell.rb', line 45

def provided_features
  []
end

#run_command(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/bolt/shell.rb', line 29

def run_command(*_args)
  raise NotImplementedError, "run_command() must be implemented by the shell class"
end

#run_script(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/bolt/shell.rb', line 37

def run_script(*_args)
  raise NotImplementedError, "run_script() must be implemented by the shell class"
end

#run_task(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/bolt/shell.rb', line 41

def run_task(*_args)
  raise NotImplementedError, "run_task() must be implemented by the shell class"
end

#select_implementation(target, task) ⇒ Object

The above methods are the API that must be implemented by a Shell. Below are helper methods.



56
57
58
59
60
# File 'lib/bolt/shell.rb', line 56

def select_implementation(target, task)
  impl = task.select_implementation(target, provided_features)
  impl['input_method'] ||= default_input_method(impl['path'])
  impl
end

#select_interpreter(executable, interpreters) ⇒ Object



62
63
64
# File 'lib/bolt/shell.rb', line 62

def select_interpreter(executable, interpreters)
  interpreters[Pathname(executable).extname] if interpreters
end

#unwrap_sensitive_args(arguments) ⇒ Object

Unwraps any Sensitive data in an arguments Hash, so the plain-text is passed to the Task/Script.

This works on deeply nested data structures composed of Hashes, Arrays, and and plain-old data types (int, string, etc).



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bolt/shell.rb', line 80

def unwrap_sensitive_args(arguments)
  # Skip this if Puppet isn't loaded
  return arguments unless defined?(Puppet::Pops::Types::PSensitiveType::Sensitive)

  case arguments
  when Array
    # iterate over the array, unwrapping all elements
    arguments.map { |x| unwrap_sensitive_args(x) }
  when Hash
    # iterate over the arguments hash and unwrap all keys and values
    arguments.each_with_object({}) { |(k, v), h|
      h[unwrap_sensitive_args(k)] = unwrap_sensitive_args(v)
    }
  when Puppet::Pops::Types::PSensitiveType::Sensitive
    # this value is Sensitive, unwrap it
    unwrap_sensitive_args(arguments.unwrap)
  else
    # unknown data type, just return it
    arguments
  end
end

#upload(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/bolt/shell.rb', line 33

def upload(*_args)
  raise NotImplementedError, "upload() must be implemented by the shell class"
end