Python fabric.api.env.host_string() Examples

The following are 30 code examples of fabric.api.env.host_string(). 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: __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 #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: base.py    From docker-fabric with MIT License 6 votes vote down vote up
def get_connection(self, *args, **kwargs):
        """
        Create a new connection, or return an existing one from the cache. Uses Fabric's current ``env.host_string``
        and the URL to the Docker service.

        :param args: Additional arguments for the client constructor, if a new client has to be instantiated.
        :param kwargs: Additional keyword args for the client constructor, if a new client has to be instantiated.
        """
        key = env.get('host_string'), kwargs.get('base_url', env.get('docker_base_url'))
        default_config = _get_default_config(None)
        if default_config:
            if key not in self:
                self[key] = default_config
            return default_config.get_client()
        config = self.get_or_create_connection(key, self.configuration_class, *args, **kwargs)
        return config.get_client() 
Example #4
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 6 votes vote down vote up
def _dseperf():
    """ based on updated DSE performance doc from 4/23/2015 """
    sudo("add-apt-repository -y ppa:webupd8team/java")
    sudo("apt-get update")
    sudo("apt-get install -y oracle-java8-installer oracle-java8-set-default")

    sudo("echo tsc > /sys/devices/system/clocksource/clocksource0/current_clocksource")

    sudo("apt-get install -y schedtool")

    #sudo('echo "vm.dirty_expire_centisecs = 10" >> /etc/sysctl.d/dirty.conf')
    #sudo("sysctl -p /etc/sysctl.d/dirty.conf")

    # FIX HOSTS FILE for metrics to record remote IP properly
    host  = 'ip-%s' % env.host_string.replace('.','-')
    sudo('sed -i "/127/c\\127.0.0.1 {} localhost" /etc/hosts'.format(host)) 
Example #5
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 6 votes vote down vote up
def _bootstrapcass():

    # FIX HOSTS FILE for metrics to record remote IP properly
    host  = 'ip-%s' % env.host_string.replace('.','-')
    sudo('sed -i "/127/c\\127.0.0.1 {} localhost" /etc/hosts'.format(host))

    sudo("apt-get update")

    # install required packages
    sudo("""apt-get -y install --fix-missing libjna-java binutils pssh pbzip2 xfsprogs schedtool zip unzip ruby openssl ruby-dev libruby1.9.1 curl liblzo2-dev ntp subversion python-pip  unzip xfsprogs ethtool""")

    # install sysadmin tools
    sudo("apt-get -y install --fix-missing iftop sysstat htop s3cmd nethogs nmon dstat tree collectd collectd-utils")

    execute(_installjava)

    #fix clocksource -> network performance
    sudo("echo tsc > /sys/devices/system/clocksource/clocksource0/current_clocksource") 
Example #6
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 #7
Source File: apiclient.py    From docker-fabric with MIT License 6 votes vote down vote up
def _get_connection_args(base_url, remote_port, local_port):
    if env.host_string:
        if base_url:
            proto_idx = base_url.find(':/')
            if proto_idx >= 0:
                proto = base_url[:proto_idx]
                address = base_url[proto_idx + 2:]
                if proto in ('http+unix', 'unix'):
                    if address[:3] == '//':
                        address = address[1:]
                    elif address[0] != '/':
                        address = ''.join(('/', address))
                    return _get_socat_tunnel(address, local_port)
                return _get_local_tunnel(address.lstrip('/'), remote_port, local_port)
            elif base_url[0] == '/':
                return _get_socat_tunnel(base_url, local_port)
            return _get_local_tunnel(base_url, remote_port, local_port)
        return _get_socat_tunnel(DEFAULT_SOCKET, local_port)
    return base_url, None 
Example #8
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 6 votes vote down vote up
def _yamlfile(config):
    host = env.host_string
    src_file = "configs/{}/cassandra.yaml".format(config)
    green("Syncing YAML File from [{}]".format(src_file))
    dest_file = "{}/cassandra.yaml".format(config_dir)
    put(src_file, dest_file, use_sudo=True)
    aftertext = "rpc_address: {}".format(host)
    sed(dest_file,before='rpc_address: \$HOST',after=aftertext,use_sudo=True,backup=".bak")

    aftertext = "listen_address: {}".format(host)
    sed(dest_file,before='listen_address: \$HOST',after=aftertext,use_sudo=True,backup='')

    # grab the first 3 hosts in the host file and use as seed nodes, could be improved
    seed_str = ",".join(env.hosts[0:3])
    aftertext = 'seeds: "{}"'.format(seed_str)
    sed(dest_file,before='seeds: \$SEEDS',after=aftertext,use_sudo=True,backup='') 
Example #9
Source File: base.py    From docker-fabric with MIT License 5 votes vote down vote up
def get_current_roles():
    """
    Determines the list of roles, that the current host is assigned to. If ``env.roledefs`` is not set, an empty list
    is returned.

    :return: List of roles of the current host.
    :rtype: list
    """
    current_host = env.host_string
    roledefs = env.get('roledefs')
    if roledefs:
        return [role for role, hosts in six.iteritems(roledefs) if current_host in hosts]
    return [] 
Example #10
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 #11
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 pull {0}".format(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 #12
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
    cmd = ""
    if jdata['data']['use_sudo'] == "true":
        cmd = "sudo "
    cmd = cmd + "docker inspect {0}".format(jdata['data']['container_name'])
    try:
        results = run_cmd(cmd)
    except:
        return None
    logger.debug("docker-container-running: requested command" +
                 " returned with exit code {0}".format(results.return_code))
    if results.succeeded:
        container_data = json.loads(results)
        if "State" not in container_data[0]:
            return False
        logger.debug("docker-container-running: container state" +
                     " returned running {0}".format(container_data[0]['State']['Running']))
        if container_data[0]['State']['Running']:
            return True
        else:
            return False
    else:
        return False 
Example #13
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 #14
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 #15
Source File: base.py    From docker-fabric with MIT License 5 votes vote down vote up
def _get_default_config(client_configs):
    clients = client_configs or env.get('docker_clients')
    host_string = env.get('host_string')
    if not host_string or not clients:
        return None
    for c in clients.values():
        host = c.get('fabric_host')
        if host == host_string:
            return c
    return None 
Example #16
Source File: base.py    From docker-fabric with MIT License 5 votes vote down vote up
def get_client(self):
        if 'fabric_host' in self:
            with settings(host_string=self.fabric_host):
                return super(FabricClientConfiguration, self).get_client()
        return super(FabricClientConfiguration, self).get_client() 
Example #17
Source File: fabutils.py    From bsdploy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def rsync_project(*args, **kwargs):
    from bsdploy import log
    log.warning("rsync_project only works properly with direct ssh connections, you should use the rsync helper instead.")
    ssh_info = env.instance.init_ssh_key()
    ssh_info.pop('host')
    ssh_args = env.instance.ssh_args_from_info(ssh_info)
    kwargs['ssh_opts'] = '%s %s' % (kwargs.get('ssh_opts', ''), shjoin(ssh_args))
    with env.instance.fabric():
        env.host_string = "{user}@{host}".format(
            user=env.instance.config.get('user', 'root'),
            host=env.instance.config.get(
                'host', env.instance.config.get(
                    'ip', env.instance.uid)))
        _rsync_project(*args, **kwargs) 
Example #18
Source File: fabutils.py    From bsdploy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def rsync(*args, **kwargs):
    """ wrapper around the rsync command.

        the ssh connection arguments are set automatically.

        any args are just passed directly to rsync.
        you can use {host_string} in place of the server.

        the kwargs are passed on the 'local' fabric command.
        if not set, 'capture' is set to False.

        example usage:
        rsync('-pthrvz', "{host_string}:/some/src/directory", "some/destination/")
    """
    kwargs.setdefault('capture', False)
    replacements = dict(
        host_string="{user}@{host}".format(
            user=env.instance.config.get('user', 'root'),
            host=env.instance.config.get(
                'host', env.instance.config.get(
                    'ip', env.instance.uid))))
    args = [x.format(**replacements) for x in args]
    ssh_info = env.instance.init_ssh_key()
    ssh_info.pop('host')
    ssh_info.pop('user')
    ssh_args = env.instance.ssh_args_from_info(ssh_info)
    cmd_parts = ['rsync']
    cmd_parts.extend(['-e', "ssh %s" % shjoin(ssh_args)])
    cmd_parts.extend(args)
    cmd = shjoin(cmd_parts)
    return local(cmd, **kwargs) 
Example #19
Source File: fabfile_daemonology.py    From bsdploy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def bootstrap(**kwargs):
    """ Bootstrap an EC2 instance that has been booted into an AMI from http://www.daemonology.net/freebsd-on-ec2/
    Note: deprecated, current AMI images are basically pre-bootstrapped, they just need to be configured.
    """
    # the user for the image is `ec2-user`, there is no sudo, but we can su to root w/o password
    original_host = env.host_string
    env.host_string = 'ec2-user@%s' % env.instance.uid
    bootstrap_files = env.instance.config.get('bootstrap-files', 'bootstrap-files')
    put('%s/authorized_keys' % bootstrap_files, '/tmp/authorized_keys')
    put(join(bsdploy_path, 'enable_root_login_on_daemonology.sh'), '/tmp/', mode='0775')
    run("""su root -c '/tmp/enable_root_login_on_daemonology.sh'""")
    # revert back to root
    env.host_string = original_host
    # give sshd a chance to restart
    sleep(2)
    run('rm /tmp/enable_root_login_on_daemonology.sh')

    # allow overwrites from the commandline
    env.instance.config.update(kwargs)

    bu = BootstrapUtils()
    bu.ssh_keys = None
    bu.upload_authorized_keys = False
    bu.bootstrap_files_yaml = 'daemonology-files.yml'
    bu.print_bootstrap_files()

    bu.create_bootstrap_directories()
    bu.upload_bootstrap_files({})
    # we need to install python here, because there is no way to install it via
    # ansible playbooks
    bu.install_pkg('/', chroot=False, packages=['python27']) 
Example #20
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def _start_cass(sleep):
    with settings(warn_only=True):
        print "Starting {} with a sleep of {}".format(env.host_string, sleep)
        sudo("service cassandra start")
        time.sleep(float(sleep)) 
Example #21
Source File: fabfile.py    From learning-python with MIT License 5 votes vote down vote up
def restart():
    env.host_string = config.HOST_STRING
    run('supervisorctl restart test') 
Example #22
Source File: fabfile.py    From Flask-Boost with MIT License 5 votes vote down vote up
def deploy():
    env.host_string = config.HOST_STRING
    with cd('/var/www/#{project}'):
        with shell_env(MODE='PRODUCTION'):
            run('git reset --hard HEAD')
            run('git pull')
            run('npm install')
            run('gulp')
            with prefix('source venv/bin/activate'):
                run('pip install -r requirements.txt')
                run('python manage.py db upgrade')
                run('python manage.py build')
            run('supervisorctl restart #{project}') 
Example #23
Source File: fabfile.py    From Flask-Boost with MIT License 5 votes vote down vote up
def restart():
    env.host_string = config.HOST_STRING
    run('supervisorctl restart #{project}') 
Example #24
Source File: fabfile.py    From volontulo with MIT License 5 votes vote down vote up
def update():
    u"""Function defining all steps required to properly update application."""

    # Django app refresh:
    with cd('/var/www/volontulo'):
        run('git checkout -f {}'.format(env_vars[env.host_string]['git_branch']))
        run('git pull')

    with contextlib.nested(
        prefix('workon volontulo'),
        cd('/var/www/volontulo/backend'),
    ):
        run('pip install --upgrade -r requirements/base.txt')

    # Django site refresh:
    with contextlib.nested(
        cd('/var/www/volontulo/backend'),
        prefix('workon volontulo')
    ):
        run('python manage.py migrate --traceback')

    # Angular assets refresh:
    with contextlib.nested(
        prefix('nvm use {}'.format(NODE_VERSION)),
        cd('/var/www/volontulo/frontend'),
    ):
        run('npm install .')
        run('$(npm bin)/ng build --prod --env={}'.format(env.host_string))
        run('$(npm bin)/ng build --prod --env={} --app 1 --output-hashing=false'.format(env.host_string))
        run('./node_modules/.bin/webpack --config webpack.server.config.js --progress --colors')

    run('systemctl restart uwsgi.service')
    run('systemctl restart nginx')
    run('systemctl restart pm2-www-data.service') 
Example #25
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def _restart_cass(sleep):
    with settings(warn_only=True):
        print "Restarting {} with a sleep of {}".format(env.host_string, sleep)
        sudo("service cassandra restart")
        time.sleep(float(sleep))

#@parallel 
Example #26
Source File: fabfile.py    From learning-python with MIT License 5 votes vote down vote up
def deploy():
    env.host_string = config.HOST_STRING
    with cd('/var/www/test'):
        with shell_env(MODE='PRODUCTION'):
            run('git reset --hard HEAD')
            run('git pull')
            run('npm install')
            run('gulp')
            with prefix('source venv/bin/activate'):
                run('pip install -r requirements.txt')
                run('python manage.py db upgrade')
                run('python manage.py build')
            run('supervisorctl restart test') 
Example #27
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def _restart_dse(sleep):
    with settings(warn_only=True):
        print "Restarting {} with a sleep of {}".format(env.host_string, sleep)
        sudo("service dse restart")
        time.sleep(float(sleep)) 
Example #28
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def _envfile(config):
    src_file = "configs/{}/cassandra-env.sh".format(config)
    green("Syncing ENV File from [{}]".format(src_file))

    dest_file = "{}/cassandra-env.sh".format(config_dir)
    put(src_file, dest_file, use_sudo=True)
    aftertext = 'rmi.server.hostname={}"'.format(env.host_string)
    sed(dest_file,before='rmi.server.hostname=\$HOST"',after=aftertext,use_sudo=True,backup='') 
Example #29
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def _agentip(config):
    ''' Use this task to control what IP is used for opscenter '''

    src_file = "configs/{}/address.yaml".format(config)
    dest_file = "/var/lib/datastax-agent/conf/address.yaml"

    put(src_file, dest_file, use_sudo=True)

    aftertext = 'hosts: ["{}"]'.format(env.host_string)
    sed(dest_file,before='hosts: \["\$HOST"\]',after=aftertext,use_sudo=True,backup='')


    #sed("/var/lib/datastax-agent/conf/address.yaml", \
        #before="^stomp_interface: .*$", \
        #after="stomp_interface: {}".format(ip), use_sudo=True, backup='.bak') 
Example #30
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')