Python fabric.api.execute() Examples

The following are 30 code examples of fabric.api.execute(). 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 , or try the search function .
Example #1
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 6 votes vote down vote up
def getrunning(config):
    """
    Gets all the running Cassandra processes so you know if C* is fully running across the cluster
    """
    _set_hosts(config)
    results = execute(_getpid)
    i = 1
    completed = 0
    for host, result in results.items():
        print "host {} [{}] pid is [{}]".format(i, host, result.strip())
        i += 1
        if len(result) > 1:
            completed += 1

    print "-"*50
    print "{} out of {} hosts are running C*".format(completed, len(results))
    print "-"*50 
Example #2
Source File: __init__.py    From automation-tools with GNU General Public License v3.0 6 votes vote down vote up
def configure_idm_external_auth(idm_password=None):
    """Configure the Satellite6 Server for External Authentication.

    Expects the following environment variables:

    IDM_PASSWORD
        IDM Server Password to fetch a token.

    """
    result = run('id admin')
    if result.failed:
        print('Please execute enroll_idm before configuring External Auth')
        sys.exit(1)
    if idm_password is None:
        idm_password = os.environ.get('IDM_PASSWORD')
    run('echo {0} | kinit admin'.format(idm_password))
    run('ipa service-add HTTP/$(hostname)')
    run('satellite-installer --foreman-ipa-authentication=true')
    run('foreman-maintain service restart') 
Example #3
Source File: test_tasks.py    From fabricio with MIT License 6 votes vote down vote up
def test_rollback(self, revert, migrate_back):
        tasks_list = tasks.DockerTasks(service=TestContainer(), hosts=['host'])
        rollback = mock.Mock()
        rollback.attach_mock(migrate_back, 'migrate_back')
        rollback.attach_mock(revert, 'revert')
        revert.return_value = True

        # with migrate_back disabled
        tasks_list.rollback.name = '{0}__migrate_disabled'.format(self)
        fab.execute(tasks_list.rollback, migrate_back='no')
        migrate_back.assert_not_called()
        revert.assert_called_once()
        rollback.reset_mock()

        # default case
        tasks_list.rollback.name = '{0}__default'.format(self)
        fab.execute(tasks_list.rollback)
        self.assertListEqual(
            [mock.call.migrate_back(), mock.call.revert()],
            rollback.mock_calls,
        )
        rollback.reset_mock() 
Example #4
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 #5
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 #6
Source File: fabfile.py    From fabricio with MIT License 6 votes vote down vote up
def swarm_init():
    """
    enable Docker swarm mode
    """
    def init():
        if not init.join_command:
            fabricio.run(
                'docker swarm init --advertise-addr {0}'.format(fab.env.host),
                ignore_errors=True,
                quiet=False,
            )
            join_token = fabricio.run(
                'docker swarm join-token --quiet manager',
                ignore_errors=True,
            )
            init.join_command = (
                'docker swarm join --token {join_token} {host}:2377'
            ).format(join_token=join_token, host=fab.env.host)
        else:
            fabricio.run(init.join_command, ignore_errors=True, quiet=False)

    init.join_command = None
    with fab.settings(hosts=hosts):
        fab.execute(init) 
Example #7
Source File: tasks.py    From fabricio with MIT License 6 votes vote down vote up
def execute(*args, **kwargs):  # pragma: no cover
    warnings.warn(
        'fabricio.execute() is deprecated in favour of fabric.api.execute()',
        DeprecationWarning,
    )
    warnings.warn(
        'fabricio.execute() is deprecated and will be removed in v0.6, '
        'use fabric.api.execute() instead',
        RuntimeWarning, stacklevel=2,
    )
    try:
        task, args = args[0], args[1:]
    except IndexError:
        raise TypeError('must provide task to execute')
    default_name = '{command}.{task_name}({id})'.format(
        command=fab.env.command,
        task_name=getattr(task, 'name', task.__name__),
        id=id(task),
    )
    with utils.patch(task, 'name', get_task_name(task) or default_name):
        return fab.execute(task, *args, **kwargs) 
Example #8
Source File: fabfile.py    From fabricio with MIT License 6 votes vote down vote up
def swarm_init():
    """
    enable Docker swarm mode
    """
    def init():
        if not init.join_command:
            fabricio.run(
                'docker swarm init --advertise-addr {0}'.format(fab.env.host),
                ignore_errors=True,
                quiet=False,
            )
            join_token = fabricio.run(
                'docker swarm join-token --quiet manager',
                ignore_errors=True,
            )
            init.join_command = (
                'docker swarm join --token {join_token} {host}:2377'
            ).format(join_token=join_token, host=fab.env.host)
        else:
            fabricio.run(init.join_command, ignore_errors=True, quiet=False)

    init.join_command = None
    with fab.settings(hosts=hosts):
        fab.execute(init) 
Example #9
Source File: test_decorators.py    From fabricio with MIT License 5 votes vote down vote up
def test_skip_unknown_host(self):

        mocked_task = mock.Mock()

        @fabricio.skip_unknown_host
        def task():
            mocked_task()

        with fab.settings(fab.hide('everything')):
            fab.execute(task)
            mocked_task.assert_not_called()

            fab.execute(task, host='host')
            mocked_task.assert_called_once() 
Example #10
Source File: tasks.py    From fabricio with MIT License 5 votes vote down vote up
def deploy(self, tag=None, force=False, backup=False, migrate=True):
        """
        deploy service (prepare -> push -> backup -> pull -> migrate -> update)
        """
        self.prepare(tag=tag)
        self.push(tag=tag)
        fab.execute(
            self.upgrade,
            tag=tag,
            force=force,
            backup=backup,
            migrate=migrate,
        ) 
Example #11
Source File: __init__.py    From automation-tools with GNU General Public License v3.0 5 votes vote down vote up
def configure_realm(admin_password=None, keytab_url=None, realm=None,
                    idm_server_ip=None):
    """Configure the Satellite6 Server for REALM Integration

    Expects the following environment variables:

    IDM_SERVER_IP
        IP Address of the IDM Server.
    VM_DOMAIN
        The domain name of the IDM Server.
    KEYTAB_URL
        The URL from which to fetch the Keytab file.
    ADMIN_PASSWORD
        The admin password for Satellite 6.

    """
    if idm_server_ip is None:
        idm_server_ip = os.environ.get('IDM_SERVER_IP')
    domain = os.environ.get('VM_DOMAIN')
    result = run('id admin')
    if result.failed:
        print('Please execute enroll_idm before configuring External Auth')
        sys.exit(1)
    if keytab_url is None:
        keytab_url = os.environ.get('KEYTAB_URL')
    if admin_password is None:
        admin_password = os.environ.get('ADMIN_PASSWORD', 'changeme')
    if realm is None:
        realm = domain.upper()
    run('yum -y --disableplugin=foreman-protector install wget')
    run('wget -O /root/freeipa.keytab {0}'.format(keytab_url))
    run('mv /root/freeipa.keytab /etc/foreman-proxy')
    run('chown foreman-proxy:foreman-proxy /etc/foreman-proxy/freeipa.keytab')
    run('satellite-installer --foreman-proxy-realm true '
        '--foreman-proxy-realm-principal realm-proxy@{0} '
        '--foreman-proxy-dhcp-nameservers {1}'.format(realm, idm_server_ip))
    run('cp /etc/ipa/ca.crt /etc/pki/ca-trust/source/anchors/ipa.crt')
    run('update-ca-trust enable ; update-ca-trust')
    run('service foreman-proxy restart') 
Example #12
Source File: test_tasks.py    From fabricio with MIT License 5 votes vote down vote up
def test_destroy(self, confirm, destroy):
        service = docker.Container(name='name')
        tasks_list = tasks.DockerTasks(service=service)
        cases = dict(
            explicit=dict(
                execute=tasks_list.destroy,
                expected_calls=[mock.call.destroy('args', kwargs='kwargs')],
            ),
            default=dict(
                execute=tasks_list.destroy.default,
                expected_calls=[
                    mock.call.confirm(mock.ANY, default=mock.ANY),
                    mock.call.destroy('args', kwargs='kwargs'),
                ],
            ),
            confirm=dict(
                execute=tasks_list.destroy.confirm,
                expected_calls=[mock.call.destroy('args', kwargs='kwargs')],
            ),
        )
        calls = mock.Mock()
        calls.attach_mock(destroy, 'destroy')
        calls.attach_mock(confirm, 'confirm')
        for case, data in cases.items():
            with self.subTest(case):
                calls.reset_mock()
                fab.execute(data['execute'], 'args', kwargs='kwargs')
                self.assertListEqual(data['expected_calls'], calls.mock_calls) 
Example #13
Source File: test_tasks.py    From fabricio with MIT License 5 votes vote down vote up
def test_infrastructure(self):
        class AbortException(Exception):
            pass

        def task():
            pass

        cases = dict(
            default=dict(
                decorator=tasks.infrastructure,
                expected_infrastructure='task',
            ),
            invoked=dict(
                decorator=tasks.infrastructure(),
                expected_infrastructure='task',
            ),
        )

        with fab.settings(abort_on_prompts=True, abort_exception=AbortException):
            with mock.patch.object(fab, 'abort', side_effect=AbortException):
                for case, data in cases.items():
                    with self.subTest(case=case):
                        decorator = data['decorator']
                        infrastructure = decorator(task)

                        self.assertTrue(is_task_object(infrastructure.confirm))
                        self.assertTrue(is_task_object(infrastructure.default))

                        fab.execute(infrastructure.confirm)
                        self.assertEqual(data['expected_infrastructure'], fab.env.infrastructure)

                        fab.env.infrastructure = None
                        with mock.patch.object(console, 'confirm', side_effect=[True, False]):
                            fab.execute(infrastructure.default)
                            self.assertEqual(data['expected_infrastructure'], fab.env.infrastructure)
                            with self.assertRaises(AbortException):
                                fab.execute(infrastructure.default) 
Example #14
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def putstress():
    """ Puts a stress yaml file on the stress runner boxes """
    _set_hosts_stress()
    execute(_putstress) 
Example #15
Source File: tasks.py    From fabricio with MIT License 5 votes vote down vote up
def confirm(self, *args, **kwargs):
            """
            delete service skipping confirmation dialog
            """
            self.run.hosts = self.hosts
            self.run.roles = self.roles
            return fab.execute(self.run, *args, **kwargs) 
Example #16
Source File: fabfile.py    From fabricio with MIT License 5 votes vote down vote up
def k8s_init():
    """
    create Kubernetes cluster
    """
    def init():
        if not init.join_command:
            initialization = list(filter(None, fabricio.run(
                'kubeadm init '
                '--apiserver-advertise-address {0} '
                '--pod-network-cidr 10.244.0.0/16'
                ''.format(fab.env.host),
                sudo=True,
                quiet=False,
            ).splitlines()))
            init.join_command = initialization[-1].strip()

            # master setup
            fabricio.run('mkdir -p $HOME/.kube')
            fabricio.run('cp /etc/kubernetes/admin.conf /home/vagrant/.kube/config', sudo=True)
            fabricio.run('chown vagrant /home/vagrant/.kube/config', sudo=True)

            # install Kubernetes network plugin
            fabricio.run(
                'kubectl apply --filename /vagrant/kube-rbac.yml '
                '&& kubectl apply --filename /vagrant/kube-canal.yml --validate=false',
                quiet=False,
            )
        else:
            fabricio.run(init.join_command, quiet=False, sudo=True)

    init.join_command = None
    with fab.settings(hosts=hosts):
        fab.execute(init) 
Example #17
Source File: ssh.py    From clusterdock with Apache License 2.0 5 votes vote down vote up
def quiet_ssh(command, hosts, ssh_key):
    """Execute command over SSH on hosts, suppressing all output. This is useful for instances where
    you may only want to see if a command succeeds or times out, since stdout is otherwise
    discarded."""
    return execute(_quiet_task, command=command, hosts=hosts, ssh_key=ssh_key) 
Example #18
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def dirty(config):
    _set_hosts(config)
    execute(_dirty, config=config) 
Example #19
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def _installstress():
     ''' installs a stress files on stress machines '''
     src = "../stress/CASSANDRA-STRESS-2.1.9.tgz"
     dest = "~/STRESS.tgz"
     put(src, dest)
     run("tar -xzf ~/STRESS.tgz")
     execute(_putstress)
     execute(_installjava) 
Example #20
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def maskcpu(config):
    """
    Mask out the 0 CPU. You can only run this one while cassandra is runnning, because you need the PID
    """
    _set_hosts(config)
    execute(_maskCPU) 
Example #21
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def push_jar(config):
    """ Push a custom jar up to the servers """
    _set_hosts(config)
    execute(push_jar_impl, config=config) 
Example #22
Source File: ssh.py    From clusterdock with Apache License 2.0 5 votes vote down vote up
def ssh(command, hosts, ssh_key):
    """Execute command over SSH on hosts."""
    return execute(_task, command=command, hosts=hosts, ssh_key=ssh_key) 
Example #23
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def installstress():
    """ Installs the Stress code and runner files """
    _set_hosts_stress()
    execute(_installstress) 
Example #24
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def restart_agent(config):
    """ Restart Datastax Opscenter Agent """
    _set_hosts(config)
    execute(_restart_agent) 
Example #25
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def start(config):
    """ Start DSE only """
    _set_hosts(config)
    execute(_start) 
Example #26
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def stop(config):
    """
    Stop DSE across the hosts
    """
    _set_hosts(config)
    execute(_stop) 
Example #27
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def start_cass(config, sleep=1):
    """ Restart Cassandra only """
    _set_hosts(config)
    execute(_start_cass, sleep=sleep) 
Example #28
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def restart_cass(config, sleep=0):
    """ Restart Cassandra only """
    _set_hosts(config)
    execute(_restart_cass, sleep=sleep) 
Example #29
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def set_seeds(config, seeds):
    """
    Manually set the seeds you want to use for your yaml file:  fab -i ~/.ssh/id_cass -u ubuntu -P set_seeds:config=c4-ebs-hvm,seeds='10.10.10.x'
    """
    _set_hosts(config)
    execute(_set_seeds, seeds=seeds) 
Example #30
Source File: fabfile.py    From cassandra-tools with Apache License 2.0 5 votes vote down vote up
def opscenter_address(config):
    """
    set the opscenter address in the address.yaml file for the datastax agent
    """
    _set_hosts(config)
    execute(_agentip, config=config)