summaryrefslogtreecommitdiff
path: root/scripts/priority.pl
blob: 412b600a56788103e7060f17ba42df03a080285b (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
#!/usr/bin/perl

# Read all the configuration templates in the configuration
# template directory and produce an ordered list of the priority
# of configuration actions

use strict;
use warnings;
use File::Find;

my %priorities;
my @parent_priorities;

# Open node file and extract priority and comment if any
sub get_priority {
    open( my $f, '<', $_ )
      or return;
    my $priority;
    my $comment;

    while (<$f>) {
        chomp;
        next unless m/^priority:\s((PARENT)|(\d+))/;
        $priority = $1;

        $comment = $1 if (/#(.*)$/);

        last;
    }
    close $f;
    return ( $priority, $comment );
}

# Called by find and returns true iff
#  file is named node.def
#  file contains priority tag
# Side effect: tores resulting line in $priorities hash for display
sub wanted {
    return unless ( $_ eq 'node.def' );

    my ( $priority, $comment ) = get_priority($File::Find::name);
    return unless $priority;

    my $dir = $File::Find::dir;
    $dir =~ s/^.*\/templates\///;

    $dir .= " #" . $comment
      if $comment;

    # append line to list of entries with same priority
    if ($priority ne 'PARENT') {
	push @{ $priorities{$priority} }, $dir;
    }
    else {
	push(@parent_priorities, $dir);
    }
    return 1;
}

# main program
my $cfgdir = '/opt/vyatta/share/vyatta-cfg/templates';
$cfgdir = $ARGV[0] if $#ARGV == 0;
die "$cfgdir does not exist!" unless -d $cfgdir;

# walk config file tree
find( \&wanted, $cfgdir );

#resolve parent dependencies
foreach my $pp (@parent_priorities) {
    my $tmp = $pp;
    while (1) {
	my $pos = rindex($tmp, "/");
	if ($pos == -1) {
	    last;
	}
	$tmp = substr($tmp,0,$pos);

	#search through second value in collection for match
	foreach (keys %priorities) {
            foreach my $a (@{ $priorities{$_} }) {
		if ($a eq $tmp) {
		    push @{ $priorities{$_} }, $pp . " [PARENT]";
		    last;
		}
	    }
	}
    }
}

# display resulting priorities
foreach my $key ( sort { $a <=> $b } keys %priorities ) {
    my @a = @{ $priorities{$key} };
    foreach my $val ( sort @a ) {
        print $key, " ", $val, "\n";
    }
}