Fix onap-odl-neon image
[ccsdk/distribution.git] / opendaylight / neon / neon-alpine / src / main / odlscripts / configure-cluster-ipdetect.sh
1 #!/bin/bash
2 #
3 # Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
4 # Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
5 #
6 # This program and the accompanying materials are made available under the
7 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 # and is available at http://www.eclipse.org/legal/epl-v10.html
9 #
10
11
12 function usage()
13 {
14     # Print any error messages
15     test "$1" != "" && echo " ERROR: $1"
16
17     # Print standard usage help
18     cat << EOF
19  This script is used to configure cluster parameters on this
20  controller. The user should restart controller to apply changes.
21
22  Usage: $0 <seed_nodes_list>
23   - seed_nodes_list: List of seed nodes, separated by comma or space.
24
25  The script checks that one (any) of the the controller's active IP
26  addresses is present in the seed_nodes_list. When running this script
27  on multiple  seed nodes, keep the seed_node_list same on all nodes.
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 This script is currently limited to IPv4 addresses. If you have
34 problems running this script, please use 'configure_cluster.sh'.
35
36 EOF
37
38     exit 1
39 }
40
41
42 function start_banner
43 {
44 cat <<EOF
45 ################################################
46 ##             Configure Cluster              ##
47 ################################################
48 EOF
49 }
50
51 function end_banner
52 {
53 cat <<EOF
54 ################################################
55 ##   NOTE: Manually restart controller to     ##
56 ##         apply configuration.               ##
57 ################################################
58 EOF
59 }
60
61 # Utility function for joining strings.
62 function join {
63     delim=',\n\t\t\t\t'
64     final=$1; shift
65
66     for str in $* ; do
67         final=${final}${delim}${str}
68     done
69
70     echo ${final}
71 }
72
73 function create_strings
74 {
75     # Using a list of controller IPs, create the strings for data
76     # and rpc seed nodes, as well as the list of members.
77
78     # First create an arrays with one string per controller.
79     # Then merge the array using the join utility defined above.
80     count=1
81     for ip in ${CONTROLLERIPS[@]} ; do
82         ds[$count]=\\\"akka.tcp:\\/\\/opendaylight-cluster-data@${ip}:2550\\\"
83         rpc[$count]=\\\"akka.tcp:\\/\\/odl-cluster-rpc@${ip}:2551\\\"
84         members[$count]=\\\"member-${count}\\\"
85         count=$[count + 1]
86     done
87
88     DATA_SEED_LIST=$(join ${ds[@]})
89     RPC_SEED_LIST=$(join ${rpc[@]})
90     MEMBER_NAME_LIST=$(join ${members[@]})
91 }
92
93 function module_shards_builder
94 {
95
96     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}"
97     for name in ${FRIENDLY_MODULE_NAMES[@]} ; do
98         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}"
99     done
100
101     echo -e ${module_shards_string}"\n]"
102 }
103
104 function modules_builder
105 {
106
107     modules_string="modules = [\n\t"
108     count=1
109     for name in ${FRIENDLY_MODULE_NAMES[@]} ; do
110         modules_string="${modules_string}\n\t{\n\t\tname = \"${name}\"\n\t\tnamespace = \"${MODULE_NAMESPACES[${count}]}\"\n\t\tshard-strategy = \"module\"\n\t},"
111         count=$[count + 1]
112     done
113
114     # using ::-1 below to remove the extra comma we get from the above loop
115     echo -e ${modules_string::-1}"\n]"
116 }
117
118
119 function get_index ()
120 {
121     # Determine if the local IP address is in the CONTROLLER_LIST
122     # and its index in the list. Return the index.
123
124     local MY_IP=$1
125     shift
126     local IP_ADDRS=("$@")
127     local COUNTER=1
128
129     for IP in ${IP_ADDRS[@]} ;
130     do
131         if [ "$MY_IP" == "$IP" ]; then
132             echo "$COUNTER"
133             return
134         fi
135         COUNTER=$[$COUNTER + 1]
136     done
137     echo "$COUNTER"
138 }
139
140 function get_local_ip_addresses
141 {
142     # Get the local node's IP addresses as list
143     LOCAL_IPS=()
144     for IP in `hostname -I`
145     do
146         LOCAL_IPS+=("$IP")
147     done
148     echo ${LOCAL_IPS[@]}
149 }
150
151 function get_cli_params
152 {
153     # Check if params have been supplied
154     CONTROLLER_LIST=$*
155
156     # Verify we have controller list
157     test "${CONTROLLER_LIST}" == "" && usage "Missing controller list"
158
159     # Create the list of controllers from the CONTROLLER_LIST variable
160     CONTROLLERIPS=( ${CONTROLLER_LIST//,/ } )
161
162     # Get the local node's IP addresses
163     LOCAL_IPS=$(get_local_ip_addresses)
164
165     for CONTROLLER_IP in ${LOCAL_IPS[@]} ;
166     do
167         INDEX=$(get_index $CONTROLLER_IP ${CONTROLLERIPS[@]})
168         if [ ${INDEX} -le ${#CONTROLLERIPS[@]} ] ; then
169             break
170         fi
171     done
172
173     test ${INDEX} -le 0 -o ${INDEX} -gt ${#CONTROLLERIPS[@]} && \
174         usage "Controller's local IP address not in the controller list"
175
176     CONTROLLER_ID="member-${INDEX}"
177 }
178
179
180 function modify_conf_files
181 {
182     BIN_DIR=`dirname $0`
183     CUSTOM_SHARD_CONFIG_FILE=${BIN_DIR}'/custom_shard_config.txt'
184     echo "Configuring unique name in akka.conf"
185     sed -i -e "/roles[ ]*=/ { :loop1 /.*\]/ b done1; N; b loop1; :done1 s/roles.*\]/roles = [\"${CONTROLLER_ID}\"]/}" ${AKKACONF}
186
187     echo "Configuring hostname in akka.conf"
188     sed -i -e "s/hostname[ ]*=.*\"/hostname = \"${CONTROLLER_IP}\"/" ${AKKACONF}
189
190     echo "Configuring data and rpc seed nodes in akka.conf"
191     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}
192
193     if [ -f ${CUSTOM_SHARD_CONFIG_FILE} ]; then
194         source ${CUSTOM_SHARD_CONFIG_FILE}
195         if [ "${#FRIENDLY_MODULE_NAMES[@]}" -ne "${#MODULE_NAMESPACES[@]}" ]; then
196             echo -e "\ncustom shard config file \"${CUSTOM_SHARD_CONFIG_FILE}\" does not have the same number of FRIENDLY_MODULE_NAMES[] and MODULE_NAMESPACES[]\n"
197             exit 1
198         fi
199         module_shards_builder > ${MODULESHARDSCONF}
200         modules_builder > ${MODULESCONF}
201         cat ${MODULESCONF}
202     fi
203
204     echo "Configuring replication type in module-shards.conf"
205     sed -i -e "/^[^#].*replicas[ ]*=/ { :loop /.*\]/ b done; N; b loop; :done s/replicas.*\]/replicas = [${MEMBER_NAME_LIST}]/}" ${MODULESHARDSCONF}
206 }
207
208
209 function verify_configuration_files
210 {
211     # Constants
212     BIN_DIR=`dirname $0`
213     test ${BIN_DIR} == '.' && BIN_DIR=${PWD}
214     CONTROLLER_DIR=`dirname ${BIN_DIR}`
215     CONF_DIR=${CONTROLLER_DIR}/configuration/initial
216     AKKACONF=${CONF_DIR}/akka.conf
217     MODULESCONF=${CONF_DIR}/modules.conf
218     MODULESHARDSCONF=${CONF_DIR}/module-shards.conf
219
220     # Verify configuration files are present in expected location.
221     if [ ! -f ${AKKACONF} -o ! -f ${MODULESHARDSCONF} ]; then
222         # Check if the configuration files exist in the system
223         # directory, then copy them over.
224         ORIG_CONF_DIR=${CONTROLLER_DIR}/system/org/opendaylight/controller/sal-clustering-config
225         version=$(sed -n -e 's/.*<version>\(.*\)<\/version>/\1/p' ${ORIG_CONF_DIR}/maven-metadata-local.xml)
226         ORIG_CONF_DIR=${ORIG_CONF_DIR}/${version}
227         ORIG_AKKA_CONF=sal-clustering-config-${version}-akkaconf.xml
228         ORIG_MODULES_CONF=sal-clustering-config-${version}-moduleconf.xml
229         ORIG_MODULESHARDS_CONF=sal-clustering-config-${version}-moduleshardconf.xml
230
231         if [ -f ${ORIG_CONF_DIR}/${ORIG_AKKA_CONF} -a \
232              -f ${ORIG_CONF_DIR}/${ORIG_MODULES_CONF} -a \
233              -f ${ORIG_CONF_DIR}/${ORIG_MODULESHARDS_CONF} ]; then
234             cat <<EOF
235  NOTE: Cluster configuration files not found. Copying from
236  ${ORIG_CONF_DIR}
237 EOF
238             mkdir -p ${CONF_DIR}
239             cp ${ORIG_CONF_DIR}/${ORIG_AKKA_CONF} ${AKKACONF}
240             cp ${ORIG_CONF_DIR}/${ORIG_MODULES_CONF} ${MODULESCONF}
241             cp ${ORIG_CONF_DIR}/${ORIG_MODULESHARDS_CONF} ${MODULESHARDSCONF}
242
243         else
244             cat << EOF
245  ERROR: Cluster configurations files not found. Please\
246  configure clustering feature.
247 EOF
248             exit 1
249         fi
250     fi
251 }
252
253 function main
254 {
255     get_cli_params $*
256     start_banner
257     verify_configuration_files
258     create_strings
259     modify_conf_files
260     end_banner
261 }
262
263 main $*
264
265 # vim: ts=4 sw=4 sts=4 et ft=sh :