Python fabric.api.env.user() Examples

The following are 30 code examples of fabric.api.env.user(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fabric.api.env , or try the search function .
Example #1
Source File: fabfile.py    From crestify with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setup_supervisor():
    # We use supervisord to keep Crestify running in the background
    # Recover from crashes, and to start automatically on bootup
    # Also, using more than 1 gunicorn worker resulted in socket not being released, so only 1 worker will be used
    sudo('apt-get -y install supervisor')
    sudo('mkdir /var/log/crestify/')
    sudo(
        'cd /home/crestify/crestify && ../crestifyenv/bin/honcho export -s /bin/sh -a crestify supervisord /etc/supervisor/conf.d')
    fd = StringIO()
    get('/etc/supervisor/conf.d/crestify.conf', fd)
    content = fd.getvalue().splitlines()
    for n, i in enumerate(content):
        if i.startswith("environment="):
            content[n] = i + ",PATH=/home/crestify/crestifyenv/bin:%(ENV_PATH)s"
        if i.startswith("user="):
            content[n] = "user=crestify"
        if i.startswith("stopsignal="):
            content[n] = "stopsignal=TERM"  # Both Gunicorn and Celery use SIGTERM for graceful shutdown
    content = StringIO("\n".join(content))
    put(content, "/etc/supervisor/conf.d/crestify.conf", use_sudo=True)
    sudo('supervisorctl reread')
    sudo('supervisorctl update') 
Example #2
Source File: __init__.py    From runbook with Apache License 2.0 6 votes vote down vote up
def __action(**kwargs):
    redata = kwargs['redata']
    jdata = kwargs['jdata']
    if ShouldRun(redata, jdata):
        env.gateway = redata['data']['gateway']
        env.host_string = redata['data']['host_string']
        env.user = redata['data']['username']
        env.key = redata['data']['sshkey']
        env.disable_known_hosts = True
        env.warn_only = True
        env.abort_on_prompts = True
        env.shell = "/bin/sh -c"
        try:
            results = run_cmd(redata['data']['cmd'])
            if results.succeeded:
                return True
            else:
                raise Exception(
                    'Command Execution Failed: {0} - {1}'.format(results.return_code, results))
        except:
            raise Exception(
                'Command failed to execute') 
Example #3
Source File: init.py    From boss with MIT License 6 votes vote down vote up
def configure_env():
    ''' Configures the fabric env. '''
    config = get_config()
    stage = get_stage()
    stage_config = get_stage_config(stage)
    env.user = stage_config.get('user') or config['user']
    env.port = stage_config.get('port') or config['port']
    env.cwd = stage_config.get('cwd') or config['cwd']
    env.key_filename = stage_config.get(
        'key_filename') or config['key_filename']
    env.hosts = [stage_config['host']]
    ssh_forward_agent = stage_config.get(
        'ssh_forward_agent') or config['ssh_forward_agent']

    env.forward_agent = (
        ssh_forward_agent and
        str(ssh_forward_agent).lower() == 'true'
    )

    # If Verbose logging is turned on show verbose logs.
    verbose_logging = stage_config.get('verbose_logging') or config[
        'verbose_logging']

    if str(verbose_logging).lower() == 'true':
        set_verbose_logging() 
Example #4
Source File: fabfile.py    From commcare-cloud with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _setup_path():
    # using posixpath to ensure unix style slashes.
    # See bug-ticket: http://code.fabfile.org/attachments/61/posixpath.patch
    env.root = posixpath.join(env.home, 'www', env.deploy_env)
    env.log_dir = posixpath.join(env.home, 'www', env.deploy_env, 'log')
    env.releases = posixpath.join(env.root, 'releases')
    env.code_current = posixpath.join(env.root, 'current')
    env.code_root = posixpath.join(env.releases, env.deploy_metadata.timestamp)
    env.project_root = posixpath.join(env.code_root, env.project)
    env.project_media = posixpath.join(env.code_root, 'media')

    env.py3_virtualenv_current = posixpath.join(env.code_current, 'python_env-3.6')
    env.py3_virtualenv_root = posixpath.join(env.code_root, 'python_env-3.6')

    env.services = posixpath.join(env.code_root, 'services')
    env.db = '%s_%s' % (env.project, env.deploy_env)
    env.offline_releases = posixpath.join('/home/{}/releases'.format(env.user))
    env.offline_code_dir = posixpath.join('{}/{}'.format(env.offline_releases, 'offline'))
    env.airflow_home = posixpath.join(env.home, 'airflow')
    env.airflow_env = posixpath.join(env.airflow_home, 'env')
    env.airflow_code_root = posixpath.join(env.airflow_home, 'pipes') 
Example #5
Source File: server.py    From presto-admin with Apache License 2.0 6 votes vote down vote up
def check_server_status():
    """
    Checks if server is running for env.host. Retries connecting to server
    until server is up or till RETRY_TIMEOUT is reached

    Parameters:
        client - client that executes the query

    Returns:
        True or False
    """
    if len(get_coordinator_role()) < 1:
        warn('No coordinator defined.  Cannot verify server status.')
    with closing(PrestoClient(get_coordinator_role()[0], env.user)) as client:
        node_id = lookup_string_config('node.id', os.path.join(constants.REMOTE_CONF_DIR, 'node.properties'), env.host)

        try:
            return query_server_for_status(client, node_id)
        except RetryError:
            return False 
Example #6
Source File: server.py    From presto-admin with Apache License 2.0 6 votes vote down vote up
def collect_node_information():
    with closing(PrestoClient(get_coordinator_role()[0], env.user)) as client:
        with settings(hide('warnings')):
            error_message = check_presto_version()
        if error_message:
            external_ip = 'Unknown'
            is_running = False
        else:
            with settings(hide('warnings', 'aborts', 'stdout')):
                try:
                    external_ip = get_ext_ip_of_node(client)
                except:
                    external_ip = 'Unknown'
                try:
                    is_running = service('status')
                except:
                    is_running = False
        return external_ip, is_running, error_message 
Example #7
Source File: __init__.py    From runbook with Apache License 2.0 6 votes vote down vote up
def check(**kwargs):
    ''' Login over SSH and execute shell command '''
    jdata = kwargs['jdata']
    logger = kwargs['logger']

    env.gateway = jdata['data']['gateway']
    env.host_string = jdata['data']['host_string']
    env.user = jdata['data']['username']
    env.key = jdata['data']['sshkey']
    env.shell = "/bin/sh -c"
    env.disable_known_hosts = True
    env.warn_only = True
    env.abort_on_prompts = True
    results = run_cmd("uptime")
    if results.succeeded:
        data = results.split()
        load = float(data[-3].rstrip(","))
    else:
        return None

    logger.debug("load-average: 1 minute load average is {0}".format(load))
    if load < float(jdata['data']['threshold']):
        return True
    else:
        return False 
Example #8
Source File: utils.py    From commcare-cloud with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_command(self, hosts, parallel_pool_size=1):
        from fabric.api import execute, sudo, env, parallel
        if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)):
            env.use_ssh_config = True
        env.forward_agent = True
        # pass `-E` to sudo to preserve environment for ssh agent forwarding
        env.sudo_prefix = "sudo -SE -p '%(sudo_prompt)s' "
        env.user = self.user_name
        env.password = self.password
        env.hosts = hosts
        env.warn_only = True

        def _task():
            result = sudo(self.command, user=self.user_as)
            return result

        task = _task
        if parallel_pool_size > 1:
            task = parallel(pool_size=parallel_pool_size)(_task)

        res = execute(task)
        return res 
Example #9
Source File: 6_7_configure_Apache_for_hosting_website_remotely.py    From Python-Network-Programming-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
def setup_vhost():
    """ Setup a test website """
    print ("Preparing the Apache vhost setup...")
    print ("Setting up the document root...")
    if exists(WWW_DOC_ROOT):
        sudo("rm -rf %s" %WWW_DOC_ROOT)
    sudo("mkdir -p %s" %WWW_DOC_ROOT)
    sudo("chown -R %s.%s %s" %(env.user, env.user, WWW_DOC_ROOT))
    put(local_path="index.html", remote_path=WWW_DOC_ROOT)
    sudo("chown -R %s.%s %s" %(WWW_USER, WWW_GROUP, WWW_DOC_ROOT))
    print ("Setting up the vhost...")
    sudo("chown -R %s.%s %s" %(env.user, env.user, APACHE_SITES_PATH))
    put(local_path="vhost.conf", remote_path=APACHE_SITES_PATH)
    sudo("chown -R %s.%s %s" %('root', 'root', APACHE_SITES_PATH))
    sudo("%s restart" %APACHE_INIT_SCRIPT)
    print ("Setup complete. Now open the server path http://abc.remote-server.org/ in your web browser.") 
Example #10
Source File: __init__.py    From runbook with Apache License 2.0 6 votes vote down vote up
def check(**kwargs):
    ''' Login over SSH and execute shell command '''
    jdata = kwargs['jdata']
    logger = kwargs['logger']

    env.gateway = jdata['data']['gateway']
    env.host_string = jdata['data']['host_string']
    env.user = jdata['data']['username']
    env.key = jdata['data']['sshkey']
    env.shell = "/bin/sh -c"
    env.disable_known_hosts = True
    env.warn_only = True
    env.abort_on_prompts = True
    try:
        results = run_cmd(jdata['data']['cmd'])
        logger.debug("execute-shell-command: requested command" +
                     " returned with exit code {0}".format(results.return_code))
        if results.succeeded:
            return True
        else:
            return False
    except:
        return None 
Example #11
Source File: __init__.py    From urbanfootprint with GNU General Public License v3.0 6 votes vote down vote up
def localhost(skip_ssh=False):
    """
    Sets up a development environment to pretend that localhost is a remote server
    """
    if not skip_ssh:
        env.hosts = ['127.0.0.1']

    env.user = env.deploy_user = 'calthorpe'
    env.deploy_user = 'calthorpe'
    env.virtualenv_directory = '/srv/calthorpe_env'
    env.password = 'Calthorpe123'
    env.DATA_DUMP_PATH = '/srv/datadump'
    env.settings = 'dev'
    env.dev = True
    env.use_ssl = False
    env.client = 'default' 
Example #12
Source File: fabfile.py    From openprescribing with MIT License 5 votes vote down vote up
def npm_install():
    installed = run("if [[ -n $(which npm) ]]; then echo 1; fi")
    if not installed:
        sudo(
            "curl -sL https://deb.nodesource.com/setup_6.x |"
            "bash - && apt-get install -y  "
            "nodejs binutils libproj-dev gdal-bin libgeoip1 libgeos-c1;",
            user=env.local_user,
        )
        sudo("npm install -g browserify && npm install -g eslint", user=env.local_user) 
Example #13
Source File: 6_7_configure_Apache_for_hosting_website_remotely.py    From Python-Network-Programming-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
def remote_server():
    env.hosts = ['127.0.0.1']
    env.user = prompt('Enter user name: ')
    env.password = getpass('Enter your system password: ') 
Example #14
Source File: fabfile.py    From openprescribing with MIT License 5 votes vote down vote up
def sudo_script(script, www_user=False):
    """Run script under `deploy/fab_scripts/` as sudo.

    We don't use the `fabric` `sudo()` command, because instead we
    expect the user that is running fabric to have passwordless sudo
    access.  In this configuration, that is achieved by the user being
    a member of the `fabric` group (see `setup_sudo()`, below).

    """
    if www_user:
        sudo_cmd = "sudo -u www-data "
    else:
        sudo_cmd = "sudo "
    return run(sudo_cmd + os.path.join(env.path, "deploy/fab_scripts/%s" % script)) 
Example #15
Source File: 16_4_install_python_package_remotely.py    From Python-Network-Programming with MIT License 5 votes vote down vote up
def remote_server():
    env.hosts = ['127.0.0.1']
    env.user = prompt('Enter user name: ')
    env.password = getpass('Enter password: ') 
Example #16
Source File: 16_5_run_mysql_command_remotely.py    From Python-Network-Programming with MIT License 5 votes vote down vote up
def create_db():
    """Create a MySQL DB for App version"""
    if not env.db_name:
        db_name = prompt("Enter the DB name:")
    else:
        db_name = env.db_name
    run('echo "CREATE DATABASE %s default character set utf8 collate utf8_unicode_ci;"|mysql --batch --user=%s --password=%s --host=%s'\
         % (db_name, env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True) 
Example #17
Source File: 16_5_run_mysql_command_remotely.py    From Python-Network-Programming with MIT License 5 votes vote down vote up
def remote_server():
    env.hosts = ['127.0.0.1']
    env.user = prompt('Enter your system username: ')
    env.password = getpass('Enter your system user password: ')
    env.mysqlhost = 'localhost'
    env.mysqluser = prompt('Enter your db username: ')
    env.mysqlpassword = getpass('Enter your db user password: ')
    env.db_name = '' 
Example #18
Source File: fabfile.py    From speakerfight with MIT License 5 votes vote down vote up
def load_initial_data():
    fixtures = [
        'deck/fixtures/user.json',
        'deck/fixtures/event.json',
        'deck/fixtures/proposal.json',
    ]
    with cd(env.app_dir):
        print yellow('Collecting the initial data')
        for fixture in fixtures:
            _run('manage.py loaddata {}'.format(fixture))
        print green('Data succefully loaded') 
Example #19
Source File: __init__.py    From runbook with Apache License 2.0 5 votes vote down vote up
def __action(**kwargs):
    redata = kwargs['redata']
    jdata = kwargs['jdata']
    if ShouldRun(redata, jdata):
        env.gateway = redata['data']['gateway']
        env.host_string = redata['data']['host_string']
        env.user = redata['data']['username']
        env.key = redata['data']['sshkey']
        env.disable_known_hosts = True
        env.warn_only = True
        env.abort_on_prompts = True
        env.shell = "/bin/sh -c"
        cmd = ""
        if redata['data']['use_sudo'] == "true":
            cmd = "sudo "
        cmd = cmd + "docker run -d {0} {1}".format(redata['data']['add_options'],
                                                   redata['data']['image'])
        try:
            results = run_cmd(cmd)
            if results.succeeded:
                return True
            else:
                raise Exception(
                    'Command Execution Failed: {0} - {1}'.format(results.return_code, results))
        except:
            raise Exception(
                'Command failed to execute') 
Example #20
Source File: __init__.py    From runbook with Apache License 2.0 5 votes vote down vote up
def __action(**kwargs):
    redata = kwargs['redata']
    jdata = kwargs['jdata']
    if ShouldRun(redata, jdata):
        env.gateway = redata['data']['gateway']
        env.host_string = redata['data']['host_string']
        env.user = redata['data']['username']
        env.key = redata['data']['sshkey']
        env.disable_known_hosts = True
        env.warn_only = True
        env.abort_on_prompts = True
        env.shell = "/bin/sh -c"
        cmd = ""
        if redata['data']['use_sudo'] == "true":
            cmd = "sudo "
        cmd = cmd + "docker restart {0}".format(redata['data']['container_name'])
        try:
            results = run_cmd(cmd)
            if results.succeeded:
                return True
            else:
                raise Exception(
                    'Command Execution Failed: {0} - {1}'.format(results.return_code, results))
        except:
            raise Exception(
                'Command failed to execute') 
Example #21
Source File: __init__.py    From runbook with Apache License 2.0 5 votes vote down vote up
def __action(**kwargs):
    redata = kwargs['redata']
    jdata = kwargs['jdata']
    if ShouldRun(redata, jdata):
        env.gateway = redata['data']['gateway']
        env.host_string = redata['data']['host_string']
        env.user = redata['data']['username']
        env.key = redata['data']['sshkey']
        env.disable_known_hosts = True
        env.warn_only = True
        env.abort_on_prompts = True
        env.shell = "/bin/sh -c"
        sudo = ""
        if redata['data']['use_sudo'] == "true":
            sudo = "sudo"
        cmd = "{0} systemctl -q {1} {2} || {0} service {2} {1}".format(
            sudo, redata['data']['action'], redata['data']['service_name'])
        try:
            results = run_cmd(cmd)
            if results.succeeded:
                return True
            else:
                raise Exception(
                    'Command Execution Failed: {0} - {1}'.format(results.return_code, results))
        except:
            raise Exception(
                'Command failed to execute') 
Example #22
Source File: __init__.py    From runbook with Apache License 2.0 5 votes vote down vote up
def check(**kwargs):
    ''' Login over SSH and execute shell command '''
    jdata = kwargs['jdata']
    logger = kwargs['logger']

    env.gateway = jdata['data']['gateway']
    env.host_string = jdata['data']['host_string']
    env.user = jdata['data']['username']
    env.key = jdata['data']['sshkey']
    env.shell = "/bin/sh -c"
    env.disable_known_hosts = True
    env.warn_only = True
    env.abort_on_prompts = True
    sudo = ""
    if jdata['data']['use_sudo'] == "true":
        sudo = "sudo"
    service = jdata['data']['service_name']
    try:
        results = run_cmd("{0} service {1} status".format(sudo, service))
    except:
        return None
    if results.succeeded and "running" in results:
        return True
    else:
        try:
            results = run_cmd("{0} systemctl status {1}".format(sudo, service))
        except:
            return None
        if results.succeeded:
            return True
        else:
            return False 
Example #23
Source File: 6_5_run_mysql_command_remotely.py    From Python-Network-Programming-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
def remote_server():
    env.hosts = ['127.0.0.1']
    env.user = prompt('Enter your system username: ')
    env.password = getpass('Enter your system user password: ')
    env.mysqlhost = 'localhost'
    env.mysqluser = prompt('Enter your db username: ')
    env.mysqlpassword = getpass('Enter your db user password: ')
    env.db_name = '' 
Example #24
Source File: __init__.py    From runbook with Apache License 2.0 5 votes vote down vote up
def __action(**kwargs):
    redata = kwargs['redata']
    jdata = kwargs['jdata']
    if ShouldRun(redata, jdata):
        env.gateway = redata['data']['gateway']
        env.host_string = redata['data']['host_string']
        env.user = redata['data']['username']
        env.key = redata['data']['sshkey']
        env.disable_known_hosts = True
        env.warn_only = True
        env.abort_on_prompts = True
        env.shell = "/bin/sh -c"
        cmd = ""
        if redata['data']['use_sudo'] == "true":
            cmd = "sudo "
        cmd = cmd + "docker kill {0}".format(redata['data']['container_name'])
        try:
            results = run_cmd(cmd)
            if results.succeeded:
                return True
            else:
                raise Exception(
                    'Command Execution Failed: {0} - {1}'.format(results.return_code, results))
        except:
            raise Exception(
                'Command failed to execute') 
Example #25
Source File: 6_4_install_python_package_remotely.py    From Python-Network-Programming-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
def remote_server():
    env.hosts = ['127.0.0.1']
    env.user = prompt('Enter user name: ')
    env.password = getpass('Enter password: ') 
Example #26
Source File: topology.py    From presto-admin with Apache License 2.0 5 votes vote down vote up
def get_conf_from_fabric():
    return {'coordinator': util.get_coordinator_role()[0],
            'workers': util.get_worker_role(),
            'port': env.port,
            'username': env.user} 
Example #27
Source File: collect.py    From presto-admin with Apache License 2.0 5 votes vote down vote up
def system_info():
    """
    Gather system information like nodes in the system, presto
    version, presto-admin version, os version etc.
    """
    if env.host not in fabricapi.get_coordinator_role():
        return
    err_msg = 'Unable to access node information. ' \
              'Please check that server is up with command: server status'
    req = get_request(request_url(NODES_REQUEST_EXT), err_msg)

    downloaded_sys_info_loc = os.path.join(TMP_PRESTO_DEBUG, "sysinfo")
    try:
        os.makedirs(downloaded_sys_info_loc)
    except OSError:
        if not os.path.isdir(downloaded_sys_info_loc):
            raise

    node_info_file_name = os.path.join(downloaded_sys_info_loc, 'node_info.json')
    with open(node_info_file_name, 'w') as out_file:
        out_file.write(json.dumps(req.json(), indent=4))

    _LOGGER.debug('Gathered node information in file: ' + node_info_file_name)

    catalog_file_name = os.path.join(downloaded_sys_info_loc, 'catalog_info.txt')
    client = PrestoClient(env.host, env.user)
    catalog_info = get_catalog_info_from(client)

    with open(catalog_file_name, 'w') as out_file:
        out_file.write(catalog_info + '\n')

    _LOGGER.debug('Gathered catalog information in file: ' + catalog_file_name)

    execute(get_catalog_configs, downloaded_sys_info_loc, roles=env.roles)
    execute(get_system_info, downloaded_sys_info_loc, roles=env.roles)

    make_tarfile(OUTPUT_FILENAME_FOR_SYS_INFO, downloaded_sys_info_loc)
    print 'System info archive created: ' + OUTPUT_FILENAME_FOR_SYS_INFO 
Example #28
Source File: server.py    From presto-admin with Apache License 2.0 5 votes vote down vote up
def wait_for_presto_user():
    ret = sudo('getent passwd presto', quiet=True)
    if not ret.succeeded:
        raise Exception('Presto package was not installed successfully. '
                        'Presto user was not created.') 
Example #29
Source File: fabfile.py    From ansible-mezzanine with MIT License 5 votes vote down vote up
def postgres(command):
    """
    Runs the given command as the postgres user.
    """
    show = not command.startswith("psql")
    return run("sudo -u root sudo -u postgres %s" % command, show=show) 
Example #30
Source File: fabfile.py    From serapis with MIT License 5 votes vote down vote up
def pack():
    # Make sure machine and dev tools are up to date
    sudo('sudo yum -y update')
    sudo('sudo yum -y upgrade')
    sudo('yum install -y atlas-devel atlas-sse3-devel blas-devel gcc gcc-c++ lapack-devel python27-devel --enablerepo=epel')
    sudo('pip install -U pip')

    with warn_only():
        run('rm ~/wordnik.zip')

    sudo('dd if=/dev/zero of=/swapfile bs=1024 count=1500000')
    sudo('mkswap /swapfile')
    sudo('chmod 0600 /swapfile')
    sudo('swapon /swapfile')

    run('/usr/bin/virtualenv --python /usr/bin/python build --always-copy --no-site-packages')
    run('source build/bin/activate')

    # Order is important here, so let's make sure we've got these right
    run('pip install -U pip')
    run('pip install --use-wheel numpy')
    run('pip install --use-wheel scipy')
    run('pip install --use-wheel sklearn')
    run('pip install --use-wheel pandas')

    with open('requirements.txt') as f:
        for req in f.read().splitlines():
            if req.split("=")[0].lower() not in ('numpy', 'scipy', 'scikit-learn', 'sklearn', 'pandas'):
                run('pip install --use-wheel {}'.format(req))

    for lib in ('lib', 'lib64'):
        # Strip SO files
        run('find "$VIRTUAL_ENV/{}/python2.7/site-packages/" -name "*.so" | xargs strip'.format(lib))
        with cd('$VIRTUAL_ENV/{}/python2.7/site-packages/'.format(lib)):
            run('zip -r -9 -q ~/wordnik.zip *')

    # Get the file back onto our local machine
    local('scp %s@%s:~/wordnik.zip %s' % (env.user, env.hosts[0], lambdafile))
    update()