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