Cluster configuration script should copy initial configuration files.
[integration/distribution.git] / distributions / karaf / src / main / assembly / bin / configure_cluster.sh
1 #!/bin/bash
2 #
3 # Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
4 #
5 # This program and the accompanying materials are made available under the
6 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 # and is available at http://www.eclipse.org/legal/epl-v10.html
8 #
9
10
11 function usage()
12 {
13     # Print any error messages
14     test "$1" != "" && echo " ERROR: $1"
15
16     # Print standard usage help
17     cat << EOF
18  This script is used to configure cluster parameters on this
19  controller. The user should restart controller to apply changes.
20
21  Usage: $0 <index> <seed_nodes_list>
22   - index: Integer within 1..N, where N is the number of seed nodes.
23   - seed_nodes_list: List of seed nodes, separated by comma or space.
24
25  The address at the provided index should belong this controller.
26  When running this script on multiple seed nodes, keep the
27  seed_node_list same, and vary the index from 1 through N.
28
29 EOF
30
31     exit -1
32 }
33
34
35 function start_banner
36 {
37 cat <<EOF
38 ################################################
39 ##             Configure Cluster              ##
40 ################################################
41 EOF
42 }
43
44 function end_banner
45 {
46 cat <<EOF
47 ################################################
48 ##   NOTE: Manually restart controller to     ##
49 ##         apply configuration.               ##
50 ################################################
51 EOF
52 }
53
54 # Utility function for joining strings.
55 function join {
56     delim=',\n\t\t\t'
57     final=$1; shift
58
59     for str in $* ; do
60         final=${final}${delim}${str}
61     done
62
63     echo ${final}
64 }
65
66 function create_strings
67 {
68     # Using a list of controller IPs, create the strings for data
69     # and rpc seed nodes, as well as the list of members.
70
71     # First create an arrays with one string per controller.
72     # Then merge the array using the join utility defined above.
73     count=1
74     for ip in ${CONTROLLERIPS[@]} ; do
75         ds[$count]=\\\"akka.tcp:\\/\\/opendaylight-cluster-data@${ip}:2550\\\"
76         rpc[$count]=\\\"akka.tcp:\\/\\/odl-cluster-rpc@${ip}:2551\\\"
77         members[$count]=\\\"member-${count}\\\"
78         count=$[count + 1]
79     done
80
81     DATA_SEED_LIST=$(join ${ds[@]})
82     RPC_SEED_LIST=$(join ${rpc[@]})
83     MEMBER_NAME_LIST=$(join ${members[@]})
84 }
85
86 function get_cli_params
87 {
88     # Check if params have been supplied
89     test $# -eq 0 && usage
90
91     # First param is index, and rest are controller list
92     INDEX=$1; shift
93     CONTROLLER_LIST=$*
94
95     # Verify we have controller list
96     test "${CONTROLLER_LIST}" == "" && usage "Missing controller list"
97
98     # Create the list of controllers from the CONTROLLER_LIST variable
99     CONTROLLERIPS=( ${CONTROLLER_LIST//,/ } )
100
101     test ${INDEX} -le 0 -o ${INDEX} -gt ${#CONTROLLERIPS[@]} && \
102         usage "Invalid index"
103
104     CONTROLLER_ID="member-${INDEX}"
105     CONTROLLER_IP="${CONTROLLERIPS[((${INDEX} - 1))]}"
106 }
107
108
109 function modify_conf_files
110 {
111     echo "Configuring unique name in akka.conf"
112     sed -i -e "/roles[ ]*=/ { :loop1 /.*\]/ b done1; N; b loop1; :done1 s/roles.*\]/roles = [\"${CONTROLLER_ID}\"]/}" ${AKKACONF}
113
114     echo "Configuring hostname in akka.conf"
115     sed -i -e "s/hostname[ ]*=.*\"/hostname = \"${CONTROLLER_IP}\"/" ${AKKACONF}
116
117     echo "Configuring data and rpc seed nodes in akka.conf"
118     sed -i -e "/seed-nodes[ ]*=/ { :loop2 /.*\]/ b done2; N; b loop2; :done2 s/seed-nodes.*opendaylight-cluster-data.*\]/seed-nodes = [${DATA_SEED_LIST}]/; s/seed-nodes.*odl-cluster-rpc.*\]/seed-nodes = [${RPC_SEED_LIST}]/}" ${AKKACONF}
119
120     echo "Configuring replication type in module-shards.conf"
121     sed -i -e "/^[^#].*replicas[ ]*=/ { :loop /.*\]/ b done; N; b loop; :done s/replicas.*\]/replicas = [${MEMBER_NAME_LIST}]/}" ${MODULESHARDSCONF}
122 }
123
124
125 function verify_configuration_files
126 {
127     # Constants
128     BIN_DIR=`dirname $0`
129     test ${BIN_DIR} == '.' && BIN_DIR=${PWD}
130     CONTROLLER_DIR=`dirname ${BIN_DIR}`
131     CONF_DIR=${CONTROLLER_DIR}/configuration/initial
132     AKKACONF=${CONF_DIR}/akka.conf
133     MODULESCONF=${CONF_DIR}/modules.conf
134     MODULESHARDSCONF=${CONF_DIR}/module-shards.conf
135
136     # Verify configuration files are present in expected location.
137     if [ ! -f ${AKKACONF} -o ! -f ${MODULESHARDSCONF} ]; then
138         # Check if the configuration files exist in the system
139         # directory, then copy them over.
140         ORIG_CONF_DIR=${CONTROLLER_DIR}/system/org/opendaylight/controller/sal-clustering-config
141         version=$(sed -n -e 's/.*<version>\(.*\)<\/version>/\1/p' ${ORIG_CONF_DIR}/maven-metadata-local.xml)
142         ORIG_CONF_DIR=${ORIG_CONF_DIR}/${version}
143         ORIG_AKKA_CONF=sal-clustering-config-${version}-akkaconf.xml
144         ORIG_MODULES_CONF=sal-clustering-config-${version}-moduleconf.xml
145         ORIG_MODULESHARDS_CONF=sal-clustering-config-${version}-moduleshardconf.xml
146
147         if [ -f ${ORIG_CONF_DIR}/${ORIG_AKKA_CONF} -a \
148              -f ${ORIG_CONF_DIR}/${ORIG_MODULES_CONF} -a \
149              -f ${ORIG_CONF_DIR}/${ORIG_MODULESHARDS_CONF} ]; then
150             cat <<EOF
151  NOTE: Cluster configuration files not found. Copying from
152  ${ORIG_CONF_DIR}
153 EOF
154             mkdir -p ${CONF_DIR}
155             cp ${ORIG_CONF_DIR}/${ORIG_AKKA_CONF} ${AKKACONF}
156             cp ${ORIG_CONF_DIR}/${ORIG_MODULES_CONF} ${MODULESCONF}
157             cp ${ORIG_CONF_DIR}/${ORIG_MODULESHARDS_CONF} ${MODULESHARDSCONF}
158
159         else
160             cat << EOF
161  ERROR: Cluster configurations files not found. Please\
162  configure clustering feature.
163 EOF
164             exit -1
165         fi
166     fi
167 }
168
169 function main
170 {
171     get_cli_params $*
172     start_banner
173     verify_configuration_files
174     create_strings
175     modify_conf_files
176     end_banner
177 }
178
179 main $*
180
181 # vim: ts=4 sw=4 sts=4 et ft=sh :
182