Class: R10K::Puppetfile

Inherits:
Object
  • Object
show all
Includes:
Logging, Util::Purgeable
Defined in:
lib/r10k/puppetfile.rb

Defined Under Namespace

Classes: DSL

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Purgeable

#current_contents, #logger, #pending_contents, #purge!, #stale_contents

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initialize(basedir, moduledir = nil, puppetfile_path = nil) ⇒ Puppetfile

Returns a new instance of Puppetfile.

Parameters:

  • basedir (String)
  • moduledir (String) (defaults to: nil)

    The directory to install the modules, default to ##basedir/modules

  • puppetfile_path (String) (defaults to: nil)

    The path to the Puppetfile, default to ##basedir/Puppetfile



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/r10k/puppetfile.rb', line 39

def initialize(basedir, moduledir = nil, puppetfile_path = nil)
  @basedir         = basedir
  @moduledir       = moduledir  || File.join(basedir, 'modules')
  @puppetfile_path = puppetfile_path || File.join(basedir, 'Puppetfile')

  @modules = []
  @managed_content = {}
  @forge   = 'forgeapi.puppetlabs.com'

  @loaded = false
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



22
23
24
# File 'lib/r10k/puppetfile.rb', line 22

def basedir
  @basedir
end

#environmentR10K::Environment

Returns Optional R10K::Environment that this Puppetfile belongs to.

Returns:

  • (R10K::Environment)

    Optional R10K::Environment that this Puppetfile belongs to.



34
35
36
# File 'lib/r10k/puppetfile.rb', line 34

def environment
  @environment
end

#forgeObject (readonly)

Returns the value of attribute forge.



14
15
16
# File 'lib/r10k/puppetfile.rb', line 14

def forge
  @forge
end

#moduledirObject (readonly)

Returns the value of attribute moduledir.



26
27
28
# File 'lib/r10k/puppetfile.rb', line 26

def moduledir
  @moduledir
end

#modulesObject (readonly)

Returns the value of attribute modules.



18
19
20
# File 'lib/r10k/puppetfile.rb', line 18

def modules
  @modules
end

#puppetfile_pathObject (readonly)

Returns the value of attribute puppetfile_path.



30
31
32
# File 'lib/r10k/puppetfile.rb', line 30

def puppetfile_path
  @puppetfile_path
end

Instance Method Details

#accept(visitor) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/r10k/puppetfile.rb', line 129

def accept(visitor)
  visitor.visit(:puppetfile, self) do
    modules.each do |mod|
      mod.accept(visitor)
    end
  end
end

#add_module(name, args) ⇒ Object

Parameters:

  • name (String)
  • args (*Object)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/r10k/puppetfile.rb', line 83

def add_module(name, args)
  if args.is_a?(Hash) && install_path = args.delete(:install_path)
    install_path = resolve_install_path(install_path)
    validate_install_path(install_path, name)
  else
    install_path = @moduledir
  end

  # Keep track of all the content this Puppetfile is managing to enable purging.
  @managed_content[install_path] = Array.new unless @managed_content.has_key?(install_path)

  mod = R10K::Module.new(name, install_path, args, @environment)

  @managed_content[install_path] << mod.name
  @modules << mod
end

#desired_contentsArray<String>

Note:

This implements a required method for the Purgeable mixin

Returns an array of the full paths to all the content being managed.

Returns:

  • (Array<String>)


111
112
113
114
115
116
117
# File 'lib/r10k/puppetfile.rb', line 111

def desired_contents
  self.load unless @loaded

  @managed_content.flat_map do |install_path, modnames|
    modnames.collect { |name| File.join(install_path, name) }
  end
end

#loadObject



51
52
53
54
55
56
57
# File 'lib/r10k/puppetfile.rb', line 51

def load
  if File.readable? @puppetfile_path
    self.load!
  else
    logger.debug _("Puppetfile %{path} missing or unreadable") % {path: @puppetfile_path.inspect}
  end
end

#load!Object



59
60
61
62
63
64
65
# File 'lib/r10k/puppetfile.rb', line 59

def load!
  dsl = R10K::Puppetfile::DSL.new(self)
  dsl.instance_eval(puppetfile_contents, @puppetfile_path)
  @loaded = true
rescue SyntaxError, LoadError, ArgumentError => e
  raise R10K::Error.wrap(e, _("Failed to evaluate %{path}") % {path: @puppetfile_path})
end

#managed_directoriesObject



102
103
104
105
106
# File 'lib/r10k/puppetfile.rb', line 102

def managed_directories
  self.load unless @loaded

  @managed_content.keys
end

#purge_exclusionsObject



119
120
121
122
123
124
125
126
127
# File 'lib/r10k/puppetfile.rb', line 119

def purge_exclusions
  exclusions = managed_directories

  if environment && environment.respond_to?(:desired_contents)
    exclusions += environment.desired_contents
  end

  exclusions
end

#set_forge(forge) ⇒ Object

Parameters:

  • forge (String)


68
69
70
# File 'lib/r10k/puppetfile.rb', line 68

def set_forge(forge)
  @forge = forge
end

#set_moduledir(moduledir) ⇒ Object

Parameters:

  • moduledir (String)


73
74
75
76
77
78
79
# File 'lib/r10k/puppetfile.rb', line 73

def set_moduledir(moduledir)
  @moduledir = if Pathname.new(moduledir).absolute?
    moduledir
  else
    File.join(basedir, moduledir)
  end
end