Python fabric.Connection() Examples

The following are 30 code examples of fabric.Connection(). 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 , or try the search function .
Example #1
Source File: fabfile.py    From hknweb with MIT License 6 votes vote down vote up
def setup(c: Connection, commit=None, release=None):
    print('== Setup ==')
    if release is None:
        c.release = timestamp(c)
    else:
        c.release = release
    c.deploy_path = path.deploy_path(c)
    c.repo_path = path.repo_path(c)
    c.releases_path = path.releases_path(c)
    c.current_path = path.current_path(c)
    c.shared_path = path.shared_path(c)
    c.release_path = path.release_path(c)
    if commit is None:
        c.commit = c.deploy.branch
    else:
        c.commit = commit
    print('release: {}'.format(c.release))
    print('commit: {}'.format(c.commit))
    create_dirs(c)
    if not path.file_exists(c, '{}/venv/bin/activate'.format(c.shared_path)):
        create_venv(c) 
Example #2
Source File: dlab.py    From incubator-dlab with Apache License 2.0 6 votes vote down vote up
def ssh(ip, name, pkey):
        attempt = 0
        while attempt < 12:
            logging.info('connection attempt {}'.format(attempt))
            connection = Connection(
                host=ip,
                user=name,
                connect_kwargs={'key_filename': pkey,
                                'allow_agent': False,
                                'look_for_keys': False,
                                })
            try:
                connection.run('ls')
                return connection
            except Exception as ex:
                logging.error(ex)
                attempt += 1
                time.sleep(10) 
Example #3
Source File: device.py    From integration with Apache License 2.0 6 votes vote down vote up
def __init__(self, host_string="localhost:8822", user="root"):
        """Create a MenderDevice object-

        Keyword arguments:
        host_string -- Remote SSH host of the form host:port
        user -- Remote SSH user
        """
        self.host, self.port = host_string.split(":")
        self.user = user
        self._conn = Connection(
            host=self.host,
            user=self.user,
            port=self.port,
            connect_timeout=60,
            connect_kwargs={"password": "", "banner_timeout": 60, "auth_timeout": 60},
        )
        self._conn.client.set_missing_host_key_policy(IgnorePolicy())
        self._service_name = None 
Example #4
Source File: setup.py    From redisai-examples with MIT License 6 votes vote down vote up
def setup_master(master: str, passphrase: str, user: str) -> None:
    connect_kwargs = {'passphrase': passphrase}
    node = Connection(master, user=user, connect_kwargs=connect_kwargs)
    home = get_home_path(node)[0]
    # install(node)  # Not calling because of Fabric bug

    # redis conf
    logfile = f'logfile "{home}/redis.log"'
    node.put('redis_configs/redis.conf', 'redis-stable/redis.conf')
    node.run(f'echo {logfile} >> redis-stable/redis.conf')

    # sentinal conf
    logfile = f'logfile "{home}/sentinel.log"'
    sentinel_monitor = f'sentinel monitor mymaster {master} 6379 2'
    node.put('redis_configs/sentinel.conf', 'redis-stable/sentinel.conf')
    node.run(f'echo {logfile} >> redis-stable/sentinel.conf')
    node.run(f"sed -i 's/placeholder-line/{sentinel_monitor}/g' redis-stable/sentinel.conf")
    # bring_up_server(node)  # Not calling because of Fabric bug 
Example #5
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def symlink_release(c: Connection):
    print('-- Symlinking current@ to release')
    c.run('ln -sfn {} {}'.format(c.release_path, c.current_path), echo=True) 
Example #6
Source File: deploy_docs.py    From ciscoconfparse with GNU General Public License v3.0 5 votes vote down vote up
def deploy_ccp_docs(ccp_doc_root="public_html/py/ciscoconfparse",
    ccp_bundle_name="ccp.tar.gz", doc_host=""):

    # Run 'make html' in directory: sphinx-doc/
    run('cd sphinx-doc && make html')  # local command

    run('cd sphinx-doc/_build/html && tar cvfz {0} *'.format(
        os.path.expanduser("~/"+ccp_bundle_name)))

    # Run 'make clean' in directory: sphinx-doc/
    run('cd sphinx-doc && make clean')  # local command

    # ssh with a password...
    conn = Connection('mpenning@{}'.format(doc_host),
        connect_kwargs={"password": getpass()})
    conn.put(local=os.path.expanduser("~/{0}".format(ccp_bundle_name)), 
        remote=ccp_bundle_name)

    # Delete all the old files
    conn.run("rm -rf {0}/*".format(ccp_doc_root))
    # Move the new files to ccp_doc_root
    conn.run("mv {0} {1}".format(ccp_bundle_name, ccp_doc_root))

    with conn.cd(ccp_doc_root):
        conn.run("tar xvfz {0}".format(ccp_bundle_name))
        conn.run("rm {0}".format(ccp_bundle_name)) 
Example #7
Source File: path.py    From hknweb with MIT License 5 votes vote down vote up
def shared_path(c: Connection) -> str:
    return posixpath.join(deploy_path(c), c.deploy.path.shared) 
Example #8
Source File: path.py    From hknweb with MIT License 5 votes vote down vote up
def release_path(c: Connection) -> str:
    return posixpath.join(releases_path(c), c.release) 
Example #9
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def create_dirs(c: Connection):
    dirs = (
        c.repo_path,
        c.deploy_path,
        c.releases_path,
        c.shared_path,
        c.release_path,
    )
    for d in dirs:
        c.run('mkdir -p {}'.format(d)) 
Example #10
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def create_release(c: Connection):
    print('-- Creating release')
    git.check(c)
    git.update(c)
    c.commit = git.revision_number(c, c.commit)
    git.create_archive(c) 
Example #11
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def symlink_shared(c: Connection):
    print('-- Symlinking shared files')
    with c.cd(c.release_path):
        c.run('ln -s {}/venv ./.venv'.format(c.shared_path), echo=True) 
Example #12
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def install_deps(c: Connection):
    print('-- Installing dependencies')
    with c.cd(c.release_path):
        c.run('source .venv/bin/activate && pipenv install --deploy', echo=True, env={'PIPENV_VENV_IN_PROJECT': 'true'}) 
Example #13
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def django_migrate(c: Connection):
    print('-- Migrating tables')
    with c.cd(c.release_path):
        c.run('HKNWEB_MODE=prod .venv/bin/python ./manage.py migrate') 
Example #14
Source File: __init__.py    From kitten with MIT License 5 votes vote down vote up
def __init__(self, host, user, timeout, key_filename, color):
        self.host = host
        self.color = color
        self.conn = fabric.Connection(
            host,
            user=user,
            connect_timeout=timeout,
            connect_kwargs={
                "key_filename": key_filename,
                "auth_timeout": timeout,
                "banner_timeout": timeout,
            },
        ) 
Example #15
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def systemd_restart(c: Connection):
    print('-- Restarting systemd unit')
    c.run('systemctl --user restart hknweb.service', echo=True) 
Example #16
Source File: path.py    From hknweb with MIT License 5 votes vote down vote up
def current_path(c: Connection) -> str:
    return posixpath.join(deploy_path(c), c.deploy.path.current) 
Example #17
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def create_venv(c: Connection):
    c.run('python3.7 -m venv {}/venv'.format(c.shared_path)) 
Example #18
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def update(c: Connection):
    print('== Update ==')
    create_release(c)
    symlink_shared(c)
    decrypt_secrets(c)
    if not path.dir_exists(c, '{}/venv'.format(c.shared_path)):
        create_venv(c)
    install_deps(c)
    django_migrate(c)
    django_collectstatic(c) 
Example #19
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def deploy(c, target=None, commit=None):
    with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
        setup(c, commit=commit)
        update(c)
        publish(c)
        finish(c) 
Example #20
Source File: fabfile.py    From hknweb with MIT License 5 votes vote down vote up
def rollback(c, release=None):
    with Connection(c.deploy.host, user=c.deploy.user, config=c.config) as c:
        setup(c, release=release)
        update(c)
        publish(c)
        finish(c) 
Example #21
Source File: __init__.py    From kitten with MIT License 5 votes vote down vote up
def get_conns(args):
    colors = list(get_colors())
    for i, host in enumerate(args.hosts):
        if host:
            yield Connection(
                host, args.user, args.timeout, args.i, colors[i % len(colors)]
            ) 
Example #22
Source File: git.py    From hknweb with MIT License 5 votes vote down vote up
def update(c: Connection):
    if repo_exists(c):
        fetch(c)
    else:
        clone(c) 
Example #23
Source File: endpoint_fab.py    From incubator-dlab with Apache License 2.0 5 votes vote down vote up
def create_user():
    initial_user = 'ubuntu'
    sudo_group = 'sudo'
    with Connection(host=args.hostname, user=initial_user,
                    connect_kwargs={'key_filename': args.pkey}) as conn:
        try:
            if not exists(conn,
                          '/home/{}/.ssh_user_ensured'.format(initial_user)):
                conn.sudo('useradd -m -G {1} -s /bin/bash {0}'
                          .format(args.os_user, sudo_group))
                conn.sudo(
                    'bash -c \'echo "{} ALL = NOPASSWD:ALL" >> /etc/sudoers\''.format(args.os_user, initial_user))
                conn.sudo('mkdir /home/{}/.ssh'.format(args.os_user))
                conn.sudo('chown -R {0}:{0} /home/{1}/.ssh/'
                          .format(initial_user, args.os_user))
                conn.sudo('cat /home/{0}/.ssh/authorized_keys > '
                          '/home/{1}/.ssh/authorized_keys'
                          .format(initial_user, args.os_user))
                conn.sudo(
                    'chown -R {0}:{0} /home/{0}/.ssh/'.format(args.os_user))
                conn.sudo('chmod 700 /home/{0}/.ssh'.format(args.os_user))
                conn.sudo('chmod 600 /home/{0}/.ssh/authorized_keys'
                          .format(args.os_user))
                conn.sudo(
                    'touch /home/{}/.ssh_user_ensured'.format(initial_user))
        except Exception as err:
            logging.error('Failed to create new os_user: ', str(err))
            sys.exit(1) 
Example #24
Source File: endpoint_fab.py    From incubator-dlab with Apache License 2.0 5 votes vote down vote up
def init_dlab_connection(ip=None, user=None,
                         pkey=None):
    global conn
    if not ip:
        ip = args.hostname
    if not user:
        user = args.os_user
    if not pkey:
        pkey = args.pkey
    try:
        conn = Connection(ip, user, connect_kwargs={'key_filename': pkey})
    except Exception as err:
        logging.error('Failed connect as dlab-user: ', str(err))
        traceback.print_exc()
        sys.exit(1) 
Example #25
Source File: terraform-cli.py    From incubator-dlab with Apache License 2.0 5 votes vote down vote up
def ssh(ip, name, pkey):
        while True:
            return Connection(host=ip,
                              user=name,
                              connect_kwargs={'key_filename': pkey,
                                              'allow_agent': False,
                                              'look_for_keys': False,
                                              }) 
Example #26
Source File: provisioning.py    From incubator-dlab with Apache License 2.0 5 votes vote down vote up
def create_user():
    initial_user = 'ubuntu'
    sudo_group = 'sudo'
    with Connection(host=args.hostname, user=initial_user,
                    connect_kwargs={'key_filename': args.pkey}) as conn:
        try:
            if not exists(conn,
                          '/home/{}/.ssh_user_ensured'.format(initial_user)):
                conn.sudo('useradd -m -G {1} -s /bin/bash {0}'
                          .format(args.os_user, sudo_group))
                conn.sudo(
                    'bash -c \'echo "{} ALL = NOPASSWD:ALL" >> /etc/sudoers\''.format(args.os_user, initial_user))
                conn.sudo('mkdir /home/{}/.ssh'.format(args.os_user))
                conn.sudo('chown -R {0}:{0} /home/{1}/.ssh/'
                          .format(initial_user, args.os_user))
                conn.sudo('cat /home/{0}/.ssh/authorized_keys > '
                          '/home/{1}/.ssh/authorized_keys'
                          .format(initial_user, args.os_user))
                conn.sudo(
                    'chown -R {0}:{0} /home/{0}/.ssh/'.format(args.os_user))
                conn.sudo('chmod 700 /home/{0}/.ssh'.format(args.os_user))
                conn.sudo('chmod 600 /home/{0}/.ssh/authorized_keys'
                          .format(args.os_user))
                conn.sudo(
                    'touch /home/{}/.ssh_user_ensured'.format(initial_user))
        except Exception as err:
            logging.error('Failed to create new os_user: ', str(err))
            sys.exit(1) 
Example #27
Source File: file_system_input_utils.py    From sagemaker-python-sdk with Apache License 2.0 5 votes vote down vote up
def _connect_ec2_instance(ec2_instance):
    public_ip_address = ec2_instance.public_ip_address
    connected_instance = Connection(
        host=public_ip_address,
        port=22,
        user="ec2-user",
        connect_kwargs={"key_filename": [KEY_PATH]},
    )
    return connected_instance 
Example #28
Source File: module.py    From LuWu with Apache License 2.0 5 votes vote down vote up
def exec_remote_cmd(cls, conn: Connection, command: str, **kwargs):
        return conn.run(command, **kwargs) 
Example #29
Source File: module.py    From LuWu with Apache License 2.0 5 votes vote down vote up
def upload_remote_file(cls, conn: Connection, source_file: str, remote_file: str):
        return conn.put(source_file, remote_file) 
Example #30
Source File: module.py    From LuWu with Apache License 2.0 5 votes vote down vote up
def gen_ssh_connection(cls, private_key_path: str, addr: str):
        return Connection(
            addr,
            connect_kwargs={
                "key_filename": private_key_path,
            },
        )