diff options
author | Michael Larson <slioch@slioch.vyatta.com> | 2010-05-14 13:47:21 -0700 |
---|---|---|
committer | Michael Larson <slioch@slioch.vyatta.com> | 2010-05-14 13:47:21 -0700 |
commit | d36d0a6ea76d443c5e4072401eaa9dbf563c74bd (patch) | |
tree | 9daed3e19d3d5b8231a9894891ac00d4df9b9c3a | |
parent | 91bb77e328c9cad994609aeeccb33971eca1486f (diff) | |
download | vyatta-cfg-d36d0a6ea76d443c5e4072401eaa9dbf563c74bd.tar.gz vyatta-cfg-d36d0a6ea76d443c5e4072401eaa9dbf563c74bd.zip |
initial checkin of comment feature for cli.
/* to create a comment */
> comment [PATH-TO-NODE] "my comments"
/* to delete a comment */
> comment [PATH-TO-NODE] ""
-rw-r--r-- | Makefile.am | 1 | ||||
-rwxr-xr-x | etc/bash_completion.d/20vyatta-cfg | 14 | ||||
-rwxr-xr-x | lib/Vyatta/Config.pm | 21 | ||||
-rwxr-xr-x | lib/Vyatta/ConfigLoad.pm | 41 | ||||
-rwxr-xr-x | lib/Vyatta/ConfigOutput.pm | 26 | ||||
-rwxr-xr-x | scripts/vyatta-cfg-cmd-wrapper | 4 | ||||
-rw-r--r-- | scripts/vyatta-comment-config.pl | 92 | ||||
-rwxr-xr-x | scripts/vyatta-config-loader.pl | 28 | ||||
-rwxr-xr-x | scripts/vyatta-load-config.pl | 37 |
9 files changed, 256 insertions, 8 deletions
diff --git a/Makefile.am b/Makefile.am index 69c10c7..4bb3630 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,6 +51,7 @@ sbin_SCRIPTS += scripts/vyatta-output-config.pl sbin_SCRIPTS += scripts/vyatta-save-config.pl sbin_SCRIPTS += scripts/vyatta-load-config.pl sbin_SCRIPTS += scripts/vyatta-activate-config.pl +sbin_SCRIPTS += scripts/vyatta-comment-config.pl sbin_SCRIPTS += scripts/vyatta-cfg-notify sbin_SCRIPTS += scripts/vyatta-irqaffin sbin_SCRIPTS += scripts/vyatta-auto-irqaffin.pl diff --git a/etc/bash_completion.d/20vyatta-cfg b/etc/bash_completion.d/20vyatta-cfg index e17fd36..62a5440 100755 --- a/etc/bash_completion.d/20vyatta-cfg +++ b/etc/bash_completion.d/20vyatta-cfg @@ -431,6 +431,17 @@ loadkey() eval "${vyatta_sbindir}/vyatta-load-user-key.pl $@" } +comment() +{ + if [ "$#" -eq "0" ]; then + return 0 + fi + args=("$@") + #now need to replace last element + args[${#args[*]}-1]="\"${args[${#args[*]}-1]i}\""; + eval "${vyatta_sbindir}/vyatta-comment-config.pl ${args[*]}" +} + activate() { #create or remove activate file @@ -942,6 +953,7 @@ vyatta_config_complete () if (( ${#COMP_WORDS[@]} < 2 )); then declare -a hitems=( "activate" \ + "comment" \ "commit" \ "copy" \ "deactivate" \ @@ -959,6 +971,7 @@ vyatta_config_complete () "show" ) declare -a hstrs=( \ "Enable this portion of the configuration" \ + "Add comment to this configuration element" \ "Commit the current set of changes" \ "Copy a configuration element" \ "Inactivate this portion of the configuration" \ @@ -1310,6 +1323,7 @@ complete -F vyatta_loadsave_complete save complete -F vyatta_loadsave_complete load complete -F vyatta_loadsave_complete merge complete -F vyatta_loadkey_complete loadkey +complete -F vyatta_config_complete comment complete -F vyatta_config_complete activate complete -F vyatta_config_complete deactivate complete -F vyatta_config_complete copy diff --git a/lib/Vyatta/Config.pm b/lib/Vyatta/Config.pm index 82d835f..c0c8494 100755 --- a/lib/Vyatta/Config.pm +++ b/lib/Vyatta/Config.pm @@ -329,6 +329,27 @@ sub returnValue { return $tmp; } +## returnComment("node") +# returns the value of "node" or undef if the node doesn't exist . +# node is relative +sub returnComment { + my ( $self, $node ) = @_; + my $tmp = undef; + + $node =~ s/\//%2F/g; + $node =~ s/\s+/\//g; + + return unless + open my $file, '<', + "$self->{_new_config_dir_base}/$node/.comment"; + + read $file, $tmp, 16384; + close $file; + + $tmp =~ s/\n$//; + return $tmp; +} + ## returnOrigPlusComValue("node") # returns the value of "node" or undef if the node doesn't exist . # node is relative diff --git a/lib/Vyatta/ConfigLoad.pm b/lib/Vyatta/ConfigLoad.pm index 28e932e..5bad31d 100755 --- a/lib/Vyatta/ConfigLoad.pm +++ b/lib/Vyatta/ConfigLoad.pm @@ -58,6 +58,8 @@ sub applySingleQuote { return @return; } +my @comment_list = (); + sub enumerate_branch { my $cur_node = shift; my @cur_path = @_; @@ -72,14 +74,28 @@ sub enumerate_branch { } my $terminal = 0; if (!defined($cur_node->{'children'})) { - $terminal = 1; + $terminal = 1; } else { - foreach (@{$cur_node->{'children'}}) { - if (defined($_->{'name'})) { - enumerate_branch($_, @cur_path); - $terminal = 0; + my $comment; + foreach (@{$cur_node->{'children'}}) { + if (defined($_->{'comment'})) { + $comment = $_->{'comment'}; + } + + if (defined($_->{'name'})) { + if (defined $comment) { + push @comment_list, join(" ", (@cur_path, $_->{'name'}, "\"" . $comment . "\"")); + $comment = undef; + } + else { + #need to check for existance of .comment file here. + push @comment_list, join(" ", (@cur_path, $_->{'name'}, "\"\"")); + } + enumerate_branch($_, @cur_path); + $terminal = 0; + } + } - } } if ($terminal) { my $val = $cur_node->{'value'}; @@ -111,6 +127,8 @@ sub getStartupConfigStatements { if (!defined($load_cfg)) { return (); } + + my $comments = shift; my $xcp = new XorpConfigParser(); $xcp->parse($load_cfg); @@ -120,6 +138,15 @@ sub getStartupConfigStatements { } enumerate_branch($root, ( )); + if (defined $comments && $comments eq 'true') { + #add comment commands to all nodes + foreach my $c (@comment_list) { + if ($c !~ /\"\"$/) { + my @pth = split(" ",'comment ' . $c); + push @all_nodes, [\@pth, 1]; + } + } + } return @all_nodes; } @@ -354,6 +381,7 @@ sub findSetNodes { my @tmp = @plist[1..$#plist]; push @disable_list, [\@tmp, 0]; } + findSetNodes($new_ref->{$_}, [ @active_path, $_ ]); } # we recur regardless of whether it's in active. all changes will be @@ -392,6 +420,7 @@ sub getConfigDiff { 'delete' => \@new_delete_list, 'set' => \@set_list, 'deactivate' => \@disable_list, + 'comment' => \@comment_list, ); return %diff; } diff --git a/lib/Vyatta/ConfigOutput.pm b/lib/Vyatta/ConfigOutput.pm index caa9356..99a0da8 100755 --- a/lib/Vyatta/ConfigOutput.pm +++ b/lib/Vyatta/ConfigOutput.pm @@ -82,6 +82,7 @@ sub displayValues { my $HIDE_PASSWORD = '****************'; $config->setLevel(join ' ', @cur_path); + if ($is_multi) { my @ovals = $config->returnOrigValues(''); my @nvals = $config->returnValues(''); @@ -201,6 +202,11 @@ sub displayDeletedOrigChildren { if (!defined $is_tag) { my $path = join(' ',( @cur_path, $child )); + my $comment = $config->returnComment($path); + if (defined $comment) { + print "$prefix /* $comment */\n"; + } + my ($state, $n) = $config->getDeactivated($path); if (defined $state) { $dis = '! '; @@ -211,6 +217,7 @@ sub displayDeletedOrigChildren { } $config->setLevel(join ' ', (@cur_path, $child)); + my @cnames = sort $config->listOrigNodesNoDef(); if ($cnames[0] eq 'node.val') { @@ -227,8 +234,14 @@ sub displayDeletedOrigChildren { next; } - #need separate check here my $path = join(' ',( @cur_path, $child, $cname )); + + my $comment = $config->returnComment($path); + if (defined $comment) { + print "$prefix /* $comment */\n"; + } + + #need separate check here my ($state, $n) = $config->getDeactivated($path); if (defined $state) { $dis = '! '; @@ -288,6 +301,11 @@ sub displayChildren { if (!defined($is_tag)) { my $path = join(' ',( @cur_path, $child )); + my $comment = $config->returnComment($path); + if (defined $comment) { + print "$prefix /* $comment */\n"; + } + my ($state, $n) = $config->getDeactivated($path); if (defined $state) { $dis = '! '; @@ -325,6 +343,11 @@ sub displayChildren { } my $path = join(' ',( @cur_path, $child, $cname )); + my $comment = $config->returnComment($path); + if (defined $comment) { + print "$prefix /* $comment */\n"; + } + my ($state, $n) = $config->getDeactivated($path); if (defined $state) { $dis = '! '; @@ -388,6 +411,7 @@ sub outputNewConfig { $config = new Vyatta::Config; $config->setLevel(join ' ', @_); my %rnodes = $config->listNodeStatus(undef,'true'); + if (scalar(keys %rnodes) > 0) { my @rn = keys %rnodes; diff --git a/scripts/vyatta-cfg-cmd-wrapper b/scripts/vyatta-cfg-cmd-wrapper index 3b2f040..4941e37 100755 --- a/scripts/vyatta-cfg-cmd-wrapper +++ b/scripts/vyatta-cfg-cmd-wrapper @@ -220,6 +220,10 @@ case "$1" in /opt/vyatta/sbin/vyatta-activate-config.pl activate "${@:2}" RET_STATUS=$? ;; + comment) + /opt/vyatta/sbin/vyatta-comment-config.pl "${@:2}" + RET_STATUS=$? + ;; commit) # debug file /tmp/bar should be deleted before release /opt/vyatta/sbin/my_commit -a >> /tmp/bar diff --git a/scripts/vyatta-comment-config.pl b/scripts/vyatta-comment-config.pl new file mode 100644 index 0000000..d19268e --- /dev/null +++ b/scripts/vyatta-comment-config.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +# Author: Michael Larson <mike@vyatta.com> +# Date: 2010 +# Description: Perl script for adding comments to portions of the configuration + +# **** 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) 2006, 2007, 2008, 2009, 2010 Vyatta, Inc. +# All Rights Reserved. +# **** End License **** + +use strict; +use warnings; +use File::Find; +use lib "/opt/vyatta/share/perl5"; + + +sub usage() { + print "Usage: $0 <path>\n"; + exit 0; +} + +if ($#ARGV == 0) { + exit 0; +} + +#adjust for leaf node +my $i = 0; +my @path = @ARGV[0..$#ARGV-1]; +foreach my $elem (@path) { + $elem =~ s/\//%2F/g; + $elem =~ s/\s+/\//g; + $path[$i++] = $elem; +} +my $path = join '/', @path; + +my $full_path = "$ENV{VYATTA_TEMP_CONFIG_DIR}/$path"; + +if (! -e $full_path) { + $path = join '/', @path[0..$#path-1]; + my $leaf = "$ENV{VYATTA_TEMP_CONFIG_DIR}/$path/node.val"; + if (-e $leaf) { + $full_path = "$ENV{VYATTA_TEMP_CONFIG_DIR}/$path"; + } + else { + `echo \"Path is not valid\n\"`; + exit 0; + } +} +#else { +# print "echo \"$ARGV[$#ARGV]\" > $full_path/node.comment\n"; +# `echo \"$ARGV[$#ARGV]\" > $full_path/node.comment`; +#} + +if ($ARGV[$#ARGV] eq '') { + `rm -f $full_path/.comment`; +} +else { + `echo \"$ARGV[$#ARGV]\" > $full_path/.comment`; +} + +#first let's check and ensure that there is not another child .disable node... +#also needs to be enforced when committing +#my $active_dir = "$ENV{VYATTA_ACTIVE_CONFIGURATION_DIR}/$path"; +#my $local_dir = $full_path; +#if (-e $active_dir) { +# find( \&wanted, $active_dir ); +#} +#if (-e $local_dir) { +# find( \&wanted, $local_dir ); +#} +#`touch $full_path/node.comment`; + +#if this is activate +# make sure no activate subnodes +# create .disable file in node +#else +# ensure .disable file exists +# remove node + +print "Done\n"; +exit 0; diff --git a/scripts/vyatta-config-loader.pl b/scripts/vyatta-config-loader.pl index 7a3df2b..b8e594e 100755 --- a/scripts/vyatta-config-loader.pl +++ b/scripts/vyatta-config-loader.pl @@ -46,7 +46,7 @@ sub restore_fds { } # get a list of all config statement in the startup config file -my @all_nodes = Vyatta::ConfigLoad::getStartupConfigStatements($ARGV[0]); +my @all_nodes = Vyatta::ConfigLoad::getStartupConfigStatements($ARGV[0],'true'); if (scalar(@all_nodes) == 0) { # no config statements restore_fds(); @@ -72,6 +72,32 @@ foreach (@all_nodes) { my ($path_ref, $rank) = @$_; my @pr = @$path_ref; + if (@pr[0] =~ /^comment$/) { + my $ct = 0; + my $rel_path; + foreach my $rp (@pr[1..$#pr]) { + $ct++; + my $tmp_path = $rel_path . "/" . $rp; + my $node_path = "/opt/vyatta/share/vyatta-cfg/templates/" . $tmp_path . "/node.def"; + if ($rp eq '"') { + last; + } + elsif ($rp eq '""') { + last; + } + elsif (!-e $node_path) { + #pop this element + delete @pr[$ct]; + last; + } + $rel_path = $tmp_path; + } + + my $comment_cmd = "$CWRAPPER " . join(" ",@pr) ; + `$comment_cmd`; + next; + } + if (@pr[0] eq '!') { @pr = @pr[1..$#pr]; my $deactivate_cmd = "$CWRAPPER deactivate " . (join ' ', @pr) . " 1>/dev/null"; diff --git a/scripts/vyatta-load-config.pl b/scripts/vyatta-load-config.pl index ab5a47e..62d6e8b 100755 --- a/scripts/vyatta-load-config.pl +++ b/scripts/vyatta-load-config.pl @@ -191,6 +191,7 @@ if ( scalar( keys %cfg_hier ) == 0 ) { my %cfg_diff = Vyatta::ConfigLoad::getConfigDiff( \%cfg_hier ); my @set_list = @{ $cfg_diff{'set'} }; my @deactivate_list = @{ $cfg_diff{'deactivate'} }; +my @comment_list = @{ $cfg_diff{'comment'} }; if ($merge_mode eq 'false') { my @delete_list = @{ $cfg_diff{'delete'} }; @@ -226,6 +227,42 @@ foreach (@deactivate_list) { #ignore error on complaint re: nested nodes } +foreach (@comment_list) { + my ( $cmd_ref ) = $_; + #apply comment if it doesn't have an empty element at the array and a .comment file exists and this is not a merge + if ($merge_mode eq 'false' && $cmd_ref =~ /\"\"$/) { + my @cmd_array = split(" ",$cmd_ref); + pop(@cmd_array); + my $rel_path = join '/', @cmd_array; + my $path = "/opt/vyatta/config/active/" . $rel_path . "/.comment"; + if (-e $path) { + my @cmd = ( "$sbindir/vyatta-comment-config.pl ", $cmd_ref ); + my $cmd_str = join ' ', @cmd; + system("$cmd_str 1>/dev/null"); + } + else { + #not found, maybe a leaf? + pop(@cmd_array); + $rel_path = join '/', @cmd_array; + my $leaf = "/opt/vyatta/config/active/" . $rel_path . "/node.val"; + if (-e $leaf) { + $path = "/opt/vyatta/config/active/" . $rel_path . "/.comment"; + if (-e $path) { + my @cmd = ( "$sbindir/vyatta-comment-config.pl ", $cmd_ref ); + my $cmd_str = join ' ', @cmd; + system("$cmd_str 1>/dev/null"); + } + } + } + } + else { + my @cmd = ( "$sbindir/vyatta-comment-config.pl ", $cmd_ref ); + my $cmd_str = join ' ', @cmd; + system("$cmd_str 1>/dev/null"); + } + #ignore error on complaint re: nested nodes +} + system("$sbindir/my_commit"); if ( $? >> 8 ) { print "Load failed (commit failed)\n"; |