Update of Packer builder for Vagrant and various clouds
[osm/devops.git] / packer / packer_templates / osm / scripts / update_osm_info_docker.sh
1 #!/bin/bash
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 function usage(){
16 echo -e "usage: $0 (OPTIONS)"
17 echo -e "Replaces original host IP address (typically saved at /home/vagrant/oldIPaddress.txt) where VCA listens in OSM configuration by a new one. Also used by docker swarm."
18 echo -e " OPTIONS"
19 echo -e " -n <new_ip_addr>: new host ip address (default: current IP address)"
20 echo -e " -o <old_ip_addr>: old host ip address during initial install (default: content of /etc/osm/oldIPaddress.txt)"
21 echo -e " -h / --help: print this help"
22 }
23
24 while getopts ":n:o:-:h" o; do
25 case "${o}" in
26 n)
27 CURRENT_IP_ADDRESS="${OPTARG}"
28 ;;
29 o)
30 OLD_IP_ADDRESS="${OPTARG}"
31 ;;
32 -)
33 [ "${OPTARG}" == "help" ] && usage && exit 0
34 ;;
35 :)
36 echo "Option -$OPTARG ip address required" >&2
37 usage && exit 1
38 ;;
39 \?)
40 echo -e "Invalid option: '-$OPTARG'\n" >&2
41 usage && exit 1
42 ;;
43 h)
44 usage && exit 0
45 ;;
46 *)
47 usage && exit 1
48 ;;
49 esac
50 done
51
52 if [ -z "$CURRENT_IP_ADDRESS" ]
53 then
54 DEFAULT_IF=$(route -n |awk '$1~/^0.0.0.0/ {print $8}')
55 CURRENT_IP_ADDRESS=$(ip -o -4 a |grep ${DEFAULT_IF}|awk '{split($4,a,"/"); print a[1]}')
56 fi
57 [ -z "$OLD_IP_ADDRESS" ] && OLD_IP_ADDRESS=$(cat /etc/osm/oldIPaddress.txt)
58
59 VCA_HOST=$(cat /etc/osm/docker/lcm.env | grep OSMLCM_VCA_HOST | cut -f2 -d=)
60
61 sudo sed -i "s/$OLD_IP_ADDRESS/$CURRENT_IP_ADDRESS/g" /etc/osm/docker/lcm.env
62 sudo sed -i "s/$OLD_IP_ADDRESS/$CURRENT_IP_ADDRESS/g" /etc/osm/docker/mon.env
63 docker stack rm osm
64 sleep 20
65
66 # Clean previous ip address info from Docker Swarm and reinitialize
67 docker swarm leave --force
68 docker swarm init --advertise-addr $CURRENT_IP_ADDRESS
69 sudo systemctl restart docker
70 docker network create --driver=overlay --attachable --opt com.docker.network.driver.mtu=1500 netosm
71
72 # Deploy docker stack
73 source /etc/osm/docker/osm_ports.sh
74 docker stack deploy -c /etc/osm/docker/docker-compose.yaml osm
75
76 sudo iptables -t nat -D PREROUTING -p tcp -m tcp -d $OLD_IP_ADDRESS --dport 17070 -j DNAT --to-destination $VCA_HOST
77 sudo iptables -t nat -A PREROUTING -p tcp -m tcp -d $CURRENT_IP_ADDRESS --dport 17070 -j DNAT --to-destination $VCA_HOST
78
79 echo "[DONE]"