Class: Puppet::Module::Task

Inherits:
Object show all
Defined in:
lib/puppet/module/task.rb

Defined Under Namespace

Classes: Error, InvalidFile, InvalidMetadata, InvalidName, InvalidTask, TaskNotFound

Constant Summary collapse

FORBIDDEN_EXTENSIONS =
%w{.conf .md}
MOUNTS =
%w[lib files tasks]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pup_module, task_name, module_executables, metadata_file = nil) ⇒ Task

file paths must be relative to the modules task directory



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/puppet/module/task.rb', line 201

def initialize(pup_module, task_name,  module_executables,  = nil)
  if !Puppet::Module::Task.is_task_name?(task_name)
    raise InvalidName, _("Task names must start with a lowercase letter and be composed of only lowercase letters, numbers, and underscores")
  end

  name = task_name == "init" ? pup_module.name : "#{pup_module.name}::#{task_name}"

  @module = pup_module
  @name = name
  @metadata_file = 
  @module_executables = module_executables || []
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



198
199
200
# File 'lib/puppet/module/task.rb', line 198

def 
  @metadata
end

#metadata_fileObject (readonly)

Returns the value of attribute metadata_file.



198
199
200
# File 'lib/puppet/module/task.rb', line 198

def 
  @metadata_file
end

#moduleObject (readonly)

Returns the value of attribute module.



198
199
200
# File 'lib/puppet/module/task.rb', line 198

def module
  @module
end

#nameObject (readonly)

Returns the value of attribute name.



198
199
200
# File 'lib/puppet/module/task.rb', line 198

def name
  @name
end

Class Method Details

.find_files(name, directory, metadata, executables, envname = nil) ⇒ Object



172
173
174
175
# File 'lib/puppet/module/task.rb', line 172

def self.find_files(name, directory, , executables, envname = nil)
  # PXP agent relies on 'impls' (which is the task file) being first if there is no metadata
  find_implementations(name, directory, , executables) + find_extra_files(, envname)
end

.is_task_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/puppet/module/task.rb', line 50

def self.is_task_name?(name)
  return true if name =~ /^[a-z][a-z0-9_]*$/
  return false
end

.is_tasks_executable_filename?(name) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/puppet/module/task.rb', line 181

def self.is_tasks_executable_filename?(name)
  is_tasks_filename?(name) && !name.end_with?('.json')
end

.is_tasks_filename?(path) ⇒ Boolean

Determine whether a file has a legal name for either a task’s executable or metadata file.

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/puppet/module/task.rb', line 56

def self.is_tasks_filename?(path)
  name_less_extension = File.basename(path, '.*')
  return false if not is_task_name?(name_less_extension)
  FORBIDDEN_EXTENSIONS.each do |ext|
    return false if path.end_with?(ext)
  end
  return true
end

.is_tasks_metadata_filename?(name) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/puppet/module/task.rb', line 177

def self.(name)
  is_tasks_filename?(name) && name.end_with?('.json')
end

.read_metadata(file) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/puppet/module/task.rb', line 214

def self.(file)
  Puppet::Util::Json.load(Puppet::FileSystem.read(file, :encoding => 'utf-8')) if file
rescue SystemCallError, IOError => err
  msg = _("Error reading metadata: %{message}" % {message: err.message})
  raise InvalidMetadata.new(msg, 'puppet.tasks/unreadable-metadata')
rescue Puppet::Util::Json::ParseError => err
  raise InvalidMetadata.new(err.message, 'puppet.tasks/unparseable-metadata')
end

.tasks_in_module(pup_module) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/puppet/module/task.rb', line 185

def self.tasks_in_module(pup_module)
  task_files = Dir.glob(File.join(pup_module.tasks_directory, '*'))
    .keep_if { |f| is_tasks_filename?(f) }

  module_executables = task_files.reject(&method(:is_tasks_metadata_filename?)).map.to_a

  tasks = task_files.group_by { |f| task_name_from_path(f) }

  tasks.map do |task, executables|
    new_with_files(pup_module, task, executables, module_executables)
  end
end

Instance Method Details

#==(other) ⇒ Object



236
237
238
239
# File 'lib/puppet/module/task.rb', line 236

def ==(other)
  self.name == other.name &&
  self.module == other.module
end

#filesObject



227
228
229
# File 'lib/puppet/module/task.rb', line 227

def files
  @files ||= self.class.find_files(@name, @module.tasks_directory, , @module_executables, environment_name)
end

#validateObject



231
232
233
234
# File 'lib/puppet/module/task.rb', line 231

def validate
  files
  true
end