summaryrefslogtreecommitdiff
path: root/ivylaunch
blob: 8504a2a358e9bdf47517534c95643e76740b3c9a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/perl -w
#
#	IvyLaunch, a perl script to launch applications on ivy
# 
#	Copyright (C) 1997-1999
#	Centre d'Études de la Navigation Aérienne
#
#	Main
#
#	Authors: Michelle Jacomi <jacomi@cena.fr>
#		Johnny Accot <accot@cena.fr>
#       Modified by: Christophe Mertz Mertz@cena.fr
#                    Daniel Etienne <etienne@cena.fr>
#
#	$Id$
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU GPL General Public License
#	as published by the Free Software Foundation; either version 2
#	of the License, or (at your option) any later version.
#	Or look at http://www.gnu.org/copyleft/gpl.html
#
#	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.
#	
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

our $VERSION = 1.6;


BEGIN {
    if (-d "/usr/lib/ivylaunch") {
	# comment this line if FugueConfig is located elsewhere
	unshift(@INC, "/usr/lib/ivylaunch");
	print "/usr/lib/ivylaunch is prepend to \@INC\n";
    }
}

$SIG{INT} = 'RunEndCmd';


use Ivy;
use strict;
use Sys::Hostname;
use File::Basename;
use Getopt::Long qw(:config pass_through);
use POSIX 'sys_wait_h';
use FugueConfig;

my $appliname = "ivylaunch";
my $HEADER = "$appliname";

my %OPTIONS = ("b=s" => "ivy bus");

my %opt;
my @apps;

Getopt::Long::GetOptions(\%opt, "b=s", 'help', "override") ;
&usage if $opt{help};


my $bus = defined $ENV{"IVYBUS"} ? $ENV{"IVYBUS"} : '127.255.255.255:2010';
$bus = $opt{'b'} if defined $opt{'b'};

my $file = pop @ARGV;
&usage unless defined($file);

my @cppopts = @ARGV;


my (%child);

my %cmd = ('begin' => [], 'end' => []);
my %agent = ('global' => [], 'local' => []);

$| = 1;

print "$HEADER Reading configuration file $file...\n";

my ($type, %ready);

for (FugueConfig::parse($file, @cppopts)) {
    my ($type, $host, $name, $command, $params) = @$_;
    next if $type eq 'comment';
    if ($type eq 'begin' or $type eq 'end') {
	push @{$cmd{$type}}, [$host, $command, $params];
    } else {
	# add filename to ivycontrolpanel command (not destructive)
	$params .= "  $file" if ($name eq 'ivycontrolpanel');
	push @{$agent{$type}}, [$name, $host, $command, $params, $bus];
	push (@apps, $name);
    }
}

print "$HEADER needs ", join (",", @apps), "\n";


Ivy->init(-loopMode => 'LOCAL', 
	  -onDieFunc => [\&Quit],
	  -appName => $appliname,
	  -ivyBus => $bus);

my $ivy = Ivy->new (-appName => $appliname,
		    -ivyBus => $bus, 
		    -statusFunc => \&Status,
		    -neededApp => [@apps]);

print "$HEADER $appliname is being launched using bus $bus (pid=$$)...\n";

# Now execute commands and launch the necessary agents
foreach my $app (@{$cmd{'begin'}}) {
    my $pid = FugueConfig::launchCommand ('begin', @$app);
    $child{$pid} = $app->[1];
    $child{$pid} .= " ".$app->[2] if $app->[2] !~ /^\s*$/;
}
foreach my $app (@{$agent{'local'}}) {
    my $pid = FugueConfig::launchAgent(@$app);
    $child{$pid} = $app->[0];
}

# if after 5 seconds the "global" agents are not detected, launch them.
if (@{$agent{'global'}}) {
    print "$HEADER Wait 5 seconds before global apps checking...\n";
    Ivy::after (5000, [\&Global]);
}

my $host = hostname;


$SIG{CHLD} = 'Child';

$ivy->start;

$ivy->mainLoop;


sub Global {

    print "$HEADER Now looking at global apps...\n";
    my $n = 0;
    foreach my $app (@{$agent{'global'}}) {
    
        my $name = @$app[0];
        unless (grep { $name eq $_ } (keys(%ready))) {
            print ("$HEADER appli $name not found. Launching it.\n");
            my $pid = FugueConfig::launchAgent(@$app);
	    $child{$pid} = $app->[0];
            $n++;
        }
    }
    print "$HEADER $n applis launched.\n";
}




# function called when a child exits
sub Child {

    $SIG{CHLD} = 'Child';

    if (my $pid = waitpid (-1, WNOHANG)) {
	return if $pid == -1;
	return unless $child{$pid};
	my $status = $? & 255;
	print "$HEADER Warning!  $child{$pid}".
	    " (pid=$pid) exited with status $status.\n";
	delete $child{$pid};
    }
}


# function called on a die message
sub Quit {
    
    print "$HEADER Quit (on Die message)\n";
    # kill begin commands (if running) and agents
    foreach (keys %child) {
  	print "$HEADER Stopping '$child{$_}' (pid=$_)...\n";
  	kill INT => $_;
    }
    &RunEndCmd;
}


sub RunEndCmd {


    # execute end commands
    foreach my $app (@{$cmd{'end'}}) {
      FugueConfig::launchCommand('end',@$app);
    }
    $ivy->stop();
    exit;

}


sub Status {
    
    my ($ref_array_present, $ref_array_absent, $ref_hash_present,
	$agent, $status, $host) = @_;

    if ($status eq 'new') {
	print ("$HEADER $agent connected from host $host.\n");
	print ("$HEADER All agents are running.\n") unless @$ref_array_absent;
	$ready{$agent}++;
    }
    elsif ($status eq 'died') {
	print ("$HEADER $agent disconnected from host $host..\n");
	print ("$HEADER Agents are missing.\n") if @$ref_array_absent;
	$ready{$agent}--;
	delete $ready{$agent} if $ready{$agent} <= 0;
    }
}

sub usage {
    
    print "Version : $VERSION\n";
    print "Usage: $appliname [-b bus] [-override] [cpp options] <config_file>\n";
    exit;
}

__END__
    
=head1 NAME

ivylaunch - a script which launches ivy agents and commands, according to a configuration file.

=head1 SYNOPSIS

ivylaunch [-help] [-b bus] [-override] [cpp options] configfile

=head1 DESCRIPTION

ivylaunch forks ivy agents and commands described in configuration file. ivylaunch is also an ivy agent : it reports agents connection and disconnection, and can be killed by an Ivy die message. 
    
    
=back

=head1 OPTIONS

=over
    
=item B<-b> bus 

Specify the ivy bus address on which ivylaunch will communicate. If this option is not provided, the value of the environment variable IVYBUS is used. If the variable is not defined,  the default  value is 127:2010, that is port 2010 on localhost.

=item B<-override>

Infer ivylaunch behavior when a B<global> agent is detected on the bus. See below for explanation. 
    
=back

=head1 CONFIGURATION FILE FORMAT

Each line of configuration file should contain at least 4 mandatory fields separated by space or tabulation : B<type>, B<host>, B<name> and B<command> and its options.
    
=over

=item B<type>

the type of forked commands should take one of the following values : begin, local, global, end.

=over

=item B<begin>

links up with simple commands (not ivy agents) which are executed at first,

=item B<local>

links up with ivy agents which can have one or more instances on the bus.

=item B<global> 

links up with ivy agents which must be single. Before launching a global agent, ivylaunch checks for it on the bus; if this agent is not present, ivylaunch launches it. If it's already connected, ivylaunch preserves it if the B<-override> option is false (the default value), or kills it (by sending an ivy die message) and forks a new one if -override is true. global agents are launched after local ones.

=item B<end>

links up with simple commands which are executed after agents have been killed, just before exiting. These commands are kept alive when ivylaunch exits.

=back

=item B<host>

the host where the command will be executed.

=item B<name>

This indicative field is used to detect double agents. Insure that this name corresponds to the real ivy name detected on the bus. This field make sense only for ivy agent; usage is to set 'none' to other commands. 

=item B<command>

the command to execute and its options. 

=back

Before being analysed, the configuration file is parsed by the preprocessor B<cpp>, which provides some facilities like macro expansion. cpp options can be passed on command line, or be defined in configfile using the #cpp_options keyword. Options defined in configfile overload those that are passed on command line. See the example below.
        
=head1 EXAMPLE 

 % ivylaunch -b 127:3456 -D ACC=reims -D WP=WP0 configfile

where configfile contains :

    
 #ifndef ACC
 #define ACC paris
 #endif
 #ifndef WP
 #define WP WP1
 #endif
 #ifndef POSITION
 #define POSITION --acc ACC --wp WP
 #endif

#cpp_options: -traditional

 begin   lasra  none   xset fp+ tcp/10.192.36.68:7100

 global  anglo  Rejeu  rejeu -s 9:10 STR_ATH_01_06_26.rej

 global  tibot  ivycontrolpanel  ivycontrolpanel -nocursor  

 local   lasra  twinkle:ACC:WP:TC  twinkle POSITION --role TC 

 local   astik  IvyMon  ivymon

 end     lasra  none   xset fp- tcp/10.192.36.68:7100

=head1 SEE ALSO

ivycontrolpanel(1), ivybanner(1), cpp(1)

=head1 AUTHORS

Daniel Etienne <etienne@cena.fr>

Michelle Jacomi

Johnny Accot