Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -o xtrace |
| 4 | |
| 5 | source /var/onap/commons |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 6 | source /var/onap/_composed_functions |
| 7 | source /var/onap/_onap_functions |
| 8 | |
Victor Morales | 9651265 | 2017-08-16 13:44:28 -0500 | [diff] [blame] | 9 | export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1) |
| 10 | export IP_ADDRESS=$(ifconfig eth0 | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2) |
| 11 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 12 | mvn_conf_file=/root/.m2/settings.xml |
| 13 | git_src_folder=/opt |
| 14 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 15 | # configure_dns() - DNS/GW IP address configuration |
| 16 | function configure_dns { |
| 17 | echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head |
| 18 | resolvconf -u |
| 19 | } |
| 20 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 21 | # _git_timed() - git can sometimes get itself infinitely stuck with transient network |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 22 | # 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 Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 25 | function _git_timed { |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 26 | 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 |
| 46 | function clone_repo { |
| 47 | local repo_url=https://git.onap.org/ |
| 48 | local repo=$1 |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 49 | local dest_folder=${2:-$git_src_folder/$repo} |
| 50 | if [ ! -d $dest_folder ]; then |
Victor Morales | 9651265 | 2017-08-16 13:44:28 -0500 | [diff] [blame] | 51 | _git_timed clone ${repo_url}${repo} $dest_folder |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 52 | else |
| 53 | pushd $dest_folder |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 54 | _git_timed pull |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 55 | popd |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | # install_dev_tools() - Install basic dependencies |
| 60 | function install_dev_tools { |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 61 | install_packages apt-transport-https ca-certificates curl |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 64 | # _install_bind() - Install bind utils |
| 65 | function _install_bind { |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 66 | install_packages bind9 bind9utils |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 69 | # install_java() - Install java binaries |
| 70 | function install_java { |
Victor Morales | ece790c | 2017-08-08 11:11:36 -0500 | [diff] [blame] | 71 | if is_package_installed openjdk-8-jdk; then |
| 72 | return |
| 73 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 74 | install_package software-properties-common |
| 75 | add-apt-repository -y ppa:openjdk-r/ppa |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 76 | |
| 77 | # Remove Java 7 |
| 78 | uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless |
| 79 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 80 | install_package openjdk-8-jdk |
Victor Morales | 455bece | 2017-07-31 18:40:39 -0500 | [diff] [blame] | 81 | # 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 Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | # install_maven() - Install maven binaries |
| 86 | function install_maven { |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 87 | if is_package_installed maven3; then |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 88 | return |
| 89 | fi |
Victor Morales | ece790c | 2017-08-08 11:11:36 -0500 | [diff] [blame] | 90 | install_java |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 91 | install_package software-properties-common |
| 92 | add-apt-repository -y ppa:andrei-pozolotin/maven3 |
| 93 | install_package maven3 |
| 94 | |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 95 | # Remove Java 7 |
| 96 | uninstall_package openjdk-7-jdk |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 97 | |
| 98 | _configure_maven |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Idan Amit | e17b5ec | 2017-08-16 14:17:59 +0300 | [diff] [blame] | 101 | # _configure_docker_settings() - Configures Docker settings |
| 102 | function _configure_docker_settings { |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 103 | local docker_conf_backup=/tmp/docker.backup |
| 104 | local docker_conf=/etc/default/docker |
| 105 | local chameleonsocks_filename=chameleonsocks.sh |
| 106 | |
| 107 | cp $docker_conf $docker_conf_backup |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 108 | if [ $http_proxy ]; then |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 109 | echo "export http_proxy=$http_proxy" >> $docker_conf |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 110 | fi |
| 111 | if [ $https_proxy ]; then |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 112 | echo "export https_proxy=$https_proxy" >> $docker_conf |
Victor Morales | 5ce0421 | 2017-09-24 09:03:32 -0700 | [diff] [blame] | 113 | #If you have a socks proxy, then use that to connect to the nexus repo |
Kiran | f2824c2 | 2017-09-22 18:05:29 -0700 | [diff] [blame] | 114 | #via a redsocks container |
| 115 | if [ $socks_proxy ]; then |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 116 | wget https://raw.githubusercontent.com/crops/chameleonsocks/master/$chameleonsocks_filename |
| 117 | chmod 755 $chameleonsocks_filename |
Victor Morales | 5ce0421 | 2017-09-24 09:03:32 -0700 | [diff] [blame] | 118 | socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//") |
| 119 | port=$(echo $socks_proxy | sed -e "s/^.*://") |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 120 | PROXY=$socks PORT=$port ./$chameleonsocks_filename --install |
| 121 | rm $chameleonsocks_filename |
| 122 | cp $docker_conf_backup $docker_conf |
Kiran | f2824c2 | 2017-09-22 18:05:29 -0700 | [diff] [blame] | 123 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 124 | fi |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 125 | rm $docker_conf_backup |
Idan Amit | e17b5ec | 2017-08-16 14:17:59 +0300 | [diff] [blame] | 126 | |
Victor Morales | cf26999 | 2017-10-18 09:29:55 -0700 | [diff] [blame] | 127 | echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> $docker_conf |
Idan Amit | e17b5ec | 2017-08-16 14:17:59 +0300 | [diff] [blame] | 128 | usermod -a -G docker vagrant |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 131 | # install_nodejs() - Download and install NodeJS |
| 132 | function install_nodejs { |
| 133 | if is_package_installed nodejs; then |
| 134 | return |
| 135 | fi |
| 136 | curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - |
| 137 | install_package nodejs |
| 138 | |
| 139 | # Update NPM to latest version |
| 140 | npm install npm -g |
| 141 | } |
| 142 | |
| 143 | # install_python() - Install Python 2.7 and other tools necessary for development. |
| 144 | function install_python { |
Victor Morales | 7ae05a4 | 2017-08-22 14:39:00 -0500 | [diff] [blame] | 145 | install_packages python2.7 python-dev |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Victor Morales | 970ec19 | 2017-07-31 09:10:11 -0500 | [diff] [blame] | 148 | # _install_pip() - Install Python Package Manager |
| 149 | function _install_pip { |
| 150 | install_python |
Victor Morales | 158c18c | 2017-08-06 11:23:15 -0500 | [diff] [blame] | 151 | if [ ! -f /usr/local/bin/pip ]; then |
| 152 | curl -sL https://bootstrap.pypa.io/get-pip.py | python |
| 153 | fi |
Victor Morales | 970ec19 | 2017-07-31 09:10:11 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | # install_python_package() - Install a python module |
| 157 | function install_python_package { |
| 158 | local python_package=$1 |
| 159 | |
| 160 | _install_pip |
| 161 | pip install $python_package |
| 162 | } |
| 163 | |
| 164 | # install_docker() - Download and install docker-engine |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 165 | function install_docker { |
| 166 | if is_package_installed docker-ce; then |
| 167 | return |
| 168 | fi |
| 169 | install_package software-properties-common |
| 170 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
| 171 | add-apt-repository \ |
| 172 | "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ |
| 173 | $(lsb_release -cs) \ |
| 174 | stable" |
| 175 | install_package docker-ce |
Idan Amit | e17b5ec | 2017-08-16 14:17:59 +0300 | [diff] [blame] | 176 | _configure_docker_settings |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 177 | service docker restart |
| 178 | sleep 10 |
| 179 | } |
| 180 | |
| 181 | # pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub |
| 182 | function pull_docker_image { |
| 183 | install_docker |
| 184 | local image=$1 |
| 185 | local tag=$2 |
| 186 | docker pull ${image} |
| 187 | if [ ${tag} ]; then |
| 188 | docker tag ${image} $tag |
| 189 | fi |
| 190 | } |
| 191 | |
| 192 | # install_docker_compose() - Download and install docker-engine |
| 193 | function install_docker_compose { |
| 194 | local docker_compose_version=${1:-1.12.0} |
| 195 | if [ ! -d /opt/docker ]; then |
| 196 | mkdir /opt/docker |
| 197 | curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose |
| 198 | chmod +x /opt/docker/docker-compose |
| 199 | fi |
| 200 | } |
| 201 | |
| 202 | # _install_ODL() - Download and Install OpenDayLight SDN controller |
| 203 | function _install_ODL { |
| 204 | if [ ! -d /opt/opendaylight/current ]; then |
| 205 | mkdir -p /opt/opendaylight/ |
| 206 | wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/ |
| 207 | tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/ |
| 208 | mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current |
| 209 | rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz" |
| 210 | fi |
| 211 | } |
| 212 | |
| 213 | # start_ODL() - Start OpenDayLight SDN controller |
| 214 | function start_ODL { |
| 215 | _install_ODL |
| 216 | if [ -d /opt/opendaylight ]; then |
| 217 | export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre |
| 218 | /opt/opendaylight/current/bin/start |
| 219 | sleep 180 |
| 220 | /opt/opendaylight/current/bin/client feature:install odl-dlux-all |
| 221 | fi |
| 222 | } |
| 223 | |
| 224 | # compile_src() - Function that compiles the java source code thru maven |
| 225 | function compile_src { |
| 226 | local src_folder=$1 |
| 227 | pushd $src_folder |
| 228 | if [ -f pom.xml ]; then |
| 229 | install_maven |
Victor Morales | 970ec19 | 2017-07-31 09:10:11 -0500 | [diff] [blame] | 230 | mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 231 | fi |
| 232 | popd |
| 233 | } |
| 234 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 235 | # build_docker_image() - Build Docker container image from source code |
| 236 | function build_docker_image { |
| 237 | local src_folder=$1 |
| 238 | local profile=$2 |
| 239 | install_maven |
| 240 | install_docker |
| 241 | pushd $src_folder |
| 242 | |
| 243 | # Cleanup external repo |
| 244 | sed -i 's|${docker.push.registry}/||g' pom.xml |
| 245 | local mvn_docker="mvn clean package docker:build" |
| 246 | if [ $profile ]; then |
| 247 | mvn_docker+=" -P $profile" |
| 248 | fi |
| 249 | if [ $http_proxy ]; then |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 250 | if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then |
| 251 | mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy" |
| 252 | fi |
| 253 | if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then |
| 254 | mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy" |
| 255 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 256 | fi |
| 257 | if [ $https_proxy ]; then |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 258 | if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then |
| 259 | mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy" |
| 260 | fi |
| 261 | if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then |
| 262 | mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy" |
| 263 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 264 | fi |
| 265 | eval $mvn_docker |
| 266 | popd |
| 267 | } |