Skip to content

Instantly share code, notes, and snippets.

@arkadijs
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkadijs/6094a5bb310145d10d38 to your computer and use it in GitHub Desktop.
Save arkadijs/6094a5bb310145d10d38 to your computer and use it in GitHub Desktop.
Fabric full cycle Groovy app deployment with process restart, pid file, apt, cron, tmux, credentials templating, groovy install.
import os
from fabric.api import *
from fabric.operations import run, sudo, get, put
from fabtools import require
from time import sleep
from ConfigParser import RawConfigParser
from StringIO import StringIO
# if passwordless sudo is required, please add
# /etc/sudoers.d/01-cloudf
# cloudf ALL=(ALL) NOPASSWD:ALL
env.user = 'cloudf'
env.hosts = ['server.example.com']
sudo_user = 'admin'
app = 'containers'
pid_file = 'ws{}.pid'.format(app)
tmux_session = 'tmux new-session -d'
tmux_session_at_reboot = '@reboot ' + tmux_session
service = 'tmux new-window -d "exec bash -i -c \'cd $HOME/{} && /usr/local/groovy/bin/groovy wscontainers.groovy; echo ======= exit code \\\\$?; read\'"'.format(app)
service_at_reboot = '@reboot sleep 5 && ' + service
aws_config = '{}/.aws/config'.format(os.environ['HOME'])
csc_config = '_csc_credentials'
wscontainers = 'wscontainers.groovy'
files = ['cloudformation.json', 'cloud-config.yml', 'app.sh']
groovy_version = '2.3.9'
@task
def packages():
_user = env.user; env.user = sudo_user
require.deb.uptodate_index(max_age={'day': 1})
require.deb.packages(['openjdk-7-jdk', 'curl', 'unzip'])
env.user = _user
@task
def groovy():
packages()
_user = env.user; env.user = sudo_user
with cd('/usr/local'):
zip_file = 'groovy-binary-{}.zip'.format(groovy_version)
sudo('curl -sSL --retry 5 --retry-delay 2 -O http://dl.bintray.com/groovy/maven/' + zip_file)
sudo('unzip -q ' + zip_file)
sudo('rm -rf groovy/ ' + zip_file)
sudo('mv groovy-{} groovy'.format(groovy_version))
env.user = _user
@task
def upload():
aws = RawConfigParser()
aws.read(aws_config)
with open(csc_config) as cscfile:
csc = [l.rstrip('\n') for l in cscfile]
with open(wscontainers) as wsfile:
script = wsfile.read()
script = script.replace('{{ws_aws_access_key}}', aws.get('default', 'aws_access_key_id')) \
.replace('{{ws_aws_secret_key}}', aws.get('default', 'aws_secret_access_key')) \
.replace('{{ws_csc_user}}', csc[0]) \
.replace('{{ws_csc_password}}', csc[1])
put(StringIO(script), '{}/{}'.format(app, wscontainers))
for f in files:
put(f, app, mirror_local_mode=True)
def _tmux():
with settings(warn_only=True):
no_tmux_session = run('tmux list-sessions').failed
if no_tmux_session:
run(tmux_session)
@task
def terminate():
with cd(app):
with settings(warn_only=True):
do_pkill = True
pid = StringIO()
if get(pid_file, local_path=pid).succeeded:
pid = pid.getvalue()
if pid:
pid = pid.strip()
if pid.isdigit():
do_pkill = run('kill ' + pid).failed
if not do_pkill:
sleep(2)
run('kill -9 ' + pid)
run('rm ' + pid_file)
if do_pkill:
run('pkill -f {0} && sleep 2 && pkill -9 -f {0}'.format('java.*ws{}\\\\.groovy'.format(app)))
@task
def start():
run(service)
# not using fabtools.cron, but user crontab instead
# https://github.com/ronnix/fabtools/blob/master/fabtools/cron.py
def _crontab(cmd, grep_not):
crontab = [line for line in run('crontab -l').splitlines() if grep_not not in line]
crontab.insert(0, cmd)
new = app + '/crontab.txt'
put(StringIO('\n'.join(crontab)), new)
run('crontab ' + new)
@task
def crontab():
_crontab(service_at_reboot, 'groovy wscontainers')
_crontab(tmux_session_at_reboot, 'tmux new-session')
@task
def deploy():
upload()
_tmux()
terminate()
#crontab()
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment