Skip to content

Commit

Permalink
Support multiple envs_dir directories
Browse files Browse the repository at this point in the history
This module adds support for the puppet environmentpath setting to be configured
using multiple directories. An example use-case is where r10k is in use to
deploy some but not all environments, to avoid unmanaged environments being
purged by r10k.

- Each listed environmentpath directory will be created and managed by puppet
- The default post-receive hook script uses the first listed directory in
  `envs_dir` to create initial environments to maintain backward compatibility.
- The config_version_cmd is updated to use only the first listed directory from
  `envs_dir`, this is only used in the environment.conf.erb template which does
  not appear to be deployed anywhere in the current version of this module

Tests are included to ensure that all listed directories are created and
managed, and that any other references to the envs_dir parameter behave the
same as if only a single directory were specified to maintain backward
compatibility.

Fixes theforeman#708
  • Loading branch information
optiz0r committed Oct 14, 2021
1 parent 7534cbe commit 0809901
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
6 changes: 5 additions & 1 deletion manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
# $environments_mode:: Environments directory mode.
#
# $envs_dir:: Directory that holds puppet environments
# Multiple paths can be specified, separated by colons.
# All listed directories will be created and attributes managed,
# but only the first listed path will be used to populate
# environments from git repo branches.
#
# $envs_target:: Indicates that $envs_dir should be
# a symbolic link to this target
Expand Down Expand Up @@ -488,7 +492,7 @@

if $config_version == undef {
if $git_repo {
$config_version_cmd = "git --git-dir ${envs_dir}/\$environment/.git describe --all --long"
$config_version_cmd = "git --git-dir ${envs_dir.split(':')[0]}/\$environment/.git describe --all --long"
} else {
$config_version_cmd = undef
}
Expand Down
18 changes: 10 additions & 8 deletions manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,15 @@
$ensure = 'directory'
}

file { $puppet::server::envs_dir:
ensure => $ensure,
owner => $puppet::server::environments_owner,
group => $puppet::server::environments_group,
mode => $puppet::server::environments_mode,
target => $puppet::server::envs_target,
force => true,
$puppet::server::envs_dir.split(':').each |$dir| {
file { $dir:
ensure => $ensure,
owner => $puppet::server::environments_owner,
group => $puppet::server::environments_group,
mode => $puppet::server::environments_mode,
target => $puppet::server::envs_target,
force => true,
}
}

if $puppet::server::git_repo {
Expand All @@ -269,7 +271,7 @@
mode => $puppet::server::git_repo_mode,
user => $puppet::server::git_repo_user,
group => $puppet::server::git_repo_group,
require => File[$puppet::vardir, $puppet::server::envs_dir],
require => File[$puppet::vardir, $puppet::server::envs_dir.split(':')],
}

$git_branch_map = $puppet::server::git_branch_map
Expand Down
18 changes: 17 additions & 1 deletion spec/classes/puppet_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
it { should_not contain_puppet__config__master('environment_timeout') }
it { should_not contain_puppet__config__master('manifest') }
it { should_not contain_puppet__config__master('modulepath') }
it { should_not contain_puppet__config__master('config_version') }
it { should_not contain_puppet__config__master('trusted_external_command') }

it { should contain_puppet__config__master('external_nodes').with_value("#{etcdir}\/node.rb") }
Expand Down Expand Up @@ -697,6 +696,23 @@
end
end
end

describe 'with multiple environment paths' do
let(:params) do
super().merge(
server_envs_dir: '/etc/puppetlabs/code/environments/:/etc/puppetlabs/code/unmanaged-environments/',
server_git_repo_path: '/test/puppet',
server_post_hook_name: 'post-receive',
server_git_repo: true,
)
end

it { should contain_puppet__config__main('environmentpath').with_value('/etc/puppetlabs/code/environments/:/etc/puppetlabs/code/unmanaged-environments/') }
it { should contain_file('/etc/puppetlabs/code/environments/') }
it { should contain_file('/etc/puppetlabs/code/unmanaged-environments/') }
it { should contain_git__repo('puppet_repo').that_requires('File[/etc/puppetlabs/code/environments/]') }
it { should contain_file('/test/puppet/hooks/post-receive').with_content(/ENVIRONMENT_BASEDIR\s=\s"\/etc\/puppetlabs\/code\/environments\/"/) }
end
end
end
end
2 changes: 1 addition & 1 deletion templates/server/post-receive.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $stdout.sync = true
$stderr.sync = true

# Set this to where you want to keep your environments
ENVIRONMENT_BASEDIR = "<%= scope.lookupvar("puppet::server::envs_dir") %>"
ENVIRONMENT_BASEDIR = "<%= scope.lookupvar("puppet::server::envs_dir").split(/:/)[0] %>"

# post-receive hooks set GIT_DIR to the current repository. If you want to
# clone from a non-local repository, set this to the URL of the repository,
Expand Down

0 comments on commit 0809901

Please sign in to comment.