| 1 |
#!/usr/bin/perl
|
| 2 |
|
| 3 |
# $Id$
|
| 4 |
|
| 5 |
use strict;
|
| 6 |
use warnings;
|
| 7 |
|
| 8 |
use IPC::Open2;
|
| 9 |
use Getopt::Long;
|
| 10 |
use Data::Dumper;
|
| 11 |
use File::Path;
|
| 12 |
use Sys::Hostname;
|
| 13 |
|
| 14 |
my $SCIRE_CONFIG_FILE = '../etc/scire.conf'; #will be /etc/scire.conf when released.
|
| 15 |
my %conf;
|
| 16 |
my ($SERVER_STDOUT, $SERVER_STDIN);
|
| 17 |
my $connection_pid;
|
| 18 |
|
| 19 |
run_main();
|
| 20 |
|
| 21 |
sub run_main {
|
| 22 |
parse_command_line();
|
| 23 |
my $conf_file = (defined($conf{config})) ? $conf{config} : $SCIRE_CONFIG_FILE;
|
| 24 |
read_config_file($conf_file);
|
| 25 |
|
| 26 |
check_job_dir();
|
| 27 |
|
| 28 |
my $connection_command = build_connection_command();
|
| 29 |
|
| 30 |
#ok folks so here's how this thang goes down.
|
| 31 |
#1. Connect.
|
| 32 |
create_connection($connection_command);
|
| 33 |
|
| 34 |
#2. Register with the DB. (only it knows if you're allowed to be active)
|
| 35 |
# If we do not have a defined key file, we assume this is the first run of this client
|
| 36 |
# so we register them instead of trying to identify.
|
| 37 |
if(defined($conf{key_file}) and (-f $conf{key_file})) {
|
| 38 |
if(!identify_client()) {
|
| 39 |
exit(1);
|
| 40 |
}
|
| 41 |
} else {
|
| 42 |
register_client();
|
| 43 |
exit(0);
|
| 44 |
}
|
| 45 |
|
| 46 |
#3. Scan the jobs directory. If there are done/failed jobs, report them. Note jobs in running or queue.
|
| 47 |
my @existing_jobs = scan_jobs_dir();
|
| 48 |
#4. Fetch the jobs list
|
| 49 |
get_jobs();
|
| 50 |
#5. ???
|
| 51 |
#6. Profit!
|
| 52 |
}
|
| 53 |
|
| 54 |
sub parse_command_line {
|
| 55 |
GetOptions(
|
| 56 |
'debug|d' => \$conf{debug},
|
| 57 |
'dry-run' => \$conf{dry_run},
|
| 58 |
'help|h' => \$conf{help},
|
| 59 |
'config|c=s' => \$conf{config},
|
| 60 |
'threads|t=i' => \$conf{max_threads},
|
| 61 |
|
| 62 |
#config overrides.
|
| 63 |
'host=s' => \$conf{host},
|
| 64 |
'port=i' => \$conf{port},
|
| 65 |
'user|u=s' => \$conf{user},
|
| 66 |
'server_script=s' => \$conf{server_script},
|
| 67 |
'job_dir' => \$conf{job_dir},
|
| 68 |
);
|
| 69 |
if ($conf{help}) {
|
| 70 |
print "\nusage: scireclient.pl [--debug or -d]\n\t [--dry-run]"
|
| 71 |
."\t [--config=CONF or -c] \n\t [--threads=# or -t] \t [--help or -h] \n"
|
| 72 |
."\t [[--host=HOST] \t [--port=PORT] \t [--user=USER or -u] \n\t"
|
| 73 |
." [--server_script=foo.pl] \t [--job_dir=/tmp/jobs] \n";
|
| 74 |
exit 0;
|
| 75 |
}
|
| 76 |
|
| 77 |
}
|
| 78 |
|
| 79 |
sub send_command {
|
| 80 |
my $cmd = shift;
|
| 81 |
my @args = @_;
|
| 82 |
my $tosend = "${cmd}";
|
| 83 |
|
| 84 |
for my $arg (@args) {
|
| 85 |
if($arg =~ /^[0-9]+$/) {
|
| 86 |
$tosend .= " ${arg}";
|
| 87 |
} else {
|
| 88 |
$arg =~ s/"/\\"/g;
|
| 89 |
$tosend .= " \"${arg}\"";
|
| 90 |
}
|
| 91 |
}
|
| 92 |
debug("Sending: ${tosend}");
|
| 93 |
print SERVER_STDIN "${tosend}\n";
|
| 94 |
#FIXME WE NEED A TIMEOUT HERE OF SOME SORT!!
|
| 95 |
#if the server doesn't give you a newline this just hangs!
|
| 96 |
my $response = <SERVER_STDOUT>;
|
| 97 |
return $response;
|
| 98 |
}
|
| 99 |
|
| 100 |
sub parse_response {
|
| 101 |
my $response = shift;
|
| 102 |
$response =~ /^(OK|ERROR)(?: (.+))?$/;
|
| 103 |
my ($status, $message) = ($1, $2);
|
| 104 |
return ($status, $message);
|
| 105 |
}
|
| 106 |
|
| 107 |
sub create_connection {
|
| 108 |
# XXX: How do we capture this error? $pid has a valid value even if the
|
| 109 |
# process fails to run, since it just returns the PID of the forked perl
|
| 110 |
# process. I tried adding 'or die' after it, but it didn't help since it
|
| 111 |
# doesn't fail in the main process. When it fails, it outputs an error
|
| 112 |
# to STDERR:
|
| 113 |
# open2: exec of ../server/scireserver.pl failed at ./scireclient.pl line 116
|
| 114 |
my $connection_command = shift;
|
| 115 |
$connection_pid = open2(*SERVER_STDOUT, *SERVER_STDIN, $connection_command);
|
| 116 |
}
|
| 117 |
|
| 118 |
sub build_connection_command {
|
| 119 |
# This will eventually be something like "ssh scire@${scireserver} /usr/bin/scireserver.pl"
|
| 120 |
my $connection_command = "ssh ";
|
| 121 |
$connection_command .= "-o BatchMode yes ";
|
| 122 |
$connection_command .= "-o SendEnv 'SCIRE_*' ";
|
| 123 |
$connection_command .= "-o ServerAliveInterval 15 -o ServerAliveCountMax 4 ";
|
| 124 |
if(defined($conf{port})) {
|
| 125 |
$connection_command .= "-o Port=$conf{port} ";
|
| 126 |
}
|
| 127 |
$connection_command .= "$conf{user}\@$conf{host} $conf{server_script}";
|
| 128 |
|
| 129 |
if (-d ".svn") {
|
| 130 |
# Overwrite $connection_command in the case of a dev environment for now
|
| 131 |
$connection_command = "../server/scireserver.pl";
|
| 132 |
}
|
| 133 |
|
| 134 |
return $connection_command;
|
| 135 |
}
|
| 136 |
|
| 137 |
sub check_job_dir {
|
| 138 |
if (! -d $conf{job_dir}) {
|
| 139 |
print "WARNING! $conf{job_dir} does not exist...creating\n";
|
| 140 |
mkpath( $conf{job_dir}, {verbose => 1, mode => 0660})
|
| 141 |
or die("Couldn't make $conf{job_dir} w/ perms 0660: $!");
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
sub read_config_file {
|
| 146 |
my $conf_file = shift;
|
| 147 |
open(FH, "< ${conf_file}") or die("Couldn't open the config file ${conf_file}: $!");
|
| 148 |
while (<FH>) {
|
| 149 |
chomp;
|
| 150 |
next if /^\s*(?:#|$)/;
|
| 151 |
if(/^\s*(.+?)\s*=\s*(.+?)\s*(?:#.*)?$/) {
|
| 152 |
unless(defined($conf{lc($1)})) { #Don't overwrite anything specified in cmdline
|
| 153 |
$conf{lc($1)} = $2;
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
close(FH) or die("Couldn't close the config file ${conf_file}: $!");
|
| 158 |
}
|
| 159 |
|
| 160 |
sub register_client {
|
| 161 |
# my $mac = "00:11:22:33:44:55";
|
| 162 |
# my $ip = "192.168.2.3";
|
| 163 |
my ($mac, $ip) = get_interface_info(defined $conf{interface} && $conf{interface} ? $conf{interface} : "eth0");
|
| 164 |
my $hostname = hostname();
|
| 165 |
my ($status, $message) = parse_response(send_command("REGISTER", $mac, $ip, $hostname));
|
| 166 |
die "Could not register client $mac w/ ip $ip and hostname $hostname. Got: $message" if (! defined $status or $status ne "OK");
|
| 167 |
debug("Client registered. Status is pending. digest is $message");
|
| 168 |
open(FILE, ">$conf{key_file}") or die("Couldn't open key file $conf{key_file} for writing: $!");
|
| 169 |
print FILE "$message\n";
|
| 170 |
close(FILE);
|
| 171 |
}
|
| 172 |
|
| 173 |
sub identify_client {
|
| 174 |
open(FILE, $conf{key_file}) or die("Couldn't open client_key $conf{key_file}: $!");
|
| 175 |
my $digest = join("", <FILE>);
|
| 176 |
close(FILE);
|
| 177 |
|
| 178 |
#my $digest = "124567890";
|
| 179 |
my ($status, $message) = parse_response(send_command("IDENTIFY", $digest));
|
| 180 |
unless (defined $status && $status eq "OK") {
|
| 181 |
print "Could not identify to server: $message\n";
|
| 182 |
return 0;
|
| 183 |
}
|
| 184 |
debug("Client identified");
|
| 185 |
return 1;
|
| 186 |
}
|
| 187 |
|
| 188 |
sub get_jobs {
|
| 189 |
my ($status, $jobs) = parse_response(send_command("GET_JOBS"));
|
| 190 |
unless (defined $status && $status eq "OK") {
|
| 191 |
print "Could not get jobs list from server: $jobs\n";
|
| 192 |
return 0;
|
| 193 |
}
|
| 194 |
$jobs =~ s/\s//g; #Remove all whitespace
|
| 195 |
my @jobs_list = split(/,/, $jobs);
|
| 196 |
foreach my $job (@jobs_list) {
|
| 197 |
my ($status, $message) = parse_response(send_command("GET_JOB", $job));
|
| 198 |
open(JOBFILE, ">$conf{job_dir}/queue/${job}.job") or do {
|
| 199 |
print "Could not open $conf{job_dir}/queue/${job}.job for writing: $!";
|
| 200 |
next;
|
| 201 |
};
|
| 202 |
# XXX: Modify this to fetch a file instead
|
| 203 |
# print JOBFILE parse_response($resp);
|
| 204 |
close(JOBFILE);
|
| 205 |
debug("Fetched job $job ");
|
| 206 |
}
|
| 207 |
#This function doesn't actually need to do anything with the list of jobs, the executor handles that part.
|
| 208 |
}
|
| 209 |
|
| 210 |
sub scan_jobs_dir {
|
| 211 |
#Scan the dirs for job files.
|
| 212 |
my @existing_jobs = glob("$conf{job_dir}/queue/*");
|
| 213 |
my @failed_jobs = glob("$conf{job_dir}/failed/*");
|
| 214 |
my @done_jobs = glob("$conf{job_dir}/done/*");
|
| 215 |
|
| 216 |
#Report on those jobs needing reporting.
|
| 217 |
foreach my $job_file (@failed_jobs) {
|
| 218 |
$job_file =~ /(\d+)\.job/;
|
| 219 |
my $jobid = $1;
|
| 220 |
my ($status, $message) = parse_response(send_command("SET_JOB_STATUS $jobid 'Failed'"));
|
| 221 |
open(FILE, $job_file) or die "Couldn't open job file $job_file: $!";
|
| 222 |
my $job_data = join("", <FILE>);
|
| 223 |
close(FILE);
|
| 224 |
|
| 225 |
}
|
| 226 |
#may be able to use same code as above.
|
| 227 |
foreach my $job_file (@done_jobs) {
|
| 228 |
$job_file =~ /(\d+)\.job/;
|
| 229 |
my $jobid = $1;
|
| 230 |
my ($status, $message) = parse_response(send_command("SET_JOB_STATUS $jobid 'Done'"));
|
| 231 |
}
|
| 232 |
|
| 233 |
return @existing_jobs;
|
| 234 |
}
|
| 235 |
|
| 236 |
sub debug {
|
| 237 |
my $msg = shift;
|
| 238 |
if($conf{debug}) {
|
| 239 |
print STDERR $msg."\n";
|
| 240 |
}
|
| 241 |
}
|
| 242 |
|
| 243 |
sub get_interface_info {
|
| 244 |
my $interface = shift;
|
| 245 |
|
| 246 |
my $info = `/sbin/ifconfig ${interface}`;
|
| 247 |
$info =~ /^.+HWaddr ([a-zA-Z0-9:]+).+inet addr:([0-9.]+).+$/s;
|
| 248 |
my ($mac, $ip) = ($1, $2);
|
| 249 |
return ($mac, $ip);
|
| 250 |
}
|