Class: R10K::Source::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/r10k/source/hash.rb

Overview

This class implements an environment source based on recieving a hash of environments

DESCRIPTION

This class implements environments defined by a hash having the following schema:

---
type: object
additionalProperties:
  type: object
  properties:
    type:
      type: string
    basedir:
      type: string
    modules:
      type: object
      additionalProperties:
        type: object
    moduledir:
      type: string
  additionalProperties:
    type: string

The top-level keys in the hash are environment names. Keys in individual environments should be the same as those which would be given to define a single source in r10k.yaml. Additionally, the “modules” key (and moduledir) can be used to designate module content for the environment, independent of the base source parameters.

Example:

---
production:
  type: git
  remote: 'https://github.com/reidmv/control-repo.git'
  ref: '1.0.0'
  modules:
    geoffwilliams-r_profile: '1.1.0'
    geoffwilliams-r_role: '2.0.0'

development:
  type: git
  remote: 'https://github.com/reidmv/control-repo.git'
  ref: 'master'
  modules:
    geoffwilliams-r_profile: '1.1.0'
    geoffwilliams-r_role: '2.0.0'

USAGE

The following is an example implementation class showing how to use the R10K::Source::Hash abstract base class. Assume an r10k.yaml file such as:

---
sources:
  proof-of-concept:
    type: demo
    basedir: '/etc/puppetlabs/code/environments'

Class implementation:

class R10K::Source::Demo < R10K::Source::Hash
  R10K::Source.register(:demo, self)

  def initialize(name, basedir, options = {})
    # This is just a demo class, so we hard-code an example :environments
    # hash here. In a real class, we might do something here such as
    # perform an API call to retrieve an :environments hash.
    options[:environments] = {
      'production' => {
        'remote'  => 'https://git.example.com/puppet/control-repo.git',
        'ref'     => 'release-141',
        'modules' => {
          'puppetlabs-stdlib' => '6.1.0',
          'puppetlabs-ntp' => '8.1.0',
          'example-myapp1' => {
            'git' => 'https://git.example.com/puppet/example-myapp1.git',
            'ref' => 'v1.3.0',
          },
        },
      },
      'development' => {
        'remote'  => 'https://git.example.com/puppet/control-repo.git',
        'ref'     => 'master',
        'modules' => {
          'puppetlabs-stdlib' => '6.1.0',
          'puppetlabs-ntp' => '8.1.0',
          'example-myapp1' => {
            'git' => 'https://git.example.com/puppet/example-myapp1.git',
            'ref' => 'v1.3.1',
          },
        },
      },
    }

    # All we need to do is supply options with the :environments hash.
    # The R10K::Source::Hash parent class takes care of the rest.
    super(name, basedir, options)
  end
end

Example output:

[root@master:~] % r10k deploy environment production -pv
INFO     -> Using Puppetfile '/etc/puppetlabs/code/environments/production/Puppetfile'
INFO     -> Using Puppetfile '/etc/puppetlabs/code/environments/development/Puppetfile'
INFO     -> Deploying environment /etc/puppetlabs/code/environments/production
INFO     -> Environment production is now at 74ea2e05bba796918e4ff1803018c526337ef5f3
INFO     -> Deploying Environment content /etc/puppetlabs/code/environments/production/modules/stdlib
INFO     -> Deploying Environment content /etc/puppetlabs/code/environments/production/modules/ntp
INFO     -> Deploying Environment content /etc/puppetlabs/code/environments/production/modules/myapp1
INFO     -> Deploying Puppetfile content /etc/puppetlabs/code/environments/production/modules/ruby_task_helper
INFO     -> Deploying Puppetfile content /etc/puppetlabs/code/environments/production/modules/bolt_shim
INFO     -> Deploying Puppetfile content /etc/puppetlabs/code/environments/production/modules/apply_helpers

Since:

  • 3.4.0

Direct Known Subclasses

Exec, Yaml, Yamldir

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary

Attributes inherited from Base

#basedir, #name, #prefix, #puppetfile_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#accept, #preload!, #reload!

Methods included from Logging

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

Constructor Details

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

Returns a new instance of Hash.

Parameters:

  • name (String)

    The identifier for this source.

  • basedir (String)

    The base directory where the generated environments will be created.

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

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

Options Hash (options):

  • :prefix (Boolean, String)

    If a String this becomes the prefix. If true, will use the source name as the prefix. All sources should respect this option. Defaults to false for no environment prefix.

  • :environments (Hash)

    The hash definition of environments

Since:

  • 3.4.0



140
141
142
# File 'lib/r10k/source/hash.rb', line 140

def initialize(name, basedir, options = {})
  super(name, basedir, options)
end

Class Method Details

.valid_environments_hash?(hash) ⇒ Boolean

Returns False if the hash is obviously invalid. A true return means maybe it’s valid.

Parameters:

  • hash (Hash)

    A hash to validate.

Returns:

  • (Boolean)

    False if the hash is obviously invalid. A true return means maybe it’s valid.

Since:

  • 3.4.0



126
127
128
129
# File 'lib/r10k/source/hash.rb', line 126

def self.valid_environments_hash?(hash)
  # TODO: more robust schema valiation
  hash.is_a?(Hash)
end

Instance Method Details

#desired_contentsArray<String>

Note:

This is required by Util::Basedir

List all environments that should exist in the basedir for this source

Returns:

  • (Array<String>)

Since:

  • 3.4.0



178
179
180
# File 'lib/r10k/source/hash.rb', line 178

def desired_contents
  environments.map {|env| env.dirname }
end

#environmentsObject

Since:

  • 3.4.0



169
170
171
172
173
# File 'lib/r10k/source/hash.rb', line 169

def environments
  @environments ||= environments_hash.map do |name, hash|
    R10K::Environment.from_hash(name, hash.merge({overrides: @options[:overrides]}))
  end
end

#environments_hashObject

Return the sanitized environments hash for this source. The environments hash should contain objects formatted for use with R10K::Environment#from_hash. If the hash does not exist it will be built based on @options.

Since:

  • 3.4.0



165
166
167
# File 'lib/r10k/source/hash.rb', line 165

def environments_hash
  @environments_hash ||= set_environments_hash(@options.fetch(:environments, {}))
end

#set_environments_hash(hash) ⇒ Object

Set the environment hash for the source. The environment hash is what the source uses to generate enviroments.

Parameters:

  • hash (Hash)

    The hash to sanitize and use as the source’s environments. Should be formatted for use with R10K::Environment#from_hash.

Since:

  • 3.4.0



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/r10k/source/hash.rb', line 148

def set_environments_hash(hash)
  @environments_hash = hash.reduce({}) do |memo,(name,opts)|
    R10K::Util::SymbolizeKeys.symbolize_keys!(opts)
    memo.merge({ 
      name => opts.merge({
        basedir: @basedir,
        dirname: R10K::Environment::Name.new(name, {prefix: @prefix,
                                                    source: @name,
                                                    strip_component: @strip_component}).dirname
      })
    })
  end
end