| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
fai-deps - class dependencies for FAI |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
fai-deps [-h] [--man] [-d] |
| 10 |
|
| 11 |
=head1 ABSTRACT |
| 12 |
|
| 13 |
implements dependencies between fai classes. |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
fai-deps uses files $FAI/class/*.deps to sort the classes in $LOGDIR/FAI_CLASSES and define additional ones. While doing so, it retains the original order as much as possible. |
| 18 |
|
| 19 |
*.deps files may contain class names, separated by whitespace. Comments ( after # or ; ) are ignored |
| 20 |
|
| 21 |
e.g. you have a class WORDPRESS that depends on the classes VHOST and POSTGRES . VHOST again may depend on WEBSERVER. |
| 22 |
So if you want to install the blogging software wordpress, you add a file |
| 23 |
|
| 24 |
$FAI/class/WORDPRESS.deps |
| 25 |
|
| 26 |
that contains the words |
| 27 |
VHOST |
| 28 |
POSTGRES |
| 29 |
|
| 30 |
and a file |
| 31 |
$FAI/class/VHOST.deps |
| 32 |
|
| 33 |
that contains the word |
| 34 |
WEBSERVER |
| 35 |
|
| 36 |
The order often is important, so this script is taking care of it. The order of the example above would be: |
| 37 |
WEBSERVER VHOST POSTGRES WORDPRESS |
| 38 |
|
| 39 |
That way, in $FAI/scripts/ first the webserver would be configured, then the vhosts, ... |
| 40 |
|
| 41 |
It removes double entries from FAI_CLASSES and handles circular dependencies[1]. |
| 42 |
|
| 43 |
I do not recommend using circular dependencies, but if you accidentally define them, they will not break your neck. But you'll get a warning ... |
| 44 |
|
| 45 |
=head1 ENVIROMENT |
| 46 |
|
| 47 |
One non-standard perl module is required: |
| 48 |
Graph::Directed; |
| 49 |
On debian install libgraph-perl |
| 50 |
|
| 51 |
The following environment variables are used: |
| 52 |
|
| 53 |
$LOGDIR : path to fai temporary files |
| 54 |
$FAI : path to fai config space |
| 55 |
|
| 56 |
=cut |
| 57 |
|
| 58 |
INIT |
| 59 |
{ |
| 60 |
unless ( $ENV{FAI} and $ENV{LOGDIR} ) { |
| 61 |
print STDERR '$ENV{FAI} and $ENV{LOGDIR} are not defined', $/; |
| 62 |
print STDERR 'This script should be called from within fai', $/; |
| 63 |
exit 1; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
use strict; |
| 68 |
use warnings; |
| 69 |
|
| 70 |
use Getopt::Long; |
| 71 |
use Pod::Usage; |
| 72 |
use Graph::Directed; |
| 73 |
#use Text::Glob qw(match_glob); |
| 74 |
#use Data::Dumper; |
| 75 |
#use GraphViz; |
| 76 |
|
| 77 |
my %opts; |
| 78 |
GetOptions( \%opts, 'help|h', 'man', 'debug|d' ) or pod2usage(1); |
| 79 |
pod2usage(1) if $opts{help}; |
| 80 |
pod2usage(-verbose => 2) if $opts{man}; |
| 81 |
|
| 82 |
my $debug = $opts{debug}; |
| 83 |
my $fai_classes_file = "$ENV{LOGDIR}/FAI_CLASSES"; |
| 84 |
my $class_dir = "$ENV{FAI}/class"; |
| 85 |
|
| 86 |
# main |
| 87 |
{ |
| 88 |
# read classes and dependencies into $digraph |
| 89 |
# retain order of first appearance in @uniq_classes |
| 90 |
my $digraph = Graph::Directed->new; |
| 91 |
my ( @uniq_classes ) = |
| 92 |
read_fai_classes( $digraph, $fai_classes_file ); |
| 93 |
push @uniq_classes, |
| 94 |
read_dependencies( $digraph, $class_dir, @uniq_classes ); |
| 95 |
exit if not $digraph->has_edges; |
| 96 |
|
| 97 |
# debug output |
| 98 |
if ( $debug ) { |
| 99 |
print STDERR 'graph:', $/; |
| 100 |
print STDERR $digraph->stringify(), $/; |
| 101 |
print STDERR 'is strongly connected', $/ |
| 102 |
if $digraph->is_strongly_connected; |
| 103 |
# create_graphviz_output($digraph->edges); |
| 104 |
|
| 105 |
print STDERR 'unique list of classes, orderd by appearence', $/; |
| 106 |
print STDERR join('-', @uniq_classes), $/; |
| 107 |
print STDERR $/; |
| 108 |
} |
| 109 |
|
| 110 |
# warn if graph has cycles |
| 111 |
if ( $digraph->has_a_cycle ) { |
| 112 |
print STDERR 'Warning: cyclic class dependencies found:', $/; |
| 113 |
my $copy = $digraph->copy; |
| 114 |
while ( my @cycle = $copy->find_a_cycle ) { |
| 115 |
print STDERR join('-', @cycle), $/; |
| 116 |
$copy->delete_cycle(@cycle); |
| 117 |
} |
| 118 |
print STDERR 'I`ll try my best to retain your class order', $/; |
| 119 |
} |
| 120 |
|
| 121 |
# sort classes: retain order where possible, respect dependencies where necessary |
| 122 |
my @sorted_classes = sort_classes( $digraph, @uniq_classes ); |
| 123 |
|
| 124 |
# debug output |
| 125 |
if ( $debug ) { |
| 126 |
print STDERR "list of all classes after resolving dependencies:", $/; |
| 127 |
print STDERR "@sorted_classes", $/; |
| 128 |
print STDERR 'in debug mode, this script has no effect at all!', $/x5; |
| 129 |
print STDERR 'Goodbye, and thank you for the fish', $/; |
| 130 |
exit; |
| 131 |
} |
| 132 |
# rewrite $fai_classes_file |
| 133 |
open FAI_CLASSES, ">$fai_classes_file" |
| 134 |
or die "$!: $fai_classes_file"; |
| 135 |
print FAI_CLASSES join($/, @sorted_classes), $/; |
| 136 |
close FAI_CLASSES; |
| 137 |
} |
| 138 |
exit; # end main |
| 139 |
|
| 140 |
# sort_classes: |
| 141 |
# topological sort classes, retaining order as much as possible |
| 142 |
my %class_finished_for; |
| 143 |
my @order; |
| 144 |
sub sort_classes { |
| 145 |
my ( $digraph, @uniq_classes ) = @_; |
| 146 |
@order = @uniq_classes if not @order; |
| 147 |
my @sorted_classes; |
| 148 |
for my $class ( @uniq_classes ) { |
| 149 |
next if exists $class_finished_for{$class}; |
| 150 |
my %unfinished_successor_for = |
| 151 |
map { $_, 1 } |
| 152 |
grep { not exists $class_finished_for{$_} } |
| 153 |
successors($digraph, $class); |
| 154 |
# retain order for successors |
| 155 |
my @unfinished_successors = |
| 156 |
grep { $unfinished_successor_for{$_} } |
| 157 |
@order; |
| 158 |
push @sorted_classes, sort_classes( $digraph, @unfinished_successors ); |
| 159 |
push @sorted_classes, $class; |
| 160 |
$class_finished_for{$class}++; |
| 161 |
} |
| 162 |
return @sorted_classes; |
| 163 |
} |
| 164 |
|
| 165 |
# successors: find successors for a given class |
| 166 |
# handle circular dependencies: |
| 167 |
# * do not return circular connected successors |
| 168 |
# * _do_ return all successors of circular connected successors |
| 169 |
sub successors { |
| 170 |
my ( $digraph, $class ) = @_; |
| 171 |
my $component = $digraph->strongly_connected_component_by_vertex($class); |
| 172 |
# strongly connected components to all successors, except own component |
| 173 |
my %successor_components = |
| 174 |
map { $_, undef } # turn list into hash for uniqueness |
| 175 |
grep { $_ ne $component } |
| 176 |
map { $digraph->strongly_connected_component_by_vertex($_) } |
| 177 |
$digraph->successors($class); |
| 178 |
# classes for these components |
| 179 |
my %successors = |
| 180 |
map { $_, undef } # turn list into hash for uniqueness |
| 181 |
map { $digraph->strongly_connected_component_by_index($_) } |
| 182 |
keys %successor_components; |
| 183 |
return keys %successors; |
| 184 |
} |
| 185 |
|
| 186 |
# read_fai_classes: reads fai classes from $fai_classes_file |
| 187 |
# usually $LOGDIR/FAI_CLASSES |
| 188 |
sub read_fai_classes { |
| 189 |
my ( $digraph, $fai_classes_file) = @_; |
| 190 |
my @uniq_classes; |
| 191 |
# read plain classes from $LOGDIR/FAI_CLASSES |
| 192 |
open FAI_CLASSES, $fai_classes_file |
| 193 |
or die "$!: $fai_classes_file"; |
| 194 |
while ( <FAI_CLASSES> ) { |
| 195 |
chomp; |
| 196 |
# skip double classes |
| 197 |
next if $digraph->has_vertex( $_ ); |
| 198 |
push @uniq_classes, $_; |
| 199 |
$digraph->add_vertex( $_ ); |
| 200 |
} |
| 201 |
close FAI_CLASSES; |
| 202 |
return @uniq_classes; |
| 203 |
} |
| 204 |
|
| 205 |
# read_dependencies: reads dependencies and its classes from $class_dir/*.deps |
| 206 |
my %deps_file_seen_for; |
| 207 |
sub read_dependencies { |
| 208 |
my ( $digraph, $class_dir, @uniq_classes) = @_; |
| 209 |
my @new_classes; |
| 210 |
# read class dependencies from $class_dir/*.deps |
| 211 |
my $prefix = quotemeta($class_dir); |
| 212 |
my @deps_files = grep { |
| 213 |
-f "$class_dir/$_.deps" |
| 214 |
and not -x "$class_dir/$_.deps" |
| 215 |
} @uniq_classes; |
| 216 |
for my $class ( @deps_files ) { |
| 217 |
next if $deps_file_seen_for{$class}++; |
| 218 |
open DEPSFILE, "$class_dir/$class.deps" |
| 219 |
or die "$!: $class"; |
| 220 |
while ( <DEPSFILE> ) { |
| 221 |
chomp; |
| 222 |
# remove comments, leading and trailing whitespace |
| 223 |
s/(#|;).*// ; |
| 224 |
s/ ^\s+ //x; |
| 225 |
s/ \s+$ //x; |
| 226 |
# allow multiple classes per line |
| 227 |
my @deps = split m/\s+/; |
| 228 |
for my $dep ( @deps ) { |
| 229 |
push @new_classes, $dep |
| 230 |
if not $digraph->has_vertex( $dep ); |
| 231 |
$digraph->add_edge($class, $dep); |
| 232 |
} |
| 233 |
} |
| 234 |
close DEPSFILE; |
| 235 |
push @new_classes, read_dependencies( $digraph, $class_dir, @new_classes ); |
| 236 |
} |
| 237 |
return @new_classes; |
| 238 |
} |
| 239 |
|
| 240 |
#sub create_graphviz_output { |
| 241 |
# my @edges = @_; |
| 242 |
# my $g = GraphViz->new(); |
| 243 |
# for ( @edges ) { |
| 244 |
# $g->add_edge( @$_ ); |
| 245 |
# } |
| 246 |
# return $g->as_png('graph-test.png'); |
| 247 |
#} |
| 248 |
|
| 249 |
=head1 SEE ALSO |
| 250 |
|
| 251 |
https://fai-project.org |
| 252 |
|
| 253 |
=head1 AUTHOR |
| 254 |
|
| 255 |
Copyright 2008 by Ingo Wichmann <[email protected]> |
| 256 |
|
| 257 |
This software ist free software; you can redistribute it and/or modify |
| 258 |
it unter the same terms as Perl itself. |
| 259 |
|
| 260 |
=cut |
| 261 |
|