Class: Bolt::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/task.rb,
lib/bolt/task/run.rb,
lib/bolt/task/puppet_server.rb

Direct Known Subclasses

PuppetServer

Defined Under Namespace

Modules: Run Classes: PuppetServer

Constant Summary collapse

STDIN_METHODS =
%w[both stdin].freeze
ENVIRONMENT_METHODS =
%w[both environment].freeze
METADATA_KEYS =
%w[description extensions files implementations
input_method parameters private puppet_task_version
remote supports_noop].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, metadata = {}, files = [], remote = false) ⇒ Task

name [String] name of the task files [Array<Hash>] where each entry includes ‘name` and `path` metadata [Hash] task metadata



24
25
26
27
28
29
30
31
32
# File 'lib/bolt/task.rb', line 24

def initialize(name,  = {}, files = [], remote = false)
  @name = name
  @metadata = 
  @files = files
  @remote = remote
  @logger = Bolt::Logger.logger(self)

  
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



19
20
21
# File 'lib/bolt/task.rb', line 19

def files
  @files
end

#metadataObject (readonly)

Returns the value of attribute metadata.



19
20
21
# File 'lib/bolt/task.rb', line 19

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/bolt/task.rb', line 19

def name
  @name
end

#remoteObject (readonly)

Returns the value of attribute remote.



19
20
21
# File 'lib/bolt/task.rb', line 19

def remote
  @remote
end

Class Method Details

.from_task_signature(task_sig) ⇒ Object



34
35
36
37
# File 'lib/bolt/task.rb', line 34

def self.from_task_signature(task_sig)
  hash = task_sig.task_hash
  new(hash['name'], hash.fetch('metadata', {}), hash.fetch('files', []))
end

Instance Method Details

#descriptionObject



43
44
45
# File 'lib/bolt/task.rb', line 43

def description
  ['description']
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


128
129
130
131
132
133
134
# File 'lib/bolt/task.rb', line 128

def eql?(other)
  self.class == other.class &&
    @name == other.name &&
    @metadata == other. &&
    @files == other.files &&
    @remote == other.remote
end

#file_path(file_name) ⇒ Object

This provides a method we can override in subclasses if the ‘path’ needs to be fetched or computed.



76
77
78
# File 'lib/bolt/task.rb', line 76

def file_path(file_name)
  file_map[file_name]['path']
end

#implementationsObject



80
81
82
# File 'lib/bolt/task.rb', line 80

def implementations
  ['implementations']
end

#module_nameObject



61
62
63
# File 'lib/bolt/task.rb', line 61

def module_name
  name.split('::').first
end

#parameter_defaultsObject



51
52
53
54
55
# File 'lib/bolt/task.rb', line 51

def parameter_defaults
  (parameters || {}).each_with_object({}) do |(name, param_spec), defaults|
    defaults[name] = param_spec['default'] if param_spec.key?('default')
  end
end

#parametersObject



47
48
49
# File 'lib/bolt/task.rb', line 47

def parameters
  ['parameters']
end

#remote_instanceObject



39
40
41
# File 'lib/bolt/task.rb', line 39

def remote_instance
  self.class.new(@name, @metadata, @files, true)
end

#select_implementation(target, provided_features = []) ⇒ Object

Returns a hash of implementation name, path to executable, input method (if defined), and any additional files (name and path)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bolt/task.rb', line 86

def select_implementation(target, provided_features = [])
  impl = if (impls = implementations)
           available_features = target.feature_set + provided_features
           impl = impls.find do |imp|
             remote_impl = imp['remote']
             remote_impl = ['remote'] if remote_impl.nil?
             Set.new(imp['requirements']).subset?(available_features) && !!remote_impl == @remote
           end
           raise NoImplementationError.new(target, self) unless impl
           impl = impl.dup
           impl['path'] = file_path(impl['name'])
           impl.delete('requirements')
           impl
         else
           raise NoImplementationError.new(target, self) unless !!['remote'] == @remote
           name = files.first['name']
           { 'name' => name, 'path' => file_path(name) }
         end

  inmethod = impl['input_method'] || ['input_method']
  impl['input_method'] = inmethod unless inmethod.nil?

  mfiles = impl.fetch('files', []) + .fetch('files', [])
  dirnames, filenames = mfiles.partition { |file| file.end_with?('/') }
  impl['files'] = filenames.map do |file|
    path = file_path(file)
    raise "No file found for reference #{file}" if path.nil?
    { 'name' => file, 'path' => path }
  end

  unless dirnames.empty?
    files.each do |file|
      name = file['name']
      if dirnames.any? { |dirname| name.start_with?(dirname) }
        impl['files'] << { 'name' => name, 'path' => file_path(name) }
      end
    end
  end

  impl
end

#supports_noopObject



57
58
59
# File 'lib/bolt/task.rb', line 57

def supports_noop
  ['supports_noop']
end

#tasks_dirObject



65
66
67
# File 'lib/bolt/task.rb', line 65

def tasks_dir
  File.join(module_name, 'tasks')
end

#to_hObject



138
139
140
141
142
143
144
# File 'lib/bolt/task.rb', line 138

def to_h
  {
    name: @name,
    files: @files,
    metadata: @metadata
  }
end

#validate_metadataObject



146
147
148
149
150
151
152
153
154
# File 'lib/bolt/task.rb', line 146

def 
  unknown_keys = .keys - METADATA_KEYS

  if unknown_keys.any?
    msg = "Metadata for task '#{@name}' contains unknown keys: #{unknown_keys.join(', ')}."
    msg += " This could be a typo in the task metadata or might result in incorrect behavior."
    Bolt::Logger.warn("unknown_task_metadata_keys", msg)
  end
end