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