예제 #1
0
def libs(lookup_param='prod', pip='install'):
    with cd(env.home_dir):
        # pip cache
        pip_cache_dir = posixpath.join(env.shared, '.pip.cache')
        with cd(env.root):
            try:
                env.pyver = conf[lookup_param]['version']
            except:
                pass
            env.virt = posixpath.join(env.home_dir, env.virt_home, conf[lookup_param]['venv'])
            with prefix('pythonbrew use {0}'.format(env.pyver)):
                run('python -V')
                with fabtools.python.virtualenv(env.virt):
                    fabtools.python_distribute.install(conf[lookup_param]['distribute'])
                    python.install_requirements(conf[lookup_param]['requirement'], use_mirrors=False, download_cache=pip_cache_dir)
예제 #2
0
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')