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

use Sys::Hostname;
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;
    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;
    }
    open (FD, "cpp -traditional $file |") 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 $file = shift;
    my $fd = &FugueConfig::open($file);
    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=".join(' ', @param)."\n";
	    if (not defined $type) {
		warn ("*** FugueConfig::parse WARNING : ".
		      "syntax error at line $line : \n\t=> $_\n");
		next;
	    }
	    unless ($type eq 'global' or $type eq 'local' or
		    $type eq 'begin' or $type eq 'end') {
		warn ("*** FugueConfig::parse WARNING : ".
		      "unknown command type '$type' at line $line. Ignored.\n");
		next;
	    }
	    if ($appname{$name}) {
		warn ("*** FugueConfig::parse WARNING : ".
		      "at 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) = @_;
    my @options = @$options if ref($options) eq 'ARRAY';
    push(@options, -b => $bus) if $bus;
    my $command_opt = $command.' '.join(' ', @options);
    my $pid = fork;
    warn ("*** FugueConfig::launchAgent WARNING: Could not fork!\n"), return
	unless defined $pid;
    
    if ($pid) {
	# parent
	print "*** FugueConfig::launchAgent INFO: $host: launching $appname agent ".
	       "with \'$command_opt\' (pid=$pid)\n";
	return ($pid);
    } else {
	# child
	my $awk_command = "awk -W interactive '{print \"$appname  \", \$0;}'";
	if ($host eq 'localhost' or $host eq hostname() ) {
	    exec "$command_opt 2>&1 | $awk_command" or
		warn "*** FugueConfig::launchAgent WARNING: Error executing $command\n";
	} else {
	    exec "$rsh -n $host '$command_opt' 2>&1 | $awk_command "
		or warn "*** FugueConfig::launchAgent WARNING: Error executing ".
		    "$command on $host\n";
	}
    }
    
} # end launchAgent


# launch a given command (not an ivy agent) on a given host
sub launchCommand {
    my ($type, $host, $command, $options) = @_;
    my @options = @$options if ref($options) eq 'ARRAY';
    my $command_opt = $command.' '.join(' ', @options);
    my $pid = fork;
    if ($pid) {
	# parent
	print "*** FugueConfig::launchCommand INFO: $host: launching command ".
	       "\'$command_opt\' (pid=$pid)\n";
	return ($pid);
    } else {
	# child
	my $awk_command = "awk -W interactive '{print \"$type  \", \$0;}'";
	if ($host eq 'localhost' or $host eq hostname() ) {
	    exec "$command_opt 2>&1 | $awk_command" or
		warn "*** FugueConfig::launchCommand WARNING: Error executing ".
		    "$command\n";
	} else {
	    exec "$rsh -n $host '$command_opt' 2>&1 | $awk_command "
		or warn "*** FugueConfig::launchCommand WARNING: Error executing ".
		    "$command on $host\n";
	}
    }
    
    
} # end launchCommand

1