Class: Puppet::Module::Task

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

Defined Under Namespace

Classes: Error, InvalidFile, InvalidName, TaskNotFound

Constant Summary collapse

FORBIDDEN_EXTENSIONS =
%w{.conf .md}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Task.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/module/task.rb', line 45

def initialize(pup_module, task_name, files,  = 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

  all_files = .nil? ? files : files + []
  all_files.each do |f|
    if !f.start_with?(pup_module.tasks_directory)
      msg = _("The file '%{path}' is not located in the %{module_name} module's tasks directory") %
                   {path: f.to_s, module_name: pup_module.name}

      # we can include some extra context for the log message:
      Puppet.err(msg + " (#{pup_module.tasks_directory})")
      raise InvalidFile, msg
    end
  end

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

  @module = pup_module
  @name = name
  @metadata_file =  if 
  @files = files
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#metadata_fileObject (readonly)

Returns the value of attribute metadata_file.



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

def 
  @metadata_file
end

#moduleObject (readonly)

Returns the value of attribute module.



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

def module
  @module
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.is_task_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/puppet/module/task.rb', line 13

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)


32
33
34
# File 'lib/puppet/module/task.rb', line 32

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)


19
20
21
22
23
24
25
26
# File 'lib/puppet/module/task.rb', line 19

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)


28
29
30
# File 'lib/puppet/module/task.rb', line 28

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

.tasks_in_module(pup_module) ⇒ Object



36
37
38
39
40
41
# File 'lib/puppet/module/task.rb', line 36

def self.tasks_in_module(pup_module)
  Dir.glob(File.join(pup_module.tasks_directory, '*'))
    .keep_if { |f| is_tasks_filename?(f) }
    .group_by { |f| task_name_from_path(f) }
    .map { |task, files| new_with_files(pup_module, task, files) }
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
73
# File 'lib/puppet/module/task.rb', line 70

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