summaryrefslogtreecommitdiffstats
path: root/scripts/sh/deploy_pxc.sh
blob: dab277cabd8ead7cfbce1e2a57db9a66d741f6eb (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/bin/sh
# Basic Percona XtraDB Cluster Server installation script
# Intended to be run on openSUSE Leap, but is easily adaptable by changing the package manager call in check_dependencies.
#
# Georg Pfuetzenreuter <georg@lysergic.dev>
# Created and last modified: 30/04/2022

SCRIPT="PXC_Boostrap"
VENDOR="Percona"
VERSION="8.0.27"
PRODUCT="$VENDOR-XtraDB-Cluster_$VERSION-18.1_Linux.x86_64.glibc2.17-minimal"
TARBALL="$PRODUCT.tar.gz"
URL="https://downloads.percona.com/downloads/Percona-XtraDB-Cluster-LATEST/Percona-XtraDB-Cluster-8.0.27/binary/tarball/$TARBALL"
TARGETPREFIX="/opt"
TARGETDIR="$TARGETPREFIX/$VENDOR"
TMPDIR="/tmp"
SHORTY="mysql"
DATADIR="/var/lib/$SHORTY"
HOMEDIR="/var/lib/${SHORTY}_home"
RUNDATE="`date +%m%d%Y-%H%M%S`"
servicefile="/etc/systemd/system/${SHORTY}d.service"
configfile="/etc/opt/my.cnf"
checkfile="/var/adm/pxc.installed"
ARGS="$@"

check_args () {
	if [ "$ARGS" = "--debug" ]
	then
		RUNMODE="debug"
		# we do not use this here
		echo "Not implemented."
		exit 1
	fi
	if [ ! "$ARGS" = "--debug" ]
	then
		RUNMODE="normal"
	fi
	if [ "$ARGS" = "--uninstall" ]
	then
		RUNMODE="uninstall"
	fi
}

check_dependencies () {
	if ! command -v ex &>/dev/null
	then
		if zypper --non-interactive install vim-small
		then
			log "Satisfied dependencies."
		else
			log "Could not satisfy dependency \"ex\"."
			return 1
		fi
	fi
	if ! command -v systemctl &>/dev/null
	then
		log "Unsure whether this actually is a systemd based system."
		return 1
	fi
}

init () {
	local step="Preparation"

	if [ ! "`id -u`" = "0" ]
	then
		log "Script needs root privileges."
		exit 1
	fi
	log "Initializing $SCRIPT for $PRODUCT"
	check_args
	if [ "$RUNMODE" = "normal" ]
	then
		if [ -f "$checkfile" ]
		then
			log "Existing installation found. Consider --uninstall first."
			exit 1
		fi
		check_dependencies
		check "$?" "dependencies"
		cd $TMPDIR
		download
		setup
		touch "$checkfile"
	fi
	if [ "$RUNMODE" = "uninstall" ]
	then
		uninstall
	fi
}
	

log () {
	local printmsg="$1"
	local prefix="[$SCRIPT]"
	local logmsg="$prefix $printmsg"
	local logfile="$TMPDIR/$SCRIPT_$RUNDATE.log"

	echo "$printmsg"
	echo "$logmsg" >> $logfile
}

check () {
	local return="$1"
	local action="$2"
	
	if [ "$return" = "0" ]
	then
		log "$step: Action \"$action\" OK"
	else
		log "$step: Action \"$action\" failed"
		abort
	fi
}

abort () {
	local trash_link="$TARGETPREFIX/$SHORTY"
	local trash_dir="$TARGETDIR/$PRODUCT"
	local trash_dl1="$TMPDIR/$TARBALL"
	local trash_dl2="$TMPDIR/$checksum"

	if [ ! "$RUNMODE" = "uninstall" ]
	then
		log "Fatal error, aborting script ..."
	fi

	if [ -h "$trash_link" ]
	then
		log "Cleaning up installation ..."
		rm $trash_link
		if [ -d $trash_dir ]
		then
			rm -r $trash_dir
		fi
	fi

	if [ -f "$trash_dl1" ]
	then
		log "Cleaning up download ..."
		rm $trash_dl1
		if [ -f $trash_dl2 ]
		then
			rm $trash_dl2
		fi
	fi

	if [ -f "$servicefile" ]
	then
		log "Removing servicefile ..."
		systemctl disable ${SHORTY}d.service &>/dev/null
		rm $servicefile
	fi

	if [ ! "$RUNMODE" = "uninstall" ]
	then
		log "Exit."
		exit 1
	fi
}

download () {
	checksum="$TARBALL.sha256sum"
	log "Downloading release tarball ..."
	if [ -f $TARBALL ]
	then
		log "Removing stale files ..."
		rm $TARBALL
	fi
	if [ -f $checksum ]
	then
		rm $checksum
	fi
	curl -sSLo $TARBALL $URL
	curl -sSLo $checksum $URL.sha256sum
	if ! download_verify $checksum
	then
		log "Corrupted download, verify URL."
		exit 1
	fi
}

download_verify () {
	log "Verifying downloaded tarball ..."
	sha256sum -c $1
}

setup () {
	local step="Setup"

	for action in extract prepare install
	do
		setup_$action
		check "$?" "$action"
	done

}

setup_extract () {
	log "Deflating ..."
	if [ ! -d "$TARGETDIR" ]
	then
		mkdir "$TARGETDIR"
	fi
	tar -C $TARGETDIR -xzf $TARBALL
}

setup_prepare () {
	log "Preparing installation ..."
	local target="$TARGETDIR/$PRODUCT"
	local target_short="$TARGETPREFIX/$SHORTY"
	ln -s $target $target_short
	if ! getent group "$SHORTY" &>/dev/null
	then
		log "Creating group ..."
		if ! groupadd -r $SHORTY
		then
			log "Failed to create group."
			return 1
		fi
	fi
	if ! id "$SHORTY" &>/dev/null
	then
		log "Creating user ..."
		if ! useradd -g $SHORTY -d $HOMEDIR -rs /usr/sbin/nologin $SHORTY
		then
			log "Failed to create user."
			return 1
		fi
		if [ ! -d "$HOMEDIR" ]
		then
			if mkdir $HOMEDIR
			then
				if chown $SHORTY:$SHORTY $HOMEDIR
				then
					if ! chmod 750 $HOMEDIR
					then
						log "Failed to set homedir permissions."
						return 1
					fi
				else
					log "Failed to set homedir ownership."
					return 1
				fi
			else
				log "Failed to create home directory."
				return 1
			fi
		fi
		
	fi
	if ! chown -R $SHORTY:$SHORTY $target
	then
		log "Failed to change ownership."
		return 1
	fi
	if ! chmod 750 $target
	then
		log "Failed to change permissions."
		return 1
	fi
}

setup_install () {
	log "Placing systemd service ..."
	ex - $servicefile <<EOF_SERVICE
insert
[Unit]
Description=Percona XtraDB Cluster Server
After=syslog.target network.target

[Service]
User=$SHORTY
Group=$SHORTY
PIDFile=/run/$SHORTY/${SHORTY}d.pid
ExecStart=$TARGETPREFIX/$SHORTY/bin/mysqld --defaults-file=$configfile
RuntimeDirectory=$SHORTY
ProtectSystem=strict
ProtectHome=yes
PrivateDevices=yes
PrivateTmp=yes
PrivateUsers=yes
ProtectKernelTunables=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ReadWritePaths=/var/lib/$SHORTY
ReadWritePaths=/var/log/$SHORTY

[Install]
WantedBy=multi-user.target
.
wq
EOF_SERVICE
	if [ ! "$?" = "0" ]
	then
		log "Failed to create systemd service file."
		return 1
	fi

	log "Placing initial configuration ..."
	ex - $configfile <<EOF_CONFIG
insert
[client]
socket=/run/mysql/mysql.sock

[mysqld]
datadir=$DATADIR
socket=/run/$SHORTY/$SHORTY.sock
log-error=/var/log/$SHORTY/${SHORTY}d.log
port=3306
bind-address = ::1
server-id=1
ssl_ca=/etc/pki/trust/anchors/syscid-ca.crt
ssl_cert=/etc/ssl/MySQL/crt
ssl_key=/etc/ssl/MySQL/key
.
wq
EOF_CONFIG
	if [ ! "$?" = "0" ]
	then
		log "Failed to create configfile"
		return 1
	fi

	if [ ! -d "$DATADIR" ]
	then
		if mkdir $DATADIR
		then
			if chown $SHORTY:$SHORTY $DATADIR
			then
				if ! chmod 750 $DATADIR
				then
					log "Failed to set datadir permissions."
					return 1
				fi
			else
				log "Failed to set datadir ownership."
				return 1
			fi
		else
			log "Failed to create data directory."
			return 1
		fi
	fi
}

uninstall () {
	log "Removing user and group ..."
	userdel -f $SHORTY &>/dev/null
	groupdel -f $SHORTY &>/dev/null

	log "Removing files and directories ..."
	abort

	echo "Remove data? y/n"
	read choice
	if [ "$choice" = "y" ]
	then
		if [ -d "$DATADIR" ]
		then
			rm -r "$DATADIR"
		fi
		if [ -d "$HOMEDIR" ]
		then
			rm -r "$HOMEDIR"
		fi
		if [ -f "$configfile" ]
		then
			rm "$configfile"
		fi
	fi

	if [ -f "$checkfile" ]
	then
		rm "$checkfile"
	fi

	log "OK"
}

# Execute workflow
init