Changeset - 7225f5e7ec44
[Not reviewed]
default
0 2 0
ethanzonca - 16 years ago 2009-08-01 14:35:23

Cleanup, commenting
2 files changed with 20 insertions and 45 deletions:
0 comments (0 inline, 0 general)
src/server/slave.c
Show inline comments
 
@@ -15,21 +15,12 @@
 

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

	
 
 */
 

	
 
/*
 
 * **********************************************************************************
 
 * Slave functions / etc resides below. Wouldn't a separate file make this easier??
 
 *
 
 * Slave listens on server for a command in the format of each function...
 
 * We need if's for returns... ==> watchdog
 
 * **********************************************************************************
 
*/
 

	
 
// Provides DISTREN_REQUEST* which is in reality, DISTREN_SEND(signal) in the minds of your average joe
 
#include "protocol.h"
 
#include "options.h" // Confuse, etc.
 
#include "slavefuncs.h" // Slave functions
 
#include <string.h>
 
#include <stdio.h>
 
@@ -37,13 +28,12 @@
 

	
 
int slavestatus = 0; // Ugh global vars
 

	
 
int main(int argc, char *argv[])
 
{
 

	
 

	
 
int counter;
 

	
 
/* Parses arguments, skips if there are no args */
 
for(counter=0; counter<argc; counter++){
 
	if(strcmp(argv[counter], "-h") == 0) {
 
		fprintf(stderr, "Usage: distrenslave [option] <username> <emailaddr>\nStarts a distren slave\n\t-h\tshow this help\n\t-c\tregisters a user with [username] and [emailaddr] \n");
 
@@ -80,13 +70,12 @@ for(counter=0; counter<argc; counter++){
 
			return 0;
 
		}
 
	}
 
/* End arg parser */
 

	
 

	
 

	
 
/* Option getter: Creates vars to grab stuff from conf, uses the options include to grab this data */
 
char *username;
 
username = NULL;
 
cfg_t * my_cfg;
 
cfg_opt_t myopts[] = {
 
		CFG_SIMPLE_STR("username", &username),
 
@@ -94,32 +83,31 @@ cfg_opt_t myopts[] = {
 
		};
 
struct options_common *commonopts;
 
options_init(argc,argv,&my_cfg, myopts, "slave", &commonopts);
 
/* End option getter */
 

	
 

	
 
/* If there's no key or username in the conf, or if they're at the default (!key/!username) let the user know! */
 
/* Notifies the user if there is no username in the conf file */
 
	if(username == NULL || strcmp(username, "!username") == 0 ){
 
	  fprintf(stderr, "\nYou didn't register!\nPlease register or edit your config. (see -h)\nIf this error persists, check distrenslave.conf to ensure all items are filled.\n");
 
	}
 
	else if( username != NULL || strcmp(username,"!username") != 0 ){
 
		// Logs ya in:
 
		if(login_user(username) == 1){
 
			fprintf(stderr,"You should now be logged into distren.\n");
 
		}
 
		else{
 
			fprintf(stderr,"Login failed. I have no clue why. Goodbye.\n");
 
			fprintf(stderr,"Login failed. I have no clue why. Sorry.\n");
 
			return 0;
 
		}
 
	}
 
	else{
 
	  fprintf(stderr,"Something is terribly wrong!!!");
 
	  fprintf(stderr,"Something is terribly wrong!");
 
	}
 

	
 

	
 

	
 
/* Somewhat pseudo Rendering code */
 
/*
 
	if('slave recieves "start frame#, job#"'){
 
		fprintf(stderr, "Received %d in job %d, preparing to render...",framenum,jobnum);
 
		get('http://distren.protofusion.org/srv/job%d.tgz', jobnum); // use curl
 
		exec('tar -xvf job%d.tgz /tmp/distren/job%d',jobnum,jobnum); // somehow
src/server/slavefuncs.c
Show inline comments
 
@@ -26,24 +26,23 @@
 
#include "blendjob.h"
 
#include "execio.h"
 

	
 
#include <stdio.h>
 
#include <string.h>
 
#include <unistd.h>
 
#include <stdlib.h> /*< malloc(), free() */
 
#include <stdlib.h>
 
#include <sys/stat.h>
 
#include <fcntl.h>
 

	
 
/** Converts an integer to a string, thanks to Dom_ (http://www.signalsondisplay.com/) */
 
/** Converts an integer to a string, in ANSI C, thanks to Dom_ (http://www.signalsondisplay.com/) */
 
char *int_to_str(int nbr)
 
{
 
  int   div;
 
  int   len;
 
  int   i;
 
  char  *res;
 

	
 
  res = malloc(4 * sizeof(*res)); // Was xmalloc, hopefully it still works :D
 
  i = 0;
 
  while (i < 3)
 
    res[i++] = '\0';
 
  res[3] = '\0';
 
  i = 0;
 
@@ -60,30 +59,28 @@ char *int_to_str(int nbr)
 
      len--;
 
      nbr %= div;
 
      div /= 10;
 
    }
 
  return (res);
 
}
 

	
 
/** Generates a SSH key with ssh-keygen */
 
int ssh_keygen(){
 
	// distren.id_rsa and distren.id_rsa.pub are now generated in SYSCONFDIR (etc)
 

	
 
	// Checks to see if the keys are already present.
 
	/* Checks to see if the keys are already present. */
 
	int status;
 
	struct stat buffer;
 
	status = stat(SYSCONFDIR "/distren.id_rsa", &buffer);
 
	if(status != -1){
 
		fprintf(stderr, "Please delete etc/distren.id_rsa and etc/distren.id_rsa.pub to register.\n");
 
		return 0;
 
	}
 

	
 
/* start execio code */
 
	/* start execio code */
 
	char *command = "ssh-keygen"; // @TODO: append .exe if win32?
 
	int ret;
 
	char *cmd[] = { command, "-q", "-f", SYSCONFDIR "/distren.id_rsa", "-N", "", (char *)NULL }; // TODO: Give me the correct args!
 

	
 
	char buf[10];
 
	struct execio *testrem;
 
	size_t readlen;
 
	ret = execio_open(&testrem, command, cmd); // This path will be absolute for testing, should be relative to install on production
 
	buf[9] = '\0'; // null-terminating the array...
 
	while(!execio_read(testrem, buf, 9, &readlen))
 
@@ -92,13 +89,13 @@ int ssh_keygen(){
 
		fprintf(stderr, "Something is terribly wrong!\n");
 
	      }
 
	      buf[readlen] = '\0';
 
	      fprintf(stderr, "read \"%s\"\n", buf);
 
	    }
 
	execio_close(testrem);
 
/* end execio code */
 
	/* end execio code */
 

	
 
	// Supposedly execio returns 1 if it has bad args.
 
	if(ret == 1){
 
		fprintf(stderr, "Generating your key failed. Ensure that ssh-keygen is present!\n"); // Use different executor that searches the path? there is one...
 
		return 0;
 
	}
 
@@ -109,28 +106,25 @@ int ssh_keygen(){
 
return 0;
 
}
 

	
 
/** Registers the user on the DistRen server */
 
int register_user(char *username, char *email)
 
{
 

	
 
	// @TODO: Check for problems (e.g. existing rsa key) BEFORE useradding!
 

	
 
	/* @TODO: Check for problems (e.g. existing rsa key) BEFORE useradding! */
 
  /*
 
   * Logs into sandboxed user on zserver2 and registers a user. Should eventually generate a key on the server and return it to the user.
 
   * All created user accounts should be sandboxed accordingly, requiring a different skel, and the default shell to be rbash. Also,
 
   * a custom path defined in the .bash_profile of the skel is needed.
 
   * a custom path defined in the .bashrc of the skel is needed.
 
   */
 
  char buf[10];
 
  struct execio *testrem;
 
  char *execargv[] =
 
    {
 
      "ssh",
 
      "distren_setup@protofusion.org",
 
      "-i",
 
      SYSCONFDIR "/setup_rsa", // default distributed key, account can only create users.
 
      SYSCONFDIR "/setup_rsa", // @TODO: How will we distribute this key?
 
      "-p",
 
      "23",
 
      "newuser",
 
      "-M",
 
      "-c",
 
      email,
 
@@ -140,13 +134,13 @@ int register_user(char *username, char *
 
      "541",
 
      username,
 
      (char *)NULL
 
    };
 
  size_t readlen;
 
  execio_open(&testrem, "ssh", execargv); // TODO: Grab returns from this someday.
 
  buf[9] = '\0'; // null-terminating the array...
 
  buf[9] = '\0';
 
  while(!execio_read(testrem, buf, 9, &readlen))
 
    {
 
      if(readlen > 9) {
 
	fprintf(stderr, "!!!! Something is terribly wrong!\n");
 
      }
 
      buf[readlen] = '\0';
 
@@ -165,19 +159,18 @@ int register_user(char *username, char *
 
  // generates keys for login, @TODO: pub key must somehow be sent to the server.
 
  if(ssh_keygen() == 0){
 
	  fprintf(stderr, "Failed!\n");
 
	  return 0;
 
  }
 

	
 
  return 1;
 
  return 1; // 1 is success
 
}
 

	
 
/** Logs the user into the server with a nice ssh session */
 
/** Logs the user into the server after ensuring that keys exist */
 
int login_user(char *username)
 
{
 

	
 
  char *userhost;
 
  userhost = malloc(strlen(username) + strlen("@protofusion.org") + 1);
 
  if(!userhost)
 
    return 43;
 
  strcpy(userhost, username);
 
  strcat(userhost, "@protofusion.org"); // Throws @protofusion.org after the username
 
@@ -195,39 +188,36 @@ int login_user(char *username)
 
      "echo",
 
      "hello", // This should eventually open a non-terminating connection to the server for communication,
 

	
 
      (char *)NULL
 
    };
 
  size_t readlen;
 

	
 
  fprintf(stderr, "Logging you in to %s\n", userhost);
 

	
 
  int status;
 
  struct stat buffer;
 
  status = stat(SYSCONFDIR "/distren.id_rsa", &buffer);
 
  if(status == -1){
 
	  fprintf(stderr,"Your key has not been found! Re-register or somehow regenerate your key!\nWe need a way to regenerate keys coded in, but we don't have the facilities yet!\n");
 
	  return 0;
 
  }
 
  execio_open(&testrem, "ssh", execargv); // TODO: Grab returns from this someday
 
  buf[9] = '\0'; // null-terminating the array...
 
  while(!execio_read(testrem, buf, 9, &readlen))
 
    {
 
      if(readlen > 9) {
 
	fprintf(stderr, "!!!! Something is terribly wrong!\n");
 
	fprintf(stderr, "Something is terribly wrong!\n");
 
      }
 
    buf[readlen] = '\0';
 
    fprintf(stderr, "read \"%s\"\n", buf);
 
    }
 
  execio_close(testrem);
 
  return 1; // 1 can be like... error-free login...
 
  return 1; // 1 means success
 
}
 

	
 
/** Replaces username and key in the slave's conf file */
 
/** Replaces !username with a username in the slave's conf file */
 
int conf_replace(char *username){
 
  /* Note: SYSCONFDIR doesn't include a trailing slash */
 
  int maxlinelen = 120;
 
  char *fileOrig = SYSCONFDIR "/distrenslave.conf";
 
  char *fileRepl = SYSCONFDIR "/distrenslave.conf.edited";
 
  char *text2find = "!username";
 
  char *text2repl = username;
 
  char buffer[maxlinelen+2];
 
@@ -256,32 +246,29 @@ int conf_replace(char *username){
 
          buff_ptr += find_len;
 
          }
 
          fputs(buff_ptr,fp2);
 
      }
 
    rename(fileRepl, fileOrig);
 
  }
 

	
 
  fclose(fp2);
 
  fclose(fp1);
 
  fprintf(stderr,"Wrote conf file...\n");
 
return 1;
 
return 1; // Success
 
}
 

	
 

	
 
/* Executors */
 

	
 
/*
 
  It seems that the client will need to know the job number. fixme.
 
*/
 

	
 
/** Executor function for Blender operations */
 
void exec_blender(struct blendjob* blendjob, char *input, char *output, int frame)
 
{
 

	
 
	char *frame_str = int_to_str(frame); // Converts the int frame to a string, so it can be in the cmd array. GNU/*nix compatible only, fix before releasing win32, although dll for windows for asprintf exists!
 

	
 
	char *frame_str = int_to_str(frame); // Converts the int frame to a string, so it can be in the cmd array.
 
  /* start execio code */
 
  	char *command = "blender"; // @TODO: append .exe if win32?
 
  	int ret;
 
    char *cmd[] = { command, "-b", "-o", output, input, "-f", frame_str, (char *)NULL };
 

	
 
  	char buf[10];
0 comments (0 inline, 0 general)