fd0ae90004e02b273a17adf3b8a63792d291da16
[demo.git] / boot / sdc_install.sh
1 #!/bin/bash
2
3 # Read configuration files
4 ARTIFACTS_VERSION=$(cat /opt/config/artifacts_version.txt)
5 DNS_IP_ADDR=$(cat /opt/config/dns_ip_addr.txt)
6 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
7 GERRIT_BRANCH=$(cat /opt/config/gerrit_branch.txt)
8 MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' | sort -n | head -1)
9 CODE_REPO=$(cat /opt/config/remote_repo.txt)
10 HTTP_PROXY=$(cat /opt/config/http_proxy.txt)
11 HTTPS_PROXY=$(cat /opt/config/https_proxy.txt)
12
13 if [ $HTTP_PROXY != "no_proxy" ]
14 then
15     export http_proxy=$HTTP_PROXY
16     export https_proxy=$HTTPS_PROXY
17 fi
18
19 # Add host name to /etc/host to avoid warnings in openstack images
20 if [[ $CLOUD_ENV != "rackspace" ]]
21 then
22         echo 127.0.0.1 $(hostname) >> /etc/hosts
23
24         # Allow remote login as root
25         mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
26         cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
27 fi
28
29 # Set private IP in /etc/network/interfaces manually in the presence of public interface
30 # Some VM images don't add the private interface automatically, we have to do it during the component installation
31 if [[ $CLOUD_ENV == "openstack_nofloat" ]]
32 then
33         LOCAL_IP=$(cat /opt/config/local_ip_addr.txt)
34         CIDR=$(cat /opt/config/oam_network_cidr.txt)
35         BITMASK=$(echo $CIDR | cut -d"/" -f2)
36
37         # Compute the netmask based on the network cidr
38         if [[ $BITMASK == "8" ]]
39         then
40                 NETMASK=255.0.0.0
41         elif [[ $BITMASK == "16" ]]
42         then
43                 NETMASK=255.255.0.0
44         elif [[ $BITMASK == "24" ]]
45         then
46                 NETMASK=255.255.255.0
47         fi
48
49         echo "auto eth1" >> /etc/network/interfaces
50         echo "iface eth1 inet static" >> /etc/network/interfaces
51         echo "    address $LOCAL_IP" >> /etc/network/interfaces
52         echo "    netmask $NETMASK" >> /etc/network/interfaces
53         echo "    mtu $MTU" >> /etc/network/interfaces
54         ifup eth1
55 fi
56
57 # Download dependencies
58 apt-get update
59 apt-get install -y apt-transport-https ca-certificates wget git ntp ntpdate make
60
61 # Download scripts from Nexus
62 unzip -p -j /opt/boot-$ARTIFACTS_VERSION.zip sdc_vm_init.sh > /opt/sdc_vm_init.sh
63 unzip -p -j /opt/boot-$ARTIFACTS_VERSION.zip sdc_serv.sh > /opt/sdc_serv.sh
64 unzip -p -j /opt/boot-$ARTIFACTS_VERSION.zip sdc_wfd_vm_init.sh > /opt/sdc_wfd_vm_init.sh
65 chmod +x /opt/sdc_vm_init.sh
66 chmod +x /opt/sdc_serv.sh
67 chmod +x /opt/sdc_wfd_vm_init.sh
68 mv /opt/sdc_serv.sh /etc/init.d
69 update-rc.d sdc_serv.sh defaults
70
71 # Download and install docker-engine and docker-compose
72 echo "deb https://apt.dockerproject.org/repo ubuntu-$(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/docker.list
73 apt-get update
74 apt-get install -y linux-image-extra-$(uname -r) linux-image-extra-virtual
75 apt-get install -y --allow-unauthenticated docker-engine
76
77 mkdir /opt/docker
78 curl -L https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
79 chmod +x /opt/docker/docker-compose
80
81 # Create partition and mount the external volume
82 unzip -p -j /opt/boot-$ARTIFACTS_VERSION.zip sdc_ext_volume_partitions.txt > /opt/sdc_ext_volume_partitions.txt
83
84 if [[ $CLOUD_ENV == "rackspace" ]]
85 then
86         DISK="xvdb"
87 else
88         DISK=$(ls /dev |grep -e '^.*db$')
89         sed -i "s/xvdb/$DISK/g" /opt/sdc_ext_volume_partitions.txt
90 fi
91
92 sfdisk /dev/$DISK < /opt/sdc_ext_volume_partitions.txt
93 mkfs -t ext4 /dev/$DISK"1"
94 mkdir -p /data
95 mount /dev/$DISK"1" /data
96 echo "/dev/"$DISK"1  /data           ext4    errors=remount-ro,noatime,barrier=0 0       1" >> /etc/fstab
97
98 # Set the MTU size of docker containers to the minimum MTU size supported by vNICs. OpenStack deployments may need to know the external DNS IP
99 DNS_FLAG=""
100 if [ -s /opt/config/dns_ip_addr.txt ]
101 then
102         DNS_FLAG=$DNS_FLAG"--dns $(cat /opt/config/dns_ip_addr.txt) "
103 fi
104 if [ -s /opt/config/external_dns.txt ]
105 then
106         DNS_FLAG=$DNS_FLAG"--dns $(cat /opt/config/external_dns.txt) "
107 fi
108 echo "DOCKER_OPTS=\"$DNS_FLAG--mtu=$MTU\"" >> /etc/default/docker
109
110 cp /lib/systemd/system/docker.service /etc/systemd/system
111 sed -i "/ExecStart/s/$/ --mtu=$MTU/g" /etc/systemd/system/docker.service
112 if [ $HTTP_PROXY != "no_proxy" ]
113 then
114 cd /opt
115 ./imagetest.sh
116 fi
117 service docker restart
118
119 # DNS IP address configuration
120 echo "nameserver "$DNS_IP_ADDR >> /etc/resolvconf/resolv.conf.d/head
121 resolvconf -u
122
123 # Clone Gerrit repository
124 cd /opt
125 mkdir -p /data/environments
126 mkdir -p /data/scripts
127 mkdir -p /data/logs/BE
128 mkdir -p /data/logs/FE
129 chmod 777 /data
130 chmod 777 /data/logs/BE
131 chmod 777 /data/logs/FE
132
133 git clone -b $GERRIT_BRANCH --single-branch $CODE_REPO
134
135 cat > /root/.bash_aliases << EOF
136 alias dcls='/data/scripts/docker_clean.sh \$1'
137 alias dlog='/data/scripts/docker_login.sh \$1'
138 alias rund='/data/scripts/docker_run.sh'
139 alias health='/data/scripts/docker_health.sh'
140 EOF
141
142 # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
143 if [[ $CLOUD_ENV != "rackspace" ]]
144 then
145         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
146         grub-mkconfig -o /boot/grub/grub.cfg
147         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
148         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
149         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
150         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
151         reboot
152 fi
153
154 # Run docker containers. For openstack Ubuntu 16.04 images this will run as a service after the VM has restarted
155 ./sdc_vm_init.sh
156 ./sdc_wfd_vm_init.sh