fixes and docs for testing crunch jobs locally
[arvados.git] / services / crunch / crunch-job
index 76ce4e4da0e8becc05387fbfc99b070463b2db98..710fc0b0c8caa404eaa19dc865270f6ef6bd3e74 100755 (executable)
@@ -97,6 +97,7 @@ if (defined $job_api_token) {
 
 my $have_slurm = exists $ENV{SLURM_JOBID} && exists $ENV{SLURM_NODELIST};
 my $job_has_uuid = $jobspec =~ /^[-a-z\d]+$/;
+my $local_job = !$job_has_uuid;
 
 
 $SIG{'HUP'} = sub
@@ -115,11 +116,12 @@ $SIG{'USR2'} = sub
 
 
 my $arv = Arvados->new;
-my $metastream = Warehouse::Stream->new;
+my $metastream = Warehouse::Stream->new(whc => new Warehouse);
 $metastream->clear;
 $metastream->write_start('log.txt');
 
-my $User = {};
+my $User = $arv->{'users'}->{'current'}->execute;
+
 my $Job = {};
 my $job_id;
 my $dbh;
@@ -127,7 +129,6 @@ my $sth;
 if ($job_has_uuid)
 {
   $Job = $arv->{'jobs'}->{'get'}->execute('uuid' => $jobspec);
-  $User = $arv->{'users'}->{'current'}->execute;
   if (!$force_unlock) {
     if ($Job->{'is_locked_by'}) {
       croak("Job is locked: " . $Job->{'is_locked_by'});
@@ -153,9 +154,12 @@ else
     qw(script script_version script_parameters);
   }
 
-  if (!defined $Job->{'uuid'}) {
-    chomp ($Job->{'uuid'} = sprintf ("%s-t%d-p%d", `hostname -s`, time, $$));
-  }
+  $Job->{'is_locked_by'} = $User->{'uuid'};
+  $Job->{'started_at'} = gmtime;
+
+  $Job = $arv->{'jobs'}->{'create'}->execute('job' => $Job);
+
+  $job_has_uuid = 1;
 }
 $job_id = $Job->{'uuid'};
 
@@ -255,8 +259,10 @@ if ($job_has_uuid)
                               'todo' => 1,
                               'running' => 0,
                               'done' => 0 };
-  unless ($Job->save() && $Job->{'is_locked_by'} == $User->{'uuid'}) {
-    croak("Error while updating / locking job");
+  if ($job_has_uuid) {
+    unless ($Job->save() && $Job->{'is_locked_by'} == $User->{'uuid'}) {
+      croak("Error while updating / locking job");
+    }
   }
 }
 
@@ -310,22 +316,22 @@ else
 
 
 my $build_script;
-do {
-  local $/ = undef;
-  $build_script = <DATA>;
-};
 
 
-$ENV{"CRUNCH_SRC_COMMIT"} = $Job->{revision};
+$ENV{"CRUNCH_SRC_COMMIT"} = $Job->{script_version};
 
-my $skip_install = (!$have_slurm && $Job->{revision} =~ m{^/});
+my $skip_install = ($local_job && $Job->{script_version} =~ m{^/});
 if ($skip_install)
 {
-  $ENV{"CRUNCH_SRC"} = $Job->{revision};
+  $ENV{"CRUNCH_SRC"} = $Job->{script_version};
 }
 else
 {
-  Log (undef, "Install revision ".$Job->{revision});
+  do {
+    local $/ = undef;
+    $build_script = <DATA>;
+  };
+  Log (undef, "Install revision ".$Job->{script_version});
   my $nodelist = join(",", @node);
 
   # Clean out crunch_tmp/work and crunch_tmp/opt
@@ -357,6 +363,7 @@ else
   $ENV{"CRUNCH_INSTALL"} = "$ENV{CRUNCH_TMP}/opt";
 
   my $commit;
+  my $git_archive;
   my $treeish = $Job->{'script_version'};
   my $repo = $git_dir || $ENV{'CRUNCH_DEFAULT_GIT_DIR'};
   # Todo: let script_version specify repository instead of expecting
@@ -379,7 +386,7 @@ else
     chomp $gitlog;
     if ($gitlog =~ /^[a-f0-9]{40}$/) {
       $commit = $gitlog;
-      Log (undef, "Using commit $commit for revision $treeish");
+      Log (undef, "Using commit $commit for script_version $treeish");
     }
   }
 
@@ -407,7 +414,7 @@ else
        Log (undef, "Using commit $commit for tree-ish $treeish");
         if ($commit ne $treeish) {
           $Job->{'script_version'} = $commit;
-          $Job->save() or croak("Error while updating job");
+          !$job_has_uuid or $Job->save() or croak("Error while updating job");
         }
       }
     }
@@ -417,6 +424,7 @@ else
     $ENV{"CRUNCH_SRC_COMMIT"} = $commit;
     @execargs = ("sh", "-c",
                 "mkdir -p $ENV{CRUNCH_INSTALL} && cd $ENV{CRUNCH_TMP} && perl -");
+    $git_archive = `cd $ENV{CRUNCH_SRC} && git archive $commit`;
   }
   else {
     croak ("could not figure out commit id for $treeish");
@@ -425,7 +433,7 @@ else
   my $installpid = fork();
   if ($installpid == 0)
   {
-    srun (\@srunargs, \@execargs, {}, $build_script);
+    srun (\@srunargs, \@execargs, {}, $build_script . $git_archive);
     exit (1);
   }
   while (1)
@@ -550,7 +558,7 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++)
     my @execargs = qw(sh);
     my $build_script_to_send = "";
     my $command =
-       "mkdir -p $ENV{CRUNCH_TMP}/revision "
+       "mkdir -p $ENV{CRUNCH_WORK} $ENV{CRUNCH_TMP} "
        ."&& cd $ENV{CRUNCH_TMP} ";
     if ($build_script)
     {
@@ -558,18 +566,6 @@ for (my $todo_ptr = 0; $todo_ptr <= $#jobstep_todo; $todo_ptr ++)
       $command .=
          "&& perl -";
     }
-    elsif (!$skip_install)
-    {
-      $command .=
-         "&& "
-         ."( "
-         ."  [ -e '$ENV{CRUNCH_INSTALL}/.tested' ] "
-         ."|| "
-         ."  ( svn export --quiet '$ENV{INSTALL_REPOS}/installrevision' "
-         ."    && ./installrevision "
-         ."  ) "
-         .") ";
-    }
     $ENV{"PYTHONPATH"} = "$ENV{CRUNCH_SRC}/sdk/python"; # xxx hack
     $command .=
         "&& exec $ENV{CRUNCH_SRC}/crunch_scripts/" . $Job->{"script"};
@@ -712,7 +708,7 @@ release_allocation();
 freeze();
 $Job->{'output'} = &collate_output();
 $Job->{'success'} = $Job->{'output'} && $success;
-$Job->save;
+$Job->save if $job_has_uuid;
 
 if ($Job->{'output'})
 {
@@ -746,7 +742,7 @@ sub update_progress_stats
   $Job->{'tasks_summary'}->{'todo'} = $todo;
   $Job->{'tasks_summary'}->{'done'} = $done;
   $Job->{'tasks_summary'}->{'running'} = $running;
-  $Job->save;
+  $Job->save if $job_has_uuid;
   Log (undef, "status: $done done, $running running, $todo todo");
   $progress_is_dirty = 0;
 }
@@ -1020,7 +1016,7 @@ sub collate_output
   {
     Log (undef, "output $joboutput");
     $Job->{'output'} = $joboutput;
-    $Job->save;
+    $Job->save if $job_has_uuid;
   }
   else
   {
@@ -1112,11 +1108,11 @@ sub croak
 sub cleanup
 {
   return if !$job_has_uuid;
-  $Job->reload;
+  $Job->reload if $job_has_uuid;
   $Job->{'running'} = 0;
   $Job->{'success'} = 0;
   $Job->{'finished_at'} = gmtime;
-  $Job->save;
+  $Job->save if $job_has_uuid;
 }
 
 
@@ -1130,7 +1126,7 @@ sub save_meta
   undef $metastream if !$justcheckpoint; # otherwise Log() will try to use it
   Log (undef, "meta key is $loglocator");
   $Job->{'log'} = $loglocator;
-  $Job->save;
+  $Job->save if $job_has_uuid;
 }
 
 
@@ -1231,7 +1227,7 @@ sub thaw
   {
     $Job->{$_} = $frozenjob->{$_};
   }
-  $Job->save;
+  $Job->save if $job_has_uuid;
 }
 
 
@@ -1308,32 +1304,15 @@ if (readlink ("$destdir.commit") eq $commit) {
     exit 0;
 }
 
+unlink "$destdir.commit";
 open STDOUT, ">", "$destdir.log";
 open STDERR, ">&STDOUT";
 
-if (-d "$destdir/.git") {
-    chdir $destdir or die "chdir $destdir: $!";
-    if (0 != system (qw(git remote set-url origin), $repo)) {
-       # awful... for old versions of git that don't know "remote set-url"
-       shell_or_die (q(perl -pi~ -e '$_="\turl = ).$repo.q(\n" if /url = /' .git/config));
-    }
-}
-elsif ($repo && $commit)
-{
-    shell_or_die('git', 'clone', $repo, $destdir);
-    chdir $destdir or die "chdir $destdir: $!";
-    shell_or_die(qw(git config clean.requireForce false));
-}
-else {
-    die "$destdir does not exist, and no repo/commit specified -- giving up";
-}
-
-if ($commit) {
-    unlink "$destdir.commit";
-    shell_or_die (qw(git stash));
-    shell_or_die (qw(git clean -d -x));
-    shell_or_die (qw(git fetch origin));
-    shell_or_die (qw(git checkout), $commit);
+mkdir $destdir;
+open TARX, "|-", "tar", "-C", $destdir, "-xf", "-";
+print TARX <DATA>;
+if(!close(TARX)) {
+  die "'tar -C $destdir -xf -' exited $?: $!";
 }
 
 my $pwd;
@@ -1365,3 +1344,5 @@ sub shell_or_die
   system (@_) == 0
       or die "@_ failed: $! exit 0x".sprintf("%x",$?);
 }
+
+__DATA__