def edit( self ) : """ Edit resources file. """ console_logger.debug( "Editing '%s' file", DRM4G_RESOURCES_CONF ) os.system( "%s %s", os.environ.get('EDITOR', 'nano') , DRM4G_RESOURCES_CONF ) self.check( )
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 start(self): def _start(): # 's' option generates Bourne shell commands on stdout out, err = exec_cmd('ssh-agent -s ') console_logger.debug(out) match = re.search( 'SSH_AUTH_SOCK=(?P<SSH_AUTH_SOCK>[^;]+);.*' \ + 'SSH_AGENT_PID=(?P<SSH_AGENT_PID>\d+);', out, re.DOTALL) if match: console_logger.debug(" OK") self.agent_env = match.groupdict() console_logger.debug(' Agent pid: %s' % self.agent_env['SSH_AGENT_PID']) else: console_logger.error(err) raise Exception( ' Cannot determine agent data from output: %s' % out) with open(self.agent_file, 'w') as f: f.write(self.agent_env['SSH_AGENT_PID'] + '\n' + self.agent_env['SSH_AUTH_SOCK']) console_logger.debug('Starting ssh-agent ...') if not self.is_alive(): _start() elif self.is_alive(): console_logger.debug(" ssh-agent is already running") elif not self.agent_env: self.get_agent_env()
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 is_alive( self ): if not exists( self.agent_file ) : console_logger.debug(" '%s' does not exist", self.agent_file ) return False else : if process_is_runnig( self.agent_file ): return True else : return False
def run(arg): if arg['daemon']: conf_file = DRM4G_GWD_CONF elif arg['console_logger']: conf_file = DRM4G_console_logger_CONF else: conf_file = DRM4G_SCHED_CONF console_logger.debug("Editing '%s' file" % conf_file) os.system("%s %s" % (os.environ.get('EDITOR', 'nano'), conf_file))
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 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 exec_cmd( cmd , stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=os.environ ): """ Execute shell commands """ console_logger.debug( "Executing command ... %s", cmd ) cmd_to_exec = subprocess.Popen( cmd , shell=True , stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env ) out , err = cmd_to_exec.communicate() return out.decode() , err.decode()
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 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 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 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 get_agent_env( self ): console_logger.debug("Reading '%s' file", self.agent_file ) with open( self.agent_file , 'r' ) as f: lines = f.readlines() self.agent_env['SSH_AGENT_PID'] = lines[0].strip() self.agent_env['SSH_AUTH_SOCK'] = lines[1].strip()