-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy.sh
executable file
·159 lines (135 loc) · 4.02 KB
/
deploy.sh
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
153
154
155
156
157
158
159
#!/usr/bin/env bash
# abort on errors
set -e
# "parse" command line arguments
if [ $# -eq 0 ]; then
MOD_WSGI=yes
elif [ $# -eq 1 ] && [ "$1" = "--no-mod-wsgi" -o "$1" = "-n" ]; then
MOD_WSGI=no
else
echo "Klangbecken deployment script"
echo "Usage:./deploy [-n]"
echo ""
echo "Options:"
echo " -n, --no-mod-wsgi Do not download and install mod_wsgi"
echo ""
echo "Note: Downloading mod_wsgi requires httpd-devel libraries"
echo " to be installed locally"
exit 1
fi
echo "##################"
echo "# Perform checks #"
echo "##################"
# are there prod and upstream remotes?
if ! git remote | grep --quiet "^prod$"; then
echo "ERROR: No 'prod' remote configured"
exit 1
fi
if ! git remote | grep --quiet "^upstream$"; then
echo "ERROR: No 'upstream' remote configured"
exit 1
fi
PROD_HOST=$(git remote get-url prod | cut -d: -f1)
# are we clean?
if ! git diff --exit-code --quiet; then
echo "ERROR: Working directory is NOT clean."
exit 1
fi
# are we on the master branch?
if [ "$(git branch --show-current)" != "master" ]
then
echo "ERROR: We are NOT on the 'master' branch."
exit 1
fi
# fetch latest version from upstream
if ! git fetch upstream master; then
echo
echo "ERROR: cannot fetch current upstream version."
exit 1
fi
# are we pointing at upstream/master?
if ! git show --no-patch --pretty=format:%D | grep --quiet upstream/master
then
echo "ERROR: 'master' branch is NOT pointing at 'upstream/master'."
echo " Make sure, your local version is in sync with upstream."
exit 1
fi
# check connection to prod and fetch state
if ! git fetch prod master; then
echo "ERROR: cannot connect to prod to fetch current version."
exit 1
fi
# is prod/master already up to date?
if git show --no-patch --pretty=format:%D | grep --quiet prod/master
then
echo
echo "ERROR: 'prod' is already up to date."
exit 1
fi
# check current version
INIT_VERSION=$(sed -n -e 's/^__version__\s*=\s*"\(.*\)"\s*$/\1/p' klangbecken/__init__.py )
SETUP_VERSION=$(sed -n -e 's/^\s*version\s*=\s*"\(.*\)".*$/\1/p' setup.py)
if [ "$INIT_VERSION" != "$SETUP_VERSION" ]
then
echo "ERROR: Version numbers in 'setup.py' and 'klangbecken/__init__.py' do not match."
exit 1
fi
TAG_VERSION=$(git tag --merged HEAD -l "v*" | sort -V | tail -n 1)
if [ "v$SETUP_VERSION" != "$TAG_VERSION" ]
then
echo "ERROR: Tag and package versions do not match."
exit 1
fi
echo
echo -n "Everything looks good. Start deployment? [Y/n]"
read -n 1 ANS
if ! [ -z "$ANS" -o "$ANS" = "y" -o "$ANS" = "Y" ]; then
echo " Bye ..."
exit 1
fi
echo
echo "#####################"
echo "# Increment version #"
echo "#####################"
OLD_VERSION=$SETUP_VERSION
LAST_DIGIT=$(echo "$OLD_VERSION" | cut -d. -f3)
LAST_DIGIT=$((LAST_DIGIT + 1))
NEW_VERSION=$(echo "$OLD_VERSION" | cut -d. -f1-2)."$LAST_DIGIT"
sed -i "s/__version__ = \"$OLD_VERSION\"/__version__ = \"$NEW_VERSION\"/" klangbecken/__init__.py
sed -i "s/version=\"$OLD_VERSION\",/version=\"$NEW_VERSION\",/" setup.py
git add klangbecken/__init__.py setup.py
git commit -m "Version bump v$NEW_VERSION"
TEMP=$(mktemp -d)
echo
echo "#########################"
echo "# Download dependencies #"
echo "#########################"
pip download --dest "$TEMP" --no-binary :all: -r requirements.txt
if [ "$MOD_WSGI" = "yes" ]; then
if ! pip download --dest "$TEMP" --no-binary :all: mod_wsgi; then
echo
echo "Note: Downloading mod_wsgi requires httpd-devel libraries"
echo " to be installed locally"
exit 1
fi
fi
echo
echo "###############################"
echo "# Copy dependencies to server #"
echo "###############################"
scp "$TEMP"/* "$PROD_HOST":"dependencies/"
rm "$TEMP"/*
rmdir "$TEMP"
echo
echo "######################"
echo "# Deploy application #"
echo "######################"
git push prod master
echo
echo "#######################"
echo "# Finalize deployment #"
echo "#######################"
git tag "v$NEW_VERSION"
git push upstream master --tags
echo
echo "Deployment successful!"