This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
146 lines (111 loc) · 4.32 KB
/
fabfile.py
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
from __future__ import with_statement
import os
from time import time
from StringIO import StringIO
from tempfile import NamedTemporaryFile
from fabric.api import local, env, run, cd, get
from fabric.decorators import task
from fabric.contrib.files import exists, upload_template
env.use_ssh_config = True
env.hosts = ['serial-styde']
releases_dir = "/home/serial/serialapp.com/releases"
git_branch = "master"
git_repo = "[email protected]:dimitriacosta/deploy-demo.git"
repo_dir = "/home/serial/serialapp.com/repo"
persist_dir = "/home/serial/serialapp.com/persist"
next_release = "%(time).0f" % {'time': time()}
current_release = "/home/serial/serialapp.com/current"
last_release_file = "/home/serial/serialapp.com/LAST_RELEASE"
current_release_file = "/home/serial/serialapp.com/CURRENT_RELEASE"
@task
def deploy(migrate='no'):
init()
update_git()
create_release()
build_site()
if migrate=='yes':
migrate_from = "%s/%s" % (releases_dir, next_release)
migrate_forward(migrate_from)
swap_symlinks()
@task
def rollback(migrate_back='no'):
last_release = get_last_release()
current_release = get_current_release()
if migrate_back=='yes':
migrate_from = "%s/%s" % (releases_dir, current_release)
migrate_backward(migrate_from)
rollback_release(last_release)
write_last_release(current_release)
write_current_release(last_release)
@task
def migrate():
migrate_forward()
@task
def migrate_back():
migrate_backward()
def migrate_forward(release_dir=None, env='production'):
if not release_dir:
release_dir=current_release
with cd(release_dir):
run('php artisan migrate --env=%s' % env)
def migrate_backward(release_dir=None, env='production'):
if not release_dir:
release_dir=current_release
with cd(release_dir):
run('php artisan migrate:rollback --env=%s' % env)
def get_last_release():
fd = StringIO()
get(last_release_file, fd)
return fd.getvalue()
def get_current_release():
fd = StringIO()
get(current_release_file, fd)
return fd.getvalue()
def write_last_release(last_release):
last_release_tmp = NamedTemporaryFile(delete=False)
last_release_tmp.write("%(release)s")
last_release_tmp.close()
upload_template(last_release_tmp.name, last_release_file, {'release':last_release}, backup=False)
os.remove(last_release_tmp.name)
def write_current_release(current_release):
current_release_tmp = NamedTemporaryFile(delete=False)
current_release_tmp.write("%(release)s")
current_release_tmp.close()
upload_template(current_release_tmp.name, current_release_file, {'release':current_release}, backup=False)
os.remove(current_release_tmp.name)
def rollback_release(to_release):
release_into = "%s/%s" % (releases_dir, to_release)
run("ln -nfs %s %s" % (release_into, current_release))
run("sudo service php7.0-fpm reload")
def init():
if not exists(releases_dir):
run("mkdir -p %s" % releases_dir)
if not exists(repo_dir):
run("git clone -b %s %s %s" % (git_branch, git_repo, repo_dir))
if not exists("%s/storage" % persist_dir):
run("mkdir -p %s/storage/app/public" % persist_dir)
run("mkdir -p %s/storage/framework/cache" % persist_dir)
run("mkdir -p %s/storage/framework/sessions" % persist_dir)
run("mkdir -p %s/storage/framework/views" % persist_dir)
run("mkdir -p %s/storage/logs" % persist_dir)
def update_git():
with cd(repo_dir):
run("git checkout %s" % git_branch)
run("git pull origin %s" % git_branch)
def create_release():
release_into = "%s/%s" % (releases_dir, next_release)
run("mkdir -p %s" % release_into)
with cd(repo_dir):
run("git archive --worktree-attributes %s | tar -x -C %s" % (git_branch, release_into))
def build_site():
with cd("%s/%s" % (releases_dir, next_release)):
run("composer install")
def swap_symlinks():
release_into = "%s/%s" % (releases_dir, next_release)
run("ln -nfs %s/.env %s/.env" % (persist_dir, release_into))
run("rm -rf %s/storage" % release_into)
run("ln -nfs %s/storage %s/storage" % (persist_dir, release_into))
run("ln -nfs %s %s" % (release_into, current_release))
write_last_release(get_current_release())
write_current_release(next_release)
run("sudo service php7.0-fpm reload")