def run(cmd): """fabric执行远程命令""" logger.debug("cmd: %s\n", cmd) if ENV_USER != "root": sudo(cmd) else: _run(cmd)
def run(cmd, capture=True, remote_only=False, **kwargs): capture = parse_bool(capture) try: if dconf.HOST_CONN == 'remote': res = _run(cmd, **kwargs) elif dconf.HOST_CONN == 'local': res = local(cmd, capture=capture, **kwargs) else: # docker or remote_docker opts = '' cmdd = cmd if cmd.endswith('&'): cmdd = cmd[:-1].strip() opts = '-d ' if remote_only: docker_cmd = cmdd else: docker_cmd = 'docker exec {} -ti {} /bin/bash -c "{}"'.format( opts, dconf.CONTAINER_NAME, cmdd) if dconf.HOST_CONN == 'docker': res = local(docker_cmd, capture=capture, **kwargs) elif dconf.HOST_CONN == 'remote_docker': res = _run(docker_cmd, **kwargs) else: raise Exception('wrong HOST_CONN type {}'.format( dconf.HOST_CONN)) except TypeError as e: err = str(e).strip() if 'unexpected keyword argument' in err: offender = err.rsplit(' ', 1)[-1][1:-1] kwargs.pop(offender) res = run(cmd, **kwargs) else: raise e return res
def host_print(msg, remote=True, leading_chars='\n'): ''' Print a raw message on the host. ''' cmd = 'echo "{0}{1}"'.format(leading_chars, msg) with hide('running'): if remote: _run(cmd) else: _local(cmd)
def logs(): ''' Tail the logs. ''' stage = shell.get_stage() stage_specific_logging = get_stage_config(stage).get('logging') logging_config = stage_specific_logging or get_config().get('logging') # If log files are configured tail them. if logging_config and logging_config.get('files'): # Tail the logs from log files log_paths = ' '.join(logging_config.get('files')) _run('tail -f ' + log_paths) return # If logs script is defined, run it runner.run_script_safely(constants.SCRIPT_LOGS)
def run(*args, **kwargs): # if env.is_local: # kwargs['capture'] = True # if env.dryrun: # print args, kwargs # else: # print args, kwargs # cmd = ' '.join(args) # #cmd = ' '.join(args + ('2>&1',)) # try: # output = StringIO() # error = StringIO() # sys.stdout = output # sys.stderr = error # result = local(cmd, **kwargs) # except: # raise # finally: # sys.stdout = sys.__stdout__ # sys.stderr = sys.__stderr__ # print 'stdout:',output.getvalue() # print 'stderr:',error.getvalue() ## print 'result:',result ## print 'stdout:',result.stdout ## print 'stderr:',result.stderr # print # return result if env.dryrun: print ' '.join(map(str, args)), kwargs else: return _run(*args, **kwargs)
def run(command, show=True): """ Runs a shell command on the remote server. """ if show: print_command(command) with hide("running"): return _run(command)
def run(command, show=True, *args, **kwargs): """ Runs a shell comand on the remote server. """ if show: print_command(command) with hide("running"): return _run(command, *args, **kwargs)
def run(command, show=True): """ Runs a shell command on the remote server. """ if show: print_command(command) with hide("running"): if env.deploy_user: return _sudo(command, user=env.deploy_user) return _run(command)
def run(command, quiet=False, show=True): """ Runs a shell command on the remote server. """ if show: print_command(command) with hide("running"): if 'deploy_user' in env: # If a deploy_user is specified, run as that user return _sudo(command, quiet=quiet, user=env.deploy_user) return _run(command, quiet=quiet)
def sudo(command, show=True): """ Runs a command as sudo. """ if show: print_command(command) with hide("running"): #return _sudo(command) # deployment user doesn't have root in our setup return _run(command)
def run(command, show=True, args=("running",), quiet=False, shell=True): """Runs a shell comand on the remote server.""" if show: print_command(command) else: args += ('stdout',) with hide(*args): return _run(command, quiet=quiet, shell=True)
def pull(): with prefix('pyenv shell drf3'), cd(env.remote_dir): _run('git pull') _run('pip install -r requirements.txt') migrate() _run('/usr/bin/supervisorctl reØ browser')
def virtualenv_exists(virtualenv_dir=None): render_paths() #base_dir = os.path.split(env.pip_virtual_env_dir)[0] base_dir = virtualenv_dir or env.pip_virtual_env_dir ret = True with settings(warn_only=True): ret = _run('ls %s' % base_dir) or '' ret = 'cannot access' not in ret.strip().lower() if common.get_verbose(): if ret: print('Yes') else: print('No') return ret
def venv(): with prefix('source /usr/local/bin/virtualenvwrapper.sh'): _run('mkvirtualenv browser') with prefix('pyenv shell drf3'), cd(env.remote_dir): _run('pip install -r requirements_dev.txt')
def restart(): with prefix('pyenv shell drf3'), cd(env.remote_dir): _run('/usr/bin/supervisorctl restart browser')
def migrate(): with prefix('pyenv shell drf3'), cd(env.remote_dir): _run('''DJANGO_SETTINGS_MODULE='config.settings.local' python manage.py migrate''')
def test(): ''' The npm test command. ''' _run('npm test')
def has_virtualenv(): with settings(warn_only=True): ret = _run('which virtualenv').strip() return bool(ret)
def install(): ''' The npm install command. ''' _run('npm install')
def start(): ''' The npm start command. ''' _run('npm start')
def run(command, capture=False, shell=True, pty=True): if _is_local(env.host): return _local(command % (env), capture=capture) else: return _run(command % (env), shell=shell, pty=pty)
def run(command, show=True): with hide("running"): return _run(command)
def run(script): ''' Run an npm script defined in the package.json file. ''' _run('npm run %s' % script)
def run(command, remote=True): ''' Run a command using fabric. ''' if remote: return _run(command) else: return _local(command)
def run(command): return _run(command)
def prune(): ''' The npm prune command. ''' _run('npm prune')
def build(): ''' The npm build command. ''' _run('npm run build')
def has_pip(): with settings(warn_only=True): ret = _run('which pip').strip() return bool(ret)
def run(*args, **kwargs): if not env.sudo_forced: return _run(*args, **kwargs) return sudo(*args, **kwargs)