-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathsync_gateway_service_install.sh
executable file
·323 lines (301 loc) · 9.7 KB
/
sync_gateway_service_install.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/sh
# Copyright 2014-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included in
# the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
# file, in accordance with the Business Source License, use of this software
# will be governed by the Apache License, Version 2.0, included in the file
# licenses/APL2.txt.
# Set default values
OS=""
VER=""
SERVICE_NAME="sync_gateway"
# Determine the absolute path of the installation directory
# $( dirname "$0" ) get the directory containing this script
# if we are already in the containing directory we will get '.'
# && pwd will prepend the current directory if the path is relative
# this will result in an absolute path
# Note this line is evaluated not executed in the current shell so
# the current directory is not changed by the 'cd' command
SCRIPT_DIR="$(cd "$(dirname "$0")" >/dev/null && pwd)"
INSTALL_DIR="$(dirname "${SCRIPT_DIR}")"
SRCCFGDIR=${INSTALL_DIR}/examples
SRCCFG=serviceconfig.json
RUNAS_TEMPLATE_VAR=sync_gateway
PIDFILE_TEMPLATE_VAR=/var/run/sync-gateway.pid
RUNBASE_TEMPLATE_VAR=/home/sync_gateway
GATEWAY_TEMPLATE_VAR=${INSTALL_DIR}/bin/sync_gateway
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
SERVICE_CMD_ONLY=false
usage() {
echo "This script creates an init service to run a sync_gateway instance."
echo "If you want to install more than one service instance"
echo "create additional services with different names."
echo ""
echo "sync_gateway_service_install.sh"
echo " -h --help"
echo " --runas=<The user account to run sync_gateway as; default (sync_gateway)>"
echo " --runbase=<The directory to run sync_gateway from; defaut (/home/sync_gateway)>"
echo " --sgpath=<The path to the sync_gateway executable; default (/opt/couchbase-sync-gateway/bin/sync_gateway)>"
echo " --cfgpath=<The path to the sync_gateway JSON config file; default (/home/sync_gateway/sync_gateway.json)>"
echo " --logsdir=<The path to the log file direcotry; default (/home/sync_gateway/logs)>"
echo ""
}
ostype() {
if command -v lsb_release; then
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/os-release ]; then
. /etc/os-release
OS=$(echo "${ID}")
if [ "${OS}" = "debian" ]; then
VER=$(cat /etc/debian_version)
else
VER=$VERSION_ID
fi
elif [ -f /etc/redhat-release ]; then
OS=rhel
VER=$(cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//)
elif [ -f /etc/system-release ]; then
OS=rhel
VER=5.0
else
OS=$(uname -s)
VER=$(uname -r)
fi
OS=$(echo "${OS}" | tr "[:upper:]" "[:lower:]")
OS_MAJOR_VERSION=$(echo $VER | sed 's/\..*$//')
OS_MINOR_VERSION=$(echo $VER | sed s/[0-9]*\.//)
}
# expand template variables + preserve formatting
render_template() {
eval "echo \"$(cat $1)\""
}
# sets up the output directories for logs and data
setup_output_dirs() {
mkdir -p ${LOGS_TEMPLATE_VAR}
chown -R ${RUNAS_TEMPLATE_VAR} ${LOGS_TEMPLATE_VAR}
mkdir -p ${RUNBASE_TEMPLATE_VAR}/data
chown -R ${RUNAS_TEMPLATE_VAR} ${RUNBASE_TEMPLATE_VAR}/data
}
# Run pre installation actions
pre_install_actions() {
# Check that runtime user account exists
if [ "$OS" != "darwin" ] && [ -z $(id -u $RUNAS_TEMPLATE_VAR 2>/dev/null) ]; then
echo "The sync_gateway runtime user account does not exist \"$RUNAS_TEMPLATE_VAR\"." >/dev/stderr
exit 1
fi
setup_output_dirs
# Check that the runtime base directory exists
if [ ! -d "$RUNBASE_TEMPLATE_VAR" ]; then
echo "The runtime base directory does not exist \"$RUNBASE_TEMPLATE_VAR\"." >/dev/stderr
exit 1
fi
# Check that the sync_gateway executable exists
if [ ! -x "$GATEWAY_TEMPLATE_VAR" ]; then
echo "The sync_gateway executable does not exist \"$GATEWAY_TEMPLATE_VAR\"." >/dev/stderr
exit 1
fi
# Check that the sync_gateway src JSON config directory exists
if [ ! -d "$SRCCFGDIR" ]; then
echo "The sync_gateway source JSON config file directory does not exist \"$SRCCFGDIR\"." >/dev/stderr
exit 1
fi
# Check that the sync_gateway src JSON config file exists
if [ ! -r "$SRCCFGDIR/$SRCCFG" ]; then
echo "The sync_gateway source JSON config file does not exist\"$SRCCFGDIR/$SRCCFG\"." >/dev/stderr
exit 1
fi
# If a /tmp/log_upr_client.sock socket exists from a previous installation remove it
if [ -S /tmp/log_upr_client.sock ]; then
rm -f /tmp/log_upr_client.sock
fi
# Copy a default config if defined config file does not exist
if [ ! -e "$CONFIG_TEMPLATE_VAR" ]; then
mkdir -p $(dirname ${CONFIG_TEMPLATE_VAR})
cp $SRCCFGDIR/$SRCCFG $CONFIG_TEMPLATE_VAR
chown ${RUNAS_TEMPLATE_VAR}:${RUNAS_TEMPLATE_VAR} ${CONFIG_TEMPLATE_VAR}
fi
}
#
#script starts here
#
#Figure out the OS type of the current system
ostype
#If the OS is MAC OSX, set the default user account home path to /Users/sync_gateway
if [ "$OS" = "darwin" ]; then
RUNBASE_TEMPLATE_VAR=/Users/sync_gateway
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
fi
# Make sure we are running with root privilages
if [ $(id -u) != 0 ]; then
echo "This script should be run as root." >/dev/stderr
exit 1
fi
# Process the command line args
while [ "$1" != "" ]; do
PARAM=$(echo $1 | awk -F= '{print $1}')
VALUE=$(echo $1 | awk -F= '{print $2}')
case $PARAM in
-h | --help)
usage
exit
;;
--runas)
RUNAS_TEMPLATE_VAR=$VALUE
if [ "$OS" != "darwin" ]; then
RUNBASE_TEMPLATE_VAR=$(getent passwd "$VALUE" | cut -d: -f 6)
else
RUNBASE_TEMPLATE_VAR=$(eval "echo ~$VALUE")
fi
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
;;
--runbase)
RUNBASE_TEMPLATE_VAR=$VALUE
;;
--sgpath)
GATEWAY_TEMPLATE_VAR=$VALUE
;;
--cfgpath)
CONFIG_TEMPLATE_VAR=$VALUE
;;
--logsdir)
LOGS_TEMPLATE_VAR=$VALUE
;;
--servicecmd)
SERVICE_CMD_ONLY=true
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
#Install the service for the specific platform
case $OS in
debian)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 8))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
mkdir -p /usr/lib/systemd/system
render_template script_templates/systemd_debian_sync_gateway.tpl >/usr/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
esac
;;
ubuntu)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 16))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/systemd_debian_sync_gateway.tpl >/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
$((OS_MAJOR_VERSION >= 12))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "service ${SERVICE_NAME} start"
else
pre_install_actions
render_template script_templates/upstart_ubuntu_sync_gateway.tpl >/etc/init/${SERVICE_NAME}.conf
service ${SERVICE_NAME} start
fi
;;
*)
echo "ERROR: Unsupported Ubuntu Version \"$VER\""
usage
exit 1
;;
esac
;;
redhat* | rhel* | centos | ol)
case $OS_MAJOR_VERSION in
5)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "service ${SERVICE_NAME} start"
else
#override location for logs and sync gateway config
LOGS_TEMPLATE_VAR=/var/log/${SERVICE_NAME}
CONFIG_TEMPLATE_VAR=/opt/${SERVICE_NAME}/etc/sync_gateway.json
pre_install_actions
render_template script_templates/sysv_sync_gateway.tpl >/etc/init.d/${SERVICE_NAME}
chmod 755 /etc/init.d/${SERVICE_NAME}
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
chkconfig --add ${SERVICE_NAME}
chkconfig ${SERVICE_NAME} on
service ${SERVICE_NAME} start
fi
;;
6)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "initctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/upstart_redhat_sync_gateway.tpl >/etc/init/${SERVICE_NAME}.conf
initctl start ${SERVICE_NAME}
fi
;;
7 | 8)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/systemd_sync_gateway.tpl >/usr/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
*)
echo "ERROR: Unsupported RedHat/CentOS Version \"$VER\""
usage
exit 1
;;
esac
;;
amzn*)
case $OS_MAJOR_VERSION in
2)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/systemd_sync_gateway.tpl >/usr/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
*)
echo "ERROR: Unsupported Amazon Linux Version \"$VER\""
usage
exit 1
;;
esac
;;
darwin)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "launchctl start /Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist"
else
pre_install_actions
render_template script_templates/com.couchbase.mobile.sync_gateway.plist >/Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist
launchctl load /Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist
fi
;;
*)
echo "ERROR: unknown OS \"$OS\""
usage
exit 1
;;
esac