forked from smn/karachi
/
git.py
73 lines (60 loc) · 3.52 KB
/
git.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
from fabric.api import *
import helpers
def checkout_code():
"""
Do a git clone or pull depending on whether this is a cold deploy or not
"""
helpers.check_minimum_requirements()
# remove the newest release completely if something goes wrong
helpers.add_rollback("Removing checkout of project '%(current_release)s'" % env,
lambda: sudo("rm -rf %(current_release)s" % env, user=env.sudo_user))
# FIXME: this is hacky, we should have a file.exists() type if function
# to check the server if a file exists
# run a check to see if we've already got a repository checked out
cold_check = run("if [ -d %(shared_path)s/repositories/%(project)s ]; then "
"echo 'warm'; "
"else "
"echo 'cold'; fi" % env)
# FIXME: this probably needs to be split into two different functions,
# one that does 'git clone' and the other that does 'git pull'
# if it's a cold start then checkout the full repository and switch to the
# deploy branch
if cold_check == "cold":
sudo("git clone %(repository)s %(shared_path)s/repositories/%(project)s" % env, user=env.sudo_user)
# update the now warm repository to the latest code, pointless if we've
# just cloned
with cd("%(shared_path)s/repositories/%(project)s" % env):
sudo("git pull", user=env.sudo_user)
# create the release path
sudo("mkdir -p %(current_release)s/%(project)s" % env, user=env.sudo_user)
# copy it to the current release path
sudo("cp -RPp %(shared_path)s/repositories/%(project)s %(current_release)s/" % env, user=env.sudo_user)
with cd("%(current_release)s/%(project)s" % env):
# create a new local deploy branch, tracking the development/production
# branches in origin
sudo("git checkout -b deploy origin/%(branch)s" % env, user=env.sudo_user)
# store the BRANCH and REVISION in a local file for future reference
# when the shit certainly sometime will hit the fan
sudo("echo %(branch)s > BRANCH" % env, user=env.sudo_user)
sudo("echo `git rev-list --max-count=1 deploy` > REVISION" % env, user=env.sudo_user)
def _restore_revision(revision):
with cd("%(current_path)s/%(project)s" % env):
# create a new local deploy branch, tracking the development/production
# branches in origin
sudo("git checkout -b deploy %s" % revision, user=env.sudo_user)
# store the BRANCH and REVISION in a local file for future reference
# when the shit certainly sometime will hit the fan
sudo("echo %(branch)s > BRANCH" % env, user=env.sudo_user)
sudo("echo `git rev-list --max-count=1 deploy` > REVISION" % env, user=env.sudo_user)
def update_code():
helpers.check_minimum_requirements()
env.previous_revision = sudo("cat %(current_path)s/%(project)s/REVISION" % env, user=env.sudo_user)
helpers.add_rollback("Restoring to previous REVISION", lambda: _restore_revision(env.previous_revision))
# update the now warm repository to the latest code, pointless if we've
# just cloned
with cd("%(current_path)s/%(project)s" % env):
sudo("git pull", user=env.sudo_user)
# store the BRANCH and REVISION in a local file for future reference
# when the shit certainly sometime will hit the fan
sudo("echo %(branch)s > BRANCH" % env, user=env.sudo_user)
sudo("echo `git rev-list --max-count=1 deploy` > REVISION" % env, user=env.sudo_user)