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