def get_release_properties(): # get current version from maven current_version = get_current_version() # remove -SNAPSHOT for release version release_version = current_version.replace("-SNAPSHOT", "") # scm tag scm_tag = "uht-traktor-" + release_version # get next version based on current release version new_dev_version = release_version last_num = re.compile(r'(?:[^\d]*(\d+)[^\d]*)+') m = last_num.search(release_version) if m: next = str(int(m.group(1))+1) start, end = m.span(1) new_dev_version = release_version[:max(end-len(next), start)] + next + release_version[end:] new_dev_version = new_dev_version + "-SNAPSHOT" # prompt for the final values, give default values from above release_version = prompt("What is the release version: ", None, release_version) scm_tag = prompt("What is SCM release tag or label: ", None, scm_tag) new_dev_version = prompt("What is the new development version: ", None, new_dev_version) # new dev version must end with -SNAPSHOT as otherwise we will run into maven troubles later if not new_dev_version.endswith("-SNAPSHOT"): abort(red("Invalid new development version, must end with '-SNAPSHOT'.")) return [release_version, scm_tag, new_dev_version]
def test(): result = run('cd /srv/liberweb/liberweb/ && source virtual/bin/activate && cd petateca && python manage.py test serie') if result.failed: abort('Fallo en el test de serie') result = run('cd /srv/liberweb/liberweb/ && source virtual/bin/activate && cd petateca && python manage.py test api') if result.failed: abort('Fallo en el test de API')
def solr_update_index(): print('SOLR: rebuilding index') if not exists(config('basedir')): abort( "Directory %s doesn't exist yet. Run 'create_srv_dir'." % config('basedir')) with cd(config('basedir')): if confirm("Are you sure you want to update the index?"): sudo("bin/django update_index", user='******')
def solr_rebuild_index(): print('SOLR: rebuilding index') if not exists(config('basedir')): abort( "Directory %s doesn't exist yet. Run 'create_srv_dir'." % config('basedir')) with cd(config('basedir')): if confirm("Rebuilding (instead of updating) the index, are you sure?"): sudo("bin/django rebuild_index", user='******')
def switch_and_buildout(): """Checks out latest tag and runs buildout.""" if not exists(config('basedir')): abort( "Directory %s doesn't exist yet. Run 'create_srv_dir'." % config('basedir')) with cd(config('basedir')): # Confirm if config('checkout') == 'trunk': if not confirm("Switch to trunk on %s?" % env['host'], default=True): abort("Keeping previous checkout.") elif config('checkout') == 'tag': if not confirm("Switch to tag %s on %s?" % ( config('tag'), env['host']), default=True): abort("Keeping previous checkout.") else: abort("Config('checkout') should be either 'trunk' or 'tag'.") if config('svn'): if config('checkout') == 'tag': tag_url = config('vcs').tag_url(config('tag')) else: tag_url = config('vcs').tag_url('').replace('tags/', 'trunk/') print("Switching to %s" % tag_url) sudo("svn switch %s" % tag_url, user='******') if config('git'): sudo("git fetch", user='******') if config('checkout') == 'tag': sudo("git checkout %s" % config('tag'), user='******') else: sudo("git checkout master", user='******') sudo("git merge origin/master", user='******') check_is_symlink = ("python -c \"import os;" + "print(os.path.islink('buildout.cfg'))\"") if exists(os.path.join(config('basedir'), 'buildout.cfg')): if 'True' in sudo(check_is_symlink, user='******'): sudo('rm buildout.cfg') else: abort("buildout.cfg isn't a symlink.") sudo("ln -s %s buildout.cfg" % config('buildout-file'), user='******') if not exists(os.path.join(config('basedir'), 'bin', 'buildout')): print("bin/buildout doesn't exist yet. Running bootstrap.") sudo("python bootstrap.py", user='******') sudo("bin/buildout", user='******')
def solr_build_schema(): print('SOLR: building schema') if not exists(config('basedir')): abort( "Directory %s doesn't exist yet. Run 'create_srv_dir'." % config('basedir')) with cd(config('basedir')): sudo(("bin/django build_solr_schema > " "{basedir}/etc/solr/conf/schema.xml".format( basedir=config('basedir')) ), user='******')
def remote_production_data_restore(environment, version=None): """Restore latests database and media files""" env.ver = version or remote_production_latest_version() ver_dir = "v%d" % env.ver env.backup_location = os.path.join(env.backup_folder, ver_dir) if environment == "staging": env.restore_location = env.staging_folder elif environment == "production": env.restore_location = os.path.join(env.production_folder, ver_dir) else: operations.abort("unknown '%s' restore environment" % environment) with settings(warn_only=True): return run('cd %(restore_location)s && bin/fab local_production_data_restore:%(backup_location)s -H localhost' % env).succeeded
def local_production_data_restore(backup_location): """Restore latests database and media files""" local_clear_database() env.backup_location = backup_location if not exists(env.backup_location): operations.abort(red("No backup yet: %(backup_location)s" % env, bold=True)) # restore static files local('tar xvfz %(backup_location)s/mediafiles.tar.gz -C media/' % env) # restore database django.project(env.django_project) from django.conf import settings env.update(settings.DATABASES['default']) local('pg_restore -Fc --no-acl -e --no-owner -p %(PORT)s -U %(USER)s -d %(NAME)s %(backup_location)s/db.sql' % env)
def secure_create_directory(filepath, user_group, mode=755): user, group = user_group.split(':') missing_owner_code = 42 command = \ "( getent passwd {user} >/dev/null || exit {missing_owner_code} ) && " \ "mkdir -p {filepath} && " \ "chown {user_group} {filepath} && " \ "chmod {mode} {filepath} ".format( filepath=filepath, user=user, user_group=user_group, mode=mode, missing_owner_code=missing_owner_code) with settings(warn_only=True): result = sudo(command) if result.return_code == missing_owner_code: abort("User %s does not exist. Make sure the Presto server RPM " "is installed and try again" % user) elif result.failed: abort("Failed to securely create file %s" % (filepath))
def remote_production_rollback(): """Clean latest production and deploy previous""" env.environment = 'production' env.ver = remote_production_latest_version() env.prev_ver = env.ver - 1 print red("Starting rollback...", bold=True) with cd(env.production_folder): # cleanup with settings(warn_only=True): run('v%d/bin/supervisorctl shutdown' % env.ver) run('rm -rf v%d' % env.ver) if env.ver == 1: operations.abort('Could not rollback since this is first production deploy.') with cd('v%d' % env.prev_ver): remote_production_data_restore('production', env.prev_ver) deploy()
def install_defaults(): """Populates sane defaults""" # install default buildout run('mkdir -p %(home_folder)s/.buildout/{eggs,downloads}' % env) run('mkdir -p %(production_folder)s' % env) run('mkdir -p %(production_media_folder)s' % env) upload_template('etc/default.cfg.in', '%(home_folder)s/.buildout/default.cfg' % env, env) # warn about ssh pub key for -H localhost with settings(warn_only=True): if run('grep -q -f ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys').return_code != 0: operations.abort('%(user)s must be able to run "ssh localhost", please configure .ssh/authorized_keys' % env) # warn about localsettings for f in [env.staging_django_settings, env.production_django_settings]: if not exists(f): operations.abort('%s does not exists. Please upload the file and rerun fabric.' % f) # prepare code if not exists(env.code_folder): run('git clone -b %(branch)s %(repository)s %(code_folder)s' % env)
def local_production_data_restore(backup_location): """Restore latests database and media files""" local_clear_database() env.backup_location = backup_location if not exists(env.backup_location): operations.abort(red("No backup yet: %(backup_location)s" % env, bold=True)) # restore static files local("tar xvfz %(backup_location)s/mediafiles.tar.gz -C media/" % env) # restore database django.settings_module(env.django_project) from django.conf import settings env.update(settings.DATABASES["default"]) local( "pg_restore --list %(backup_location)s/db.sql | grep -v LANGUAGE | grep -v FUNCTION | grep -v AGGREGATE > %(backup_location)s/db.list" % env ) local( "pg_restore -Fc --no-acl -e --no-owner -p %(PORT)s -U %(USER)s -d %(NAME)s -L %(backup_location)s/db.list %(backup_location)s/db.sql" % env )