def run(arg): try: daemon = Daemon() if not daemon.is_alive(): raise Exception('DRM4G is stopped. ') if arg['submit']: dependencies = '-d "%s"' % ' '.join( arg['--dep']) if arg['--dep'] else '' number_of_tasks = '-n %s' % arg['--ntasks'] if arg[ '--ntasks'] else '' cmd = 'gwsubmit %s -v %s %s' % (dependencies, arg['<template>'], number_of_tasks) elif arg['list']: cmd = 'gwps -o Jsetxjh ' if arg['<job_id>']: cmd = cmd + arg['<job_id>'][0] elif arg['history']: cmd = 'gwhistory %s' % (arg['<job_id>'][0]) elif arg['log']: directory = join( DRM4G_DIR_VAR, '%d00-%d99' % (int(int(float(arg['<job_id>'][0])) / 100), int(int(float(arg['<job_id>'][0])) / 100)), arg['<job_id>'][0], 'job.log') if not exists(directory): raise Exception('There is not a log available for this job.') cmd = 'cat %s' % (directory) else: cmd = 'gwkill -9 %s' % (' '.join(arg['<job_id>'])) out, err = exec_cmd(cmd) console_logger.info(out) if err: console_logger.info(err) except Exception as err: console_logger.error(str(err))
def delete_key(self): console_logger.info('--> Remove key %s' % self.private_key) out, err = exec_cmd('ssh-add -d %s' % self.private_key, stdin=sys.stdin, stdout=sys.stdout, env=self.update_agent_env()) if err: console_logger.info(err)
def features( self ) : """ List the features of a given resource. """ self.check( ) for resname, resdict in sorted( self.config.resources.items() ) : console_logger.info( "Resource '%s' :", resname ) for key , val in sorted( resdict.items() ) : console_logger.info( " --> '%s' : '%s'", key, val )
def configure(self): certificate = self.resource.get('grid_cert') if not certificate: console_logger.warning( " WARNING: It is assumed that the grid certificate has been already configured" ) else: dir_certificate = dirname(certificate) base_certificate = basename(certificate) console_logger.info("--> Converting '%s' key to pem format ... " % base_certificate) cmd = "openssl pkcs12 -nocerts -in %s -out %s" % ( certificate, join(dir_certificate, 'userkey.pem')) out, err = exec_cmd(cmd, stdin=sys.stdin, stdout=sys.stdout) if "invalid password" in err: raise Exception(err) console_logger.info( "--> Converting '%s' certificate to pem format ... " % base_certificate) cmd = "openssl pkcs12 -clcerts -nokeys -in %s -out %s" % ( certificate, join(dir_certificate, 'usercert.pem')) out, err = exec_cmd(cmd, stdin=sys.stdin, stdout=sys.stdout) if "invalid password" in err: raise Exception(err) console_logger.debug("--> Creating '~/.globus' directory ... ") cmd = "mkdir -p ~/.globus" console_logger.debug("Executing command ... " + cmd) out, err = self.communicator.execCommand(cmd) if err: raise Exception(err) for file in ['userkey.pem', 'usercert.pem']: cmd = "rm -rf $HOME/.globus/%s" % file console_logger.debug("Executing command ... " + cmd) out, err = self.communicator.execCommand(cmd) if err: raise Exception(err) console_logger.info("--> Copying '%s' to '%s' ..." % (file, self.resource.get('frontend'))) self.communicator.copy( 'file://%s' % join(dir_certificate, file), 'ssh://_/%s' % join('.globus', file)) console_logger.info("--> Modifying userkey.pem permissions ... ") cmd = "chmod 400 $HOME/.globus/userkey.pem" console_logger.debug("Executing command ... " + cmd) out, err = self.communicator.execCommand(cmd) if err: raise Exception(err) console_logger.info("--> Modifying usercert.pem permissions ... ") cmd = "chmod 644 $HOME/.globus/usercert.pem" console_logger.debug("Executing command ... " + cmd) out, err = self.communicator.execCommand(cmd) if err: raise Exception(err)
def run(arg): try: daemon = Daemon() if not daemon.is_alive(): raise Exception('DRM4G is stopped.') cmd = 'gwhost ' if arg['<hid>']: cmd = cmd + arg['<hid>'] out, err = exec_cmd(cmd) console_logger.info(out) if err: console_logger.info(err) except Exception as err: console_logger.error(str(err))
def destroy( self ): console_logger.info( "--> Remove grid credentials" ) cmd = self.prefix + "myproxy-destroy" console_logger.debug( "Executing command ... %s", cmd ) out , err = self.communicator.execCommand( cmd ) console_logger.info( out ) if err : console_logger.info( err ) cmd = self.prefix + "grid-proxy-destroy" console_logger.debug( "Executing command ... %s", cmd ) out , err = self.communicator.execCommand( cmd ) console_logger.info( out ) if err : console_logger.info( err )
def start( self ): console_logger.info( "Starting DRM4G .... " ) if not exists( self.gwd_pid ) or ( exists( self.gwd_pid ) and not process_is_runnig( self.gwd_pid ) ) : lock = join(DRM4G_DIR_VAR , '/.lock' ) if exists( lock ) : os.remove( lock ) console_logger.debug( "Starting gwd .... " ) out , err = exec_cmd( 'gwd' ) if err : console_logger.info( err ) if out : console_logger.info( out ) if not err and not out : console_logger.info( " OK" ) else : console_logger.info( " WARNING: DRM4G is already running" )
def list( self ) : """ Check if the resource.conf file has been configured well and list the resources available. """ self.check( ) console_logger.info( "\033[1;4m%-20.20s%-20.20s\033[0m", 'RESOURCE', 'STATE' ) for resname, resdict in sorted( self.config.resources.items() ) : # To ignore VMs created by the DRM4G, since they should appear as hosts, not resources if '_' in resname: first_half, _ = resname.rsplit('_',1) if first_half in self.config.resources.keys() : continue if resdict[ 'enable' ] == 'true' : state = 'enabled' else : state = 'disabled' console_logger.info( "%-20.20s%s", resname, state )
def check_frontends( self ) : """ Check if the frontend of a given resource is reachable. """ self.check( ) communicators = self.config.make_communicators() for resname, resdict in sorted( self.config.resources.items() ) : if resdict[ 'enable' ] == 'true' : communicator = communicators.get( resname ) try : # if resdict[ 'communicator' ] == 'op_ssh' : # #it will use im's socket instead of creating a new one # communicator.parent_module = 'im' # communicator.configfile = join(DRM4G_DIR, 'etc', 'openssh_im.conf') communicator.connect() console_logger.info( "Resource '%s' :", resname ) console_logger.info( "--> The front-end '%s' is accessible\n", communicator.frontend ) except Exception : console_logger.error( "Resource '%s' :", resname ) console_logger.error( "--> The front-end '%s' is not accessible\n", communicator.frontend )
def stop( self ): console_logger.info( "Stopping DRM4G .... " ) console_logger.debug( "Stopping gwd .... " ) if self.is_alive(): with open(self.gwd_pid) as pid_file: pid = next(pid_file) processes_to_kill = [pid] while processes_to_kill: for process in processes_to_kill: try: cmd = "ps ho pid --ppid %s" % (process) out , _ = exec_cmd( cmd ) processes_to_kill = [line.lstrip() for line in out.splitlines()] + processes_to_kill processes_to_kill.remove(process) os.kill( int(process), signal.SIGTERM ) except OSError as exc: if "No such process" in str(exc): continue raise while self.is_alive() : time.sleep(1) try: os.remove(self.gwd_pid) except OSError: console_logger.error(" '%s' does not exist", self.gwd_pid) console_logger.info( " OK" ) else: console_logger.info(" WARNING: daemon is already stopped")
def copy_key( self ): console_logger.info("--> Copying '%s' to ~/.ssh/authorized_keys file on '%s'", self.private_key, self.frontend ) private_key_path = expanduser(self.private_key) public_key_path = expanduser(self.public_key) error_message = "Could not find the %s in %s.\n" \ "Be sure to first generate the authentication keys with 'ssh_keygen'" \ " and then specify it's path when defining your resource.\n" \ "By default '~/.ssh/id_rsa' and '~/.ssh/id_rsa.pub' will be " \ "considered to be your private and public keys respectively" if not exists(private_key_path): raise Exception(error_message % ('private key', private_key_path)) if not public_key_path: public_key_path = private_key_path+'.pub' if not exists(public_key_path): raise Exception(error_message % ('public key', public_key_path)) cmd='cat %s | ssh %s@%s "mkdir -p ~/.ssh;' \ ' cat > ~/temp_pub_key && grep -q -f temp_pub_key ~/.ssh/authorized_keys || cat temp_pub_key >> ~/.ssh/authorized_keys &&' \ ' chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh && rm temp_pub_key"' % (public_key_path, self.user, self.frontend) answer = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) _ , err = answer.communicate() if err: if "grep" in err: pass else: console_logger.info( err ) raise Exception(err) console_logger.info( "The copy of the public key %s has been successful", public_key_path )
def check( self ): console_logger.info( "--> Display information about the proxy certificate" ) cmd = self.prefix + "grid-proxy-info" console_logger.debug( "Executing command ... %s", cmd ) out, err = self.communicator.execCommand( cmd ) console_logger.info( out ) if err : console_logger.info( err )
def clear( self ): yes_choise = yes_no_choice( "Do you want to continue clearing DRM4G? " ) if yes_choise : console_logger.info( "Clearing DRM4G .... " ) cmd = "%s -c" % ( "gwd" ) out , err = exec_cmd( cmd ) console_logger.debug( out ) if err : console_logger.info( err ) if out : console_logger.info( out ) if not err and not out : console_logger.info( " OK" ) else : self.start()
def list_key( self ): console_logger.info("--> Display '%s' key", self.private_key ) out , _ = exec_cmd( 'ssh-add -L' , env=self.update_agent_env() ) match = re.search( '.*%s' % basename( self.private_key ) , out ) if match : console_logger.info( match.group() ) else : console_logger.info( " The private key '%s' is not available anymore", self.private_key )
def add_key( self, lifetime ): console_logger.info("--> Add '%s' into ssh-agent for %s hours", self.private_key , lifetime ) _ , err = exec_cmd( 'ssh-add -t %sh %s' % ( lifetime , self.private_key ), stdin=sys.stdin, env=self.update_agent_env() ) mo = re.compile(r'.* (\d*) .*').search( err ) if mo : console_logger.info( " Lifetime set to %s", str( datetime.timedelta( seconds=int( mo.group(1) ) ) ) ) else : console_logger.info( err )
def create( self , proxy_lifetime ): console_logger.info("--> Creating '%s' directory to store the proxy ... ", REMOTE_VOS_DIR ) cmd = "mkdir -p %s" % REMOTE_VOS_DIR console_logger.debug( "Executing command ... %s", cmd ) out, err = self.communicator.execCommand( cmd ) if not err : console_logger.info("--> Create a local proxy credential ... ") message = 'Insert your Grid password: ' grid_passwd = getpass.getpass(message) cmd = self.prefix + "myproxy-init -S --cred_lifetime %s --proxy_lifetime %s --local_proxy -n -d" % ( proxy_lifetime , proxy_lifetime ) console_logger.debug( "Executing command ... %s", cmd ) out , err = self.communicator.execCommand( cmd , input = grid_passwd ) console_logger.info( out ) if err : console_logger.info( err ) else : raise Exception( err )
def stop( self ): console_logger.info( 'Stopping ssh-agent ... ' ) if self.is_alive(): out , err = exec_cmd( 'ssh-agent -k' , env=self.update_agent_env() ) if out : console_logger.debug( out ) if err : console_logger.info( err ) else : console_logger.info( " OK" ) else: console_logger.warning( ' WARNING: ssh-agent is already stopped' ) try: os.remove( self.agent_file ) except : pass
def list_resources(self): """ List all resources, the ones configured by the user and the ones configured internally For example, when creating cloud virtual machines """ self.check( ) console_logger.info("Resources:") for resname, resdict in self.config.resources.items(): console_logger.info(" %s", str(resname)) console_logger.info(" communicator: %s", str(resdict['communicator'])) if 'username' in resdict.keys(): console_logger.info(" username: %s", str(resdict['username'])) console_logger.info(" frontend: %s", str(resdict['frontend'])) if 'private_key' in resdict.keys(): console_logger.info(" private key: %s", str(resdict['private_key'])) console_logger.info(" lrms: %s", str(resdict['lrms']))
def status( self ): if self.is_alive() : console_logger.info( "DRM4G is running" ) else : console_logger.info( "DRM4G is stopped" )