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
# File 'lib/bolt/shell.rb', line 7

def initialize(target, conn)
  @target = target
  @conn = conn
  @logger = Logging.logger[@target.safe_name]
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



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

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.



52
53
54
55
56
57
# File 'lib/bolt/shell.rb', line 52

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



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

def provided_features
  []
end

#run_command(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/bolt/shell.rb', line 13

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

#run_script(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/bolt/shell.rb', line 21

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

#run_task(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/bolt/shell.rb', line 25

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.



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

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



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

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).



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bolt/shell.rb', line 64

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)


17
18
19
# File 'lib/bolt/shell.rb', line 17

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