summaryrefslogtreecommitdiff
path: root/ivylaunch
blob: 9b03434b9aeedfaa6433d106e06ff0607368ec7a (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
#!/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.

#
# All applications that should be launched should be specified in the <project file>.
# @Global is the list of applications that are independent of projet instances. If one
# of these applications is not present, it is launched. @Local are the applications
# needed for one instance of the project program. @Begin is the list of command that I
# should execute before launching the projet applications. @End is the list of command
# that I should execute when leaving.

# signal handlers


$SIG{INT} = $SIG{KILL} = 'Quit';

# where you may find the FugueConfig module
use lib "/usr/lib/ivylaunch";
use Ivy;
use strict;
use Sys::Hostname;
use File::Basename;
use Getopt::Long;
use POSIX 'sys_wait_h';
use FugueConfig;

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

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

my %opt;
my @apps;

&usage unless 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 = shift @ARGV;
&usage unless defined($file);

my (%child);

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

$| = 1;

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

my ($type, @param, %ready);

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

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


Ivy->init(-loopMode => 'LOCAL', 
	  -onDieFunc => [\&Terminate],
	  -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];
}
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.
print "$HEADER Wait 5 seconds before global apps checking...\n";
Ivy::after (5000, [\&Global]) if @{$agent{'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";
}



######################################################

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};
    }
}

######################################################

sub Terminate {
    print "$HEADER Die message received\n";
    foreach (values %child) {
	print "$HEADER sendDieTo $_ ...\n";
	$ivy->sendDieTo($_);
    }

    foreach my $app (@{$cmd{'end'}}) {
        FugueConfig::launchCommand('end',@$app);
    }
    print "$HEADER Terminated\n";
}

######################################################

sub Quit {
    print "$HEADER Quit\n";
    foreach (keys %child) {
  	print "$HEADER Stopping $child{$_} (pid=$_)...\n";
  	kill 15, $_;
    }
    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 "Usage: $appliname [-b bus] [-override] <config_file>\n";
    exit;
}

__END__
    
=head1 NAME

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

=head1 SYNOPSIS

ivylaunch [-help] [-b bus] [-override] fugueconfigfile

=head1 DESCRIPTION

ivylaunch forks ivy agents described in a Fugue configuration file. ivylaunch is also an ivy agent : it reports agents connection and disconnection, and can be killed by an Ivy die message which is transmitted to its forked agents. It can be also killed by sending an interrupt signal (SIGINT) or kill signal (SIGKILL); in this case, ivylaunch sends a termination signal to its forked children before exiting.
    
    
=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 FUGUE CONFIGURATION FILE FORMAT

Each line of a fugue configuration file should contain at least 4 mandatory fields separated by space or tabulation : the first three fields are B<type>, B<host> and B<name>. The next ones are dedicated to the B<command> and its options.
    
=over

=item B<type>

the type of command should take one of the following values : begin, end, global or local. B<begin> links up with commands which are executed before launching ivy agents, B<end> with commands which are executed after agents have been killed, just before exiting. B<global> and B<local> relate to ivy agents. The B<global> tag assures the unicity of an agent : before launching a global agent, ivylaunch checks for it on the bus; if this agent is 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. B<local> agents are launched without checking before global ones.


=item B<host>

the host where the command will be executed.

=item B<name>

the application 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.
        
=head1 EXAMPLE OF CONFIGURATION FILE

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

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 -norender 

local   astik  IvyMon  ivymon

=head1 SEE ALSO

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

=head1 AUTHORS

Daniel Etienne <etienne@cena.fr>

Michelle Jacomi

Johnny Accot