-
Notifications
You must be signed in to change notification settings - Fork 31
/
install.sh
187 lines (171 loc) · 8.64 KB
/
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
#!/bin/bash
# Copyright 2012 Severalnines AB
#
# MODIFY THE BELOW TO SUIT YOU ENV:
function init
{
FILES=`ls /etc/cmon.cnf 2>&1`
FILES2=`ls /etc/cmon.d/*.cnf 2>&1`
FILES="$FILES $FILES2"
configfile=""
for f in $FILES
do
X=`grep -l cluster_id=${CLUSTER_ID} $f 2>&1 `
if [ $? -eq 0 ]; then
source $f
configfile=$f
fi
done
if [ -z "$configfile" ]; then
echo "No matching configuration file found having cluster_id=${CLUSTER_ID}"
exit 1
fi
source $configfile
CMON_DB_HOST=127.0.0.1
CMON_DB_PORT=$mysql_port
CMON_USER=cmon
CMON_DB_DB=cmon
CMON_PASSWORD=$mysql_password
MYSQL_BIN=$mysql_basedir/bin/mysql
CONNECT_TIMEOUT=10
CLUSTER_TYPE=$type
MYSQL_OPTS="--connect-timeout=$CONNECT_TIMEOUT"
if (( EUID != 0 )); then
echo "must be executed as 'root' or with 'sudo'"
exit 1
fi
}
function load_opts
{
local CLUSTER_ID=$1
echo "load opts $CLUSTER_ID"
OS=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='OS' AND cid=$CLUSTER_ID" 2>/dev/null`
CONFIGDIR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='CONFIGDIR' AND cid=$CLUSTER_ID" 2>/dev/null`
MYSQL_PORT=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='MYSQL_PORT' AND cid=$CLUSTER_ID" 2>/dev/null`
GALERA_PORT=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='GALERA_PORT' AND cid=$CLUSTER_ID" 2>/dev/null`
MYSQL_BASEDIR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='MYSQL_BASEDIR' AND cid=$CLUSTER_ID" 2>/dev/null`
MYSQL_SCRIPTDIR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='SCRIPTDIR' AND cid=$CLUSTER_ID" 2>/dev/null`
SSH_IDENTITY=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='SSH_IDENTITY' AND cid=$CLUSTER_ID" 2>/dev/null`
SSH_USER=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='SSH_USER' AND cid=$CLUSTER_ID" 2>/dev/null`
SSH_PORT=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='SSH_PORT' AND cid=$CLUSTER_ID" 2>/dev/null`
SSH_OPTSX=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='SSH_OPTS' AND cid=$CLUSTER_ID"`
SUDO=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='SUDO' AND cid=$CLUSTER_ID" 2>/dev/null`
OS_USER_HOME=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='OS_USER_HOME' AND cid=$CLUSTER_ID" 2>/dev/null`
TMPDIR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='STAGING_DIR' AND cid=$CLUSTER_ID"`
S9S_TMPDIR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='STAGING_DIR' AND cid=$CLUSTER_ID"`
VENDOR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon_configuration where param='VENDOR' AND cid=$CLUSTER_ID" 2>/dev/null`
DATADIR=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select value from cmon.cluster_config where variable='datadir' and cid=$CLUSTER_ID order by id asc limit 1" 2>/dev/null`
MYCNF=`$MYSQL_BIN -N -B -A --user=$CMON_USER --password=$CMON_PASSWORD --database=$CMON_DB_DB --host=$CMON_DB_HOST --port=$CMON_DB_PORT -e "select data from cmon.cluster_configuration where cid=$CLUSTER_ID limit 1" 2>/dev/null`
echo $MYCNF | sed 's#\\n#\n\r#g' > /tmp/my.cnf
SSH_OPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oNumberOfPasswordPrompts=0 -oConnectTimeout=10"
if [ "$SSH_IDENTITY" = "" ]; then
SSH_IDENTITY="-oIdentityFile=${OS_USER_HOME}/.ssh/id_rsa"
else
SSH_IDENTITY="-oIdentityFile=$SSH_IDENTITY"
fi
SSH_OPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oNumberOfPasswordPrompts=0 -oConnectTimeout=10 $SSH_IDENTITY"
SSH_OPTS2="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oNumberOfPasswordPrompts=0 -oConnectTimeout=10 $SSH_IDENTITY"
if [ "$SSH_USER" != "root" ]; then
SSH_OPTS_EXTRA="-ftt"
SSH_OPTS=" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oNumberOfPasswordPrompts=0 -oConnectTimeout=10 $SSH_IDENTITY"
if [ "$SUDO" = "" ] || [ "$SUDO" = "NULL" ]; then
SUDO="sudo"
fi
fi
if [ "$SSH_PORT" = "" ] || [ "$SSH_PORT" = "NULL" ]; then
SSH_PORT="22"
fi
if [ "$S9S_TMPDIR" = "" ] || [ "$TMPDIR" = "NULL" ]; then
S9S_TMPDIR="/tmp/s9s/"
TMPDIR="/tmp/s9s/"
fi
SSH_OPTS="$SSH_OPTSX -p$SSH_PORT $SSH_OPTS"
SSH_OPTS2="-P$SSH_PORT $SSH_OPTS2"
}
function remote_cmd()
{
desthost=$1
xcommand=$2
MAX_RETRIES=1
printf "%-4s: Executing '%s' " "$desthost" "$xcommand"
retry=0
while [ $retry -lt $MAX_RETRIES ];
do
x=`ssh -q $SSH_OPTS $SSH_USER@$desthost "$SUDO $xcommand " 2>&1 >> $HOME/s9s_deploy.log`
if [ $? -eq 0 ]; then
printf "\033[32m[ok]\033[0m\n"
return 0
fi
retry=`expr $retry + 1`
printf "\033[31m[failed: retrying ${retry}/${MAX_RETRIES}]\033[0m\n"
ssh -q $SSH_OPTS $SSH_USER@$desthost " sync " 2>&1 >> $HOME/s9s_deploy.log
sleep 1
done
printf "\033[31m[failed]\033[0m\n"
echo $x
echo 'The following command failed:'
echo "ssh -q $SSH_OPTS $SSH_USER@$desthost \"$SUDO $xcommand \""
echo 'Try running the command on the line above again, contact http://support.severalnines.com/ticket/new, attach the output from deploy.sh and the error from running the command to the Support issue.'
exit 1
}
function remote_copy()
{
srcfile=$1
desthost=$2
destfile=$3
printf "%-4s: Copying '%s' " "$desthost" "$srcfile"
scp $SSH_OPTS2 $srcfile $SSH_USER@$desthost:$destfile > $HOME/s9s_cmd.log 2>&1
if [ $? -eq 0 ]; then
printf "\033[32m[ok]\033[0m\n"
return 0
else
printf "\033[31m[failed]\033[0m\n"
cat $HOME/s9s_cmd.log
exit 1
fi
}
args=`getopt p:i:P:t:d:j: $*`
set -- $args
echo $args
for i
do
case "$i" in
-i)
CLUSTER_ID="$2"; shift;
shift;;
esac
done
if [ -z "$CLUSTER_ID" ]; then
echo "./install.sh -i <cluster id> missing"
exit 1
fi
init
load_opts $CLUSTER_ID
QUERY="select group_concat(hostname SEPARATOR ' ') from ${CMON_DB_DB}.hosts where cid=${CLUSTER_ID}"
SERVER_LIST=`$MYSQL_BIN $MYSQL_OPTS --user=$CMON_USER --password=$CMON_PASSWORD --host=127.0.0.1 --port=$CMON_DB_PORT -N -Bse "$QUERY" 2>/dev/null`
if [ "$SERVER_LIST" = "NULL" ]; then
echo "No servers found"
exit
fi
if [ -z "$SERVER_LIST" ]; then
echo "No servers found"
exit
fi
if [ -d ccadmin/s9s_repo ]; then
rm -rf ccadmin/repo
mv -f ccadmin/s9s_repo ccadmin/repo
fi
for H in $SERVER_LIST
do
echo "Installing on $H "
remote_cmd $H "mkdir -p $S9S_TMPDIR"
remote_cmd $H "chown -R $SSH_USER:$SSH_USER $S9S_TMPDIR"
remote_copy "cluster/s9s_*" $H $S9S_TMPDIR
remote_copy "ccadmin/s9s_*" $H $S9S_TMPDIR
remote_cmd $H "mv $S9S_TMPDIR/s9s_* /usr/bin"
echo "-----------------"
done
if [ -d ccadmin/s9s_repo ]; then
mv ccadmin/repo ccadmin/s9s_repo
fi
echo "Done."