summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xclient/scireclient.pl103
1 files changed, 88 insertions, 15 deletions
diff --git a/client/scireclient.pl b/client/scireclient.pl
index 4cd8f0e..2b961e2 100755
--- a/client/scireclient.pl
+++ b/client/scireclient.pl
@@ -9,6 +9,8 @@ use IPC::Open2;
use Getopt::Long;
use Data::Dumper;
use File::Path;
+#use Net::SSH::Perl::Key;
+
my $SCIRE_CONFIG_FILE = '../etc/scire.conf'; #will be /etc/scire.conf when released.
my %conf;
@@ -30,24 +32,29 @@ sub run_main {
create_connection($connection_command);
#2. Register with the DB. (only it knows if you're allowed to be active)
-# if(-f "../etc/client_key") {
+ # If we do not have a defined key file, we assume this is the first run of this client
+ # so we register them instead of trying to identify.
+ if(defined($conf{key_file}) and (-f $conf{key_file})) {
if(!identify_client()) {
exit(1);
}
-# } else {
-# register_client();
-# }
+ } else {
+ register_client();
+ exit(0);
+ }
#3. Scan the jobs directory. If there are done/failed jobs, report them. Note jobs in running or queue.
+ my @existing_jobs;
+ @existing_jobs = scan_jobs_dir();
#4. Fetch the jobs list
+ get_jobs(@existing_jobs);
#5. ?
- run_test();
+ #run_test();
}
sub run_test {
for(('PING', 'FOO', 'QUIT')) {
- send_command($_);
- my $response = get_response();
+ my $response = send_command($_);
}
}
@@ -91,6 +98,10 @@ sub send_command {
}
print "Sending: ${tosend}\n" if $conf{debug};
print SERVER_STDIN "${tosend}\n";
+ #FIXME WE NEED A TIMEOUT HERE OF SOME SORT!!
+ #if the server doesn't give you a newline this just hangs!
+ my $response = get_response();
+ return $response;
}
sub get_response {
@@ -100,6 +111,11 @@ sub get_response {
return $response;
}
+sub parse_response {
+ my $resp = shift;
+ return "Not sure how this is gonna work yet";
+}
+
sub create_connection {
# XXX: How do we capture this error? $pid has a valid value even if the
# process fails to run, since it just returns the PID of the forked perl
@@ -151,21 +167,78 @@ sub read_config_file {
}
sub register_client {
- send_command("REGISTER 00:11:22:33:44:55 192.168.2.3 myhostname");
+ my $mac = "00:11:22:33:44:55";
+ my $ip = "192.168.2.3";
+ my $response = send_command("REGISTER",$mac,$ip);
+ die "Could not register client $mac w/ ip $ip. got $response" unless ($response =~ /OK/);
+ print "Client registered. Status is pending.\n";
}
sub identify_client {
-# open FILE, "< ../etc/client_key" or die "Couldn't open client_key: $!";
-# my $client_key = join("", <FILE>);
-# close FILE;
- my $client_key = "124567890";
- send_command("IDENTIFY", $client_key);
- my $response = get_response();
+ open(FILE, $conf{key_file}) or die "Couldn't open client_key $conf{key_file}: $!";
+ my $client_key = join("", <FILE>);
+ close(FILE);
+ $conf{key_type} ||= "DSA";
+# my $key_obj = Net::SSH::Perl::Key->new($conf{key_type}, $client_key);
+# my $fingerprint = $key_obj->fingerprint();
+
+ my $fingerprint = "124567890";
+ my $response = send_command("IDENTIFY", $fingerprint);
$response =~ /^(OK|ERROR)(?: (.+))?$/;
unless ($1 and ($1 eq "OK")) {
print "Could not identify to server: $response\n";
return 0;
}
-# print "Registered client $conf{client_id}\n" if $conf{debug};
+ print "Client identified\n" if $conf{debug};
return 1;
}
+
+sub get_jobs {
+ my @existing_jobs = @_;
+ my $response = send_command("GET_JOBS", @existing_jobs);
+ $response =~ /^(OK|ERROR)(?: (.+))?$/;
+ unless ($1 and ($1 eq "OK")) {
+ print "Could not get jobs list from server: $response\n";
+ return 0;
+ }
+ my $jobs = $2;
+ $jobs =~ s/\s//g; #Remove all whitespace
+ my @jobs_list = split(/,/, $jobs);
+ foreach my $job (@jobs_list) {
+ my $resp = send_command("GET_JOB",$job);
+ open(JOBFILE, ">$conf{job_dir}/queue/${job}.job") or do {
+ print "Could not open $conf{job_dir}/queue/${job}.job for writing: $!";
+ next;
+ };
+ print JOBFILE parse_response($resp);
+ close(JOBFILE);
+ print "Fetched job $job \n" if $conf{debug};
+ }
+ #This function doesn't actually need to do anything with the list of jobs, the executor handles that part.
+}
+
+sub scan_jobs_dir {
+ #Scan the dirs for job files.
+ my @existing_jobs = glob("$conf{job_dir}/queue/*");
+ my @failed_jobs = glob("$conf{job_dir}/failed/*");
+ my @done_jobs = glob("$conf{job_dir}/done/*");
+
+ #Report on those jobs needing reporting.
+ foreach my $job_file (@failed_jobs) {
+ $job_file =~ /(\d+)\.job/;
+ my $jobid = $1;
+ my $response = send_command("SET_JOB_STATUS $jobid 'Failed'");
+ open(FILE, $job_file) or die "Couldn't open job file $job_file: $!";
+ my $job_data = join("", <FILE>);
+ close(FILE);
+
+ }
+ #may be able to use same code as above.
+ foreach my $job_file (@done_jobs) {
+ $job_file =~ /(\d+)\.job/;
+ my $jobid = $1;
+ my $response = send_command("SET_JOB_STATUS $jobid 'Done'");
+ }
+
+ return @existing_jobs;
+}