blob: 49ea0378b0deb7e99b4ce200571ed46df2507d2e [file] [log] [blame]
Victor Morales89ce3212017-06-16 18:32:48 -05001#!/bin/bash
2
3set -o xtrace
4
5source /var/onap/commons
Victor Moralesdd074802017-07-26 16:06:35 -05006source /var/onap/_composed_functions
7source /var/onap/_onap_functions
8
Victor Morales96512652017-08-16 13:44:28 -05009export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1)
10export IP_ADDRESS=$(ifconfig eth0 | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
11
Victor Moralesdd074802017-07-26 16:06:35 -050012mvn_conf_file=/root/.m2/settings.xml
13git_src_folder=/opt
14
Victor Morales89ce3212017-06-16 18:32:48 -050015# configure_dns() - DNS/GW IP address configuration
16function configure_dns {
17 echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head
18 resolvconf -u
19}
20
Victor Moralesdd074802017-07-26 16:06:35 -050021# _git_timed() - git can sometimes get itself infinitely stuck with transient network
Victor Morales89ce3212017-06-16 18:32:48 -050022# errors or other issues with the remote end. This wraps git in a
23# timeout/retry loop and is intended to watch over non-local git
24# processes that might hang.
Victor Moralesdd074802017-07-26 16:06:35 -050025function _git_timed {
Victor Morales89ce3212017-06-16 18:32:48 -050026 local count=0
27 local timeout=0
28
29 install_package git
30 until timeout -s SIGINT ${timeout} git "$@"; do
31 # 124 is timeout(1)'s special return code when it reached the
32 # timeout; otherwise assume fatal failure
33 if [[ $? -ne 124 ]]; then
34 exit 1
35 fi
36
37 count=$(($count + 1))
38 if [ $count -eq 3 ]; then
39 exit 1
40 fi
41 sleep 5
42 done
43}
44
45# clone_repo() - Clone Git repository into specific folder
46function clone_repo {
47 local repo_url=https://git.onap.org/
48 local repo=$1
Victor Moralesdd074802017-07-26 16:06:35 -050049 local dest_folder=${2:-$git_src_folder/$repo}
50 if [ ! -d $dest_folder ]; then
Victor Morales96512652017-08-16 13:44:28 -050051 _git_timed clone ${repo_url}${repo} $dest_folder
Victor Morales89ce3212017-06-16 18:32:48 -050052 else
53 pushd $dest_folder
Victor Moralesdd074802017-07-26 16:06:35 -050054 _git_timed pull
Victor Morales89ce3212017-06-16 18:32:48 -050055 popd
56 fi
57}
58
59# install_dev_tools() - Install basic dependencies
60function install_dev_tools {
Victor Morales7ae05a42017-08-22 14:39:00 -050061 install_packages apt-transport-https ca-certificates curl
Victor Morales89ce3212017-06-16 18:32:48 -050062}
63
Victor Moralesdd074802017-07-26 16:06:35 -050064# _install_bind() - Install bind utils
65function _install_bind {
Victor Morales7ae05a42017-08-22 14:39:00 -050066 install_packages bind9 bind9utils
Victor Morales89ce3212017-06-16 18:32:48 -050067}
68
Victor Morales89ce3212017-06-16 18:32:48 -050069# install_java() - Install java binaries
70function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050071 if is_package_installed openjdk-8-jdk; then
72 return
73 fi
Victor Morales89ce3212017-06-16 18:32:48 -050074 install_package software-properties-common
75 add-apt-repository -y ppa:openjdk-r/ppa
Victor Morales7ae05a42017-08-22 14:39:00 -050076
77 # Remove Java 7
78 uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless
79
Victor Morales89ce3212017-06-16 18:32:48 -050080 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -050081 # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed.
82 /var/lib/dpkg/info/ca-certificates-java.postinst configure
Victor Morales89ce3212017-06-16 18:32:48 -050083}
84
85# install_maven() - Install maven binaries
86function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -050087 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -050088 return
89 fi
Victor Moralesece790c2017-08-08 11:11:36 -050090 install_java
Victor Morales89ce3212017-06-16 18:32:48 -050091 install_package software-properties-common
92 add-apt-repository -y ppa:andrei-pozolotin/maven3
93 install_package maven3
94
Victor Morales7ae05a42017-08-22 14:39:00 -050095 # Remove Java 7
96 uninstall_package openjdk-7-jdk
Victor Moralesdd074802017-07-26 16:06:35 -050097
98 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -050099}
100
Idan Amite17b5ec2017-08-16 14:17:59 +0300101# _configure_docker_settings() - Configures Docker settings
102function _configure_docker_settings {
Victor Morales89ce3212017-06-16 18:32:48 -0500103 if [ $http_proxy ]; then
104 echo "export http_proxy=$http_proxy" >> /etc/default/docker
105 fi
106 if [ $https_proxy ]; then
107 echo "export https_proxy=$https_proxy" >> /etc/default/docker
Kiranf2824c22017-09-22 18:05:29 -0700108 #If you have a socks proxy, then use that to connect to the nexus repo
109 #via a redsocks container
110 if [ $socks_proxy ]; then
111 wget https://raw.githubusercontent.com/crops/chameleonsocks/master/chameleonsocks.sh
112 chmod 755 chameleonsocks.sh
113 local socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//")
114 local port=$(echo $socks_proxy | sed -e "s/^.*://")
115 PROXY=$socks PORT=$port ./chameleonsocks.sh --install
116 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500117 fi
Idan Amite17b5ec2017-08-16 14:17:59 +0300118
119 echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> /etc/default/docker
120 usermod -a -G docker vagrant
Victor Morales89ce3212017-06-16 18:32:48 -0500121}
122
Victor Moralesdd074802017-07-26 16:06:35 -0500123# install_nodejs() - Download and install NodeJS
124function install_nodejs {
125 if is_package_installed nodejs; then
126 return
127 fi
128 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
129 install_package nodejs
130
131 # Update NPM to latest version
132 npm install npm -g
133}
134
135# install_python() - Install Python 2.7 and other tools necessary for development.
136function install_python {
Victor Morales7ae05a42017-08-22 14:39:00 -0500137 install_packages python2.7 python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500138}
139
Victor Morales970ec192017-07-31 09:10:11 -0500140# _install_pip() - Install Python Package Manager
141function _install_pip {
142 install_python
Victor Morales158c18c2017-08-06 11:23:15 -0500143 if [ ! -f /usr/local/bin/pip ]; then
144 curl -sL https://bootstrap.pypa.io/get-pip.py | python
145 fi
Victor Morales970ec192017-07-31 09:10:11 -0500146}
147
148# install_python_package() - Install a python module
149function install_python_package {
150 local python_package=$1
151
152 _install_pip
153 pip install $python_package
154}
155
156# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500157function install_docker {
158 if is_package_installed docker-ce; then
159 return
160 fi
161 install_package software-properties-common
162 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
163 add-apt-repository \
164 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
165 $(lsb_release -cs) \
166 stable"
167 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300168 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500169 service docker restart
170 sleep 10
171}
172
173# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
174function pull_docker_image {
175 install_docker
176 local image=$1
177 local tag=$2
178 docker pull ${image}
179 if [ ${tag} ]; then
180 docker tag ${image} $tag
181 fi
182}
183
184# install_docker_compose() - Download and install docker-engine
185function install_docker_compose {
186 local docker_compose_version=${1:-1.12.0}
187 if [ ! -d /opt/docker ]; then
188 mkdir /opt/docker
189 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
190 chmod +x /opt/docker/docker-compose
191 fi
192}
193
194# _install_ODL() - Download and Install OpenDayLight SDN controller
195function _install_ODL {
196 if [ ! -d /opt/opendaylight/current ]; then
197 mkdir -p /opt/opendaylight/
198 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
199 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
200 mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
201 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
202 fi
203}
204
205# start_ODL() - Start OpenDayLight SDN controller
206function start_ODL {
207 _install_ODL
208 if [ -d /opt/opendaylight ]; then
209 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
210 /opt/opendaylight/current/bin/start
211 sleep 180
212 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
213 fi
214}
215
216# compile_src() - Function that compiles the java source code thru maven
217function compile_src {
218 local src_folder=$1
219 pushd $src_folder
220 if [ -f pom.xml ]; then
221 install_maven
Victor Morales970ec192017-07-31 09:10:11 -0500222 mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none
Victor Moralesdd074802017-07-26 16:06:35 -0500223 fi
224 popd
225}
226
Victor Morales89ce3212017-06-16 18:32:48 -0500227# build_docker_image() - Build Docker container image from source code
228function build_docker_image {
229 local src_folder=$1
230 local profile=$2
231 install_maven
232 install_docker
233 pushd $src_folder
234
235 # Cleanup external repo
236 sed -i 's|${docker.push.registry}/||g' pom.xml
237 local mvn_docker="mvn clean package docker:build"
238 if [ $profile ]; then
239 mvn_docker+=" -P $profile"
240 fi
241 if [ $http_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500242 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
243 mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
244 fi
245 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
246 mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
247 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500248 fi
249 if [ $https_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500250 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
251 mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
252 fi
253 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
254 mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
255 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500256 fi
257 eval $mvn_docker
258 popd
259}