File: oci-poc-save

package info (click to toggle)
openstack-cluster-installer 43.0.18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,484 kB
  • sloc: php: 19,127; sh: 18,142; ruby: 75; makefile: 31; xml: 8
file content (143 lines) | stat: -rwxr-xr-x 4,709 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

# EXPERIMENTAL: never tested

set -e

usage () {
	echo "$0 <CLUSTER-NAME>"
	echo "This command saves the state of a running PoC cluster."
	echo "See oci-poc-restore to restore the state of a cluster."
	exit 1
}

. /etc/oci-poc/oci-poc.conf

RED="\033[1;31m"
NO_COL="\033[0m"
GREEN="\033[1;32m"
green_echo () {
        echo ${GREEN}${1}${NO_COL}
}
red_echo () {
        echo ${RED}${1}${NO_COL}
}

# Wait until a PID file is gone, meaning the VM has stopped.
# Do so with a timeout, until wich we kill the PID.
wait_for_pidfile_gone () {
	local PID_FILE TIMEOUT
	PID_FILE=${1}
	TIMEOUT=${2}

	CNT=${TIMEOUT}
	while [ -e ${PID_FILE} -a ${CNT} -gt 0 ] ; do
		CNT=$((${CNT} - 1))
	done
	if [ -e ${PID_FILE} ] ; then
		green_echo "Killing VM"
		kill $(cat ${PID_FILE})
		rm -f ${PID_FILE}
	else
		echo "VM is gone"
	fi
}

if [ "${#}" != 1 ] || [ "${1}" = "-h" ] || [ "${1}" = "--help" ] || [ "${1}" = "-help" ] ; then
	usage
fi

CLUSTER_NAME=${1}

shutdown_mysql_servers () {
	local CLUSTER_LIST
	CLUSTER_LIST=$(ocicli -csv cluster-list | q -H -d, "SELECT name FROM -")

	for CLUSTER in ${CLUSTER_LIST} ; do
		green_echo "===> Turning galera off on ${CLUSTER}"
		SQL_NODES=$(ocicli -csv machine-list -a | q -H -d, "SELECT Cur_ip FROM - WHERE status='installed' AND role='sql' AND cluster='${CLUSTER}'" | sort -r)
		if [ -z "${SQL_NODES}" ] ; then
			green_echo "===> Turning off MySQL in controllers"
			for i in $(ocicli -csv machine-list -a | q -H -d, "SELECT Cur_ip FROM - WHERE status='installed' AND role='controller' AND cluster='${CLUSTER}'" | sort -r) ; do
				ssh $i "systemctl stop puppet"
				ssh $i "systemctl stop mysql"
				sleep 5
			done
		else
			green_echo "===> Turning off MySQL in sql nodes"
			for i in ${SQL_NODES} ; do
				ssh $i "systemctl stop puppet"
				ssh $i "systemctl stop mysql"
				sleep 5
			done
		fi
		SQLMSG_NODES=$(ocicli -csv machine-list -a | q -H -d, "SELECT Cur_ip FROM - WHERE status='installed' AND role='sqlmsg' AND cluster='${CLUSTER}'" | sort -r)
		if [ -z "${SQLMSG_NODES}" ] ; then
			green_echo "===> Turning off MySQL in messaging nodes (if any)"
			for i in $(ocicli -csv machine-list -a | q -H -d, "SELECT Cur_ip FROM - WHERE status='installed' AND role='messaging' AND cluster='${CLUSTER}'" | sort -r) ; do
				ssh $i "systemctl stop puppet"
				ssh $i "systemctl stop mysql"
				sleep 5
			done
		else
			green_echo "===> Turning off MySQL in sqlmsg nodes"
			for i in ${SQLMSG_NODES} ; do
				ssh $i "systemctl stop puppet"
				ssh $i "systemctl stop mysql"
				sleep 5
			done
		fi
	done
}

shutdown_all_installed_vms () {
	green_echo "===> Turning installed VMs off..."
	for i in $(ocicli -csv machine-list -a | q -H -d, "SELECT Cur_ip FROM - WHERE status='installed'") ; do
		HOSTNAME=$(ocicli -csv machine-list | q -H -d, "SELECT hostname FROM - WHERE Cur_ip='${i}'")
		green_echo "Shutting down: ${HOSTNAME}"
		timeout 3 ssh root@${i} "shutdown -h now" || true
	done

	for i in $(ocicli -csv machine-list -a | q -H -d, "SELECT Cur_ip FROM - WHERE status='installed'") ; do
		HOSTNAME=$(ocicli -csv machine-list | q -H -d, "SELECT hostname FROM - WHERE Cur_ip='${i}'")
		SERIAL=$(ocicli -csv machine-list | q -H -d, "SELECT serial FROM - WHERE Cur_ip='${i}'")
		VM_NUM=$(printf "%d" $((0x${SERIAL} - 0xC0)))
		PID_FILE=/var/run/oci-poc/slave-node-${VM_NUM}.pid
		green_echo "Waiting for host to be down: ${HOSTNAME}"
		wait_for_pidfile_gone ${PID_FILE} 120
	done

	timeout 5 ssh root@${HOST_NETWORK_PREFIX}.2 "shutdown -h now" || true
	wait_for_pidfile_gone /var/run/oci-poc/pxe-server-node.pid 60
}

kill_remaining_processes () {
	green_echo "===> Killing not-installed VMs..."
	VMS_PID_FILES=$(ls /var/run/oci-poc/slave-node-*.pid | grep -v ipmisim | tr '\n' ' ')
	for i in ${VMS_PID_FILES} ; do
		kill $(cat $i) || true
		rm -f $i
	done
	green_echo "===> Killing all IPMI SIM..."
	IPMI_SIM_PID_FILES=$(ls /var/run/oci-poc/slave-node-*.ipmisim.pid | tr '\n' ' ')
	for i in ${IPMI_SIM_PID_FILES} ; do
		kill $(cat $i) || true
		rm -f $i
	done
	kill $(cat /var/run/oci-poc/pxe-server-node.pid.ipmisim.pid) || true
	rm -f /var/run/oci-poc/pxe-server-node.pid.ipmisim.pid
}
make_backup_of_files () {
	green_echo "===> Making backup of .qcow2 files..."
	mkdir -p /var/lib/openstack-cluster-installer-poc/saved/${CLUSTER_NAME}
	cp -axufv /var/lib/openstack-cluster-installer-poc/runtime/* /var/lib/openstack-cluster-installer-poc/saved/${CLUSTER_NAME}/

	green_echo "===> Making backup of ipmi_sim config files..."
	cp -axuf /var/lib/openstack-cluster-installer-poc/ipmi_sim/*.conf /var/lib/openstack-cluster-installer-poc/saved/${CLUSTER_NAME}/
}


shutdown_mysql_servers
shutdown_all_installed_vms
kill_remaining_processes
make_backup_of_files