#!/usr/bin/python -u import os import sys import subprocess import logging from humbug_tools import DEPLOYMENTS_DIR, LOCK_DIR, TIMESTAMP_FORMAT, FAIL, WARNING, ENDC import datetime import shutil import time logging.basicConfig(format="%(asctime)s update-deployment: %(message)s", level=logging.INFO) if len(sys.argv) != 2: print FAIL + "Usage: update-deployment refname" + ENDC sys.exit(1) refname = sys.argv[1] subprocess.check_call(["mkdir", '-p', DEPLOYMENTS_DIR, '/home/humbug/logs']) start_time = time.time() got_lock = False while time.time() - start_time < 300: try: os.mkdir(LOCK_DIR) got_lock = True break except OSError: print WARNING + "Another deployment in progress; waiting for lock..." + ENDC time.sleep(10) if not got_lock: print FAIL + "Deployment already in progress. Please run\n" \ + " %s/current/tools/update-deployment %s\n" % (DEPLOYMENTS_DIR, refname) \ + "manually when the current deployment finishes." + ENDC sys.exit(1) timestamp = datetime.datetime.now().strftime(TIMESTAMP_FORMAT) deploy_path = os.path.join(DEPLOYMENTS_DIR, timestamp) logging.info("Cloning the repository") subprocess.check_call(["git", "clone", "-q", "-b", refname, "humbug@git.humbughq.com:/srv/git/humbug.git", deploy_path], stdout=open('/dev/null', 'w')) os.chdir(deploy_path) # Delete all .pyc files to avoid old module files hanging around subprocess.check_call(["find", ".", "-name", "*.pyc", "-delete"], stdout=open('/dev/null', 'w')) # Update static files logging.info("Updating static files") subprocess.check_call(["./tools/update-prod-static", "--prev-deploy", os.path.join(DEPLOYMENTS_DIR, 'current')]) logging.info("Restarting server...") subprocess.check_call(["./tools/restart-server"]) logging.info("Deployment complete") shutil.rmtree(LOCK_DIR) subprocess.check_call(["./tools/purge-deployments"])