blob: cc90523044480ab35a50b1c7d912d68b2a4c7dd3 [file] [log] [blame]
Victor Morales89ce3212017-06-16 18:32:48 -05001#!/bin/bash
2
Victor Morales89ce3212017-06-16 18:32:48 -05003source /var/onap/commons
Victor Moralesdd074802017-07-26 16:06:35 -05004source /var/onap/_composed_functions
5source /var/onap/_onap_functions
6
Victor Morales96512652017-08-16 13:44:28 -05007export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1)
8export IP_ADDRESS=$(ifconfig eth0 | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
9
Victor Moralesdd074802017-07-26 16:06:35 -050010mvn_conf_file=/root/.m2/settings.xml
11git_src_folder=/opt
12
Victor Morales89ce3212017-06-16 18:32:48 -050013# configure_dns() - DNS/GW IP address configuration
14function configure_dns {
15 echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head
16 resolvconf -u
17}
18
Victor Moralesdd074802017-07-26 16:06:35 -050019# _git_timed() - git can sometimes get itself infinitely stuck with transient network
Victor Morales89ce3212017-06-16 18:32:48 -050020# errors or other issues with the remote end. This wraps git in a
21# timeout/retry loop and is intended to watch over non-local git
22# processes that might hang.
Victor Moralesdd074802017-07-26 16:06:35 -050023function _git_timed {
Victor Morales89ce3212017-06-16 18:32:48 -050024 local count=0
25 local timeout=0
26
27 install_package git
28 until timeout -s SIGINT ${timeout} git "$@"; do
29 # 124 is timeout(1)'s special return code when it reached the
30 # timeout; otherwise assume fatal failure
31 if [[ $? -ne 124 ]]; then
32 exit 1
33 fi
34
35 count=$(($count + 1))
36 if [ $count -eq 3 ]; then
37 exit 1
38 fi
39 sleep 5
40 done
41}
42
43# clone_repo() - Clone Git repository into specific folder
44function clone_repo {
Victor Morales9f1c94b2017-11-07 07:56:04 -080045 local repo_url=${3:-"https://git.onap.org/"}
Victor Morales89ce3212017-06-16 18:32:48 -050046 local repo=$1
Victor Moralesdd074802017-07-26 16:06:35 -050047 local dest_folder=${2:-$git_src_folder/$repo}
48 if [ ! -d $dest_folder ]; then
Victor Morales7abf1a82017-11-06 09:20:27 -080049 if [[ "$debug" == "False" ]]; then
50 _git_timed clone --quiet ${repo_url}${repo} $dest_folder
51 else
52 _git_timed clone ${repo_url}${repo} $dest_folder
53 fi
Victor Morales89ce3212017-06-16 18:32:48 -050054 else
55 pushd $dest_folder
Victor Moralesdd074802017-07-26 16:06:35 -050056 _git_timed pull
Victor Morales89ce3212017-06-16 18:32:48 -050057 popd
58 fi
59}
60
Victor Moralesdd074802017-07-26 16:06:35 -050061# _install_bind() - Install bind utils
62function _install_bind {
Victor Morales7ae05a42017-08-22 14:39:00 -050063 install_packages bind9 bind9utils
Victor Morales89ce3212017-06-16 18:32:48 -050064}
65
Victor Morales89ce3212017-06-16 18:32:48 -050066# install_java() - Install java binaries
67function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050068 if is_package_installed openjdk-8-jdk; then
69 return
70 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -080071 source /etc/os-release || source /usr/lib/os-release
72 case ${ID,,} in
73 *suse)
74 ;;
75 ubuntu|debian)
76 install_package software-properties-common
77 add-apt-repository -y ppa:openjdk-r/ppa
78 ;;
79 rhel|centos|fedora)
80 ;;
81 esac
Victor Morales332d7d82017-11-06 11:51:37 -080082 update_repos
Victor Morales7ae05a42017-08-22 14:39:00 -050083
84 # Remove Java 7
85 uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless
86
Victor Morales89ce3212017-06-16 18:32:48 -050087 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -050088 # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed.
89 /var/lib/dpkg/info/ca-certificates-java.postinst configure
Victor Morales89ce3212017-06-16 18:32:48 -050090}
91
92# install_maven() - Install maven binaries
93function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -050094 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -050095 return
96 fi
Victor Moralesece790c2017-08-08 11:11:36 -050097 install_java
Victor Morales9f1c94b2017-11-07 07:56:04 -080098 source /etc/os-release || source /usr/lib/os-release
99 case ${ID,,} in
100 *suse)
101 ;;
102 ubuntu|debian)
103 install_package software-properties-common
104 add-apt-repository -y ppa:andrei-pozolotin/maven3
105 ;;
106 rhel|centos|fedora)
107 ;;
108 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800109 update_repos
Victor Morales89ce3212017-06-16 18:32:48 -0500110 install_package maven3
111
Victor Morales7ae05a42017-08-22 14:39:00 -0500112 # Remove Java 7
113 uninstall_package openjdk-7-jdk
Victor Moralesdd074802017-07-26 16:06:35 -0500114
115 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500116}
117
Idan Amite17b5ec2017-08-16 14:17:59 +0300118# _configure_docker_settings() - Configures Docker settings
119function _configure_docker_settings {
Victor Moralescf269992017-10-18 09:29:55 -0700120 local docker_conf_backup=/tmp/docker.backup
121 local docker_conf=/etc/default/docker
122 local chameleonsocks_filename=chameleonsocks.sh
123
124 cp $docker_conf $docker_conf_backup
Victor Morales89ce3212017-06-16 18:32:48 -0500125 if [ $http_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700126 echo "export http_proxy=$http_proxy" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500127 fi
128 if [ $https_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700129 echo "export https_proxy=$https_proxy" >> $docker_conf
Victor Morales5ce04212017-09-24 09:03:32 -0700130 #If you have a socks proxy, then use that to connect to the nexus repo
Kiranf2824c22017-09-22 18:05:29 -0700131 #via a redsocks container
132 if [ $socks_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700133 wget https://raw.githubusercontent.com/crops/chameleonsocks/master/$chameleonsocks_filename
134 chmod 755 $chameleonsocks_filename
Victor Morales5ce04212017-09-24 09:03:32 -0700135 socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//")
136 port=$(echo $socks_proxy | sed -e "s/^.*://")
Victor Moralescf269992017-10-18 09:29:55 -0700137 PROXY=$socks PORT=$port ./$chameleonsocks_filename --install
138 rm $chameleonsocks_filename
139 cp $docker_conf_backup $docker_conf
Kiranf2824c22017-09-22 18:05:29 -0700140 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500141 fi
Victor Moralescf269992017-10-18 09:29:55 -0700142 rm $docker_conf_backup
Idan Amite17b5ec2017-08-16 14:17:59 +0300143
Victor Moralescf269992017-10-18 09:29:55 -0700144 echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> $docker_conf
Idan Amite17b5ec2017-08-16 14:17:59 +0300145 usermod -a -G docker vagrant
Victor Morales89ce3212017-06-16 18:32:48 -0500146}
147
Victor Moralesdd074802017-07-26 16:06:35 -0500148# install_nodejs() - Download and install NodeJS
149function install_nodejs {
150 if is_package_installed nodejs; then
151 return
152 fi
153 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
154 install_package nodejs
155
156 # Update NPM to latest version
157 npm install npm -g
158}
159
160# install_python() - Install Python 2.7 and other tools necessary for development.
161function install_python {
Victor Morales7ae05a42017-08-22 14:39:00 -0500162 install_packages python2.7 python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500163}
164
Victor Morales970ec192017-07-31 09:10:11 -0500165# _install_pip() - Install Python Package Manager
166function _install_pip {
167 install_python
Victor Morales158c18c2017-08-06 11:23:15 -0500168 if [ ! -f /usr/local/bin/pip ]; then
169 curl -sL https://bootstrap.pypa.io/get-pip.py | python
170 fi
Victor Morales970ec192017-07-31 09:10:11 -0500171}
172
173# install_python_package() - Install a python module
174function install_python_package {
175 local python_package=$1
176
177 _install_pip
178 pip install $python_package
179}
180
181# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500182function install_docker {
183 if is_package_installed docker-ce; then
184 return
185 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -0800186 source /etc/os-release || source /usr/lib/os-release
187 case ${ID,,} in
188 *suse)
189 ;;
190 ubuntu|debian)
Victor Morales071b81c2017-11-07 14:13:07 -0800191 install_packages software-properties-common linux-image-extra-$(uname -r) linux-image-extra-virtual apt-transport-https ca-certificates curl
Victor Morales9f1c94b2017-11-07 07:56:04 -0800192 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
193 add-apt-repository \
194 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
195 $(lsb_release -cs) stable"
196 ;;
197 rhel|centos|fedora)
198 ;;
199 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800200 update_repos
Victor Morales7abf1a82017-11-06 09:20:27 -0800201
Victor Moralesdd074802017-07-26 16:06:35 -0500202 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300203 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500204 service docker restart
205 sleep 10
206}
207
208# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
209function pull_docker_image {
210 install_docker
211 local image=$1
212 local tag=$2
213 docker pull ${image}
214 if [ ${tag} ]; then
215 docker tag ${image} $tag
216 fi
217}
218
219# install_docker_compose() - Download and install docker-engine
220function install_docker_compose {
221 local docker_compose_version=${1:-1.12.0}
222 if [ ! -d /opt/docker ]; then
223 mkdir /opt/docker
224 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
225 chmod +x /opt/docker/docker-compose
226 fi
227}
228
229# _install_ODL() - Download and Install OpenDayLight SDN controller
230function _install_ODL {
231 if [ ! -d /opt/opendaylight/current ]; then
232 mkdir -p /opt/opendaylight/
233 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
234 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
235 mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
236 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
237 fi
238}
239
240# start_ODL() - Start OpenDayLight SDN controller
241function start_ODL {
242 _install_ODL
243 if [ -d /opt/opendaylight ]; then
244 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
245 /opt/opendaylight/current/bin/start
246 sleep 180
247 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
248 fi
249}
250
251# compile_src() - Function that compiles the java source code thru maven
252function compile_src {
253 local src_folder=$1
254 pushd $src_folder
Victor Morales7abf1a82017-11-06 09:20:27 -0800255 local mvn_build='mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none'
256 if [[ "$debug" == "False" ]]; then
257 mvn_build+=" -q"
258 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500259 if [ -f pom.xml ]; then
260 install_maven
Victor Morales7abf1a82017-11-06 09:20:27 -0800261 echo "Compiling $src_folder folder..."
262 eval $mvn_build
Victor Moralesdd074802017-07-26 16:06:35 -0500263 fi
264 popd
265}
266
Victor Morales89ce3212017-06-16 18:32:48 -0500267# build_docker_image() - Build Docker container image from source code
268function build_docker_image {
269 local src_folder=$1
270 local profile=$2
271 install_maven
272 install_docker
273 pushd $src_folder
274
275 # Cleanup external repo
276 sed -i 's|${docker.push.registry}/||g' pom.xml
277 local mvn_docker="mvn clean package docker:build"
278 if [ $profile ]; then
279 mvn_docker+=" -P $profile"
280 fi
281 if [ $http_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500282 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
283 mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
284 fi
285 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
286 mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
287 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500288 fi
289 if [ $https_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500290 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
291 mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
292 fi
293 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
294 mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
295 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500296 fi
297 eval $mvn_docker
298 popd
299}