Last active
July 3, 2017 10:20
-
-
Save dkarchmer/dd99c06d03aff74442ca to your computer and use it in GitHub Desktop.
Fabric based deploy script for a Python/Django Server with a Docker based worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Example of a deployment script for: | |
- A Django based server running as an Elastic Beanstalk server tier, with Load Balancing | |
(Assumes a pre-configured RDS database - best practice) | |
- A Docker based running as an Elastic Beanstalk worker tier | |
Assumes following directory structure | |
- ./server # With Django based project and appropriate .ebextensions | |
- ./worker # with Docker based project and appropriate .ebextensions, Dockerfile and Dockerrun.aws.json file | |
# (similar to https://github.com/dkarchmer/aws-eb-docker-django) | |
fab create_server | |
fab create_single_worker | |
''' | |
AWS_PROFILE = 'myAWSProfileName' | |
AWS_REGION = 'us-east-1' | |
DEFAULT_SERVER_APP_NAME = 'web' | |
DEFAULT_SERVER_ENV_NAME = 'web-1' | |
DEFAULT_WORKER_APP_NAME = 'worker' | |
DEFAULT_WORKER_ENV_NAME = 'worker-1' | |
PROFILE_OPT = '--profile {profile}'.format(profile=AWS_PROFILE) | |
REGION_OPT = '--region {region}'.format(region=AWS_REGION) | |
SERVER_AMI = '64bit Amazon Linux 2015.09 v2.0.4 running Python 3.4' | |
WORKER_AMI = '64bit Amazon Linux 2015.09 v2.0.4 running Docker 1.7.1' | |
def cleanup(): | |
local('find . -name "*.pyc" -depth -exec rm {} ";"') | |
local('find . -name "__pycache__" -depth -exec rm -r {} ";"') | |
def create_server(name=DEFAULT_SERVER_ENV_NAME): | |
cleanup() | |
with lcd('server'): | |
local('eb init -p "{ami}" {region} {profile} {name}'.format(region=REGION_OPT, | |
ami=SERVER_AMI, | |
profile=PROFILE_OPT, | |
name=DEFAULT_SERVER_APP_NAME)) | |
basic = '--timeout 30 --instance_type t2.micro' | |
local("eb create {basic} {region} {profile} -c {cname} {name}".format(basic=basic, | |
region=REGION_OPT, | |
profile=PROFILE_OPT, | |
cname=name, | |
name=name)) | |
def create_single_worker(name=DEFAULT_WORKER_APP_NAME, count=1): | |
cleanup() | |
with lcd('worker'): | |
# Latest docker image | |
local('eb init -p {ami} {region} {profile} {name}-{count}'.format(region=REGION_OPT, | |
profile=PROFILE_OPT, | |
ami=SERVER_AMI, | |
name=DEFAULT_WORKER_APP_NAME, | |
count=count)) | |
basic = '-t worker --timeout=30 --instance_type t2.small --single' | |
local('eb create {basic} {region} {profile} {name}-{count}'.format(basic=basic, | |
profile=PROFILE_OPT, | |
region=REGION_OPT, | |
count=count, | |
name=name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment