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_name = nil, force = 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

  • puppetfile_name (String) (defaults to: nil)

    The name of the Puppetfile, default to ‘Puppetfile’

  • force (Boolean) (defaults to: nil)

    Shall we overwrite locally made changes?



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/r10k/puppetfile.rb', line 45

def initialize(basedir, moduledir = nil, puppetfile_path = nil, puppetfile_name = nil, force = nil )
  @basedir         = basedir
  @force           = force || false
  @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

#forceBoolean

Returns Overwrite any locally made changes.

Returns:

  • (Boolean)

    Overwrite any locally made changes



38
39
40
# File 'lib/r10k/puppetfile.rb', line 38

def force
  @force
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



148
149
150
151
152
153
154
# File 'lib/r10k/puppetfile.rb', line 148

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)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/r10k/puppetfile.rb', line 102

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>)


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

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



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

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



66
67
68
69
70
71
72
73
# File 'lib/r10k/puppetfile.rb', line 66

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

#managed_directoriesObject



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

def managed_directories
  self.load unless @loaded

  @managed_content.keys
end

#purge_exclusionsObject



138
139
140
141
142
143
144
145
146
# File 'lib/r10k/puppetfile.rb', line 138

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)


76
77
78
# File 'lib/r10k/puppetfile.rb', line 76

def set_forge(forge)
  @forge = forge
end

#set_moduledir(moduledir) ⇒ Object

Parameters:

  • moduledir (String)


92
93
94
95
96
97
98
# File 'lib/r10k/puppetfile.rb', line 92

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

#validate_no_duplicate_names(modules) ⇒ Object

Parameters:

  • modules (Array<String>)


81
82
83
84
85
86
87
88
89
# File 'lib/r10k/puppetfile.rb', line 81

def validate_no_duplicate_names(modules)
  dupes = modules
          .group_by { |mod| mod.name }
          .select { |_, v| v.size > 1 }.map(&:first)
  unless dupes.empty?
    logger.warn _('Puppetfiles should not contain duplicate module names and will result in an error in r10k v3.x.')
    logger.warn _("Remove the duplicates of the following modules: %{dupes}" % {dupes: dupes.join(" ")})
  end
end