summaryrefslogtreecommitdiff
path: root/scripts/vyatta-protocol-restart.pl
blob: 806fafef83bee9c28950ae4f574e1e34ff606168 (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
#! /usr/bin/perl
#
# Script used to restore protocol configuration

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

my $sbindir           = $ENV{vyatta_sbindir};
my $cfg               = 'vyatta-cfg-cmd-wrapper';
my $active_config_dir = "/opt/vyatta/config/active";

sub usage {
    die "Usage: $0 protocol\n", "protocol := bgp|ospf|rip|ripng\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 die "Can not open file '$file': $!\n";

    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 config {
    my @args = @_;
    push @args, $cfg;
    print join( ' ', @args ), "\n";
    return system "$sbindir/$cfg", @args == 0;
}

sub cleanup {
    config('cleanup');
}

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 = @{$cmd_ref};

        warn "Set failed: ", join(' '), @cmd
          unless config( 'set', @{$cmd_ref} );
    }

    die "Commit failed"
      unless config('commit');
}

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

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

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

$SIG{__DIE__} = \&cleanup;
$SIG{TERM}    = \&cleanup;

# Step 0: lock out any new transactions
config('begin');

# Step 1: save current configuration
my $save_file = "/tmp/$0-$proto.$$";
my $save      = save_config($save_file);

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

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

config('end');

close $save;
## unlink $save_file;