Exemplo n.º 1
0
def venv_dump():
    """
    Archive a virtualenv

    The task will create an archive ``<virtualenv name>.tar.gz`` of the entire
    virtual environment. If an existing archive already exists, the user will
    be asked for confirmation to remove it. Normally this command is invoked
    indirectly via the compound task :meth:`inveniofab.compound.dump` which
    takes care of dumping the database prior to archiving the virtual
    environment.
    """
    puts(cyan(">>> Creating archive of virtualenv in %(CFG_INVENIO_PREFIX)s..." % env))

    ctx = {
        'dirname': os.path.dirname(env.CFG_INVENIO_PREFIX),
        'basename': os.path.basename(env.CFG_INVENIO_PREFIX),
    }

    archive_file = "%(dirname)s/%(basename)s.tar.gz" % ctx
    if exists_local(archive_file):
        res = confirm("Existing archive already exists - remove?")
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("rm -Rf %s" % archive_file, user=env.CFG_INVENIO_USER)

    cmds = [
        "cd %(dirname)s",
        "tar -cvzf %(basename)s.tar.gz %(basename)s",
    ]
    sudo_local(" && ".join(cmds) % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 2
0
def venv_load():
    """
    Load an archived virtualenv

    The task will extract an archived virtual environment created with
    :meth:`~venv_dump`. Normally this command is invoked
    indirectly via the compound task :meth:`inveniofab.compound.load` which
    takes care of loading the database after extracting the virtual
    environment.
    """
    puts(cyan(">>> Loading archived virtualenv..."))

    ctx = {
        'dirname': os.path.dirname(env.CFG_INVENIO_PREFIX),
        'basename': os.path.basename(env.CFG_INVENIO_PREFIX),
    }

    archive_file = "%(dirname)s/%(basename)s.tar.gz" % ctx
    if not exists_local(archive_file):
        abort(red("Archived virtualenv does not exists - cannot continue") % env)

    # Remove previous installation
    if exists_local(env.CFG_INVENIO_PREFIX):
        res = confirm("Remove installation in %(CFG_INVENIO_PREFIX)s ?" % env)
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("rm -Rf %(CFG_INVENIO_PREFIX)s" % env)

    cmds = [
        "cd %(dirname)s",
        "tar -xvzf %(basename)s.tar.gz",
    ]
    sudo_local(" && ".join(cmds) % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 3
0
def repo_setup(repo, ref):
    """ Clone repository """
    puts(cyan(">>> Setting up repository %s with ref %s..." % (repo, ref)))

    topsrcdir = repo_check(repo, check_path=False, workdir=False)
    workdir = repo_check(repo, check_path=False, workdir=True)
    gitdir = os.path.join(topsrcdir, '.git')

    if not exists_local(env.CFG_SRCDIR):
        res = confirm("Create repository root %s?" % env.CFG_SRCDIR)
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("mkdir -p %s" % env.CFG_SRCDIR, user=env.CFG_INVENIO_USER)

    if not exists_local(gitdir) and exists_local(topsrcdir):
        res = confirm("Remove %s (it does not seem to be a git repository)?" % topsrcdir)
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("rm -Rf %s" % topsrcdir, user=env.CFG_INVENIO_USER)

    if not exists_local(gitdir):
        git_clone(repo)
    if not exists_local(workdir):
        git_newworkdir(repo)
    git_checkout(repo, ref)
    repo_prepare(repo)
Exemplo n.º 4
0
def apache_conf():
    """ Upload and update Apache configuration """
    puts(cyan(">>> Configuring Apache..." % env))

    conf_files = ['etc/apache/invenio-apache-vhost.conf', 'etc/apache/invenio-apache-vhost-ssl.conf']
    conf_files = [(p, os.path.join(env.CFG_INVENIO_PREFIX, p)) for p in conf_files]

    for local_file, remote_file in conf_files:
        puts(">>> Writing %s ..." % remote_file)

        try:
            if not exists_local(os.path.dirname(remote_file)):
                sudo_local("mkdir -p %s" % os.path.dirname(remote_file), user=env.CFG_INVENIO_USER)
            write_template(remote_file, env, tpl_file=local_file, use_sudo=True)
        except TemplateNotFound:
            abort(red("Could not find template %s" % local_file))

    apache_conf = env.get('CFG_APACHE_CONF', '/etc/httpd/conf/httpd.conf')
    if confirm("Include created files in %s?" % apache_conf):
        if exists_local(apache_conf, use_sudo=True):
            lines = ["Include %s" % r for (l, r) in conf_files]
            if is_local():
                with settings(host_string="localhost"):
                    append(apache_conf, lines, use_sudo=True)
            else:
                append(apache_conf, lines, use_sudo=True)
        else:
            warn(red("File %s does not exists" % apache_conf))

    sudo_local("%(CFG_INVENIO_APACHECTL)s configtest" % env)
Exemplo n.º 5
0
def git_reset(repo, ref):
    topsrcdir = repo_check(repo, workdir=True)
    ctx = {
        'topsrcdir': topsrcdir,
        'ref': ref,
    }
    ctx.update(env)
    sudo_local("cd %(topsrcdir)s; git reset --hard %(ref)s " % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 6
0
def git_describe(repo):
    topsrcdir = repo_check(repo, workdir=True)
    version_gen = os.path.join(topsrcdir, 'git-version-gen')

    if exists_local(version_gen):
        output = sudo_local("cd %s; %s" % (topsrcdir, version_gen), capture=True, user=env.CFG_INVENIO_USER)
    else:
        output = sudo_local("cd %s; git describe --always --abbrev=4 HEAD" % topsrcdir, capture=True, user=env.CFG_INVENIO_USER)
    return output
Exemplo n.º 7
0
def test_load(repo=None, quite=True):
    """ Load test environment """
    if quite or confirm(cyan("Run step load_files?")):
        puts(cyan(">>> Loading test package..." % env))
        sudo_local("rsync --delete -rLptgoDv %(CFG_INVENIO_PREFIX)s/tests/var/ %(CFG_INVENIO_PREFIX)s/var/" % env)
        sudo_local("rsync --delete -rLptgoDv %(CFG_INVENIO_PREFIX)s/tests/etc/ %(CFG_INVENIO_PREFIX)s/etc/" % env)

    if quite or confirm(cyan("Run step load_db?")):
        mysql_load(dumpfile=os.path.join(env.CFG_INVENIO_PREFIX, "tests/%s.sql.gz" % env.CFG_DATABASE_NAME))

    if quite or confirm(cyan("Run step configure_make_install?")):
        repo_all_configure_make(repo, target_key='deploy_targets')
Exemplo n.º 8
0
def git_newworkdir(repo):
    if env.WITH_WORKDIR and env.CFG_SRCDIR != env.CFG_SRCWORKDIR:
        srcdir = repo_check(repo, workdir=False, check_path=False)
        srcworkdir = repo_check(repo, workdir=True, check_path=False)

        ctx = {
            'srcdir': srcdir,
            'srcworkdir': srcworkdir,
        }
        ctx.update(env)

        sudo_local("%(CFG_INVENIO_PREFIX)s/bin/git-new-workdir %(srcdir)s %(srcworkdir)s" % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 9
0
def mysql_load(dumpfile=None, stored_answers=None):
    """
    Load MySQL dump file
    """
    with hide('commands'):
        puts(cyan(">>> Loading database dump..."))

        if not dumpfile:
            dumpfile = os.path.join(env.CFG_DATABASE_DUMPDIR, "%s.sql.gz" % env.CFG_DATABASE_NAME)

        if not exists_local(dumpfile):
            abort("File %s does not exists." % dumpfile)

        answers = prompt_and_check([
            ("MySQL admin user:"******"user"),
            ("MySQL admin password:"******"password")
        ], mysql_admin_check(env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        cache_key="mysql://%s:%s" % (env.CFG_DATABASE_HOST, env.CFG_DATABASE_PORT),
        stored_answers=stored_answers)

        # Escape quote characters
        answers['password'] = answers['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')
        user_pw = '-u %(user)s --password=%(password)s' % answers if answers['password'] else '-u %(user)s' % answers

        if dumpfile.endswith(".gz"):
            dumpfile_stream = "gunzip -c %s" % dumpfile
        else:
            dumpfile_stream = "cat %s" % dumpfile

        ctx = {
            'user_pw': user_pw,
            'host':  env.CFG_DATABASE_HOST,
            'name':  env.CFG_DATABASE_NAME,
            'user':  env.CFG_DATABASE_USER,
            'password': env.CFG_DATABASE_PASS,
            'port': env.CFG_DATABASE_PORT,
            'dumpfile': dumpfile,
            'dumpfile_stream': dumpfile_stream,
        }

        # Escape quote characters
        ctx['password'] = ctx['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')

        if confirm("This will erease all data in the existing database. Are you sure you want to load %(dumpfile)s?" % ctx):
            mysql_dropdb(stored_answers=answers)
            mysql_createdb(stored_answers=answers)

            sudo_local('%(dumpfile_stream)s | mysql %(user_pw)s -h %(host)s -P %(port)s -f %(name)s' % ctx, user=env.CFG_INVENIO_USER)

        return dumpfile
Exemplo n.º 10
0
def test_dump():
    """ Dump a test environment """
    test_reset_admin()
    test_clean()

    puts(cyan(">>> Creating test package..." % env))

    sudo_local("rm -Rf %(CFG_INVENIO_PREFIX)s/tests/" % env, user=env.CFG_INVENIO_USER)
    sudo_local("mkdir -p %(CFG_INVENIO_PREFIX)s/tests/var/" % env, user=env.CFG_INVENIO_USER)
    sudo_local("mkdir -p %(CFG_INVENIO_PREFIX)s/tests/etc/" % env, user=env.CFG_INVENIO_USER)
    sudo_local("sudo rsync --delete -rLptgoDv %(CFG_INVENIO_PREFIX)s/var/ %(CFG_INVENIO_PREFIX)s/tests/var/" % env)
    sudo_local("sudo rsync --delete -rLptgoDv %(CFG_INVENIO_PREFIX)s/etc/ %(CFG_INVENIO_PREFIX)s/tests/etc/" % env)

    mysql_dump(outputdir=os.path.join(env.CFG_INVENIO_PREFIX, "tests"))
Exemplo n.º 11
0
def celery_initd():
    """ Upload and update celeryd init script """
    puts(cyan(">>> Creating and configuring celery init.d script..." % env))

    local = 'celeryd'
    remote = '/etc/init.d/celeryd'

    puts(">>> Writing celeryd to %s ..." % remote)
    try:
        write_template(remote, env, tpl_file=local, use_sudo=True)
    except TemplateNotFound:
        puts(red("Could not find template %s" % local))

    sudo_local("chmod a+x %s" % remote)
Exemplo n.º 12
0
def git_isdirty(dir):
    """
    Check working directory for uncommitted changes
    """
    with settings(hide('everything'), warn_only=True):
        output = sudo_local("cd %s && git diff-index --exit-code HEAD --" % dir, capture=True, user=env.CFG_INVENIO_USER)
    return output.return_code != 0
Exemplo n.º 13
0
def mysql_dump(outputdir=None):
    """
    Dump database to file
    """
    with hide('commands'):
        puts(cyan(">>> Dumping database ..." % env))

        answers = {
            'user': env.CFG_DATABASE_USER,
            'password': env.CFG_DATABASE_PASS,
        }

        # Escape quote characters
        answers['password'] = answers['password'].replace("'", "\'").replace('"', '\"').replace('$', '\\$')

        if not outputdir:
            outputdir = env.CFG_DATABASE_DUMPDIR

        if not exists_local(outputdir):
            abort(red("Output directory %s does not exists" % outputdir))

        user_pw = '-u %(user)s --password=%(password)s' % answers if answers['password'] else '-u %(user)s' % answers
        outfile = os.path.join(outputdir, "%s.sql" % env.CFG_DATABASE_NAME)
        outfile_gz = "%s.gz" % outfile

        for f in [outfile, outfile_gz]:
            if exists_local(f):
                res = confirm("Remove existing DB dump in %s ?" % f)
                if not res:
                    abort(red("Cannot continue") % env)
                else:
                    sudo_local("rm -Rf %s" % f)

        ctx = {
            'user_pw': user_pw,
            'host':  env.CFG_DATABASE_HOST,
            'name':  env.CFG_DATABASE_NAME,
            'port': env.CFG_DATABASE_PORT,
            'outfile_gz': outfile_gz,
            'outfile': outfile,
        }

        # Run commands
        sudo_local('mysqldump %(user_pw)s -h %(host)s -P %(port)s --skip-opt '
              '--add-drop-table --add-locks --create-options --quick '
              '--extended-insert --set-charset --disable-keys %(name)s '
              '| gzip -c > %(outfile_gz)s' % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 14
0
def venv_drop():
    """ Drop virtualenv environment """
    # Checks
    if 'CFG_INVENIO_PREFIX' not in env:
        abort(red("CFG_INVENIO_PREFIX is not specified in env.") % env)

    puts(cyan(">>> Dropping virtualenv in %(CFG_INVENIO_PREFIX)s..." % env))

    # Remove previous installation
    if exists_local(env.CFG_INVENIO_PREFIX):
        res = confirm("Remove installation in %(CFG_INVENIO_PREFIX)s ?" % env)
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("sudo rm -Rf %(CFG_INVENIO_PREFIX)s" % env)
    else:
        puts(">>> Nothing to remove - %(CFG_INVENIO_PREFIX)s does not exists..." % env)
Exemplo n.º 15
0
def repo_make(repo, *targets):
    """ Run make in repository """
    topsrcdir = repo_check(repo, workdir=True)

    if not targets:
        try:
            targets = dict(env.CFG_INVENIO_REPOS)[repo]['targets']
        except KeyError:
            abort(red("No default targets found for repository %s" % repo))

    puts(cyan(">>> Running make for %s targets: %s ..." % (repo, " ".join(targets))))

    ctx = {
        'topsrcdir': topsrcdir,
        'targets': " ".join(targets),
    }
    ctx.update(env)

    sudo_local("cd %(topsrcdir)s && make %(targets)s" % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 16
0
def venv_pyuno_install():
    """
    Install Python OpenOffice binding

    The task will try to locate ``uno.py`` and ``unohelper.py`` in ``/usr/``,
    and copy it to your virtualenv's site-packages.

    .. warning::

       The Python OpenOffice bindings from your system is specific to
       your system's Python interpreter, hence if your system Python is 2.7 and
       you are installing the bindings into virtualenv with Python 2.4, the
       bindings will not work.
    """
    pyver = python_version()
    ctx = {'pyver': pyver}
    ctx.update(env)

    sudo_local("cp  `find /usr/ -name uno.py 2>/dev/null | head -n 1` %(CFG_INVENIO_PREFIX)s/lib/python%(pyver)s/site-packages/" % ctx, user=env.CFG_INVENIO_USER)
    sudo_local("cp `find /usr/ -name unohelper.py 2>/dev/null | head -n 1` %(CFG_INVENIO_PREFIX)s/lib/python%(pyver)s/site-packages/" % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 17
0
def mysql_conf_type(conf='', name=''):
    """ Upload and update MySQL configuration """
    puts(cyan(">>> Configuring MySQL (%s)..." % name))

    conf_files = [(conf, '/etc/my.cnf')]

    ctx = {}
    ctx.update(env)

    for local_file, remote_file in conf_files:
        print local_file
        puts(">>> Writing %s ..." % remote_file)

        try:
            write_template(remote_file, ctx, tpl_file=local_file, use_sudo=True)
        except TemplateNotFound:
            abort(red("Could not find template %s" % local_file))

    if confirm(cyan("Reload MySQL daemon?")):
        sudo_local("service mysqld reload")
Exemplo n.º 18
0
def git_clone(repo):
    topsrcdir = repo_check(repo, check_path=False, workdir=False)

    try:
        repo_url = dict(env.CFG_INVENIO_REPOS)[repo]['repository']
    except KeyError:
        abort(red("Repository URL for %s not defined" % repo))

    basename = os.path.basename(topsrcdir)
    parent = os.path.dirname(topsrcdir)

    if exists_local(topsrcdir):
        res = confirm("Remove existing source code in %s ?" % topsrcdir)
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("rm -Rf %s" % topsrcdir, user=env.CFG_INVENIO_USER)
    else:
        if not exists_local(parent):
            sudo_local("mkdir -p %s" % parent, user=env.CFG_INVENIO_USER)

    ctx = {
        'basename': basename,
        'parent': parent,
        'topsrcdir': topsrcdir,
        'url': repo_url,
    }

    sudo_local("cd %(parent)s; git clone %(url)s %(basename)s " % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 19
0
def venv_requirements():
    """
    Install Python packages

    The task will install Python packages defined in PIP requirements file.
    """
    requirements = []
    for repo, info in env.CFG_INVENIO_REPOS:
        requirements.extend([(repo, x) for x in info.get('requirements', [])])

    if not requirements:
        puts(cyan(">>> No requirements defined..."))
        return

    puts(cyan(">>> Installing requirements..." % env))

    pyver = python_version()
    reqpaths = []

    for repo, reqfile in requirements:
        reqfile = reqfile % env
        reqpath = os.path.join(env.CFG_INVENIO_PREFIX, "%s_%s" % (repo, os.path.basename(reqfile)))

        ctx = {'reqpath': reqpath, 'reqfile': reqfile, 'pyver': pyver}
        ctx.update(env)

        puts(">>> Writing requirements to %(reqpath)s ..." % ctx)
        write_template(reqpath, ctx, remote_tpl_file=reqfile, use_sudo=True)
        reqpaths.append(reqpath)

    for reqpath in reqpaths:
        puts(">>> Installing requirements from %(reqpath)s ..." % ctx)
        ctx = {'reqpath': reqpath, 'pyver': pyver}
        ctx.update(env)
        cmds = [env.ACTIVATE] if env.WITH_VIRTUALENV else []
        cmds.append("pip install -r %(reqpath)s")
        sudo_local(" && ".join(cmds) % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 20
0
def _apache_conf(files, with_python=True):
    puts(cyan(">>> Configuring Apache..." % env))

    conf_files = files
    conf_files = [(p, os.path.join(env.CFG_INVENIO_PREFIX, p)) for p in conf_files]

    if with_python:
        pyver = python_version()
        ctx = {'PYVER': pyver}
    else:
        ctx = {}
    ctx.update(env)

    for local_file, remote_file in conf_files:
        puts(">>> Writing %s ..." % remote_file)

        try:
            if not exists_local(os.path.dirname(remote_file)):
                sudo_local("mkdir -p %s" % os.path.dirname(remote_file), user=env.CFG_INVENIO_USER)
            write_template(remote_file, ctx, tpl_file=local_file, use_sudo=True)
        except TemplateNotFound:
            abort(red("Could not find template %s" % local_file))

    apache_conf = env.get('CFG_APACHE_CONF', '/etc/httpd/conf/httpd.conf')
    if confirm("Include created files in %s?" % apache_conf):
        if exists_local(apache_conf, use_sudo=True):
            lines = ["Include %s" % r for (l, r) in conf_files]
            if is_local():
                with settings(host_string="localhost"):
                    append(apache_conf, lines, use_sudo=True)
            else:
                append(apache_conf, lines, use_sudo=True)
        else:
            warn(red("File %s does not exists" % apache_conf))

    sudo_local("%(CFG_INVENIO_APACHECTL)s configtest" % env)
Exemplo n.º 21
0
def default_configure_hook(ctx):
    """
    Default way to configure a repo. Assumes repo has a configure script.
    """
    if exists_local(os.path.join(ctx['topsrcdir'], "configure")):
        with settings(warn_only=True):
            sudo_local("cd %(topsrcdir)s && make -s clean" % ctx, user=env.CFG_INVENIO_USER)
    sudo_local("cd %(topsrcdir)s && ./configure --prefix=%(CFG_INVENIO_PREFIX)s "
          "--with-python=%(CFG_INVENIO_PREFIX)s/bin/python" % ctx, user=env.CFG_INVENIO_USER)
    sudo_local("cd %(topsrcdir)s && make -s clean" % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 22
0
def git_checkout(repo, ref):
    """
    Checkout a specific git reference.
    """
    topsrcdir = repo_check(repo, workdir=True)

    ctx = {
        'topsrcdir': topsrcdir,
        'ref': ref,
    }
    ctx.update(env)

    # Stash uncommited changes
    if git_isdirty(topsrcdir):
        if not confirm("Working directory %(topsrcdir)s contains uncommited changes. Do you want to stash the changes?" % ctx):
            if not confirm("Do you want to reset the changes (required to continue)?" % ctx):
                abort("Cannot continue unless uncommitted changes are stashed or reset.")
            else:
                sudo_local("cd %(topsrcdir)s; git reset --hard HEAD" % ctx, user=env.CFG_INVENIO_USER)
        else:
            sudo_local("cd %(topsrcdir)s; git stash" % ctx, user=env.CFG_INVENIO_USER)

    sudo_local("cd %(topsrcdir)s; git checkout -f %(ref)s" % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 23
0
def rabbitmqctl(*commands):
    sudo_local("rabbitmqctl %s" % " ".join(commands))
Exemplo n.º 24
0
def haproxy_stop():
    """ Stop Celery """
    sudo_local("/etc/init.d/haproxy stop" % env)
Exemplo n.º 25
0
def haproxy_restart():
    """
    Restart Celery
    """
    sudo_local("/etc/init.d/haproxy restart" % env)
Exemplo n.º 26
0
def haproxy_start():
    """ Start Celery """
    sudo_local("/etc/init.d/haproxy start" % env)
Exemplo n.º 27
0
def venv_libxslt_install():
    """
    Install libxslt into virtualenv
    """
    ctx = {
        'libxslt': 'libxslt-1.1.26',
        'libxslt_ver': '1.1.26',
        'libxslt_path': os.path.join(env.CFG_INVENIO_PREFIX, 'libxslt-1.1.26'),
    }
    ctx.update(env)

    if exists_local(ctx['libxslt_path']):
        sudo_local("rm -Rf %(libxslt_path)s" % ctx, user=env.CFG_INVENIO_USER)
    if exists_local("%(libxslt_path)s.tar.gz" % ctx):
        sudo_local("rm -Rf %(libxslt_path)s.tar.gz" % ctx, user=env.CFG_INVENIO_USER)

    sudo_local("cd %(CFG_INVENIO_PREFIX)s; wget ftp://xmlsoft.org/libxml2/%(libxslt)s.tar.gz; tar -xzf %(libxslt)s.tar.gz" % ctx, user=env.CFG_INVENIO_USER)
    sudo_local("cd %(libxslt_path)s; ./configure --prefix=%(CFG_INVENIO_PREFIX)s --with-python=%(CFG_INVENIO_PREFIX)s/bin/python; make; cd python; make install" % ctx, user=env.CFG_INVENIO_USER)

    sudo_local("rm -Rf %(libxslt_path)s" % ctx, user=env.CFG_INVENIO_USER)
    sudo_local("rm -Rf %(libxslt_path)s.tar.gz" % ctx, user=env.CFG_INVENIO_USER)
Exemplo n.º 28
0
def inveniocfg(options):
    """
    Helper to run inveniocfg
    """
    #cmds = " && ".join([]) % env
    sudo_local(("%(CFG_INVENIO_PREFIX)s/bin/inveniocfg " % env) + options, user=env.CFG_INVENIO_USER)
Exemplo n.º 29
0
def inveniomanage(options):
    sudo_local(("%(CFG_INVENIO_PREFIX)s/bin/inveniomanage " % env) + options, user=env.CFG_INVENIO_USER)
Exemplo n.º 30
0
def venv_create():
    """
    Create virtualenv environment

    The virtualenv is created in ``env.CFG_INVENIO_PREFIX``, and will also
    create ``lib/python/invenio/`` and symlink it the virtualenv's
    site-packages, as well as ``var/tmp/ooffice-tmp-files`` (via sudo). If
    ``env.WITH_DEVSCRIPTS`` is ``True``, invenio-devscripts will be installed.
    If ``env.WITH_WORKDIR`` is ``True`` git-new-workdir will be installed.

    Lastly, it will append render the template ``activate-profile.tpl`` and
    append it to ``bin/activate``. The script will setup common needed
    environment variables that e.g. invenio-devscripts depend on.

    If an existing environment already exists, the user will be asked for
    confirmation to remove the directory (using sudo, due to the directory
    ``var/tmp/ooffice-tmp-files`` which is created using sudo).
    """
    # Checks
    if 'CFG_INVENIO_PREFIX' not in env:
        abort(red("CFG_INVENIO_PREFIX is not specified in env.") % env)

    puts(cyan(">>> Creating virtualenv in %(CFG_INVENIO_PREFIX)s..." % env))

    # Remove previous installation
    if exists_local(env.CFG_INVENIO_PREFIX):
        res = confirm("Remove installation in %(CFG_INVENIO_PREFIX)s ?" % env)
        if not res:
            abort(red("Cannot continue") % env)
        else:
            sudo_local("rm -Rf %(CFG_INVENIO_PREFIX)s" % env)

    # Create virtual environment
    dirname = os.path.dirname(env.CFG_INVENIO_PREFIX)
    basename = os.path.basename(env.CFG_INVENIO_PREFIX)

    sudo_local("mkdir -p %s" % dirname, user=env.CFG_INVENIO_USER)
    sudo_local("cd %s && virtualenv -p %s %s" % (dirname, env.PYTHON, basename), user=env.CFG_INVENIO_USER)

    # Create needed symboic links
    pyver = python_version()
    sudo_local("mkdir -p %(CFG_INVENIO_PREFIX)s/lib/python/invenio" % env, user=env.CFG_INVENIO_USER)
    sudo_local(("mkdir -p %(CFG_INVENIO_PREFIX)s/lib/python" + pyver + "/site-packages") % env, user=env.CFG_INVENIO_USER)
    sudo_local(("ln -s %(CFG_INVENIO_PREFIX)s/lib/python/invenio %(CFG_INVENIO_PREFIX)s/lib/python" + pyver + "/site-packages/invenio") % env, user=env.CFG_INVENIO_USER)

    # Write extras into the activate script
    write_template(os.path.join(env.CFG_INVENIO_PREFIX, 'bin/activate'), env, tpl_file='activate-profile.tpl', append=True, mark="ACTIVATE_PROFILE", use_sudo=True)

    # Install devscripts
    if env.WITH_DEVSCRIPTS:
        puts(">>> Installing invenio-devscripts...")
        sudo_local("cd %(CFG_INVENIO_PREFIX)s && git clone https://github.com/tiborsimko/invenio-devscripts.git" % env, user=env.CFG_INVENIO_USER)
        sudo_local("cd %(CFG_INVENIO_PREFIX)s && mv invenio-devscripts/* bin/" % env, user=env.CFG_INVENIO_USER)

    if env.WITH_WORKDIR:
        puts(">>> Installing git-new-workdir...")
        sudo_local('wget -O %(CFG_INVENIO_PREFIX)s/bin/git-new-workdir "http://repo.or.cz/w/git.git/blob_plain/HEAD:/contrib/workdir/git-new-workdir"' % env, user=env.CFG_INVENIO_USER)
        sudo_local("chmod +x %(CFG_INVENIO_PREFIX)s/bin/git-new-workdir" % env, user=env.CFG_INVENIO_USER)

    # OpenOffice temporary directory
    sudo_local("mkdir -p %(CFG_INVENIO_PREFIX)s/var/tmp/ooffice-tmp-files" % env, user=env.CFG_INVENIO_USER)
    sudo_local("sudo chown -R nobody %(CFG_INVENIO_PREFIX)s/var/tmp/ooffice-tmp-files" % env)
    sudo_local("sudo chmod -R 755 %(CFG_INVENIO_PREFIX)s/var/tmp/ooffice-tmp-files" % env)