Class: Bolt::Task
- Inherits:
-
Object
show all
- Defined in:
- lib/bolt/task.rb,
lib/bolt/task/run.rb,
lib/bolt/task/puppet_server.rb
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, metadata = {}, files = [], remote = false)
@name = name
@metadata = metadata
@files = files
@remote = remote
@logger = Bolt::Logger.logger(self)
validate_metadata
end
|
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
19
20
21
|
# File 'lib/bolt/task.rb', line 19
def files
@files
end
|
Returns the value of attribute metadata.
19
20
21
|
# File 'lib/bolt/task.rb', line 19
def metadata
@metadata
end
|
#mtime ⇒ Object
Returns the value of attribute mtime.
20
21
22
|
# File 'lib/bolt/task.rb', line 20
def mtime
@mtime
end
|
#name ⇒ Object
Returns the value of attribute name.
19
20
21
|
# File 'lib/bolt/task.rb', line 19
def name
@name
end
|
#remote ⇒ Object
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_mtimes ⇒ Object
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
|
#description ⇒ Object
44
45
46
|
# File 'lib/bolt/task.rb', line 44
def description
metadata['description']
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
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.metadata &&
@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
|
#implementations ⇒ Object
87
88
89
|
# File 'lib/bolt/task.rb', line 87
def implementations
metadata['implementations']
end
|
#module_name ⇒ Object
62
63
64
|
# File 'lib/bolt/task.rb', line 62
def module_name
name.split('::').first
end
|
#parameter_defaults ⇒ Object
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
|
#parameters ⇒ Object
48
49
50
|
# File 'lib/bolt/task.rb', line 48
def parameters
metadata['parameters']
end
|
#remote_instance ⇒ Object
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 = metadata['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 !!metadata['remote'] == @remote
name = files.first['name']
{ 'name' => name, 'path' => file_path(name) }
end
inmethod = impl['input_method'] || metadata['input_method']
impl['input_method'] = inmethod unless inmethod.nil?
mfiles = impl.fetch('files', []) + metadata.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_noop ⇒ Object
58
59
60
|
# File 'lib/bolt/task.rb', line 58
def supports_noop
metadata['supports_noop']
end
|
#tasks_dir ⇒ Object
66
67
68
|
# File 'lib/bolt/task.rb', line 66
def tasks_dir
File.join(module_name, 'tasks')
end
|
#to_h ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/bolt/task.rb', line 145
def to_h
{
name: @name,
files: @files,
metadata: @metadata
}
end
|
153
154
155
156
157
158
159
160
161
|
# File 'lib/bolt/task.rb', line 153
def validate_metadata
unknown_keys = metadata.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
|