Bump versions by x.(y+1).z
[integration/distribution.git] / karaf-scripts / 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  Optionally, shards can be configured in a more granular way by
30  modifying the file "custom_shard_configs.txt" in the same folder
31  as this tool.  Please see that file for more details
32
33 EOF
34
35     exit 1
36 }
37
38
39 function start_banner
40 {
41 cat <<EOF
42 ################################################
43 ##             Configure Cluster              ##
44 ################################################
45 EOF
46 }
47
48 function end_banner
49 {
50 cat <<EOF
51 ################################################
52 ##   NOTE: Manually restart controller to     ##
53 ##         apply configuration.               ##
54 ################################################
55 EOF
56 }
57
58 # Utility function for joining strings.
59 function join {
60     delim=',\n\t\t\t\t'
61     final=$1; shift
62
63     for str in $* ; do
64         final=${final}${delim}${str}
65     done
66
67     echo ${final}
68 }
69
70 function create_strings
71 {
72     # Using a list of controller IPs, create the strings for data
73     # and rpc seed nodes, as well as the list of members.
74
75     # First create an arrays with one string per controller.
76     # Then merge the array using the join utility defined above.
77     count=1
78     for ip in ${CONTROLLERIPS[@]} ; do
79         ds[$count]=\\\"akka:\\/\\/opendaylight-cluster-data@${ip}:2550\\\"
80         rpc[$count]=\\\"akka:\\/\\/odl-cluster-rpc@${ip}:2551\\\"
81         members[$count]=\\\"member-${count}\\\"
82         count=$[count + 1]
83     done
84
85     DATA_SEED_LIST=$(join ${ds[@]})
86     RPC_SEED_LIST=$(join ${rpc[@]})
87     MEMBER_NAME_LIST=$(join ${members[@]})
88 }
89
90 function module_shards_builder
91 {
92
93     module_shards_string="module-shards = [\n\t{\n\t\tname = \"default\"\n\t\tshards = [\n\t\t\t{\n\t\t\t\tname = \"default\"\n\t\t\t\treplicas = []\n\t\t\t}\n\t\t]\n\t}"
94     for name in ${FRIENDLY_MODULE_NAMES[@]} ; do
95         module_shards_string="${module_shards_string},\n\t{\n\t\tname = \"${name}\"\n\t\tshards = [\n\t\t\t{\n\t\t\t\tname=\"${name}\"\n\t\t\t\treplicas = []\n\t\t\t}\n\t\t]\n\t}"
96     done
97
98     echo -e ${module_shards_string}"\n]"
99 }
100
101 function modules_builder
102 {
103
104     modules_string="modules = [\n\t"
105     count=1
106     for name in ${FRIENDLY_MODULE_NAMES[@]} ; do
107         modules_string="${modules_string}\n\t{\n\t\tname = \"${name}\"\n\t\tnamespace = \"${MODULE_NAMESPACES[${count}]}\"\n\t\tshard-strategy = \"module\"\n\t},"
108         count=$[count + 1]
109     done
110
111     if [ ${count} == 1 ]; then
112         # if no modules found in custom_shard_config.txt just close the bracket
113         echo -e ${modules_string}"\n]"
114     else
115         # using ::-1 below to remove the extra comma we get from the above loop
116         echo -e ${modules_string::-1}"\n]"
117     fi
118 }
119
120 function get_cli_params
121 {
122     # Check if params have been supplied
123     test $# -eq 0 && usage
124
125     # First param is index, and rest are controller list
126     INDEX=$1; shift
127     CONTROLLER_LIST=$*
128
129     # Verify we have controller list
130     test "${CONTROLLER_LIST}" == "" && usage "Missing controller list"
131
132     # Create the list of controllers from the CONTROLLER_LIST variable
133     CONTROLLERIPS=( ${CONTROLLER_LIST//,/ } )
134
135     test ${INDEX} -le 0 -o ${INDEX} -gt ${#CONTROLLERIPS[@]} && \
136         usage "Invalid index"
137
138     CONTROLLER_ID="member-${INDEX}"
139     CONTROLLER_IP="${CONTROLLERIPS[((${INDEX} - 1))]}"
140 }
141
142
143 function modify_conf_files
144 {
145     BIN_DIR=`dirname $0`
146     CUSTOM_SHARD_CONFIG_FILE=${BIN_DIR}'/custom_shard_config.txt'
147     echo "Configuring unique name in akka.conf"
148     sed -i -e "/roles[ ]*=/ { :loop1 /.*\]/ b done1; N; b loop1; :done1 s/roles.*\]/roles = [\"${CONTROLLER_ID}\"]/}" ${AKKACONF}
149
150     echo "Configuring hostname in akka.conf"
151     sed -i -e "s/hostname[ ]*=.*\"/hostname = \"${CONTROLLER_IP}\"/" ${AKKACONF}
152
153     echo "Configuring data and rpc seed nodes in akka.conf"
154     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}
155
156     if [ -f ${CUSTOM_SHARD_CONFIG_FILE} ]; then
157         source ${CUSTOM_SHARD_CONFIG_FILE}
158         if [ "${#FRIENDLY_MODULE_NAMES[@]}" -ne "${#MODULE_NAMESPACES[@]}" ]; then
159             echo -e "\ncustom shard config file \"${CUSTOM_SHARD_CONFIG_FILE}\" does not have the same number of FRIENDLY_MODULE_NAMES[] and MODULE_NAMESPACES[]\n"
160             exit 1
161         fi
162         module_shards_builder > ${MODULESHARDSCONF}
163         modules_builder > ${MODULESCONF}
164         cat ${MODULESCONF}
165     fi
166
167     echo "Configuring replication type in module-shards.conf"
168     sed -i -e "/^[^#].*replicas[ ]*=/ { :loop /.*\]/ b done; N; b loop; :done s/replicas.*\]/replicas = [${MEMBER_NAME_LIST}]/}" ${MODULESHARDSCONF}
169 }
170
171
172 function verify_configuration_files
173 {
174     # Constants
175     BIN_DIR=`dirname $0`
176     test ${BIN_DIR} == '.' && BIN_DIR=${PWD}
177     CONTROLLER_DIR=`dirname ${BIN_DIR}`
178     CONF_DIR=${CONTROLLER_DIR}/configuration/initial
179     AKKACONF=${CONF_DIR}/akka.conf
180     MODULESCONF=${CONF_DIR}/modules.conf
181     MODULESHARDSCONF=${CONF_DIR}/module-shards.conf
182
183     # Verify configuration files are present in expected location.
184     if [ ! -f ${AKKACONF} -o ! -f ${MODULESHARDSCONF} ]; then
185         # Check if the configuration files exist in the system
186         # directory, then copy them over.
187         ORIG_CONF_DIR=${CONTROLLER_DIR}/system/org/opendaylight/controller/sal-clustering-config
188         version=$(sed -n -e 's/.*<version>\(.*\)<\/version>/\1/p' ${ORIG_CONF_DIR}/maven-metadata-local.xml)
189         ORIG_CONF_DIR=${ORIG_CONF_DIR}/${version}
190         ORIG_AKKA_CONF=sal-clustering-config-${version}-akkaconf.xml
191         ORIG_MODULES_CONF=sal-clustering-config-${version}-moduleconf.xml
192         ORIG_MODULESHARDS_CONF=sal-clustering-config-${version}-moduleshardconf.xml
193
194         if [ -f ${ORIG_CONF_DIR}/${ORIG_AKKA_CONF} -a \
195              -f ${ORIG_CONF_DIR}/${ORIG_MODULES_CONF} -a \
196              -f ${ORIG_CONF_DIR}/${ORIG_MODULESHARDS_CONF} ]; then
197             cat <<EOF
198  NOTE: Cluster configuration files not found. Copying from
199  ${ORIG_CONF_DIR}
200 EOF
201             mkdir -p ${CONF_DIR}
202             cp ${ORIG_CONF_DIR}/${ORIG_AKKA_CONF} ${AKKACONF}
203             cp ${ORIG_CONF_DIR}/${ORIG_MODULES_CONF} ${MODULESCONF}
204             cp ${ORIG_CONF_DIR}/${ORIG_MODULESHARDS_CONF} ${MODULESHARDSCONF}
205
206         else
207             cat << EOF
208  ERROR: Cluster configurations files not found. Please\
209  configure clustering feature.
210 EOF
211             exit 1
212         fi
213     fi
214 }
215
216 function main
217 {
218     get_cli_params $*
219     start_banner
220     verify_configuration_files
221     create_strings
222     modify_conf_files
223     end_banner
224 }
225
226 main $*
227
228 # vim: ts=4 sw=4 sts=4 et ft=sh :