From 57059a1db3fbe6bf308bf4a9e34082fc3bfdf0a1 Mon Sep 17 00:00:00 2001 From: Neil Hemingway Date: Thu, 12 Aug 2021 10:15:09 +0100 Subject: [PATCH] Manage git remote for device groups Closes #27 --- README.md | 11 ++++++++ manifests/init.pp | 48 ++++++++++++++++++---------------- manifests/router_db.pp | 33 ++++++++++++++++++++--- spec/defines/router_db_spec.rb | 35 +++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9e0ba6a..74d6468 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 === diff --git a/manifests/init.pp b/manifests/init.pp index 04f2106..c1fd33a 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -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." @@ -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], } diff --git a/manifests/router_db.pp b/manifests/router_db.pp index de9ad30..0765283 100644 --- a/manifests/router_db.pp +++ b/manifests/router_db.pp @@ -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 @@ -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', diff --git a/spec/defines/router_db_spec.rb b/spec/defines/router_db_spec.rb index 0a6d3a4..3b2dd3b 100644 --- a/spec/defines/router_db_spec.rb +++ b/spec/defines/router_db_spec.rb @@ -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