def handle_stop_command(): """I handle the stop command """ try: import_services() except ImportError: print( 'error: make sure you are inside a mamba application root ' 'directory and then run this command again' ) sys.exit(-1) twisted_pid = filepath.FilePath('twistd.pid') if not twisted_pid.exists(): print( 'error: twistd.pid file can\'t be found. You should be in the ' 'applicatin directory in order to stop it' ) sys.exit(-1) pid = twisted_pid.open().read() print('killing process id {} with SIGINT signal'.format( pid).ljust(73), end='') try: filepath.os.kill(int(pid), signal.SIGINT) print('[{}]'.format(darkgreen('Ok'))) except: print('[{}]'.format(darkred('Fail'))) raise
def handle_start_command(options=None, dropin_cache_wa=False): """I handle the start command """ try: mamba_services = import_services() mamba_services.config.Application('config/application.json') except ImportError: print( 'error: make sure you are inside a mamba application root ' 'directory and then run this command again' ) sys.exit(-1) if POSIX: app = mamba_services.config.Application() if app.port is None: print( 'Your application configuration file does not define a valid ' 'port. Is your configuration file valid JSON format?' ) sys.exit(-1) elif app.port <= 1024: if os.getuid() != 0: print( '[{e}]: This application is configured to use a reserved ' 'port (a port under 1025) only root can open a port from ' 'this range root access is needed to start this ' 'application using the {port} port.\n\nTry something ' 'like: sudo mamba-admin start\n\nYou can also change the ' 'configuration for this application editing ' '\'config/application.json\''.format( e=darkred('ERROR'), port=app.port ) ) sys.exit(-1) args = ['twistd'] try: app_name = glob.glob( 'twisted/plugins/*.py')[0].split(os.sep)[-1].rsplit('_', 1)[0] except IndexError: print( 'error: twisted directory can\'t be found. You should be in ' 'the application directory in order to start it' ) sys.exit(-1) if filepath.exists('twistd.pid'): print( 'error: twistd.pid found, seems like the application is ' 'running already. If the application is not running, please ' 'delete twistd.pid and try again' ) sys.exit(-1) # determine if we are running in heroku in_heroku = '.heroku' in os.environ.get('PYTHONHOME', '') args.append('--nodaemon') if not in_heroku: if not mamba_services.config.Application().auto_select_reactor: args.append(determine_platform_reactor(mamba_services)) if not mamba_services.config.Application().development: args.remove('--nodaemon') args.append('--syslog') args.append(app_name) if options is not None and options.subOptions.opts['port']: args.append('--port={}'.format(options.subOptions.opts['port'])) if in_heroku or mamba_services.config.Application().development: os.execlp('twistd', *args) else: if not dropin_cache_wa: print( 'starting application {}...'.format(app_name).ljust(73), end='' ) proc = subprocess.Popen( args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) out, err = proc.communicate() if not err: if 'exception' in out: result = darkred('Fail') exit_code = -1 elif 'already installed' in out: return handle_start_command(options, True) else: result = darkgreen('Ok') exit_code = 0 else: result = darkred('Fail') exit_code = -1 print('[{}]'.format(result)) print(err if exit_code == -1 else out) sys.exit(exit_code)