Skip to content

Instantly share code, notes, and snippets.

@roadsideseb
Last active January 26, 2016 23:07
Show Gist options
  • Save roadsideseb/3e109b052e417270ce04 to your computer and use it in GitHub Desktop.
Save roadsideseb/3e109b052e417270ce04 to your computer and use it in GitHub Desktop.
Deploying image to tutum.
#!/usr/bin/env python
import sys
import yaml
import click
import subprocess
@click.group()
def cli():
pass
@cli.command()
@click.option('--filename', default='tutum.yml')
def bump_version(filename):
with open(filename) as tutum_config:
config = yaml.safe_load(tutum_config.read())
image_name = config.get('web', {}).get('image', '')
if not image_name:
print 'Invalid image name. Aborting'
sys.exit(1)
try:
__, version_string = image_name.split(':')
except ValueError:
version_string = ''
try:
version = int(version_string.split('-', 1)[0]) + 1
except (KeyError, ValueError):
version = 1
git_sha = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
click.echo('{:05}-{}'.format(version, git_sha), nl=False)
@cli.command()
@click.option('--filename', default='tutum.yml')
@click.option('--service', default='web')
@click.argument('version')
def set_version(filename, service, version):
with open(filename) as tutum_config:
config = yaml.safe_load(tutum_config.read())
image_name, old_version = config.get('web', {}).get('image', '').rsplit(':')
if not version:
version = bump_version(filename)
if version == old_version:
return
config[service]['image'] = '{}:{}'.format(image_name, version)
lines = []
with open(filename) as tutum_config:
for line in tutum_config:
if image_name in line:
key, value = line.split(':', 1)
line = '{}: {}:{}\n'.format(key, image_name, version)
lines.append(line)
with open(filename, 'w') as tutum_config:
tutum_config.write(''.join(lines))
if __name__ == '__main__':
cli()
Status API Training Shop Blog About Pricing
.PHONY: deploy build static-upload
SHELL := /bin/bash
base_image := elbaschid/python-base
builder_image := elbaschid/django-builder
tester_image := elbaschid/django-tester
image := tutum.co/elbaschid/love-fear
image_version := $(shell python deployer.py bump_version)
build_dir := ./.build
venv_dir := ${build_dir}/venv
static_dir := ${build_dir}/static
git_sha := $(shell git rev-parse --verify HEAD --short)
base-image:
docker build --rm -t $(base_image) -f docker/python-base.Dockerfile .
builder-image:
docker build --rm -t $(builder_image) -f docker/django-builder.Dockerfile .
tester-image:
docker build --rm -t $(tester_image) -f docker/django-tester.Dockerfile .
images:
${MAKE} base-image
${MAKE} builder-image
${MAKE} tester-image
image:
docker build --rm -t $(image) -f docker/django-deploy.Dockerfile .
build:
- rm -rf $(build_dir)
docker-compose -f builder.yml run builder
stack-update:
source .env_prod && tutum stack update love-and-fear -f tutum.yml
tutum-update:
docker tag $(image) $(image):$(image_version)
docker push $(image):$(image_version)
tutum service set --image $(image):$(image_version) web.love-and-fear
python deployer.py set_version $(image_version)
tutum-deploy:
tutum service redeploy web.love-and-fear
deploy:
${MAKE} image
${MAKE} tutum-update
${MAKE} tutum-deploy
migrate:
tutum container exec `tutum container ps -s Running --no-trunc | grep web-1 | awk '{print $$2;}'` bash
app:
${MAKE} base-image
${MAKE} builder-image
${MAKE} build
${MAKE} production-image
newrelic:
image: uzyexe/newrelic
restart: on-failure
privileged: true
volumes:
- "/sys:/sys"
- "/dev:/dev"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
environment:
NEW_RELIC_LICENSE_KEY:
tags:
- lovefear
- production
net: host
pid: "host"
deployment_strategy: every_node
loadbalancer:
image: tutum/haproxy
links:
- web:web
roles:
- global
ports:
- "80:80"
tags:
- lovefear
- production
redis:
image: tutum/redis
tags:
- lovefear
- production
environment:
REDIS_PASS:
loggly:
image: iamatypeofwalrus/logspout-loggly
volumes:
- "/var/run/docker.sock:/tmp/docker.sock"
web:
image: tutum.co/elbaschid/love-fear:00007-dbdf32f
ports:
- "8000"
tags:
- lovefear
- production
links:
- "redis:redis"
target_num_containers: 2
environment:
DJANGO_CONFIGURATION: Prod
DATABASE_URL:
DJANGO_AWS_ACCESS_KEY:
DJANGO_AWS_SECRET_KEY:
RAVEN_DSN:
DJANGO_POSTMARK_API_KEY:
DJANGO_SECRET_KEY:
DJANGO_REDIS_PASSWORD:
NEW_RELIC_LICENSE_KEY:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment