blob: c4ed3c85f31aa1602920ba12b0282cd5d8f657d7 [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)
Victor Morales4ab71c12017-11-08 07:28:28 -08008export NIC=$(ip route get 8.8.8.8 | awk '{ print $5; exit }')
9export IP_ADDRESS=$(ifconfig $NIC | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
Victor Morales96512652017-08-16 13:44:28 -050010
Victor Moralesdd074802017-07-26 16:06:35 -050011mvn_conf_file=/root/.m2/settings.xml
12git_src_folder=/opt
13
Victor Morales89ce3212017-06-16 18:32:48 -050014# configure_dns() - DNS/GW IP address configuration
15function configure_dns {
16 echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head
17 resolvconf -u
18}
19
Victor Morales4ab71c12017-11-08 07:28:28 -080020# get_next_ip() - Function that provides the next ip
21function get_next_ip {
22 local ip=${1:-$IP_ADDRESS}
23 ip_hex=$(printf '%.2X%.2X%.2X%.2X\n' `echo $ip | sed -e 's/\./ /g'`)
24 next_ip_hex=$(printf %.8X `echo $(( 0x$ip_hex + 1 ))`)
25 echo $(printf '%d.%d.%d.%d\n' `echo $next_ip_hex | sed -r 's/(..)/0x\1 /g'`)
26}
27
Victor Moralesdd074802017-07-26 16:06:35 -050028# _git_timed() - git can sometimes get itself infinitely stuck with transient network
Victor Morales89ce3212017-06-16 18:32:48 -050029# errors or other issues with the remote end. This wraps git in a
30# timeout/retry loop and is intended to watch over non-local git
31# processes that might hang.
Victor Moralesdd074802017-07-26 16:06:35 -050032function _git_timed {
Victor Morales89ce3212017-06-16 18:32:48 -050033 local count=0
34 local timeout=0
35
36 install_package git
37 until timeout -s SIGINT ${timeout} git "$@"; do
38 # 124 is timeout(1)'s special return code when it reached the
39 # timeout; otherwise assume fatal failure
40 if [[ $? -ne 124 ]]; then
41 exit 1
42 fi
43
44 count=$(($count + 1))
45 if [ $count -eq 3 ]; then
46 exit 1
47 fi
48 sleep 5
49 done
50}
51
52# clone_repo() - Clone Git repository into specific folder
53function clone_repo {
Victor Morales9f1c94b2017-11-07 07:56:04 -080054 local repo_url=${3:-"https://git.onap.org/"}
Victor Morales89ce3212017-06-16 18:32:48 -050055 local repo=$1
Victor Moralesdd074802017-07-26 16:06:35 -050056 local dest_folder=${2:-$git_src_folder/$repo}
57 if [ ! -d $dest_folder ]; then
Victor Morales7abf1a82017-11-06 09:20:27 -080058 if [[ "$debug" == "False" ]]; then
59 _git_timed clone --quiet ${repo_url}${repo} $dest_folder
60 else
61 _git_timed clone ${repo_url}${repo} $dest_folder
62 fi
Victor Morales89ce3212017-06-16 18:32:48 -050063 else
64 pushd $dest_folder
Victor Moralesdd074802017-07-26 16:06:35 -050065 _git_timed pull
Victor Morales89ce3212017-06-16 18:32:48 -050066 popd
67 fi
68}
69
Victor Moralesdd074802017-07-26 16:06:35 -050070# _install_bind() - Install bind utils
71function _install_bind {
Victor Morales7ae05a42017-08-22 14:39:00 -050072 install_packages bind9 bind9utils
Victor Morales89ce3212017-06-16 18:32:48 -050073}
74
Victor Morales89ce3212017-06-16 18:32:48 -050075# install_java() - Install java binaries
76function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050077 if is_package_installed openjdk-8-jdk; then
78 return
79 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -080080 source /etc/os-release || source /usr/lib/os-release
81 case ${ID,,} in
82 *suse)
83 ;;
84 ubuntu|debian)
85 install_package software-properties-common
86 add-apt-repository -y ppa:openjdk-r/ppa
87 ;;
88 rhel|centos|fedora)
89 ;;
90 esac
Victor Morales332d7d82017-11-06 11:51:37 -080091 update_repos
Victor Morales7ae05a42017-08-22 14:39:00 -050092
93 # Remove Java 7
94 uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless
95
Victor Morales89ce3212017-06-16 18:32:48 -050096 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -050097 # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed.
98 /var/lib/dpkg/info/ca-certificates-java.postinst configure
Victor Morales89ce3212017-06-16 18:32:48 -050099}
100
101# install_maven() - Install maven binaries
102function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -0500103 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -0500104 return
105 fi
Victor Moralesece790c2017-08-08 11:11:36 -0500106 install_java
Victor Morales9f1c94b2017-11-07 07:56:04 -0800107 source /etc/os-release || source /usr/lib/os-release
108 case ${ID,,} in
109 *suse)
110 ;;
111 ubuntu|debian)
112 install_package software-properties-common
113 add-apt-repository -y ppa:andrei-pozolotin/maven3
114 ;;
115 rhel|centos|fedora)
116 ;;
117 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800118 update_repos
Victor Morales89ce3212017-06-16 18:32:48 -0500119 install_package maven3
120
Victor Morales7ae05a42017-08-22 14:39:00 -0500121 # Remove Java 7
122 uninstall_package openjdk-7-jdk
Victor Moralesdd074802017-07-26 16:06:35 -0500123
124 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500125}
126
Idan Amite17b5ec2017-08-16 14:17:59 +0300127# _configure_docker_settings() - Configures Docker settings
128function _configure_docker_settings {
Victor Moralescf269992017-10-18 09:29:55 -0700129 local docker_conf_backup=/tmp/docker.backup
130 local docker_conf=/etc/default/docker
131 local chameleonsocks_filename=chameleonsocks.sh
132
133 cp $docker_conf $docker_conf_backup
Victor Morales89ce3212017-06-16 18:32:48 -0500134 if [ $http_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700135 echo "export http_proxy=$http_proxy" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500136 fi
137 if [ $https_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700138 echo "export https_proxy=$https_proxy" >> $docker_conf
Victor Morales5ce04212017-09-24 09:03:32 -0700139 #If you have a socks proxy, then use that to connect to the nexus repo
Kiranf2824c22017-09-22 18:05:29 -0700140 #via a redsocks container
141 if [ $socks_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700142 wget https://raw.githubusercontent.com/crops/chameleonsocks/master/$chameleonsocks_filename
143 chmod 755 $chameleonsocks_filename
Victor Morales5ce04212017-09-24 09:03:32 -0700144 socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//")
145 port=$(echo $socks_proxy | sed -e "s/^.*://")
Victor Moralescf269992017-10-18 09:29:55 -0700146 PROXY=$socks PORT=$port ./$chameleonsocks_filename --install
147 rm $chameleonsocks_filename
148 cp $docker_conf_backup $docker_conf
Kiranf2824c22017-09-22 18:05:29 -0700149 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500150 fi
Victor Moralescf269992017-10-18 09:29:55 -0700151 rm $docker_conf_backup
Idan Amite17b5ec2017-08-16 14:17:59 +0300152
Victor Moralescf269992017-10-18 09:29:55 -0700153 echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500154}
155
Victor Moralesdd074802017-07-26 16:06:35 -0500156# install_nodejs() - Download and install NodeJS
157function install_nodejs {
158 if is_package_installed nodejs; then
159 return
160 fi
161 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
162 install_package nodejs
163
164 # Update NPM to latest version
165 npm install npm -g
166}
167
168# install_python() - Install Python 2.7 and other tools necessary for development.
169function install_python {
Victor Morales7ae05a42017-08-22 14:39:00 -0500170 install_packages python2.7 python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500171}
172
Victor Morales970ec192017-07-31 09:10:11 -0500173# _install_pip() - Install Python Package Manager
174function _install_pip {
175 install_python
Victor Morales4ab71c12017-11-08 07:28:28 -0800176 if ! which pip; then
Victor Morales158c18c2017-08-06 11:23:15 -0500177 curl -sL https://bootstrap.pypa.io/get-pip.py | python
178 fi
Victor Morales970ec192017-07-31 09:10:11 -0500179}
180
Victor Morales88d193e2017-11-16 10:50:28 -0800181# install_python_package() - Install python modules
Victor Morales970ec192017-07-31 09:10:11 -0500182function install_python_package {
Victor Morales4ab71c12017-11-08 07:28:28 -0800183 local python_packages=$@
Victor Morales970ec192017-07-31 09:10:11 -0500184
185 _install_pip
Victor Morales4ab71c12017-11-08 07:28:28 -0800186 pip install $python_packages
Victor Morales970ec192017-07-31 09:10:11 -0500187}
188
Victor Morales88d193e2017-11-16 10:50:28 -0800189# install_python_requirements() - Install a list of python modules defined in requirement.txt file
190function install_python_requirements {
191 local python_project_path=$1
192
193 _install_pip
194 pushd $python_project_path
195 pip install -r requirements.txt
196 popd
197}
198
Victor Morales970ec192017-07-31 09:10:11 -0500199# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500200function install_docker {
201 if is_package_installed docker-ce; then
202 return
203 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -0800204 source /etc/os-release || source /usr/lib/os-release
205 case ${ID,,} in
206 *suse)
207 ;;
208 ubuntu|debian)
Victor Morales071b81c2017-11-07 14:13:07 -0800209 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 -0800210 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
211 add-apt-repository \
212 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
213 $(lsb_release -cs) stable"
214 ;;
215 rhel|centos|fedora)
216 ;;
217 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800218 update_repos
Victor Morales7abf1a82017-11-06 09:20:27 -0800219
Victor Moralesdd074802017-07-26 16:06:35 -0500220 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300221 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500222 service docker restart
223 sleep 10
224}
225
226# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
227function pull_docker_image {
228 install_docker
229 local image=$1
230 local tag=$2
231 docker pull ${image}
232 if [ ${tag} ]; then
233 docker tag ${image} $tag
234 fi
235}
236
237# install_docker_compose() - Download and install docker-engine
238function install_docker_compose {
239 local docker_compose_version=${1:-1.12.0}
240 if [ ! -d /opt/docker ]; then
241 mkdir /opt/docker
242 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
243 chmod +x /opt/docker/docker-compose
244 fi
245}
246
247# _install_ODL() - Download and Install OpenDayLight SDN controller
248function _install_ODL {
249 if [ ! -d /opt/opendaylight/current ]; then
250 mkdir -p /opt/opendaylight/
251 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
252 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
253 mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
254 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
255 fi
256}
257
258# start_ODL() - Start OpenDayLight SDN controller
259function start_ODL {
260 _install_ODL
261 if [ -d /opt/opendaylight ]; then
262 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
263 /opt/opendaylight/current/bin/start
264 sleep 180
265 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
266 fi
267}
268
269# compile_src() - Function that compiles the java source code thru maven
270function compile_src {
271 local src_folder=$1
272 pushd $src_folder
Victor Morales7abf1a82017-11-06 09:20:27 -0800273 local mvn_build='mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none'
274 if [[ "$debug" == "False" ]]; then
275 mvn_build+=" -q"
276 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500277 if [ -f pom.xml ]; then
278 install_maven
Victor Morales7abf1a82017-11-06 09:20:27 -0800279 echo "Compiling $src_folder folder..."
280 eval $mvn_build
Victor Moralesdd074802017-07-26 16:06:35 -0500281 fi
282 popd
283}
284
Victor Morales89ce3212017-06-16 18:32:48 -0500285# build_docker_image() - Build Docker container image from source code
286function build_docker_image {
287 local src_folder=$1
288 local profile=$2
289 install_maven
290 install_docker
291 pushd $src_folder
292
293 # Cleanup external repo
294 sed -i 's|${docker.push.registry}/||g' pom.xml
295 local mvn_docker="mvn clean package docker:build"
296 if [ $profile ]; then
297 mvn_docker+=" -P $profile"
298 fi
299 if [ $http_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500300 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
301 mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
302 fi
303 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
304 mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
305 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500306 fi
307 if [ $https_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500308 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
309 mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
310 fi
311 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
312 mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
313 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500314 fi
315 eval $mvn_docker
316 popd
317}