Class: TaskHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/task_helper.rb

Overview

Sets up the transport for a remote task

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTaskHelper

Returns a new instance of TaskHelper.



9
10
11
12
13
14
15
16
# File 'lib/puppet/util/task_helper.rb', line 9

def initialize
  unless Puppet.settings.global_defaults_initialized?
    # Puppet.initialize_settings
    # FIXME: debugging support
    Puppet.initialize_settings(['--confdir', '/etc/puppetlabs/puppet/puppet.conf'])
  end
  @transport = {}
end

Instance Attribute Details

#debug_statementsObject (readonly)

Returns the value of attribute debug_statements.



6
7
8
# File 'lib/puppet/util/task_helper.rb', line 6

def debug_statements
  @debug_statements
end

#targetObject

Returns the value of attribute target.



7
8
9
# File 'lib/puppet/util/task_helper.rb', line 7

def target
  @target
end

Class Method Details

.add_plugin_paths(install_dir) ⇒ Object

Syncs across anything from the module lib



111
112
113
114
115
# File 'lib/puppet/util/task_helper.rb', line 111

def self.add_plugin_paths(install_dir)
  Dir.glob(File.join([install_dir, '*'])).each do |mod|
    $LOAD_PATH << File.join([mod, 'lib'])
  end
end

.runObject



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
# File 'lib/puppet/util/task_helper.rb', line 65

def self.run
  task = new
  # support debugging by reading command line arguments
  input = ARGV.empty? ? STDIN.read : ARGV[0]
  params = walk_keys(JSON.parse(input))
  task.target = params[:_target]

  add_plugin_paths(params[:_installdir]) if params.key? :_installdir

  result = task.task(params)

  if result.class == Hash
    STDOUT.print JSON.generate(result)
  else
    STDOUT.print result.to_s
  end
rescue TaskHelper::Error => e
  STDOUT.print({ _error: e.to_h }.to_json)
  exit 1
rescue StandardError => e
  details = {
    'backtrace' => e.backtrace,
    'debug' => task.debug_statements,
  }.compact

  error = TaskHelper::Error.new(e.message, e.class.to_s, details)
  STDOUT.print({ _error: error.to_h }.to_json)
  exit 1
end

.walk_keys(data) ⇒ Object

Accepts a Data object and returns a copy with all hash keys symbolized.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet/util/task_helper.rb', line 97

def self.walk_keys(data)
  if data.is_a? Hash
    data.each_with_object({}) do |(k, v), acc|
      v = walk_keys(v)
      acc[k.to_sym] = v
    end
  elsif data.is_a? Array
    data.map { |v| walk_keys(v) }
  else
    data
  end
end

Instance Method Details

#credentialsObject



55
56
57
# File 'lib/puppet/util/task_helper.rb', line 55

def credentials
  @credentials ||= target.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
end

#debug(statement) ⇒ Object



36
37
38
39
# File 'lib/puppet/util/task_helper.rb', line 36

def debug(statement)
  @debug_statements ||= []
  @debug_statements << statement
end

#task(_params = {}) ⇒ Object

Raises:



59
60
61
62
# File 'lib/puppet/util/task_helper.rb', line 59

def task(_params = {})
  msg = 'The task author must implement the `task` method in the task'
  raise TaskHelper::Error.new(msg, 'tasklib/not-implemented')
end

#transportObject



45
46
47
48
49
50
51
52
53
# File 'lib/puppet/util/task_helper.rb', line 45

def transport
  begin
    require 'puppet/resource_api/transport'
  rescue LoadError
    require 'puppet_x/puppetlabs/panos/transport_shim'
  end

  @transport[transport_name] ||= Puppet::ResourceApi::Transport.connect(transport_name, credentials)
end

#transport_nameObject



41
42
43
# File 'lib/puppet/util/task_helper.rb', line 41

def transport_name
  @transport_name ||= target[:'remote-transport']
end