Class: R10K::Source::Hash
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, = {})
# 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.
[: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, )
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
Constant Summary
Constants included from Logging
Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP
Instance Attribute Summary
Attributes inherited from Base
#basedir, #name, #prefix, #puppetfile_name
Class Method Summary collapse
-
.valid_environments_hash?(hash) ⇒ Boolean
False if the hash is obviously invalid.
Instance Method Summary collapse
-
#desired_contents ⇒ Array<String>
List all environments that should exist in the basedir for this source.
- #environments ⇒ Object
-
#environments_hash ⇒ Object
Return the sanitized environments hash for this source.
-
#initialize(name, basedir, options = {}) ⇒ Hash
constructor
A new instance of Hash.
-
#set_environments_hash(hash) ⇒ Object
Set the environment hash for the source.
Methods inherited from Base
Methods included from Logging
add_outputters, debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level
Constructor Details
#initialize(name, basedir, options = {}) ⇒ Hash
Returns a new instance of Hash.
140 141 142 |
# File 'lib/r10k/source/hash.rb', line 140 def initialize(name, basedir, = {}) super(name, basedir, ) 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.
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_contents ⇒ Array<String>
This is required by Util::Basedir
List all environments that should exist in the basedir for this source
178 179 180 |
# File 'lib/r10k/source/hash.rb', line 178 def desired_contents environments.map {|env| env.dirname } end |
#environments ⇒ Object
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_hash ⇒ Object
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.
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.
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 |