diff options
author | Stig Thormodsrud <stig@vyatta.com> | 2010-10-26 16:58:08 -0700 |
---|---|---|
committer | Stig Thormodsrud <stig@vyatta.com> | 2010-10-26 16:58:08 -0700 |
commit | de8b643eb1ff7c056f4e5e9c920387ed2d2d2a11 (patch) | |
tree | 333c2e0b2f020ed6469c3076d43050de08cfcb4a /lib/Vyatta/ConfigMgmt.pm | |
download | vyatta-config-mgmt-de8b643eb1ff7c056f4e5e9c920387ed2d2d2a11.tar.gz vyatta-config-mgmt-de8b643eb1ff7c056f4e5e9c920387ed2d2d2a11.zip |
Create vyatta-config-mgmt repo.debian/0.1
Diffstat (limited to 'lib/Vyatta/ConfigMgmt.pm')
-rw-r--r-- | lib/Vyatta/ConfigMgmt.pm | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/lib/Vyatta/ConfigMgmt.pm b/lib/Vyatta/ConfigMgmt.pm new file mode 100644 index 0000000..85d9c72 --- /dev/null +++ b/lib/Vyatta/ConfigMgmt.pm @@ -0,0 +1,161 @@ +# +# Module: ConfigMgmt.pm +# +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2007-2009 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Stig Thormodsrud +# Date: October 2010 +# Description: Common config-mgmt functions +# +# **** End License **** +# +package Vyatta::ConfigMgmt; + +use strict; +use warnings; + +our @EXPORT = qw(cm_commit_add_log cm_commit_get_log cm_get_archive_dir + cm_get_lr_conf_file cm_get_lr_state_file + cm_get_commit_hook_dir cm_write_file cm_read_file + cm_commit_get_file); +use base qw(Exporter); + +use Vyatta::Config; +use Vyatta::Interface; +use Vyatta::Misc; +use POSIX; +use IO::Zlib; + + +my $commit_hook_dir = '/etc/commit/'; +my $config_dir = '/opt/vyatta/etc/config'; +my $archive_dir = "$config_dir/archive"; +my $config_file = "$config_dir/config.boot"; +my $lr_conf_file = "$archive_dir/lr.conf"; +my $lr_state_file = "$archive_dir/lr.state"; +my $commit_log_file = "$archive_dir/commits"; + + +sub cm_get_commit_hook_dir { + return $commit_hook_dir; +} + +sub cm_get_archive_dir { + return $archive_dir; +} + +sub cm_get_lr_conf_file { + return $lr_conf_file; +} + +sub cm_get_lr_state_file { + return $lr_state_file; +} + +sub cm_read_file { + my ($file) = @_; + my @lines; + if ( -e $file) { + open(my $FILE, '<', $file) or die "Error: read $!"; + @lines = <$FILE>; + close($FILE); + chomp @lines; + } + return @lines; +} + +sub cm_write_file { + my ($file, $data) = @_; + + open(my $fh, '>', $file) || die "Couldn't open $file - $!"; + print $fh $data; + close $fh; + return 1; +} + +sub cm_get_max_revs { + my $config = new Vyatta::Config; + $config->setLevel('system config-mgmt'); + my $revs = $config->returnOrigValue('commit-revisions'); + return $revs; +} + +sub cm_commit_add_log { + my ($user, $via, $comment) = @_; + + my $time = time(); + if ($comment =~ /\|/) { + print "before [$comment]\n"; + $comment =~ s/\|/\%\%/g; + print "after [$comment]\n"; + } + my $new_line = "|$time|$user|$via|$comment|"; + my @lines = cm_read_file($commit_log_file); + + my $revs = cm_get_max_revs(); + unshift(@lines, $new_line); # head push() + if (defined $revs and scalar(@lines) > $revs) { + $#lines = $revs-1; + } + my $log = join("\n", @lines); + cm_write_file($commit_log_file, $log); +} + +sub cm_commit_get_log { + + my @lines = cm_read_file($commit_log_file); + + my @commit_log = (); + my $count = 0; + foreach my $line (@lines) { + if ($line !~ /^\|(.*)\|$/) { + print "Invalid log format [$line]\n"; + next; + } + $line = $1; + my ($time, $user, $via, $comment) = split(/\|/, $line); + my $time_str = strftime("%Y-%m-%d %H:%M:%S", localtime($time)); + $comment =~ s/\%\%/\|/g; + my $new_line = sprintf("%-2s %s by %s via %s\n", + $count, $time_str, $user, $via); + push @commit_log, $new_line; + if (defined $comment and $comment ne '' and $comment ne 'commit') { + push @commit_log, " $comment\n" + } + $count++; + } + return @commit_log; +} + +sub cm_commit_get_file { + my ($revnum) = @_; + + my $max_revs = cm_get_max_revs(); + if ($revnum > $max_revs) { + print "Error: Invalid config revision number\n"; + exit 1; + } + + my $filename = $archive_dir . "/config.boot." . $revnum . ".gz"; + die "File [$filename] not found." if ! -e $filename; + + my $fh = new IO::Zlib; + $fh->open($filename, "rb") or die "Error: read $!"; + my @lines = <$fh>; + $fh->close; + return join('', @lines); +} + +1; |