summaryrefslogtreecommitdiff
path: root/scripts/vyatta-config-loader.pl
blob: 0aae55b29aec1307d0be1dd475303c6adf66197f (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
128
129
130
131
132
133
134
#!/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;

umask 0002;

if (!open(OLDOUT, ">&STDOUT") || !open(OLDERR, ">&STDERR")
    || !open(STDOUT, "|/usr/bin/logger -t config-loader -p local0.notice")
    || !open(STDERR, ">&STDOUT")) {
  print STDERR "Cannot dup STDOUT/STDERR: $!\n";
  exit 1;
}
    
if (!open(WARN, "|/usr/bin/logger -t config-loader -p local0.warning")) {
  print OLDERR "Cannot open syslog: $!\n";
  exit 1;
}

sub restore_fds {
  open(STDOUT, ">&OLDOUT");
  open(STDERR, ">&OLDERR");
}

# 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'} };
my @deactivate_nodes = @{ $cfg_hier{'deactivate'} };
if (scalar(@all_nodes) == 0) {
  # no config statements
  restore_fds();
  exit 1;
}

# set up the config environment
my $CWRAPPER = '/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper';
system("$CWRAPPER begin");
if ($? >> 8) {
  print OLDOUT "Cannot set up configuration environment\n";
  print WARN "Cannot set up configuration environment\n";
  restore_fds();
  exit 1;
}

#cmd below is added to debug last set of command ordering
my $commit_cmd = "$CWRAPPER commit";
my $cleanup_cmd = "$CWRAPPER cleanup";
my $ret = 0;
my $rank; #not used
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;
  }

  my $cmd = "$CWRAPPER set " . (join ' ', @pr);
  # this debug file should be deleted before release
  system("echo [$cmd] >> /tmp/foo");
  $ret = system("$cmd");
  if ($ret >> 8) {
    $cmd =~ s/^.*?set /set /;
    print OLDOUT "[[$cmd]] failed\n";
    print WARN "[[$cmd]] failed\n";
    # continue after set failure (or should we abort?)
  }
}

# Now deactivate these nodes
for (@deactivate_nodes) {
    my $cmd = "$CWRAPPER deactivate " . $_ . " 1>/dev/null";
    system("$cmd");
}

$ret = system("$commit_cmd");
if ($ret >> 8) {
  print OLDOUT "Commit failed at boot\n";
  print WARN "Commit failed at boot\n";
  system("$cleanup_cmd");
  # exit normally after cleanup (or should we exit with error?)
}

# really clean up
system("$CWRAPPER end");
restore_fds();

exit 0;