Example #1
0
def setup_venv(code_path, requirements_file='requirements.txt'):
    """Initialise or update the virtual environment.

    It will also ensure build-essential is installed, though you may need to
    install required dev library using your own script.

    :param code_path: Base directory under which the venv dir should be made.
    :type code_path: str

    :param requirements_file: Name of the requirements file to use in the
        base directory. Defaults to ``requirements.txt``.
    :type requirements_file: str

    To run e.g.::

        fab -H 192.168.1.1:2222 setup_venv

    """
    setup_env()
    fastprint(
        blue('Setting up virtual env in: \n%s\nusing\n%s' %
             (code_path, requirements_file)))
    require_packages(['python-virtualenv', 'build-essential'])
    with cd(code_path):
        # Ensure we have a venv set up
        virtualenv('venv')
        run('venv/bin/pip install -r %s' % requirements_file)
    fastprint(green('Virtualenv setup completed.'))
Example #2
0
def setup_venv(code_path, requirements_file='requirements.txt'):
    """Initialise or update the virtual environment.

    It will also ensure build-essential is installed, though you may need to
    install required dev library using your own script.

    :param code_path: Base directory under which the venv dir should be made.
    :type code_path: str

    :param requirements_file: Name of the requirements file to use in the
        base directory. Defaults to ``requirements.txt``.
    :type requirements_file: str

    To run e.g.::

        fab -H 192.168.1.1:2222 setup_venv

    """
    setup_env()
    fastprint(blue('Setting up virtual env in: \n%s\nusing\n%s' % (
        code_path, requirements_file)))
    require_packages(['python-virtualenv', 'build-essential'])
    with cd(code_path):
        # Ensure we have a venv set up
        virtualenv('venv')
        run('venv/bin/pip install -r %s' % requirements_file)
    fastprint(green('Virtualenv setup completed.'))
Example #3
0
def test_require_virtualenv():
    """
    Test Python virtualenv creation
    """

    from fabtools.require.python import virtualenv

    try:
        virtualenv('/tmp/venv')

        assert is_dir('/tmp/venv')
        assert is_file('/tmp/venv/bin/python')

    finally:
        run('rm -rf /tmp/venv')
def push():
    """
    This function for create django site project work flow on remote server.
    Django site source cloning from remote git repository.

    NOTE: This function may be used in other fab file.
    For this need setup global `env` dict.

    **`env` settings**
    env.user - deploy user name (use for ssh)
    env.password - deploy user password (use for ssh)
    env.hosts - list deploy hosts (use for ssh)

    env.domain - django site domain (DNS) use for:
        - nginx settings
        - uWSGI start user
        - project dir name

    env.repository - remote git repository url, use for git clone site source

    env.no_input_mode - in this variable True use no input deploy mode.
        If no_input_mode==True using follow strategy:
            Abort if env.domain (env.repository) value not set or invalid.
            And using default confirm() value if needed.

    """
    # cwd => ./deploy
    env.lcwd = os.path.abspath(os.path.dirname(__file__))

    require('no_input_mode')

    #env.no_input_mode = False
    if env.no_input_mode:
        def confirm_local(question, default=True):
            puts(question)
            puts("Use no_input_mode [default: {0}]".format("Y" if default else "N"))
            return default

        confirm = confirm_local
    else:
        confirm = confirm_global

    validate = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"
    if not env.get("domain"):
        if env.no_input_mode:
            abort("Need set env.domain !")
        else:
            prompt("Project DNS url: ", "domain", env.get('domain_default', ''), validate=validate)
    else:
        if not re.findall(validate, env.domain):
            abort("Invalid env.domain !")

    if not env.get("repository"):
        if env.no_input_mode:
            env.repository = env.repository_default
        else:
            prompt("Deploy from: ", "repository", env.get('repository_default', ''))

    require('repository', 'domain')

    puts("Deploy site: {0} \nFrom: {1}".format(env.domain, env.repository))
    DOMAIN_WITHOUT_DOT = env.domain.replace('.', '_')

    env.project_user = DOMAIN_WITHOUT_DOT
    env.project_group = DOMAIN_WITHOUT_DOT
    env.project_dir_name = DOMAIN_WITHOUT_DOT
    env.root = posixpath.join(PROJECTS_ROOT, env.project_dir_name)

    env.debug = True

    deb.packages(['git'])

    files.directory(PROJECTS_ROOT, use_sudo=True, owner='root', group='root', mode='755')
    with cd(PROJECTS_ROOT):
        # pip cache
        files.directory('.pip.cache', use_sudo=True, owner='deploy', group='deploy', mode='755')
        pip_cache_dir = posixpath.join(PROJECTS_ROOT, '.pip.cache')

        # proj dir create
        if is_dir(env.project_dir_name) and confirm("proj dir exist! abort ?", default=False):
            return

        files.directory(env.project_dir_name, use_sudo=True, owner='root', group='root', mode='755')

        # proj user create
        if not fabtools.user.exists(env.project_user):
            fabtools.user.create(env.project_user, home=env.root, group=env.project_group, create_home=False,
                                 system=True, shell='/bin/false', create_group=True)

        # proj infrastructure
        with cd(env.project_dir_name):
            # proj source
            if not is_dir('src') or confirm("proj src exist! [rm all and re clone / git pull]?", default=False):
                files.directory('src', use_sudo=True, owner='deploy', group='deploy', mode='755')
                with cd('src'):
                    sudo('rm -Rf .??* *')
                    sudo('git clone {repository:s} .'.format(env), user='******')
            else:
                with cd('src'):
                    sudo('git pull', user='******')

            # proj virtual env
            if not is_dir('.virtualenvs') or confirm("proj venv dir exist! [rm all and recreate / repeat install]?",
                                                     default=False):
                files.directory('.virtualenvs', use_sudo=True, owner='deploy', group='deploy', mode='755')
                with cd('.virtualenvs'):
                    sudo('rm -Rf .??* *')

            python.virtualenv('.virtualenvs', use_sudo=True, user='******', clear=True)
            with fabtools.python.virtualenv('.virtualenvs'):
                python.install_requirements('src/requirements.txt', use_mirrors=False, use_sudo=True, user='******',
                                            download_cache=pip_cache_dir)

            # ------------------- #
            # WEB SERVER SETTINGS #
            # ------------------- #

            # I`m use nginx <-> uWSGI <-> Django

            nginx.server()
            deb.packages(['uwsgi', 'uwsgi-plugin-python'])

            # proj conf!
            if not is_dir('conf') or confirm("proj conf dir exist! [backup and update? / skip]", default=False):
                files.directory('conf', use_sudo=True, owner='root', group='root', mode='755')
                with cd('conf'):
                    local_conf_templates = os.path.join(os.path.dirname(__file__), 'template', 'conf')
                    uwsgi_conf = os.path.join(local_conf_templates, 'uwsgi.ini')
                    nginx_conf = os.path.join(local_conf_templates, 'nginx.conf')

                    sudo("rm -Rf *.back")
                    sudo("ls -d *{.conf,.ini} | sed 's/.*$/mv -fu \"&\" \"\\0.back\"/' | sh")
                    files.template_file('uwsgi.ini', template_source=uwsgi_conf, context=env,
                                        use_sudo=True, owner='root', group='root', mode='644')
                    files.file('reload', use_sudo=True, owner='root', group='root')
                    sudo('ln -sf $(pwd)/uwsgi.ini /etc/uwsgi/apps-enabled/' + env.project_dir_name + '.ini')

                    files.template_file('nginx.conf', template_source=nginx_conf, context=env,
                                        use_sudo=True, owner='root', group='root', mode='644')
                    sudo('ln -sf $(pwd)/nginx.conf /etc/nginx/sites-enabled/' + env.project_dir_name)

            sudo('service nginx restart')
            sudo('service uwsgi restart')
Example #5
0
def venv():
    from fabtools.require.python import virtualenv
    path = '/tmp/venv'
    virtualenv(path)
    yield path
    run('rm -rf %s' % quote(path))
Example #6
0
def venv(request):
    from fabtools.require.python import virtualenv
    path = '/tmp/venv'
    virtualenv(path)
    request.addfinalizer(functools.partial(run, 'rm -rf %s' % quote(path)))
    return path
Example #7
0
def venv(request):
    from fabtools.require.python import virtualenv
    path = '/tmp/venv'
    virtualenv(path)
    request.addfinalizer(functools.partial(run, 'rm -rf %s' % quote(path)))
    return path