-
Notifications
You must be signed in to change notification settings - Fork 0
/
depo.sh
executable file
·192 lines (155 loc) · 4.42 KB
/
depo.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/sh
# @name Depo
# @description A shell script for deploying node.js applications to a remote server using ssh, git and virtualhosts
# @repository https://github.com/charlie-rudenstal/depo
# @author Charlie Rudenstål <[email protected]>
#
# Place this script on the client
#
# Creates a new virtualhost setting and checkouts the repository using git
# $ depo create [nameOfVirtualHost] [repository] [optional branch]
#
# Updates the git repository to HEAD (Will remove all local modifications, this is not a merge)
# $ depo update [nameOfVirtualHost]
#
# Show a list of all virtual hosts, folders, associated git repository urls and current branches
# on the remote server.
# $ depo list
#
# Restart the proxy server (using forever). Will start it if not already running.
# $ depo restart
#
#
# Configuration
# =============
# Remote Host
# Change to your remote server host (will connect using SSH)
# Remote path proxy server
# Will look for server.js (and create a server.js.tmp during the process)
# Must end with /
REMOTE_PATH_TO_PROXY_SERVER=/home/johndoe/DepoProxy/
# Remote path to public html
# Will create folders for each site in here and look for server.js or other start point defined in package.json
# Must end with /
REMOTE_PATH_TO_PUBLIC_HTML=/home/johndoe/public_html/
if [ $# -lt 1 ]; then
echo "Usage: depo {create|update|list|restart}"
exit 2;
fi
case $1 in
create)
if [ $# -lt 2 ]; then
echo "Missing nameOfVirtualHost";
echo "Usage: depo $1 nameOfVirtualHost gitRepositoryUrl [branch]";
exit 2;
fi
PARAM_VIRTUALHOST_NAME=$2
if [ $# -lt 3 ]; then
echo "Missing gitRepositoryUrl"
echo "Usage: depo $1 nameOfVirtualHost gitRepositoryUrl [branch]";
exit 2;
fi
PARAM_GIT_REPOSITORY_URL=$3
if [ $# -lt 3 ]; then
echo "Creating virtualhost for $PARAM_VIRTUALHOST_NAME using repository $PARAM_GIT_REPOSITORY_URL"
fi
PARAM_BRANCH="master"
if [ $# -gt 3 ]; then
PARAM_BRANCH=$4
echo "Creating virtualhost for $PARAM_VIRTUALHOST_NAME using repository $PARAM_GIT_REPOSITORY_URL and branch $PARAM_BRANCH"
fi
ssh -T $REMOTE_HOST <<EOI
echo
echo Creating virtual host...
echo ========================
echo
cd $REMOTE_PATH_TO_PROXY_SERVER
sed -e '\$d' server.js > server.tmp.js
echo 'app.use(express.vhost('\''$PARAM_VIRTUALHOST_NAME'\'', require('\''$REMOTE_PATH_TO_PUBLIC_HTML$PARAM_VIRTUALHOST_NAME'\'').app));' >> server.tmp.js
cp server.tmp.js server.js
rm server.tmp.js
echo 'app.listen(8080);' >> server.js
echo
echo Checking out repository...
echo ==========================
echo
cd $REMOTE_PATH_TO_PUBLIC_HTML
mkdir $PARAM_VIRTUALHOST_NAME
cd $PARAM_VIRTUALHOST_NAME
git init
git remote add -f origin $PARAM_GIT_REPOSITORY_URL
git checkout $PARAM_BRANCH
echo
echo Restaring proxy server...
echo =========================
echo
cd $REMOTE_PATH_TO_PROXY_SERVER
forever stop server.js
forever start server.js
exit
EOI
;;
update)
if [ $# -lt 2 ]; then
echo "Usage: depo $1 nameOfVirtualHost";
exit 2;
fi
PARAM_VIRTUALHOST_NAME=$2
ssh -T $REMOTE_HOST <<EOI
echo
echo Updating repository...
echo ======================
echo
cd $REMOTE_PATH_TO_PUBLIC_HTML
cd $PARAM_VIRTUALHOST_NAME
git reset --hard HEAD
git clean -f -d
git pull
echo
echo Restaring proxy server...
echo =========================
echo
cd $REMOTE_PATH_TO_PROXY_SERVER
forever stop server.js
forever start server.js
exit
EOI
;;
restart)
ssh -T $REMOTE_HOST <<EOI
echo
echo Restaring proxy server...
echo =========================
echo
cd $REMOTE_PATH_TO_PROXY_SERVER
forever stop server.js
forever start server.js
exit
EOI
;;
list)
ssh -T $REMOTE_HOST <<EOI
echo
echo Configured virtual hosts
echo ========================
cd $REMOTE_PATH_TO_PROXY_SERVER
cat server.js | grep vhost
echo
echo Folders and Git repositories
echo ============================
cd $REMOTE_PATH_TO_PUBLIC_HTML
printf "%25s %25s\n" Folder Repo
for dir in */; do
REPO_URL=\`git --git-dir \${dir}.git config --get remote.origin.url 2> /dev/null \`
if [ -z \$REPO_URL ]; then continue; fi
REPO_BRANCH=\`git --git-dir \${dir}.git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/\1/p'\`
printf "%25s %s @ %s\n" \$dir \$REPO_URL \$REPO_BRANCH
done
echo \$OUTPUT_LIST
exit
EOI
;;
*)
echo "Usage: depo {create|update|list|restart}"
esac