Skip to content

Commit

Permalink
Manage git remote for device groups
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
nhemingway committed Aug 24, 2021
1 parent 494254a commit 8d0f51f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 26 deletions.
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
15 changes: 15 additions & 0 deletions spec/defines/router_db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@
.with_mode(0123)
}
end

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

it {
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
end

0 comments on commit 8d0f51f

Please sign in to comment.