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