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



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

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

#mtimeObject

Returns the value of attribute mtime.



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

def mtime
  @mtime
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



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

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

#add_mtimesObject



81
82
83
84
85
# File 'lib/bolt/task.rb', line 81

def add_mtimes
  @files.each do |f|
    f['mtime'] = File.mtime(f['path']) if File.exist?(f['path'])
  end
end

#descriptionObject



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

def description
  ['description']
end

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

Returns:

  • (Boolean)


135
136
137
138
139
140
141
# File 'lib/bolt/task.rb', line 135

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.



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

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

#implementationsObject



87
88
89
# File 'lib/bolt/task.rb', line 87

def implementations
  ['implementations']
end

#module_nameObject



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

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

#parameter_defaultsObject



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

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



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

def parameters
  ['parameters']
end

#remote_instanceObject



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

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)



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
127
128
129
130
131
132
133
# File 'lib/bolt/task.rb', line 93

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



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

def supports_noop
  ['supports_noop']
end

#tasks_dirObject



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

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

#to_hObject



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

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

#validate_metadataObject



153
154
155
156
157
158
159
160
161
# File 'lib/bolt/task.rb', line 153

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