summaryrefslogtreecommitdiff
path: root/scripts/vyatta-config-loader.pl
blob: b07c08b27e0ad6a2e075595573f23f1a7db94bb3 (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
#!/usr/bin/perl

# Author: An-Cheng Huang <ancheng@vyatta.com>
# Date: 2007
# Description: configuration loader

# **** 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 Vyatta, Inc.
# All Rights Reserved.
# **** End License ****

# Perl script for loading the startup config file.
# $0: startup config file.

use strict;
use lib "/opt/vyatta/share/perl5/";
use Vyatta::ConfigLoad;
use Sys::Syslog qw(:standard :macros);

my $CWRAPPER = '/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper';
my $CONFIG_LOG = '/tmp/vyatta-config.log';
my $COMMIT_CMD = "$CWRAPPER commit";
my $CLEANUP_CMD = "$CWRAPPER cleanup";

umask 0002;

# Set up logging
openlog("config-loader", "nofail", LOG_LOCAL0);
open (STDOUT, '>>', $CONFIG_LOG)
    or die "Can not open $CONFIG_LOG : $!";
open (STDERR, '>&STDOUT') 
    or die "Can not dup STDOUT";

# get a list of all config statement in the startup config file
my %cfg_hier = Vyatta::ConfigLoad::getStartupConfigStatements($ARGV[0],'true');
my @all_nodes    = @{ $cfg_hier{'set'} };

# empty configuration?
exit 1 if (scalar(@all_nodes) == 0);

# set up the config environment
unless (system("$CWRAPPER begin") == 0) {
    syslog(LOG_WARNING, "Cannot set up configuration environment");
    die "Cannot set up configuration environment";
}

#cmd below is added to debug last set of command ordering
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";

	    last if ($rp eq '"');
	    last if ($rp eq '""');

	    if ( !-e $node_path ) {
		#pop this element
		delete $pr[$ct];
		last;
	    }
	    $rel_path = $tmp_path;
      }

      my $comment_cmd = "$CWRAPPER " . join(" ",@pr) ;
      `$comment_cmd`;
      next;
    }

    # Show all commands in log
    my $cmd = join ' ', @pr;
    printf "[%s]\n", $cmd;

    $cmd =  "$CWRAPPER set " . $cmd;
    unless (system($cmd) == 0) {
	$cmd =~ s/^.*?set /set /;
	syslog(LOG_NOTICE, "[[%s]] failed", $cmd);
	warn "[[$cmd]] failed\n";
    }
}

unless (system($COMMIT_CMD) == 0) {
    syslog (LOG_NOTICE, "Commit failed at boot");
    warn "Commit failed at boot\n";
    system($CLEANUP_CMD);
    exit 1;
}

# Now process any deactivate nodes
my @deactivate_nodes = @{ $cfg_hier{'deactivate'} };
if (@deactivate_nodes) {
    my $cmd = "$CWRAPPER deactivate " . $_;
    unless (system($cmd) == 0) {
	syslog(LOG_NOTICE, "[[%s]] failed", $cmd);
	warn "[[$cmd]] failed\n";
    }

    unless (system($COMMIT_CMD) == 0) {
	syslog(LOG_NOTICE, "Commit deactivate failed at boot");
	warn "Commit deactivate failed at boot\n";
	system($CLEANUP_CMD);
    }
}

# really clean up
exec "$CWRAPPER end";

die "exec of $CWRAPPER failed";