Пример #1
0
def add_authorized_key(key_name=None, pub_key_file='.ssh/id_rsa.pub'):
    """
    Adds local ssh pub key to remote authorized keys. To use a public key from
    keygen pass the key_name attribute.

    Example: Add muni.pub key on keygen to magnet time server

        $ fab ssh.add_authorized_key:key_name=muni.pub -H time
    """
    mkdir_ssh()

    if key_name:
        repo = '[email protected]/magnet-cl/keygen.git'

        local('git archive --remote=ssh://{} master {} | tar -x'.format(
            repo, key_name))

        pub_key = open(key_name).read()[:-1]
        line_found = run('grep "{}" {} | wc -l'.format(
            pub_key, '~/.ssh/authorized_keys'))
        if line_found == '0':
            run('echo "{}" >> {}'.format(pub_key, '~/.ssh/authorized_keys'))
        local('rm {}'.format(key_name))
    else:
        pub_key = open('%s/%s' % (os.path.expanduser('~'), pub_key_file))
        append('~/.ssh/authorized_keys', pub_key)

    if not path_exists('~/.ssh/authorized_keys'):
        raise Exception("authorized_keys_not_created")
Пример #2
0
def remove_authorized_key(pub_key_file='.ssh/id_rsa.pub', key_name=None):
    """
    Removes local ssh pub key to remote authorized keys. To use a public key
    from keygen pass the key_name attribute.

    Example: Remove muni.pub key on keygen to magnet time server

        $ fab ssh.remove_authorized_key:key_name=muni.pub -H time
    """
    mkdir_ssh()

    if key_name:
        repo = '[email protected]/magnet-cl/keygen.git'

        local('git archive --remote=ssh://{} master {} | tar -x'.format(
            repo, key_name))

        pub_key = open(key_name)
    else:
        pub_key = open('%s/%s' % (os.path.expanduser('~'), pub_key_file))

    # obtain the first line
    pub_key = pub_key.readline()

    # escape regular expression special characters
    pub_key = re.escape(pub_key)

    run("sed -i.bak '/{}/d' ~/.ssh/authorized_keys".format(pub_key))

    if key_name:
        local('rm {}'.format(key_name))

    if not path_exists('~/.ssh/authorized_keys'):
        raise Exception("authorized_keys_not_created")
Пример #3
0
def add_authorized_key(pub_key_file='.ssh/id_rsa.pub', key_name=None):
    """
    Adds local ssh pub key to remote authorized keys.

    Example: Add muni.pub key on keygen to magnet time server

        $ fab ssh.add_authorized_key:key_name=muni.pub -H time
    """
    mkdir_ssh()

    if key_name:
        repo = '[email protected]/magnet-cl/keygen.git'

        local('git archive --remote=ssh://{} master {} | tar -x'.format(
            repo, key_name)
        )

        pub_key = open(key_name)
        append('~/.ssh/authorized_keys', pub_key)
        local('rm {}'.format(key_name))
    else:
        pub_key = open('%s/%s' % (os.path.expanduser('~'), pub_key_file))
        append('~/.ssh/authorized_keys', pub_key)

    if not path_exists('~/.ssh/authorized_keys'):
        raise Exception("authorized_keys_not_created")
Пример #4
0
def sync_temp(env_name, from_branch):
    """
        synchronize from given repository.
    """
    assert "tempspace" in ENVS[env_name], "tempspace does not exist."
    assert "git_branch" in ENVS[env_name], "git_branch does not exist."
    set_env(env_name)
    cmd_mkdir = "mkdir -p %s" % ENVS[env_name]["tempspace"]
    cmd_clone = "git clone %s . -b %s" % (ENVS["project"]["repository"], from_branch)
    commands = []
    commands.append("git checkout %s" % from_branch)
    commands.append("git reset --hard HEAD")
    commands.append("git clean -dfx")
    commands.append("git pull origin %s" % from_branch)
    if env_name == "local":
        if not os.direxists(ENVS[env_name]["tempspace"]):
            local(cmd_mkdir)
            with lcd(ENVS[env_name]["tempspace"]):
                local(cmd_clone)
        with lcd(ENVS[env_name]["tempspace"]):
            for cmd in commands:
                local(cmd)
    else:
        if not path_exists(ENVS[env_name]["tempspace"]):
            run(cmd_mkdir)
            with cd(ENVS[env_name]["tempspace"]):
                run(cmd_clone)
        with cd(ENVS[env_name]["tempspace"]):
            for cmd in commands:
                run(cmd)
Пример #5
0
def remove_authorized_key(pub_key_file='.ssh/id_rsa.pub', key_name=None):
    """
    Removes local ssh pub key to remote authorized keys. To use a public key
    from keygen pass the key_name attribute.

    Example: Remove muni.pub key on keygen to magnet time server

        $ fab ssh.remove_authorized_key:key_name=muni.pub -H time
    """
    mkdir_ssh()

    if key_name:
        repo = '[email protected]/magnet-cl/keygen.git'

        local('git archive --remote=ssh://{} master {} | tar -x'.format(
            repo, key_name)
        )

        pub_key = open(key_name)
    else:
        pub_key = open('%s/%s' % (os.path.expanduser('~'), pub_key_file))

    # obtain the first line
    pub_key = pub_key.readline()

    # escape regular expression special characters
    pub_key = re.escape(pub_key)

    run("sed -i.bak '/{}/d' ~/.ssh/authorized_keys".format(pub_key))

    if key_name:
        local('rm {}'.format(key_name))

    if not path_exists('~/.ssh/authorized_keys'):
        raise Exception("authorized_keys_not_created")
 def __scan(self):
     if not path_exists(self._path):
         raise OSError("No deployment of release %s found!" % self._release)
     with cd(self._path):
         with settings(hide('running', 'stdout')):
             ptrn = path.join("apps", "*", "migrations", "*.py")
             output = run("ls -1 %s" % ptrn)
     return self.__to_dict(output.splitlines())
def deploy(release):
    """
    fab deploy:<tag> -- Installs the specified release on the server
    :param release: GIT branch or tag name
    """
    print "Deploying release: %s" % release

    # Download file from git repository
    repo = Repository()
    local_path = repo.download_release(release)

    # Deploy to server
    deploy_path = Deployment.app_dir(release)
    env.project_dir = deploy_path
    to_deploy = ["apps", "settings", "flatpages.json", "manage.py", "requirements.txt", "wsgi.py"]
    files_to_retain = set([".", ".."] + to_deploy)

    if path_exists(deploy_path):
        print red("Cannot deploy release: folder already exists!")
        abort(red("Cannot continue"))
    try:
        with settings(hide('running', 'stdout')):
            run("mkdir -p %s" % deploy_path)
            put(local_path=local_path,
                remote_path=deploy_path,
                mode=0644)
            with cd(deploy_path):
                run("tar xvf %s" % path.basename(local_path))
                files = set(run("ls -a").split())
                files_to_remove = files - files_to_retain
                print "cleaning up..."
                run("rm -rf %s" % " ".join(files_to_remove))
                print "done"

            # Patch settings files
            settings_ptrn = path.join(path.dirname(path.abspath(__file__)), "settings", "production", "*.py")
            for fn in glob(settings_ptrn):
                put(local_path=fn, remote_path=path.join(deploy_path, "settings"), mode=0644)

            # Collect static files
            print "Collecting static files..."
            Deployment.collectstatic(release)

        print green("Successfully deployed release %s" % release)
    except Exception, e:
        print red("Deployment encountered error: %s" % e)
        abort(red("Cannot continue"))
def activate(release=None):
    """
    fab activate:<tag> -- Configures and restarts the services to run the specified release
    :param release: GIT branch or tag name; skip to activate the legacy deployment
    """
    def release_str(release):
        if release is not None:
            return str(release)
        else:
            return "(legacy)"
    print "Activate release: %s" % release_str(release)

    # Check if the new release has been deployed
    deploy = Deployment()
    deploy_path = deploy.app_dir(release)
    if not path_exists(deploy_path):
        print red("Cannot activate release: no matching deployment found!")
        abort(red("Cannot continue"))
    env.project_dir = deploy_path

    # Stop services before touching the database
    downtime_start = datetime.utcnow()
    nginx = Nginx()
    uwsgi = uWSGI()
    nginx.stop()
    uwsgi.stop()

    # Update the pip packages
    deploy.update_virtualenv(release)

    # Rollback superfluous migrations from the current release
    current_migrations = Migrations(deploy.active_release)
    new_migrations = Migrations(release)
    current_migrations.revert_left(new_migrations)

    # Apply database changes for the new release
    new_migrations.apply()

    # Configure and start the services
    uwsgi.configure(release)
    uwsgi.start()
    nginx.start()
    downtime_end = datetime.utcnow()

    print green("Activated release %s" % release_str(uwsgi.active_release))
    print "Downtime: %s" % (downtime_end - downtime_start)
Пример #9
0
def install_db_backups_manager():
    """ Installs project py-db-backup """

    if path_exists('py-db-backup'):
        update_manager = prompt(green('DB backups manager already installed. '
                                      'Would you like to update it?'),
                                default='Yes')
        if update_manager in ('Yes', 'yes', 'Y', 'y'):
            with cd('py-db-backup'):
                git_pull()

        return

    # install git if is not available
    git_install()

    print(green('Cloning py-db-backup repository.'))
    git_clone('git://github.com/magnet-cl/py-db-backup.git', 'py-db-backup')

    print(green('Installing py-db-backup'))
    with cd('py-db-backup'):
        cmd = './install.sh'
        run(cmd)
Пример #10
0
def install_db_backups_manager():
    """ Installs project py-db-backup """

    if path_exists('py-db-backup'):
        update_manager = prompt(green('DB backups manager already installed. '
                                      'Would you like to update it?'),
                                default='Yes')
        if update_manager in ('Yes', 'yes', 'Y', 'y'):
            with cd('py-db-backup'):
                git_pull()

        return

    # install git if is not available
    git_install()

    print(green('Cloning py-db-backup repository.'))
    git_clone('git://github.com/magnet-cl/py-db-backup.git', 'py-db-backup')

    print(green('Installing py-db-backup'))
    with cd('py-db-backup'):
        cmd = './install.sh'
        run(cmd)
Пример #11
0
def remove(release):
    """
    fab remove:<tag> -- Removes the specified release from the server
    :param release: GIT branch or tag name
    """
    print "Removing release: %s" % release
    depl = Deployment()
    try:
        if not release:
            raise ValueError("Cannot remove release from legacy path (only side by side installations may be deleted)")
        elif release == depl.active_release:
            raise ValueError("Cannot remove active release")
        elif not path_exists(Deployment.app_dir(release)):
            raise OSError("Cannot remove release: no deployment found!")
        else:
            with settings(hide('running', 'stdout'), warn_only=True):
                result = run("rm -rf %s" % Deployment.app_dir(release))
                if result.succeeded:
                    print green("Successfully removed deployment of release %s" % release)
                else:
                    raise OSError("Failed to delete deployment folder!")
    except (OSError, ValueError), e:
        print red(e.message)
        abort(red("Cannot continue"))