-
-
Save mrserverless/51fe6cbb3438079350298a9930449f46 to your computer and use it in GitHub Desktop.
Quick example of blue/green app ugprade on Rancher
This file contains hidden or 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
$ export RANCHER_HOST=foo.bar.suf | |
$ export RANCHER_PORT=8080 | |
$ export RANCHER_PROJECT_ID=1a8 | |
$ export RANCHER_STACK_NAME=killerapp | |
$ ./cattle-deploy.py | |
output: | |
snapshot epoch is1437634695 | |
created temp file /var/folders/sx/ht6gznq979v5r725sqt7_p617p38sz/T/tmpprzZsd | |
created temp file /var/folders/sx/ht6gznq979v5r725sqt7_p617p38sz/T/tmpSzspLI | |
killerapp-lb: | |
ports: | |
- 16001:5000/tcp | |
tty: true | |
image: rancher/load-balancer-service | |
links: | |
- killerapp-1437634695 | |
stdin_open: true | |
killerapp-1437634695: | |
ports: | |
- 5000/tcp | |
restart: 'no' | |
tty: true | |
image: killerapp:latest | |
stdin_open: true | |
killerapp-lb: | |
scale: 1 | |
load_balancer_config: | |
name: killerapp-lb config | |
killerapp-1437634695: | |
scale: 2 | |
Calling rancher-compose... | |
['rancher-compose', '--file', '/var/folders/sx/ht6gznq979v5r725sqt7_p617p38sz/T/tmpprzZsd', '--rancher-file', '/var/folders/sx/ht6gznq979v5r725sqt7_p617p38sz/T/tmpSzspLI', '--project-name', 'killerapp', 'up', '-d'] | |
INFO[0000] Project [killerapp]: Starting project | |
INFO[0000] [0/2] [killerapp-1437634695]: Starting | |
INFO[0000] Creating service killerapp-1437634695 | |
INFO[0006] [1/2] [killerapp-1437634695]: Started | |
INFO[0006] [1/2] [killerapp-lb]: Starting | |
INFO[0007] [2/2] [killerapp-lb]: Started | |
delete container service, killerapp-1437628007 (1s32) | |
removing temp files | |
orchestration complete | |
cattle-deploy.py: | |
#! /usr/bin/env python | |
from __future__ import print_function | |
import os | |
import time | |
import tempfile | |
import jinja2 | |
import os,sys | |
import cattle | |
from subprocess import call | |
from urlparse import urlparse | |
# required environment variables | |
# RANCHER_ACCESS_KEY | |
# RANCHER_SECRET_KEY | |
# RANCHER_HOST | |
# RANCHER_PORT | |
# RANCHER_PROJECT_ID | |
# RANCHER_STACK_NAME | |
# also expects docker-compose.jinja2.yml and rancher-compose.jinja2.yml | |
rancher_host = os.environ['RANCHER_HOST'] | |
rancher_port = os.environ['RANCHER_PORT'] | |
stack_name = os.environ['RANCHER_STACK_NAME'] | |
project_id = os.environ['RANCHER_PROJECT_ID'] | |
cattle_url = 'http://' + rancher_host + ':' + rancher_port + '/v1' | |
# use epoch datetime for service suffix | |
snapshot_date = str(int(time.time())) | |
print('snapshot epoch is' + snapshot_date) | |
# let's connect to rancher first before continuing | |
client = cattle.Client(url=cattle_url, | |
access_key=os.environ['RANCHER_ACCESS_KEY'], | |
secret_key=os.environ['RANCHER_SECRET_KEY']) | |
# create temp files for the docker+compose yml | |
th = tempfile.NamedTemporaryFile( delete=False ) | |
th.close() | |
docker_tfile = th.name | |
print('created temp file ' + docker_tfile) | |
th = tempfile.NamedTemporaryFile( delete=False ) | |
th.close() | |
rancher_tfile = th.name | |
print('created temp file ' + rancher_tfile) | |
# load up some jinja2 | |
template_loader = jinja2.FileSystemLoader( searchpath=os.getcwd() ) | |
template_env = jinja2.Environment( loader=template_loader ) | |
template_vars = { "app" : { "version": snapshot_date } } | |
# render for docker-compose.yml | |
template = template_env.get_template('docker-compose.jinja2.yml') | |
output_text = template.render( template_vars ) | |
print(output_text, file=open(docker_tfile, 'w')) | |
print(output_text) | |
# render for rancher-compose.yml | |
template = template_env.get_template('rancher-compose.jinja2.yml') | |
output_text = template.render( template_vars ) | |
print(output_text, file=open(rancher_tfile, 'w')) | |
print(output_text) | |
# run rancher-compose | |
print('Calling rancher-compose...') | |
rc_cmd = ['rancher-compose', '--file', docker_tfile, '--rancher-file', rancher_tfile, '--project-name', stack_name, 'up', '-d'] | |
print(rc_cmd) | |
call(rc_cmd) | |
# get all services for project | |
services_url = cattle_url + '/' + '/'.join(('projects', project_id, 'services')) | |
services = client.list_service() | |
# clean up older api-acme-bank container services | |
for service in services: | |
if stack_name + '-' in service['name'] and service['kind'] == 'service': | |
# expects the stack name to be in the image named used by the service | |
if stack_name in service['launchConfig']['imageUuid'] and snapshot_date not in service['name']: | |
# assume container service is old and should be deleted | |
print('delete container service, ' + service['name'] +' (' + service['id'] + ')') | |
svc = client.by_id_service(service['id']) | |
#client.stop(svc) | |
client.delete(svc) | |
# delete temp yml files | |
print('removing temp files') | |
os.remove(docker_tfile) | |
os.remove(rancher_tfile) | |
print('orchestration complete') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment