def create_virtualenv(venv_path, user, permissions='0750'): "Creates a virtualenv" check_on_path('virtualenv') if not files.exists(os.path.join(venv_path, "bin/activate")): create_directories(venv_path, user, permissions) puts(info='Creating virtualenv at %s' % venv_path) run("virtualenv --no-site-packages %s" % venv_path) puts(success='virtualenv created') return 1 else: puts(info='virtualenv already exists at %s' % venv_path)
def git_clone(repo_path, destination, user): """ Clone the git repository at repo_path to destination. If destination doesn't exist, it will be created and owned by user. """ check_on_path('git') puts(info="Cloning git repository %s into %s" % (repo_path, destination)) with settings(warn_only=True): if run("test -d %s" % destination).failed: puts(info="Creating destination directory %s" % destination) create_directories(destination, user) puts(success="Destination directory created") run('git clone %s %s' % (repo_path, destination)) puts(success="Repository cloned")
def backup_database(project_name, project_dir, destination_dir): """ Backs-up the database """ dump_file = "%s-prod-%s.sql" % (project_name, datetime.now().strftime('%Y%m%d_%H%M%S')) temp_dump_path = os.path.join('/tmp', dump_file) puts(info="Backing up database") with prefix(activate_venv()): with cd(project_dir): DATABASE_USER = run( "python -c 'import settings_production;" "print settings_production.DATABASES[\"default\"][\"USER\"]'") DATABASE_PASSWORD = run( "python -c 'import settings_production;" "print settings_production.DATABASES[\"default\"][\"PASSWORD\"]'" ) DATABASE_NAME = run( "python -c 'import settings_production;" "print settings_production.DATABASES[\"default\"][\"NAME\"]'") run('unset HISTFILE && mysqldump -u %s -p%s %s > %s' % (DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME, temp_dump_path)) puts(success="Database backed up to %s" % temp_dump_path) puts(info="Compressing database backup") run('bzip2 %s' % temp_dump_path) temp_dump_path += '.bz2' puts(success="Database backup compressed") puts(info="Moving backup to %s" % destination_dir) with settings(warn_only=True): if sudo("test -d %s" % destination_dir).failed: puts(info="Creating destination directory %s" % destination_dir) create_directories(destination_dir, 'root') puts(success="Destination directory created") sudo('mv %s %s' % (temp_dump_path, destination_dir)) puts(success="Database moved to %s" % destination_dir)
def backup_database(project_name, project_dir, destination_dir): """ Backs-up the database """ dump_file = "%s-prod-%s.sql" % (project_name, datetime.now().strftime('%Y%m%d_%H%M%S')) temp_dump_path = os.path.join('/tmp', dump_file) puts(info="Backing up database") with prefix(activate_venv()): with cd(project_dir): DATABASE_USER = run("python -c 'import settings_production;" "print settings_production.DATABASES[\"default\"][\"USER\"]'") DATABASE_PASSWORD = run("python -c 'import settings_production;" "print settings_production.DATABASES[\"default\"][\"PASSWORD\"]'") DATABASE_NAME = run("python -c 'import settings_production;" "print settings_production.DATABASES[\"default\"][\"NAME\"]'") run('unset HISTFILE && mysqldump -u %s -p%s %s > %s' % ( DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME, temp_dump_path)) puts(success="Database backed up to %s" % temp_dump_path) puts(info="Compressing database backup") run('bzip2 %s' % temp_dump_path) temp_dump_path += '.bz2' puts(success="Database backup compressed") puts(info="Moving backup to %s" % destination_dir) with settings(warn_only=True): if sudo("test -d %s" % destination_dir).failed: puts(info="Creating destination directory %s" % destination_dir) create_directories(destination_dir, 'root') puts(success="Destination directory created") sudo('mv %s %s' % (temp_dump_path, destination_dir)) puts(success="Database moved to %s" % destination_dir)