def environment(env_name, debug=False): """ Creates the configurations for the environment in which tasks will run. """ schemas_dir = "wordpress-workflow/json_schemas/" state.output['running'] = boolean(debug) state.output['stdout'] = boolean(debug) print "Establishing environment " + blue(env_name, bold=True) + "..." try: set_env_from_json_file('environments.json', env_name, schemas_dir + "environment_schema.json") if env_name == "vagrant": result = ulocal('vagrant ssh-config | grep IdentityFile', capture=True) env.key_filename = result.split()[1].replace('"', '') except ValueError: print red("environments.json has wrong format.", bold=True) sys.exit(1) try: set_env_from_json_file('settings.json', schema_path=schemas_dir + "settings_schema.json") except ValueError: print red("settings.json has wrong format.", bold=True) sys.exit(1)
def environment(env_name, debug=False): """ Creates the configurations for the environment in which tasks will run. """ schemas_dir = "wordpress-workflow/json_schemas/" state.output['running'] = boolean(debug) state.output['stdout'] = boolean(debug) print "Establishing environment " + blue(env_name, bold=True) + "..." try: set_env_from_json_file( 'environments.json', env_name, schemas_dir + "environment_schema.json" ) if env_name == "vagrant": result = ulocal('vagrant ssh-config | grep IdentityFile', capture=True) env.key_filename = result.split()[1].replace('"', '') except ValueError: print red("environments.json has wrong format.", bold=True) sys.exit(1) try: set_env_from_json_file( 'settings.json', schema_path=schemas_dir + "settings_schema.json" ) except ValueError: print red("settings.json has wrong format.", bold=True) sys.exit(1)
def certs(git_ref): """ Update certs in server. """ require('hosts', 'user', 'site_dir', 'django_settings') # Retrives git reference metadata and creates a temp directory with the # contents resulting of applying a ``git archive`` command. message = white('Creating git archive from {0}'.format(git_ref), bold=True) with cmd_msg(message): repo = ulocal('basename `git rev-parse --show-toplevel`', capture=True) commit = ulocal('git rev-parse --short {0}'.format(git_ref), capture=True) tmp_dir = '/tmp/blob-{0}-{1}/'.format(repo, commit) ulocal('rm -fr {0}'.format(tmp_dir)) ulocal('mkdir {0}'.format(tmp_dir)) ulocal('git archive {0} ./certs | tar -xC {1} --strip 1'.format( commit, tmp_dir)) # Uploads the code of the temp directory to the host with rsync telling # that it must delete old files in the server, upload deltas by checking # file checksums recursivelly in a zipped way; changing the file # permissions to allow read, write and execution to the owner, read and # execution to the group and no permissions for any other user. with cmd_msg(white('Uploading code to server...', bold=True)): ursync_project(local_dir=tmp_dir, remote_dir=env.certs_dir, delete=True, default_opts='-chrtvzP', extra_opts='--chmod=750', exclude=["*.pyc", "env/", "cover/"]) # Clean the temporary snapshot files that was just deployed to the host message = white('Cleaning up...', bold=True) with cmd_msg(message): ulocal('rm -fr {0}'.format(tmp_dir)) puts(green(SUCCESS_ART), show_prefix=False) puts(white('Code from {0} was succesfully deployed to host {1}'.format( git_ref, ', '.join(env.hosts)), bold=True), show_prefix=False)
def environment(env_name): """ Creates a dynamic environment based on the contents of the given environments_file. """ if env_name == 'vagrant': result = ulocal('vagrant ssh-config | grep IdentityFile', capture=True) env.key_filename = result.split()[1].replace('"', '') set_env_from_json_file('environments.json', env_name)
def wordpress_workflow_upgrade(repository='origin', branch='master'): """ Upgrades wordpress-workflow """ #upgrades code to current master os.chdir('wordpress-workflow') ulocal('git fetch origin') ulocal('git pull {0} {1}'.format(repository, branch)) os.chdir('../') #Updates vagrant provision ulocal('wordpress-workflow/startProject.sh') ulocal('vagrant provision') print green('wordpress-workflow upgraded', bold=True)
def deploy(git_ref, upgrade=False): """ Deploy the code of the given git reference to the previously selected environment. Pass ``upgrade=True`` to upgrade the versions of the already installed project requirements (with pip). """ require('hosts', 'user', 'group', 'site_dir', 'django_settings') # Retrives git reference metadata and creates a temp directory with the # contents resulting of applying a ``git archive`` command. message = white('Creating git archive from {0}'.format(git_ref), bold=True) with cmd_msg(message): repo = ulocal( 'basename `git rev-parse --show-toplevel`', capture=True) commit = ulocal( 'git rev-parse --short {0}'.format(git_ref), capture=True) branch = ulocal( 'git rev-parse --abbrev-ref HEAD', capture=True) tmp_dir = '/tmp/blob-{0}-{1}/'.format(repo, commit) ulocal('rm -fr {0}'.format(tmp_dir)) ulocal('mkdir {0}'.format(tmp_dir)) ulocal('git archive {0} ./src | tar -xC {1} --strip 1'.format( commit, tmp_dir)) # Uploads the code of the temp directory to the host with rsync telling # that it must delete old files in the server, upload deltas by checking # file checksums recursivelly in a zipped way; changing the file # permissions to allow read, write and execution to the owner, read and # execution to the group and no permissions for any other user. with cmd_msg(white('Uploading code to server...', bold=True)): ursync_project( local_dir=tmp_dir, remote_dir=env.site_dir, delete=True, default_opts='-chrtvzP', extra_opts='--chmod=750', exclude=[ "*.pyc", "env/", "cover/", "*.style", "bower_components" ] ) # Performs the deployment task, i.e. Install/upgrade project # requirements, syncronize and migrate the database changes, collect # static files, reload the webserver, etc. message = white('Running deployment tasks', bold=True) with cmd_msg(message, grouped=True): with virtualenv(): message = white('Installing Python requirements with pip') with cmd_msg(message, spaces=2): run('pip install -{0}r ./requirements/production.txt'.format( 'U' if upgrade else '')) message = white('Migrating database') with cmd_msg(message, spaces=2): run('python manage.py migrate --noinput') message = white('Collecting static files') with cmd_msg(message, spaces=2): run('python manage.py collectstatic --noinput') message = white('Setting file permissions') with cmd_msg(message, spaces=2): run('chgrp -R {0} .'.format(env.group)) run('chgrp -R {0} ../media'.format(env.group)) message = white('Restarting webserver') with cmd_msg(message, spaces=2): run('touch ../reload') message = white('Registering deployment') with cmd_msg(message, spaces=2): register_deployment(commit, branch) # Clean the temporary snapshot files that was just deployed to the host message = white('Cleaning up...', bold=True) with cmd_msg(message): ulocal('rm -fr {0}'.format(tmp_dir)) puts(green(SUCCESS_ART), show_prefix=False) puts(white('Code from {0} was succesfully deployed to host {1}'.format( git_ref, ', '.join(env.hosts)), bold=True), show_prefix=False)
def deploy(git_ref, upgrade=False): """Deploy project. Deploy the code of the given git reference to the previously selected environment. Args: upgrade(Optional[bool]): Pass ``upgrade=True`` to upgrade the versions of the already installed project requirements (with pip) git_ref(str): name branch you make deploy. Example: >>>fab environment:vagrant deploy:devel. """ require('hosts', 'user', 'group', 'site_dir', 'django_settings') # Retrives git reference metadata and creates a temp directory with the # contents resulting of applying a ``git archive`` command. message = white('Creating git archive from {0}'.format(git_ref), bold=True) with cmd_msg(message): repo = ulocal('basename `git rev-parse --show-toplevel`', capture=True) commit = ulocal('git rev-parse --short {0}'.format(git_ref), capture=True) branch = ulocal('git rev-parse --abbrev-ref HEAD', capture=True) tmp_dir = '/tmp/blob-{0}-{1}/'.format(repo, commit) ulocal('rm -fr {0}'.format(tmp_dir)) ulocal('mkdir {0}'.format(tmp_dir)) ulocal('git archive {0} ./src | tar -xC {1} --strip 1'.format( commit, tmp_dir)) # Uploads the code of the temp directory to the host with rsync telling # that it must delete old files in the server, upload deltas by checking # file checksums recursivelly in a zipped way; changing the file # permissions to allow read, write and execution to the owner, read and # execution to the group and no permissions for any other user. with cmd_msg(white('Uploading code to server...', bold=True)): ursync_project(local_dir=tmp_dir, remote_dir=env.site_dir, delete=True, default_opts='-chrtvzP', extra_opts='--chmod=750', exclude=["*.pyc", "env/", "cover/"]) # Performs the deployment task, i.e. Install/upgrade project # requirements, syncronize and migrate the database changes, collect # static files, reload the webserver, etc. message = white('Running deployment tasks', bold=True) with cmd_msg(message, grouped=True): with virtualenv(): message = white('Installing Python requirements with pip') with cmd_msg(message, spaces=2): run('pip install -{0}r ./requirements/production.txt'.format( 'U' if upgrade else '')) message = white('Migrating database') with cmd_msg(message, spaces=2): run('python manage.py migrate --noinput') message = white('Collecting static files') with cmd_msg(message, spaces=2): run('python manage.py collectstatic --noinput') message = white('Setting file permissions') with cmd_msg(message, spaces=2): run('chgrp -R {0} .'.format(env.group)) run('chgrp -R {0} ../media'.format(env.group)) message = white('Restarting webserver') with cmd_msg(message, spaces=2): run('touch ../reload') message = white('Registering deployment') with cmd_msg(message, spaces=2): register_deployment(commit, branch) # Clean the temporary snapshot files that was just deployed to the host message = white('Cleaning up...', bold=True) with cmd_msg(message): ulocal('rm -fr {0}'.format(tmp_dir)) puts(green(SUCCESS_ART), show_prefix=False) puts(white('Code from {0} was succesfully deployed to host {1}'.format( git_ref, ', '.join(env.hosts)), bold=True), show_prefix=False)
def deploy(git_ref, upgrade=False): """ Deploy the code of the given git reference to the previously selected environment. Pass ``upgrade=True`` to upgrade the versions of the already installed project requirements (with pip). """ require('hosts', 'user', 'group', 'site_dir', 'environment') # Retrives git reference metadata and creates a temp directory with the # contents resulting of applying a ``git archive`` command. message = white('Creating git archive from {0}'.format(git_ref), bold=True) with cmd_msg(message): repo = ulocal( 'basename `git rev-parse --show-toplevel`', capture=True) commit = ulocal( 'git rev-parse --short {0}'.format(git_ref), capture=True) tmp_dir = '/tmp/blob-{0}-{1}/'.format(repo, commit) ulocal('rm -fr {0}'.format(tmp_dir)) ulocal('mkdir {0}'.format(tmp_dir)) ulocal('git archive {0} ./src | tar -xC {1} --strip 1'.format( commit, tmp_dir)) # Uploads the code of the temp directory to the host with rsync telling # that it must delete old files in the server, upload deltas by checking # file checksums recursivelly in a zipped way; changing the file # permissions to allow read, write and execution to the owner, read and # execution to the group and no permissions for any other user. with cmd_msg(white('Uploading code to server...', bold=True)): ursync_project( local_dir=tmp_dir, remote_dir=env.site_dir, delete=True, default_opts='-chrtvzP', extra_opts='--chmod=750', exclude=["*.env", "*.lock", "storage/", "vendor/"] ) # Performs the deployment task, i.e. Install/upgrade project # requirements, syncronize and migrate the database changes, # reload the webserver, etc. message = white('Running deployment tasks', bold=True) with cmd_msg(message, grouped=True): with cd(env.site_dir): if upgrade: message = white('Update packages with composer') with cmd_msg(message, spaces=2): run('composer update') else: message = white('Installing packages with composer') with cmd_msg(message, spaces=2): run('composer install') message = white('Maintenance mode ON') with cmd_msg(message, spaces=2): run('php artisan down') message = white('Migrating database') with cmd_msg(message, spaces=2): run('php artisan migrate --env={0}'.format(env.environment)) message = white('Setting file permissions') with cmd_msg(message, spaces=2): run('chgrp -R {0} .'.format(env.group)) message = white('Maintenance mode OFF') with cmd_msg(message, spaces=2): run('php artisan up') # Clean the temporary snapshot files that was just deployed to the host message = white('Cleaning up...', bold=True) with cmd_msg(message): ulocal('rm -fr {0}'.format(tmp_dir)) puts(green(SUCCESS_ART), show_prefix=False) puts(white('Code from {0} was succesfully deployed to host {1}'.format( git_ref, ', '.join(env.hosts)), bold=True), show_prefix=False)
def deploy(git_ref, upgrade=False): """ Deploy the code of the given git reference to the previously selected environment. Pass ``upgrade=True`` to upgrade the versions of the already installed project requirements (with pip). """ require('hosts', 'user', 'group', 'site_dir', 'django_settings') # Retrives git reference metadata and creates a temp directory with the # contents resulting of applying a ``git archive`` command. message = white('Creating git archive from {0}'.format(git_ref), bold=True) with cmd_msg(message): repo = ulocal('basename `git rev-parse --show-toplevel`', capture=True) commit = ulocal('git rev-parse --short {0}'.format(git_ref), capture=True) branch = ulocal('git rev-parse --abbrev-ref HEAD', capture=True) tmp_dir = '/tmp/blob-{0}-{1}/'.format(repo, commit) ulocal('rm -fr {0}'.format(tmp_dir)) ulocal('mkdir {0}'.format(tmp_dir)) ulocal('git archive {0} ./src | tar -xC {1} --strip 1'.format( commit, tmp_dir)) # Puts the site into maintenance mode. with cmd_msg(white('Enabling maintenance mode', bold=True)): maintenance('on') # Uploads the code of the temp directory to the host with rsync telling # that it must delete old files in the server, upload deltas by checking # file checksums recursivelly in a zipped way; changing the file # permissions to allow read, write and execution to the owner, read and # execution to the group and no permissions for any other user. with cmd_msg(white('Uploading code to server', bold=True)): ursync_project(local_dir=tmp_dir, remote_dir=env.site_dir, delete=True, default_opts='-chrtvzP', extra_opts='--chmod=750', exclude=[ "*.pyc", "env/", "cover/", "*.style", "bower_components", ]) # Performs the deployment task, i.e. Install/upgrade project # requirements, syncronize and migrate the database changes, collect # static files, reload the webserver, etc. message = white('Running deployment tasks', bold=True) with cmd_msg(message, grouped=True): with virtualenv(): message = 'Installing Python requirements with pip' with cmd_msg(message, spaces=2): install_requirements(upgrade=upgrade) message = 'Migrating database' with cmd_msg(message, spaces=2): migrate(noinput=True) message = 'Installing bower components' with cmd_msg(message, spaces=2): bower_install() message = 'Collecting static files' with cmd_msg(message, spaces=2): collectstatic() message = 'Setting file permissions' with cmd_msg(message, spaces=2): run('chgrp -R {0} .'.format(env.group)) run('chgrp -R {0} ../media'.format(env.group)) message = 'Restarting webserver' with cmd_msg(message, spaces=2): run('touch ../reload') message = 'Restarting celery workers' with cmd_msg(message, spaces=2): run('sudo /usr/bin/supervisorctl restart {0}-celeryd'.format( env.user)) message = 'Registering deployment' with cmd_msg(message, spaces=2): register_deployment(commit, branch) # Disable maintenance mode. with cmd_msg(white('Disabling maintenance mode', bold=True)): maintenance('off') # Clean the temporary snapshot files that was just deployed to the host message = white('Cleaning up...', bold=True) with cmd_msg(message): ulocal('rm -fr {0}'.format(tmp_dir)) puts(green(SUCCESS_ART), show_prefix=False) puts(white('Code from {0} was succesfully deployed to host {1}'.format( git_ref, ', '.join(env.hosts)), bold=True), show_prefix=False)