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

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



21
22
23
24
25
26
27
28
29
# File 'lib/bolt/task.rb', line 21

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

  
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



16
17
18
# File 'lib/bolt/task.rb', line 16

def files
  @files
end

#metadataObject (readonly)

Returns the value of attribute metadata.



16
17
18
# File 'lib/bolt/task.rb', line 16

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/bolt/task.rb', line 16

def name
  @name
end

#remoteObject (readonly)

Returns the value of attribute remote.



16
17
18
# File 'lib/bolt/task.rb', line 16

def remote
  @remote
end

Class Method Details

.from_task_signature(task_sig) ⇒ Object



31
32
33
34
# File 'lib/bolt/task.rb', line 31

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



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

def description
  ['description']
end

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

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/bolt/task.rb', line 125

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.



73
74
75
# File 'lib/bolt/task.rb', line 73

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

#implementationsObject



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

def implementations
  ['implementations']
end

#module_nameObject



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

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

#parameter_defaultsObject



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

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



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

def parameters
  ['parameters']
end

#remote_instanceObject



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

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)



83
84
85
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
# File 'lib/bolt/task.rb', line 83

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



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

def supports_noop
  ['supports_noop']
end

#tasks_dirObject



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

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

#to_hObject



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

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

#validate_metadataObject



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

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 may result in incorrect behavior."
    @logger.warn(msg)
  end
end