|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import yaml |
| 4 | +from colors import green |
| 5 | + |
| 6 | +from fabric.api import get, execute, put, roles |
| 7 | +from fabric.context_managers import cd |
| 8 | +from fabric.decorators import task |
| 9 | +from fabric.operations import run |
| 10 | +from fabric.state import env |
| 11 | +from fabric.contrib import files |
| 12 | + |
| 13 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 14 | + |
| 15 | +config = { |
| 16 | + "name": "Python.ru dev deploy", |
| 17 | + |
| 18 | + "build_server": "dev.python.ru", |
| 19 | + "repo_url": "git@github.com:moscowpython/python.ru.git", |
| 20 | + "app_path": "~/python.ru", |
| 21 | + "app_branch": "develop", |
| 22 | + |
| 23 | + "docker_image": "korneevm/pythonru", |
| 24 | + "image_type": "dev", |
| 25 | + "image_version": "latest", |
| 26 | + |
| 27 | + "deploy_server": "dev.python.ru", |
| 28 | + "compose_path": "/opt/servers", |
| 29 | + "compose_block_name": "pythonru-dev", |
| 30 | + |
| 31 | + "post_deploy": "docker exec -i -t servers_pythonru-dev_1 python3 manage.py migrate --noinput" |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +env.user = "deployer" |
| 36 | +env.hosts = ["event-test.nendazfreeride.ch"] |
| 37 | +env.forward_agent = False |
| 38 | + |
| 39 | +env.roledefs = { |
| 40 | + 'build': [], |
| 41 | + 'deploy': [] |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +@task |
| 46 | +def deploy(): |
| 47 | + """ |
| 48 | + Make full step-by-step deploy |
| 49 | +
|
| 50 | + :param config: Configuration dict, loaded by slug |
| 51 | + :return: Nothing, if deploy passed |
| 52 | + :raises: Various exceptions, if something went wrong during deploy |
| 53 | + """ |
| 54 | + env.roledefs['build'] = [config["build_server"]] |
| 55 | + env.roledefs['deploy'] = [config["deploy_server"]] |
| 56 | + execute(get_code, config) |
| 57 | + execute(build_container, config) |
| 58 | + execute(push_container, config) |
| 59 | + execute(update_compose, config) |
| 60 | + execute(reload_docker, config) |
| 61 | + |
| 62 | + if config.get('post_deploy'): |
| 63 | + execute(post_deploy, config) |
| 64 | + |
| 65 | + print(green(""" |
| 66 | + SUCCESS |
| 67 | + """)) |
| 68 | + print("""{docker_image}:{image_type}-{image_version} ready |
| 69 | + """.format(**config)) |
| 70 | + |
| 71 | + |
| 72 | +@roles('build') |
| 73 | +@task |
| 74 | +def get_code(config): |
| 75 | + """ |
| 76 | + Pulls update from remote repository |
| 77 | +
|
| 78 | + :param config: Configuration dict |
| 79 | + :return: Nothing, if process passed |
| 80 | + """ |
| 81 | + if files.exists(config["app_path"]): |
| 82 | + # Update buld repo |
| 83 | + with cd(config["app_path"]): |
| 84 | + run("git checkout {}".format(config["app_branch"])) |
| 85 | + run("git pull origin {}".format(config["app_branch"])) |
| 86 | + config["image_version"] = run("git rev-parse --short {}".format(config["app_branch"])) |
| 87 | + else: |
| 88 | + raise Exception("App repo not found") |
| 89 | + |
| 90 | + |
| 91 | +@roles('build') |
| 92 | +@task |
| 93 | +def build_container(config): |
| 94 | + """ |
| 95 | + Builds docker image |
| 96 | +
|
| 97 | + :param config: Configuration dict |
| 98 | + :return: Nothing, if process passed |
| 99 | + """ |
| 100 | + if files.exists(config["app_path"]): |
| 101 | + # Update buld repo |
| 102 | + with cd(config["app_path"]): |
| 103 | + command = "docker build --build-arg version={image_type} -t {docker_image}:{image_type}-{image_version} ." |
| 104 | + run(command.format(**config)) |
| 105 | + |
| 106 | + |
| 107 | +@roles('build') |
| 108 | +@task |
| 109 | +def push_container(config): |
| 110 | + """ |
| 111 | + Pushes docker image to gitlab |
| 112 | +
|
| 113 | + :param config: Configuration dict |
| 114 | + :return: Nothing, if process passed |
| 115 | + """ |
| 116 | + if files.exists(config["app_path"]): |
| 117 | + # Update buld repo |
| 118 | + with cd(config["app_path"]): |
| 119 | + command = "docker push {docker_image}:{image_type}-{image_version}" |
| 120 | + run(command.format(**config)) |
| 121 | + |
| 122 | + |
| 123 | +@roles('deploy') |
| 124 | +@task |
| 125 | +def update_compose(config): |
| 126 | + filename = os.path.join(config['compose_path'], "docker-compose.yml") |
| 127 | + image = "{docker_image}:{image_type}-{image_version}".format(**config) |
| 128 | + |
| 129 | + with tempfile.TemporaryFile() as fd: |
| 130 | + get(filename, fd) |
| 131 | + fd.seek(0) |
| 132 | + data = yaml.load(fd.read()) |
| 133 | + data['services'][config['compose_block_name']]['image'] = image |
| 134 | + fd.seek(0) |
| 135 | + fd.truncate() |
| 136 | + fd.write(yaml.dump(data).encode('utf-8')) |
| 137 | + fd.flush() |
| 138 | + put(fd, filename) |
| 139 | + |
| 140 | + |
| 141 | +@roles('deploy') |
| 142 | +@task |
| 143 | +def reload_docker(config): |
| 144 | + with cd(config["compose_path"]): |
| 145 | + run('docker-compose up -d') |
| 146 | + |
| 147 | + |
| 148 | +@roles('deploy') |
| 149 | +@task |
| 150 | +def post_deploy(config): |
| 151 | + with cd(config["compose_path"]): |
| 152 | + run(config["post_deploy"]) |
0 commit comments