Changeset - cb26ccfc122d
[Not reviewed]
default
0 3 0
Ethan Zonca (ethanzonca) - 15 years ago 2010-06-26 02:35:21
e@ethanzonca.com
Made conf replacing a bit cleaner
3 files changed with 24 insertions and 11 deletions:
0 comments (0 inline, 0 general)
etc/distrenslave.conf.in
Show inline comments
 
slave
 
{
 
  username = "!username"
 
  password = "!password"
 
  datadir = "@LOCALSTATEDIR@/@PACKAGE@"
 
  server = "protofusion"
 
  hostname = "protofusion.org"
 
}
 

	
 
include("distrencommon.conf")
src/server/simpleslave.c
Show inline comments
 
/*
 
  Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 

	
 
*/
 

	
 
#include "slavefuncs.h"
 

	
 
#include "common/asprintf.h"
 
#include "common/options.h"
 

	
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <string.h>
 
#include <sys/stat.h>
 
#include <unistd.h>
 

	
 
#define DEBUG 1
 

	
 

	
 
int main(int argc, char *argv[])
 
{
 
  char *datadir;
 
  char *server;
 
  char *username;
 
  char *password;
 
  char *hostname;
 

	
 
  cfg_opt_t myopts[] = {
 
    CFG_SIMPLE_STR("username", &username),
 
    CFG_SIMPLE_STR("password", &password),
 
    CFG_SIMPLE_STR("datadir", &datadir),
 
    CFG_SIMPLE_STR("server", &server),
 
    CFG_SIMPLE_STR("hostname", &hostname),
 
    CFG_END()
 
  };
 
  cfg_t * my_cfg;
 

	
 
  struct options_common *commonopts;
 

	
 
  /**
 
     initializations
 
  */
 
  datadir = NULL;
 
  server = NULL;
 
  username = NULL;
 
  password = NULL;
 
  hostname = NULL;
 

	
 
  char curopt;
 
  int runBenchmark = 0;
 
  int updateConf = 0;
 

	
 
  while(((char)-1) != (curopt = getopt(argc, argv, "u:rh")))
 
  while(((char)-1) != (curopt = getopt(argc, argv, "u:p:rh")))
 
     {
 
       if(curopt == ':')
 
         {
 
           fprintf(stderr, "-%c: is missing an argument\n", optopt);
 
           return 1;
 
         }
 
       else if(curopt == '?')
 
         {
 
           fprintf(stderr, "-%c: invalid option specified\n", optopt);
 
           return 1;
 
         }
 
       else if(curopt == 'h')
 
         {
 
           fprintf(stderr, "Usage: distrensimpleslave [option] \nStarts a distren slave\n\t-u\tset username (run after fresh install)\n\t-r\tRecalculate render power (benchmark)\n\t-h\tshow this help\n");
 
           fprintf(stderr, "Usage: distrensimpleslave [option] \nStarts a distren slave\n\t-u\tset Slave ID/Username (run after fresh install)\n\t-p\tset Slave Passphrase (run after fresh install)\n\t-r\tRecalculate render power (benchmark)\n\t-h\tshow this help\n");
 
           return 2;
 
         }
 
       else if(curopt == 'r')
 
         {
 
           runBenchmark = 1;
 
           break;
 
         }
 
       else if(curopt == 'u')
 
       else if(curopt == 'u'){
 
         username = strdup(optarg);
 
         updateConf = 1;
 
       }
 
       else if(curopt == 'p'){
 
         password = strdup(optarg);
 
         updateConf = 1;
 
       }
 
     }
 

	
 
if(updateConf){
 
         if(DEBUG)
 
           fprintf(stderr, "Putting username \"%s\" in distrenslave.conf\n", username);
 
    fprintf(stderr, "Putting Slave ID \"%s\" and Slave Passphrase \"%s\" in distrenslave.conf\n", username, password);
 
  if(username != NULL)
 
    conf_replace("distrenslave.conf", "!username", username);
 
  if(password != NULL)
 
    conf_replace("distrenslave.conf", "!password", password);
 

	
 
         conf_replace("distrenslave.conf", "!username", username);
 
         fprintf(stderr, "Please invoke distrensimpleslave with no arguments to run with the username you just set\n");
 
  fprintf(stderr, "Please invoke distrensimpleslave with no arguments to run with the slave ID and/or passphrase you just set\n");
 
         return 0;
 
     }
 

	
 
  /* Get conf data */
 
  options_init(argc, argv, &my_cfg, myopts, "slave", &commonopts);
 

	
 
  if(!datadir)
 
    {
 
      fprintf(stderr, "datadir not set\n");
 
      return 1;
 
    }
 
  if(!server)
 
    {
 
      fprintf(stderr, "server not set\n");
 
      return 1;
 
    }
 
  if(!username)
 
    {
 
      fprintf(stderr, "username not set\n");
 
      fprintf(stderr, "username not set, try the -u flag \n");
 
      return 1;
 
    }
 
  if(!password)
 
    {
 
      fprintf(stderr, "password not set\n");
 
      fprintf(stderr, "password not set, try the -p flag\n");
 
      return 1;
 
    }
 
  if(!hostname)
 
    {
 
      fprintf(stderr, "hostname not set\n");
 
      return 1;
 
    }
 

	
 
  
 
  /* Notifies the user if there no username in .conf */
 
  if(checkUsername(username))
 
    return 1;
 
  if(!strncmp(password, "!password",10))
 
    {
 
      fprintf(stderr, "You haven't specified a password. Please edit distrenslave.conf!\n");
 
      fprintf(stderr, "You haven't specified a password. Please edit distrenslave.conf or try the -p flag!\n");
 
      return 1;
 
    }
 

	
 
  // Variables needed for main loop
 
  int jobnum = 0;
 
  int framenum = 0;
 
  int slavekey = atoi(username); // @TODO: Make this more friendly
 

	
 
  char *urltoTar;      /* Full URL to the server-side location of job#.tgz */
 
  char *pathtoTar;     /* Full path to the location of the job#.tgz */
 
  char *pathtoTardir;
 

	
 
  // Temporary for uncompressed testing
 
  // char *urltoJobfile;
 

	
 
  char *urltoOutput;   /* Full URL where output is posted */
 
  char *pathtoOutput;  /* Full path to the output (rendered) file */
 
  char *pathtoOutdir;  /* Full path to output directory */
 
  char *pathtoRenderOutput; /* Contains blender framenum placeholder */
 

	
 
  char *urltoJobfile; /* Used if we aren't using compression */
 
  char *pathtoJob; /* Path to job data folder */
 
  char *pathtoJobfile; /* Full path to the job's main file */
 
  char *outputExt = "jpg";     /* Output Extension (e.g., JPG) */
 

	
 
  int haveWork = 0;
 
  int quit = 0;
 

	
 
  fprintf(stderr,"\nDistRen SimpleSlave Pre-Alpha %s\n- Experimental build: Use at your own risk!\n\n", PACKAGE_VERSION);
 

	
 
  /* @TODO @FIXME create datadir (/var/distren/) if it doesn't exist, or notify the user */
 

	
 
  int benchmarkTime = 0;
 
  int renderPower = 0;
 

	
 
  // @TODO: create _web function to force recalc if !isset($render_power) on server
 
  if(runBenchmark)
 
    {
 
      if(slaveBenchmark(datadir, &benchmarkTime, &renderPower))
 
	{
 
	  fprintf(stderr,"Benchmark failed! Exiting.\n");
 
	  return 1;
 
	}
 
      else
 
	{
 
	  fprintf(stderr,"Benchmark successful, time taken was %d seconds, giving you a render power of %d.\n",
 
		  benchmarkTime, renderPower);
 
	  _web_setrenderpower(slavekey, password, renderPower);
 
	  return 0;
 
	}
 
    }
 

	
 
  if(!DEBUG)
 
    fprintf(stderr, "Running..");
 
  // Main loop
 
  while(!quit)
 
    {
 

	
 
    // request work
 
    if(DEBUG)
 
      fprintf(stderr,"Requesting work from %s...\n", hostname);
 
    haveWork = _web_getwork(slavekey, password, &jobnum, &framenum);
 

	
 
    /* If we got a frame */
 
    if(haveWork)
 
      {
 
        fprintf(stderr,"Got work from server...\n");
 
        if(DEBUG)
 
          fprintf(stderr, "Preparing to render frame %d in job %d\n", framenum, jobnum);
 

	
 
        prepareJobPaths(jobnum, framenum, outputExt, datadir, &urltoTar, &pathtoTar, &pathtoTardir, &pathtoJob, &pathtoJobfile, &urltoJobfile, &urltoOutput, &pathtoOutput, &pathtoRenderOutput, &pathtoOutdir);
 
        //free(outputExt);
 
        mkdir(pathtoTardir, 0700);
 
        int dlret = downloadTar(urltoTar, pathtoTar);
 
        //int dlret = downloadTar(urltoJobfile, pathtoJobfile);
 
        if(dlret == 0)
 
          fprintf(stderr,"Data retrieved successfully!\n");
 
        else if(dlret == 3){
 
          _web_resetframe(slavekey, password, jobnum, framenum);  // Unassign the frame on the server so other slaves can render it
 
          return 0; // ouput dir doesn't exist
 
        }
 
        else
 
          if(DEBUG)
 
            //fprintf(stderr,"Using existing uncompressed data %s...\n", pathtoJobfile);
 
            fprintf(stderr,"Using existing tarball %s...\n", pathtoTar);
 

	
 
        // Check if tar exists already
 
        struct stat jbuffer;
 
        int jstatus = stat(pathtoJobfile, &jbuffer);
 
        if(jstatus == -1){
 
          fprintf(stderr,"Main job file does not exist, extracting...\n");
 

	
 
          // If error unpacking tarball
 
          if(unpackJob(pathtoJob, pathtoTar)){
 
            _web_resetframe(slavekey, password, jobnum, framenum);  // Unassign the frame on the server so other slaves can render it
 
            fprintf(stderr,"Extraction failed, exiting.\n");
src/server/slavefuncs.c
Show inline comments
 
@@ -319,193 +319,193 @@ int distren_mkdir_recurse(char *dirname)
 
          mkdir(nextdir, S_IRWXU | S_IRGRP | S_IROTH);
 
          nextdir[counter] = '/';
 
        }
 
    }
 

	
 
  return 0;
 
}
 

	
 
/**
 
 @TODO: Use for constructing path to job data locally and/or on data.distren.org
 
 */
 
int job_build_path(char *filename, unsigned int jobnum)
 
{
 
  return 0;
 
}
 

	
 

	
 
int downloadTar(char *url, char *destinationPath){
 
  // Prepare to download the job tar if it isn't  present
 
  struct stat buffer;
 
  int fstatus = stat(destinationPath, &buffer);
 
  if(fstatus == -1)
 
    {
 
      // Download the Tar
 
      int ret = curlget(url, destinationPath);
 
      if(ret == 0){
 
        fprintf(stderr, "Job data retrieved successfully\n");
 
        // free(url); @FIXME: causes doublefree! Curl must free the url?
 
        return 0;
 
      }
 
      else if(ret == 1){
 
        fprintf(stderr, "Downloading job data from %s failed. Check your network connection.\n",url);
 
        free(url);
 
        return 1; // Eventually make a retry loop
 
      }
 
    }
 
  fprintf(stderr, "Tar at %s already exists! Download cancelled.\n", destinationPath);
 
  return 2;
 
}
 

	
 

	
 
int uploadOutput(char *pathtoOutput, char *urltoOutput, int jobnum, int framenum, int slavekey){
 
  //fprintf(stderr,"Uploading output %s to url %s for job %d and frame %d for slavekey %d", pathtoOutput, urltoOutput, jobnum, framenum, slavekey);
 
  if( !curlpost(pathtoOutput, urltoOutput, jobnum, framenum, slavekey)) // Uploads output
 
    {
 
      if(DEBUG)
 
        fprintf(stderr,"Upload successful, removing old output...\n");
 
      else
 
        fprintf(stderr,"Upload completed\n");
 

	
 
      remove(pathtoOutput); // Delete the file after its uploaded
 
      return 0;
 
    }
 
  else
 
    {
 
      fprintf(stderr,"Upload failed. Check your network connection. Retrying upload...\n");
 
      int tries=1;
 
      while(tries<=10 && curlpost(pathtoOutput, urltoOutput, jobnum, framenum, slavekey))
 
        {
 
          fprintf(stderr, "Upload failed. Trying again in 10 seconds... (attempt %d of 10)\n", tries);
 
          tries++;
 
          sleep(10);
 
        }
 
      return 1; // Upload failed after multiple tries
 
      // @FUTURE: Keep track of files that we were unable to upload, and upload them later
 
    }
 
}
 

	
 
void prepareJobPaths(int jobnum, int framenum, char *outputExt, char *datadir, char **urltoTar,char **pathtoTar,char **pathtoTardir,char **pathtoJob, char **pathtoJobfile, char **urltoJobfile, char **urltoOutput,char **pathtoOutput, char **pathtoRenderOutput, char **pathtoOutdir)
 
{
 
  // Variable Preparation
 
  char *jobdatapath;
 
   _distren_asprintf(&jobdatapath, "job%d", jobnum);
 
   _distren_asprintf(urltoTar, "http://data.distren.org/job%d/job%d.tar.gz", jobnum, jobnum); // Prepares URL to download from
 
   _distren_asprintf(urltoJobfile, "http://data.distren.org/job%d/job.blend", jobnum); // Prepares URL to download from
 

	
 
   _distren_asprintf(pathtoTar, "%s/%s/job%d.tar.gz", datadir, jobdatapath, jobnum); // Prepares destination to save to
 

	
 
   _distren_asprintf(pathtoTardir, "%s/%s/", datadir, jobdatapath); // Prepares destination to save to
 

	
 
   _distren_asprintf(pathtoJob, "%s/%s/", datadir, jobdatapath);
 

	
 
   _distren_asprintf(pathtoJobfile, "%s/%s/job.blend", datadir, jobdatapath ); // Prepares the path to the jobfile
 
   _distren_asprintf(urltoOutput, "http://dev.distren.org/slaveUpload.php?jobkey=%d&framekey=%d",jobnum,framenum); // Prepares the URL where output is posted
 

	
 
   _distren_asprintf(pathtoRenderOutput, "%s/%s/output/job%d-frame#.%s", datadir, jobdatapath, jobnum, outputExt ); // Note: the # is for blender to put in framenum in output file
 
   _distren_asprintf(pathtoOutput, "%s/%s/output/job%d-frame%d.%s", datadir, jobdatapath, jobnum, framenum, outputExt ); // Note: the # is for blender to put in framenum in output file
 

	
 
   _distren_asprintf(pathtoOutdir, "%s/%s/output", datadir, jobdatapath);
 
   free(jobdatapath);
 
}
 

	
 
int checkUsername(char *username)
 
{
 
  if(username == NULL || strcmp(username, "!username") == 0 )
 
    {
 
     fprintf(stderr, "\nPlease ensure that your username is present in distrenslave.conf\n");
 
     fprintf(stderr, "\nPlease ensure that your username is present in distrenslave.conf, or use the -u flag to set a username.\n");
 
     return 1;
 
   }
 
   else
 
     if( username != NULL || strcmp(username, "!username") != 0 )
 
       {
 
         // Log in the user
 
         if(login_user(username) == 1){
 
           // Logged in OK
 
           return 0;
 
         }
 
         else
 
           {
 
             fprintf(stderr, "Login failed, please check your username. If you have not registered, please do so on the DistRen website.\n");
 
             return 1;
 
           }
 
       }
 
     else
 
       {
 
         fprintf(stderr, "Please check your distrenslave.conf, it appears to be incorrectly formatted.\n");
 
         return 1;
 
       }
 
}
 

	
 
void slaveTest(char *datadir)
 
{
 
  int command;
 
  int test = 1;
 
  int jobnum = 0;
 
  int framenum = 0;
 
  int slavekey = 0;
 
  fprintf(stderr,"Hello!\n");
 
  char tmpString1[100] = "";
 
  char tmpString2[100] = "";
 
  char *uploadURL = "http://distren.org/slaveUpload.php";
 

	
 
  while(test == 1)
 
   {
 
     fprintf(stderr, "Welcome to DistRenSlave Alpha Interactive Test Mode\n\n");
 
     fprintf(stderr, "\t1 \tTest posting (uploading) data\n");
 
     fprintf(stderr, "\t2 \tTest getting frame from server\n");
 
     fprintf(stderr, "\t3 \tTest data downloading\n");
 
     fprintf(stderr, "\t4 \tTest archive decompression\n");
 
     fprintf(stderr, "\t5 \tQuit\n");
 

	
 
     scanf("%d", &command);
 

	
 
     switch(command)
 
     {
 
     case 1:
 
       fprintf(stderr,"Path to output file to upload: ");
 
       scanf("%99s", tmpString1);
 
       fprintf(stderr,"Framenum that is being uploaded: ");
 
       scanf("%d", &framenum);
 
       fprintf(stderr,"Jobnum that frame belongs to: ");
 
       scanf("%d", &jobnum);
 
       fprintf(stderr,"Slavekey that rendered this output: ");
 
       scanf("%d", &slavekey);
 
       uploadOutput(tmpString1, uploadURL, jobnum, framenum, slavekey);
 
       break;
 
     case 2:
 
       fprintf(stderr,"Remotio not implemented yet!\n");
 
       break;
 
     case 3:
 
       fprintf(stderr,"URL to file: ");
 
       scanf("%s", tmpString1);
 
       fprintf(stderr,"Local save path (including filename): ");
 
       scanf("%s", tmpString2);
 
       if(downloadTar(tmpString1, tmpString2))
 
         fprintf(stderr,"Error downloading!\n");
 
       else
 
         fprintf(stderr,"Success!\n");
 
       break;
 
     case 4:
 
       fprintf(stderr,"Jobnum to decompress: ");
 
       scanf("%d", &jobnum);
 
       fprintf(stderr,"Path to compressed data: ");
 
       scanf("%s", tmpString2);
 
       unpackJob(datadir, tmpString1);
 
       break;
 
     case 5:
 
       test = 0;
 
       break;
 
     default:
 
       fprintf(stderr, "Invalid input, please try again.\n");
 
       break;
 
     }
 
   }
 
}
 

	
 
/*
 
int runBenchmark(int slavekey, char *datadir){
 
  // Execute blender
 

	
 
  if(exec_blender(pathtoJobfile, pathtoOutput, framenum))
 
    {
 
      fprintf(stderr,"Error running Blender. Check your installation and/or your PATH.\n");
0 comments (0 inline, 0 general)