summaryrefslogtreecommitdiff
path: root/scripts/vyatta-protocol-restart.pl
blob: 5b77b76e9e63a33d9ea725dbf71815b93543a604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/perl
#
# **** 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.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'.
# You can also obtain it by writing to the Free Software Foundation,
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc.
# All Rights Reserved.
# **** End License ****
#
# Author: Stephen Hemminger
# Date: January 2009
# Description: Script used to restore protocol configuration on quagga daemon
# restart
#
# Not meant to be run directly, it is wrapped inside another script
# that sets up transaction

use strict;
use warnings;
use lib "/opt/vyatta/share/perl5/";
use Vyatta::ConfigOutput;
use Vyatta::ConfigLoad;

my $sbindir           = '/opt/vyatta/sbin';
my $active_config_dir = "/opt/vyatta/config/active";

my %protomap = (
    'bgp'  => ['protocols/bpp'],
    'ospf' => [ 'protocols/ospf', 'interfaces/*/*/ip/ospf' ],
    'rip'  => [ 'protocols/rip', 'interfaces/*/*/ip/rip' ],
);

sub usage {
    die "Usage: $0 {",join('|',keys %protomap),"}\n";
}

sub save_config {
    my $file        = shift;
    my $version_str = `/opt/vyatta/sbin/vyatta_current_conf_ver.pl`;
    die "no version string??" unless $version_str;

    open my $save, '+>', $file
	or return; #undef

    select $save;
    set_show_all(1);
    outputActiveConfig();
    print $version_str;
    select STDOUT;
    print "created $file\n";

    return $save;
}

sub clean_nodes {
    foreach my $path (@_) {
        system("rm -rf $active_config_dir/$path");
    }
}

sub load_config {
    my $file     = shift;
    my %cfg_hier = Vyatta::ConfigLoad::loadConfigHierarchy($file);
    die "Saved configuration was bad can't reload"
      unless %cfg_hier;

    my %cfg_diff = Vyatta::ConfigLoad::getConfigDiff( \%cfg_hier );

    # Only doing sets
    foreach ( @{ $cfg_diff{'set'} } ) {
        my ( $cmd_ref, $rank ) = @{$_};
        my @cmd = ( "my_set", @{$cmd_ref} );
	
	system "$sbindir/my_set", @cmd == 0
	    or warn join(' '), @cmd;
    }
}


my $proto = shift @ARGV;
usage unless $proto;

my @nodes = $protomap{$proto};
usage unless @nodes;

# set up the config environment
my $CWRAPPER = '/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper';
system ("$CWRAPPER begin") == 0
    or die "Cannot set up configuration environment\n";

# Step 1: save current configuration
my $save_file = "/tmp/$0-$proto.$$";
my $save      = save_config($save_file);
if (! defined $save) {
    system "$CWRAPPER cleanup";
    die "Can not open file '$save_file': $!\n";
}

# Step 2: remove old state
clean_nodes(@nodes);

# Step 3: reload
seek $save, 0, 0;
load_config($save);
close $save;

# Step 4: finalize
system "$CWRAPPER commit" == 0
    or die "Reload failed: check $save_file\n";

unlink $save_file;