summaryrefslogtreecommitdiff
path: root/FugueConfig.pm
blob: 9d74e5f123202c26bfc93aaf2f834be57f639371 (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
package FugueConfig;

use Sys::Hostname;
use IO::Handle;
use strict;

my $rsh = $ENV{"RSH"};
$rsh = "rsh" unless defined $rsh;

# apply cpp to a fugue config file and open resulting pipe
sub open {
    
   my $file = shift;
   my @cppopts = @_;
   if (system("which cpp > /dev/null")) {
      print "cpp command can't be located\n";
      return undef;
   } elsif (not -f $file) {
      print "No such file '$file'\n";
      return undef;
   } elsif (not -r $file) {
      print "'$file' is unreadable\n";
      return undef;
   }
   my $cpp_options = join(' ', @cppopts); 
   open (FD, "$file") or return undef;
   while(<FD>) {
      chomp;
      if (/^#cpp_options\s*:\s*(.*)/) {
         $cpp_options = $1;
         last;
      }
   }
   close(FD);
   print "in ivylaunch, cpp options are [$cpp_options]\n";
   my $cppcmd = "cpp $file $cpp_options";
   open (FD, "$cppcmd |") or return undef;
   return \*FD;
	
} # end open

# basic parsing of a cpped fugue config file; each element of the returned
# array describes a non-empty line, according to the following format :
#
#      [type, host, agent_name, command, [command_options]]
#
# where 'type' can take one of the following values 'comment', 'global',
# local', 'begin', and 'end'.
# a 'comment' type will be followed by an single field
#        
sub parse {
    
    my $fd = &FugueConfig::open(@_);
    return undef unless $fd;
    my @data;
    my %appname;
    my $line = 0;
    while(<$fd>) {
	$line++;
	# empty lines
	if (/^\s*$/) {
	    next;
	    
	# comments
	} elsif (/^\s*\#.*$/) {
	    push(@data, ['comment', $_]);
	    
	} else {
	    my ($type, $host, $name, $command, $param) =
		m/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)\#*.*$/ ;
	    #print "### t=$type h=$host n=$name c=$command p=$param\n";

	    if (not defined $type) {
		warn ("[FugueConfig][WARNING][line $line] syntax error : ".
		      "\n\t=> $_\n");
		next;
	    }
	    unless ($type eq 'global' or $type eq 'local' or
		    $type eq 'begin' or $type eq 'end') {
		warn ("[FugueConfig][WARNING][line $line] unknown command type ".
		      "'$type'\n");
		next;
	    }
	    if ($appname{$name}) {
		warn ("[FugueConfig][WARNING][line $line] at least 2 agents have ".
		      "the same name '$name'; skipping all but the first\n");
		next;
	    }
	    $appname{$name} = 1 if $type eq 'global' or $type eq 'local';
	    push(@data, [$type, $host, $name, $command, $param]);

	}
    }
    close(1, $fd);
    return @data;
    
} # end parse


# launch an named ivy agent on a given host and on a given ivy bus
# return its pid.
sub launchAgent {
    
    my ($appname, $host, $command, $options, $bus) = @_;
    $options .= " -b $bus" if $bus;
    my $command_opt = $command;
    $command_opt .= ' '.$options unless $options =~ /^\s*$/;
    &_launch($appname, $host, $command_opt, 'agent');

} # end launchAgent


sub execAgentBackground {
    
    my ($appname, $host, $command, $options, $bus) = @_;
    $options .= " -b $bus" if $bus;
    my $command_opt = $command;
    $command_opt .= ' '.$options unless $options =~ /^\s*$/;
    $command_opt = "$rsh -n $host '$command_opt'"
      unless $host eq 'localhost' or $host eq hostname();
   system("($command_opt 2>&1)&");

} # end execAgentBackground


# launch a given command (not an ivy agent) on a given host
sub launchCommand {
    
    my ($type, $host, $command, $options) = @_;
    my $command_opt = $command;
    $command_opt .= ' '.$options unless $options =~ /^\s*$/;
    &_launch($type, $host, $command_opt, 'command');
    
} # end launchCommand



sub _launch {

   my ($label, $host, $command_opt, $type) = @_;
   my $pid = fork;
   warn ("[FugueConfig][WARNING] Could not fork $type \'$command_opt\'!\n"), return
      unless defined $pid;
   
   # parent P1
   if ($pid) {
      print "[FugueConfig][INFO] launch on $host $label $type \'$command_opt\' ".
         "(pid=$pid)\n";
      return ($pid);
   
   # child P2
   } else {
   
      # overload parent trap
      $SIG{INT} = 'DEFAULT';
      
      # remote command
      $command_opt = "$rsh -n $host '$command_opt'"
         unless $host eq 'localhost' or $host eq hostname();
      
      pipe(READER, WRITER);
      WRITER->autoflush(1);
      my $pid2 = fork;
      warn ("[FugueConfig][WARNING] Could not fork $type \'$command_opt\'!\n"),
         return unless defined $pid2;
      # parent P2
      if ($pid2) {
         close READER;
         CORE::open(STDOUT, ">&=WRITER") or
         die "[FugueConfig][WARNING] Couldn't redirect STDOUT";
         exec "$command_opt 2>&1 " or
         die "[FugueConfig][WARNING] Couldn't launch $command_opt";
      # child P3
      } else {
         close WRITER;
         while(<READER>) {
            chomp();
            print "$label $_\n";
         }
         close READER;
         exit;
      }
   }  

} # end _launch


1;