Class: R10K::Environment::Base

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/environment/base.rb

Overview

This class defines a common interface for environment implementations.

Since:

  • 1.3.0

Direct Known Subclasses

SVN, WithModules

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Constructor Details

#initialize(name, basedir, dirname, options = {}) ⇒ Base

Initialize the given environment.

Parameters:

  • name (String)

    The unique name describing this environment.

  • basedir (String)

    The base directory where this environment will be created.

  • dirname (String)

    The directory name for this environment.

  • options (Hash) (defaults to: {})

    An additional set of options for this environment. The semantics of this environment may depend on the environment implementation.

Since:

  • 1.3.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/r10k/environment/base.rb', line 51

def initialize(name, basedir, dirname, options = {})
  @name    = name
  @basedir = basedir
  @dirname = dirname
  @options = options
  @puppetfile_name = options.delete(:puppetfile_name)
  @overrides = options.delete(:overrides) || {}

  @full_path = File.join(@basedir, @dirname)
  @path = Pathname.new(File.join(@basedir, @dirname))

  @puppetfile  = R10K::Puppetfile.new(@full_path,
                                      {overrides: @overrides,
                                       force: @overrides.dig(:modules, :force),
                                       puppetfile_name: @puppetfile_name})
  @puppetfile.environment = self

  loader_options = { basedir: @full_path, overrides: @overrides, environment: self }
  loader_options[:puppetfile] = @puppetfile_name if @puppetfile_name

  @loader = R10K::ModuleLoader::Puppetfile.new(**loader_options)

  if @overrides.dig(:environments, :incremental)
    @loader.
  end

  @base_modules = nil
  @purge_exclusions = nil
  @managed_directories = [ @full_path ]
  @desired_contents = []
end

Instance Attribute Details

#basedirObject (readonly)

Since:

  • 1.3.0



20
21
22
# File 'lib/r10k/environment/base.rb', line 20

def basedir
  @basedir
end

#desired_contentsObject (readonly)

Since:

  • 1.3.0



40
41
42
# File 'lib/r10k/environment/base.rb', line 40

def desired_contents
  @desired_contents
end

#dirnameObject (readonly)

Since:

  • 1.3.0



24
25
26
# File 'lib/r10k/environment/base.rb', line 24

def dirname
  @dirname
end

#loaderObject (readonly)

Since:

  • 1.3.0



42
43
44
# File 'lib/r10k/environment/base.rb', line 42

def loader
  @loader
end

#managed_directoriesObject (readonly)

Since:

  • 1.3.0



40
41
42
# File 'lib/r10k/environment/base.rb', line 40

def managed_directories
  @managed_directories
end

#nameObject (readonly)

Since:

  • 1.3.0



16
17
18
# File 'lib/r10k/environment/base.rb', line 16

def name
  @name
end

#pathObject (readonly)

Since:

  • 1.3.0



28
29
30
# File 'lib/r10k/environment/base.rb', line 28

def path
  @path
end

#puppetfileObject (readonly)

Since:

  • 1.3.0



33
34
35
# File 'lib/r10k/environment/base.rb', line 33

def puppetfile
  @puppetfile
end

#puppetfile_nameObject (readonly)

Since:

  • 1.3.0



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

def puppetfile_name
  @puppetfile_name
end

Instance Method Details

#accept(visitor) ⇒ Object

Since:

  • 1.3.0



144
145
146
147
148
# File 'lib/r10k/environment/base.rb', line 144

def accept(visitor)
  visitor.visit(:environment, self) do
    puppetfile.accept(visitor)
  end
end

#deployObject

Returns a Queue of the names of modules actually updated

Since:

  • 1.3.0



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/r10k/environment/base.rb', line 152

def deploy
  if @base_modules.nil?
    load_puppetfile_modules
  end

  if ! @base_modules.empty?
    pool_size = @overrides.dig(:modules, :pool_size)
    updated_modules = R10K::ContentSynchronizer.concurrent_sync(@base_modules, pool_size, logger)
  end

  if (@overrides.dig(:purging, :purge_levels) || []).include?(:puppetfile)
    logger.debug("Purging unmanaged Puppetfile content for environment '#{dirname}'...")
    @puppetfile_cleaner.purge!
  end

  updated_modules
end

#determine_purge_exclusions(pf_managed_dirs = @puppetfile.managed_directories, pf_desired_contents = @puppetfile.desired_contents) ⇒ Object

Since:

  • 1.3.0



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/r10k/environment/base.rb', line 186

def determine_purge_exclusions(pf_managed_dirs     = @puppetfile.managed_directories,
                               pf_desired_contents = @puppetfile.desired_contents)

  list = [File.join(@full_path, '.r10k-deploy.json')].to_set

  list += pf_managed_dirs

  list += pf_desired_contents.flat_map do |item|
    desired_tree = []

    if File.directory?(item)
      desired_tree << File.join(item, '**', '*')
    end

    Pathname.new(item).ascend do |path|
      break if path.to_s == @full_path
      desired_tree << path.to_s
    end

    desired_tree
  end

  list.to_a
end

#generate_types!Object

Since:

  • 1.3.0



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/r10k/environment/base.rb', line 219

def generate_types!
  argv = [R10K::Settings.puppet_path, 'generate', 'types', '--environment', dirname, '--environmentpath', basedir, '--config', R10K::Settings.puppet_conf]
  subproc = R10K::Util::Subprocess.new(argv)
  subproc.raise_on_fail = true
  subproc.logger = logger
  result = subproc.execute
  unless result.stderr.empty?
    logger.warn "There were problems generating types for environment #{dirname}:"
    result.stderr.split(%r{\n}).map { |msg| logger.warn msg }
  end
end

#infoHash

Returns a hash describing the current state of the environment.

Returns:

  • (Hash)

Since:

  • 1.3.0



120
121
122
123
124
125
# File 'lib/r10k/environment/base.rb', line 120

def info
  {
    :name => self.name,
    :signature => self.signature,
  }
end

#load_puppetfile_modulesObject

Since:

  • 1.3.0



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/r10k/environment/base.rb', line 170

def load_puppetfile_modules
  loaded_content = @loader.load
  @base_modules = loaded_content[:modules]

  @purge_exclusions = determine_purge_exclusions(loaded_content[:managed_directories],
                                                 loaded_content[:desired_contents])

  @puppetfile_cleaner = R10K::Util::Cleaner.new(loaded_content[:managed_directories],
                                                loaded_content[:desired_contents],
                                                loaded_content[:purge_exclusions])
end

#module_conflicts?(mod) ⇒ Array<R10K::Module::Base>

Returns Whether or not the given module conflicts with any modules already defined in the r10k environment object.

Returns:

  • (Array<R10K::Module::Base>)

    Whether or not the given module conflicts with any modules already defined in the r10k environment object.

Since:

  • 1.3.0



140
141
142
# File 'lib/r10k/environment/base.rb', line 140

def module_conflicts?(mod)
  false
end

#modulesArray<R10K::Module::Base>

Returns All modules defined in the Puppetfile associated with this environment.

Returns:

  • (Array<R10K::Module::Base>)

    All modules defined in the Puppetfile associated with this environment.

Since:

  • 1.3.0



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

def modules
  if @base_modules.nil?
    load_puppetfile_modules
  end

  @base_modules
end

#purge_exclusionsObject

Since:

  • 1.3.0



211
212
213
214
215
216
217
# File 'lib/r10k/environment/base.rb', line 211

def purge_exclusions
  if @purge_exclusions.nil?
    load_puppetfile_modules
  end

  @purge_exclusions
end

#signatureString

This method is abstract.

Returns a unique identifier for the environment’s current state.

Returns:

  • (String)

Raises:

  • (NotImplementedError)

Since:

  • 1.3.0



113
114
115
# File 'lib/r10k/environment/base.rb', line 113

def signature
  raise NotImplementedError, _("%{class} has not implemented method %{method}") %{class: self.class, method: __method__}
end

#statusSymbol

This method is abstract.

Determine the current status of the environment.

This can return the following values:

* :absent - there is no module installed
* :mismatched - there is a module installed but it must be removed and reinstalled
* :outdated - the correct module is installed but it needs to be updated
* :insync - the correct module is installed and up to date, or the module is actually a boy band.

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)

Since:

  • 1.3.0



104
105
106
# File 'lib/r10k/environment/base.rb', line 104

def status
  raise NotImplementedError, _("%{class} has not implemented method %{method}") % {class: self.class, method: __method__}
end

#syncvoid

This method is abstract.

This method returns an undefined value.

Synchronize the given environment.

Raises:

  • (NotImplementedError)

Since:

  • 1.3.0



88
89
90
# File 'lib/r10k/environment/base.rb', line 88

def sync
  raise NotImplementedError, _("%{class} has not implemented method %{method}") % {class: self.class, method: __method__}
end

#whitelist(user_whitelist = []) ⇒ Object

Since:

  • 1.3.0



182
183
184
# File 'lib/r10k/environment/base.rb', line 182

def whitelist(user_whitelist=[])
  user_whitelist.collect { |pattern| File.join(@full_path, pattern) }
end