Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage git remote for device groups #30

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ If true, we will ensure that the appropriate packages are installed for the vcs

- *Default*: false

vcs_remote_urls
---------------
Hash (keyed by group name) of remote urls. See Sample Hiera Structure.

- *Default*: undef

===

# define rancid::router_db
Expand Down Expand Up @@ -254,6 +260,11 @@ rancid::devices:
type: 'cisco',
status: 'up'
}

rancid::vcs_remote_urls:
routers: git@github.com/me/my-routers.git
switches: git@github.com/me/my-switches.git
firewalls: git@github.com/me/my-firewalls.git
</pre>

===
Expand Down
48 changes: 25 additions & 23 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
# Manage RANCID - http://www.shrubbery.net/rancid/
#
class rancid (
$filterpwds = 'ALL', # yes, no, all
$nocommstr = 'YES', # yes or no
$maxrounds = '4',
$oldtime = '4',
$locktime = '4',
$parcount = '5',
$maildomain = undef,
$groups = [ 'routers', 'switches', 'firewalls' ],
$devices = undef,
$packages = 'USE_DEFAULTS',
$rancid_config = 'USE_DEFAULTS',
$rancid_path_env = 'USE_DEFAULTS',
$homedir = 'USE_DEFAULTS',
$logdir = 'USE_DEFAULTS',
$user = 'USE_DEFAULTS',
$group = 'USE_DEFAULTS',
$shell = 'USE_DEFAULTS',
$cron_d_file = '/etc/cron.d/rancid',
$cloginrc_content = 'USE_DEFAULTS',
$show_cloginrc_diff = true,
$vcs = 'USE_DEFAULTS',
$vcsroot = 'USE_DEFAULTS',
$manage_vcs_packages = false,
$filterpwds = 'ALL', # yes, no, all
$nocommstr = 'YES', # yes or no
$maxrounds = '4',
$oldtime = '4',
$locktime = '4',
$parcount = '5',
$maildomain = undef,
$groups = [ 'routers', 'switches', 'firewalls' ],
Hash $devices = {},
$packages = 'USE_DEFAULTS',
$rancid_config = 'USE_DEFAULTS',
$rancid_path_env = 'USE_DEFAULTS',
$homedir = 'USE_DEFAULTS',
$logdir = 'USE_DEFAULTS',
$user = 'USE_DEFAULTS',
$group = 'USE_DEFAULTS',
$shell = 'USE_DEFAULTS',
$cron_d_file = '/etc/cron.d/rancid',
$cloginrc_content = 'USE_DEFAULTS',
$show_cloginrc_diff = true,
$vcs = 'USE_DEFAULTS',
$vcsroot = 'USE_DEFAULTS',
$manage_vcs_packages = false,
Hash $vcs_remote_urls = {},
) {

$default_cloginrc_content = "# This file is being maintained by Puppet.\n# DO NOT EDIT\nConsult man page for cloginrc(5) for help."
Expand Down Expand Up @@ -251,6 +252,7 @@
rancid::router_db { $groups:
devices => $devices,
rancid_cvs_path => $rancid_path_env_real,
vcs_remote_urls => $vcs_remote_urls,
subscribe => File['rancid_config'],
require => Package[$packages_real],
}
Expand Down
33 changes: 30 additions & 3 deletions manifests/router_db.pp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# == Define: rancid::router_db
#
define rancid::router_db (
$devices = undef,
$rancid_cvs_path = '/bin:/usr/bin',
$router_db_mode = '0640',
Hash $devices = {},
$rancid_cvs_path = '/bin:/usr/bin',
$router_db_mode = '0640',
Hash $vcs_remote_urls = {},
) {
include rancid

Expand All @@ -14,6 +15,32 @@
unless => "test -d ${rancid::homedir_real}/${name}/CVS",
}

if ( $vcs_remote_urls[$name] ) {
$remote_url = $vcs_remote_urls[$name]
exec { "setup git remote ${name}":
command => "git remote set-url origin ${remote_url}",
cwd => "${rancid::homedir_real}/${name}",
path => $rancid_cvs_path,
user => $rancid::user_real,
unless => "git remote -v | grep ${remote_url}"
}

file { "post-commit hook for ${name}":
path => "${rancid::homedir_real}/${name}/.git/hooks/post-commit",
content => "git push origin\n",
owner => $rancid::user_real,
group => $rancid::group_real,
mode => '0755',
}

file { "rancid default git remote ${name}":
path => "${rancid::homedir_real}/.git/${name}",
ensure => absent,
force => true,
backup => false,
}
}

if ( $devices[$name] ) {
file { "${rancid::homedir_real}/${name}/router.db":
ensure => 'file',
Expand Down
35 changes: 35 additions & 0 deletions spec/defines/router_db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,39 @@
.with_mode(0123)
}
end

context 'managing remote urls' do
context 'we are managing them' do
let(:params) {
{ :vcs_remote_urls => {
'group1' => 'http://my-server/my-path.git',
},
}
}

it 'should point at the requested remote url' do
should contain_exec('setup git remote group1')
.with_command('git remote set-url origin http://my-server/my-path.git')
.with_cwd('/var/lib/rancid/group1')
end

it 'should deploy a post commit hook to auto-push to the remote' do
should contain_file('post-commit hook for group1')
end

it 'should remove the default (local) remote' do
should contain_file('rancid default git remote group1')
end
end

context 'we are not managing them' do
let(:params) {
{}
}

it {
should_not contain_exec(/setup git remote/)
}
end
end
end