Class: R10K::Puppetfile

Inherits:
Object
  • Object
show all
Includes:
Logging, Settings::Mixin, 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

Methods included from Settings::Mixin

included

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?



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/r10k/puppetfile.rb', line 50

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_name = puppetfile_name || 'Puppetfile'
  @puppetfile_path = puppetfile_path || File.join(basedir, @puppetfile_name)

  logger.info _("Using Puppetfile '%{puppetfile}'") % {puppetfile: @puppetfile_path}

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

  @loaded = false
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



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

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.



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

def environment
  @environment
end

#forceBoolean

Returns Overwrite any locally made changes.

Returns:

  • (Boolean)

    Overwrite any locally made changes



43
44
45
# File 'lib/r10k/puppetfile.rb', line 43

def force
  @force
end

#forgeObject (readonly)

Returns the value of attribute forge.



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

def forge
  @forge
end

#moduledirObject (readonly)

Returns the value of attribute moduledir.



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

def moduledir
  @moduledir
end

#modulesObject (readonly)

Returns the value of attribute modules.



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

def modules
  @modules
end

#puppetfile_pathObject (readonly)

Returns the value of attribute puppetfile_path.



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

def puppetfile_path
  @puppetfile_path
end

Instance Method Details

#accept(visitor) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/r10k/puppetfile.rb', line 166

def accept(visitor)
  pool_size = self.settings[:pool_size]
  if pool_size > 1
    concurrent_accept(visitor, pool_size)
  else
    serial_accept(visitor)
  end
end

#add_module(name, args) ⇒ Object

Parameters:

  • name (String)
  • args (*Object)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/r10k/puppetfile.rb', line 116

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

  if args.is_a?(Hash) && @default_branch_override != nil
    args[:default_branch] = @default_branch_override
  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>)


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

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

#load(default_branch_override = nil) ⇒ Object



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

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

#load!(default_branch_override = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/r10k/puppetfile.rb', line 74

def load!(default_branch_override = nil)
  @default_branch_override = default_branch_override

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

#managed_directoriesObject



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

def managed_directories
  self.load unless @loaded

  @managed_content.keys
end

#purge_exclusionsObject



156
157
158
159
160
161
162
163
164
# File 'lib/r10k/puppetfile.rb', line 156

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)


101
102
103
# File 'lib/r10k/puppetfile.rb', line 101

def set_forge(forge)
  @forge = forge
end

#set_moduledir(moduledir) ⇒ Object

Parameters:

  • moduledir (String)


106
107
108
109
110
111
112
# File 'lib/r10k/puppetfile.rb', line 106

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


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

def validate_no_duplicate_names(modules)
  dupes = modules
          .group_by { |mod| mod.name }
          .select { |_, v| v.size > 1 }
          .map(&:first)
  unless dupes.empty?
    msg = _('Puppetfiles cannot contain duplicate module names.')
    msg += ' '
    msg += _("Remove the duplicates of the following modules: %{dupes}" % { dupes: dupes.join(' ') })
    raise R10K::Error.new(msg)
  end
end