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 |
| 6 | |
| 7 | # configure_dns() - DNS/GW IP address configuration |
| 8 | function configure_dns { |
| 9 | echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head |
| 10 | resolvconf -u |
| 11 | } |
| 12 | |
| 13 | # create_configuration_files() - Store credentials in files |
| 14 | function create_configuration_files { |
| 15 | mkdir -p /opt/config |
| 16 | echo $nexus_docker_repo > /opt/config/nexus_docker_repo.txt |
| 17 | echo $nexus_username > /opt/config/nexus_username.txt |
| 18 | echo $nexus_password > /opt/config/nexus_password.txt |
| 19 | echo $openstack_username > /opt/config/openstack_username.txt |
| 20 | echo $openstack_tenant_id > /opt/config/tenant_id.txt |
| 21 | echo $dmaap_topic > /opt/config/dmaap_topic.txt |
| 22 | echo $docker_version > /opt/config/docker_version.txt |
| 23 | } |
| 24 | |
| 25 | # pull_openecomp_image() - Pull Docker container image from a Docker Registry Hub |
| 26 | function pull_openecomp_image { |
| 27 | install_docker |
| 28 | local image=$1 |
| 29 | local tag=$2 |
| 30 | docker login -u $nexus_username -p $nexus_password $nexus_docker_repo |
| 31 | docker pull $nexus_docker_repo/openecomp/${image}:$docker_version |
| 32 | if [ ${tag} ]; then |
| 33 | docker tag $nexus_docker_repo/openecomp/${image}:$docker_version $tag |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | # git_timed() - git can sometimes get itself infinitely stuck with transient network |
| 38 | # errors or other issues with the remote end. This wraps git in a |
| 39 | # timeout/retry loop and is intended to watch over non-local git |
| 40 | # processes that might hang. |
| 41 | function git_timed { |
| 42 | local count=0 |
| 43 | local timeout=0 |
| 44 | |
| 45 | install_package git |
| 46 | until timeout -s SIGINT ${timeout} git "$@"; do |
| 47 | # 124 is timeout(1)'s special return code when it reached the |
| 48 | # timeout; otherwise assume fatal failure |
| 49 | if [[ $? -ne 124 ]]; then |
| 50 | exit 1 |
| 51 | fi |
| 52 | |
| 53 | count=$(($count + 1)) |
| 54 | if [ $count -eq 3 ]; then |
| 55 | exit 1 |
| 56 | fi |
| 57 | sleep 5 |
| 58 | done |
| 59 | } |
| 60 | |
| 61 | # clone_repo() - Clone Git repository into specific folder |
| 62 | function clone_repo { |
| 63 | local repo_url=https://git.onap.org/ |
| 64 | local repo=$1 |
| 65 | local dest_folder=$2 |
| 66 | if [ ! -d $2 ]; then |
| 67 | git_timed clone -b $gerrit_branch --single-branch ${repo_url}${repo} $dest_folder |
| 68 | else |
| 69 | pushd $dest_folder |
| 70 | git_timed pull |
| 71 | popd |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | # install_dev_tools() - Install basic dependencies |
| 76 | function install_dev_tools { |
| 77 | install_package apt-transport-https |
| 78 | install_package ca-certificates |
| 79 | install_package curl |
| 80 | } |
| 81 | |
| 82 | # install_bind() - Install bind utils |
| 83 | function install_bind { |
| 84 | install_package bind9 |
| 85 | install_package bind9utils |
| 86 | } |
| 87 | |
| 88 | # configure_bind()- Configure bind utils |
| 89 | function configure_bind { |
| 90 | install_bind |
| 91 | mkdir /etc/bind/zones |
| 92 | |
| 93 | curl -k $nexus_repo/org.openecomp.demo/boot/$artifacts_version/db_simpledemo_openecomp_org -o /etc/bind/zones/db.simpledemo.openecomp.org |
| 94 | curl -k $nexus_repo/org.openecomp.demo/boot/$artifacts_version/named.conf.options -o /etc/bind/named.conf.options |
| 95 | curl -k $nexus_repo/org.openecomp.demo/boot/$artifacts_version/named.conf.local -o /etc/bind/named.conf.local |
| 96 | |
| 97 | modprobe ip_gre |
| 98 | sed -i "s/OPTIONS=.*/OPTIONS=\"-4 -u bind\"/g" /etc/default/bind9 |
| 99 | service bind9 restart |
| 100 | } |
| 101 | |
| 102 | # install_java() - Install java binaries |
| 103 | function install_java { |
| 104 | install_package software-properties-common |
| 105 | add-apt-repository -y ppa:openjdk-r/ppa |
| 106 | install_package openjdk-8-jdk |
| 107 | } |
| 108 | |
| 109 | # install_maven() - Install maven binaries |
| 110 | function install_maven { |
| 111 | if is_package_installed maven; then |
| 112 | return |
| 113 | fi |
| 114 | if ! is_package_installed openjdk-8-jdk; then |
| 115 | install_java |
| 116 | fi |
| 117 | install_package software-properties-common |
| 118 | add-apt-repository -y ppa:andrei-pozolotin/maven3 |
| 119 | install_package maven3 |
| 120 | |
| 121 | # Force Maven3 to use jdk8 |
| 122 | apt-get purge openjdk-7-jdk -y |
| 123 | } |
| 124 | |
| 125 | # configure_docker_proxy() - Configures proxy in Docker from ENV |
| 126 | function configure_docker_proxy { |
| 127 | if [ $http_proxy ]; then |
| 128 | echo "export http_proxy=$http_proxy" >> /etc/default/docker |
| 129 | fi |
| 130 | if [ $https_proxy ]; then |
| 131 | echo "export https_proxy=$https_proxy" >> /etc/default/docker |
| 132 | fi |
| 133 | } |
| 134 | |
| 135 | # build_docker_image() - Build Docker container image from source code |
| 136 | function build_docker_image { |
| 137 | local src_folder=$1 |
| 138 | local profile=$2 |
| 139 | install_maven |
| 140 | install_docker |
| 141 | pushd $src_folder |
| 142 | |
| 143 | # Cleanup external repo |
| 144 | sed -i 's|${docker.push.registry}/||g' pom.xml |
| 145 | local mvn_docker="mvn clean package docker:build" |
| 146 | if [ $profile ]; then |
| 147 | mvn_docker+=" -P $profile" |
| 148 | fi |
| 149 | if [ $http_proxy ]; then |
| 150 | mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy" |
| 151 | mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy" |
| 152 | fi |
| 153 | if [ $https_proxy ]; then |
| 154 | mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy" |
| 155 | mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy" |
| 156 | fi |
| 157 | eval $mvn_docker |
| 158 | popd |
| 159 | } |
| 160 | |
| 161 | # compile_src() - Function that compiles the java source code thru maven |
| 162 | function compile_src { |
| 163 | local src_folder=$1 |
| 164 | pushd $src_folder |
| 165 | if [ -f pom.xml ]; then |
| 166 | install_maven |
| 167 | mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dadditionalparam=-Xdoclint:none |
| 168 | fi |
| 169 | popd |
| 170 | } |
| 171 | |
| 172 | # install_docker() - Download and install docker-engine |
| 173 | function install_docker { |
| 174 | if is_package_installed docker-ce; then |
| 175 | return |
| 176 | fi |
| 177 | install_package software-properties-common |
| 178 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
| 179 | add-apt-repository \ |
| 180 | "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ |
| 181 | $(lsb_release -cs) \ |
| 182 | stable" |
| 183 | install_package docker-ce |
| 184 | configure_docker_proxy |
| 185 | service docker restart |
| 186 | } |
| 187 | |
| 188 | # install_docker_compose() - Download and install docker-engine |
| 189 | function install_docker_compose { |
| 190 | local docker_compose_version=${1:-1.12.0} |
| 191 | if [ ! -d /opt/docker ]; then |
| 192 | mkdir /opt/docker |
| 193 | curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose |
| 194 | chmod +x /opt/docker/docker-compose |
| 195 | fi |
| 196 | } |
| 197 | |
| 198 | # configure_service() - Download and configure a specific service in upstart |
| 199 | function configure_service { |
| 200 | local service_script=$1 |
| 201 | curl -k $nexus_repo/org.openecomp.demo/boot/$artifacts_version/$service_script -o /etc/init.d/$service_script |
| 202 | chmod +x /etc/init.d/$service_script |
| 203 | update-rc.d $service_script defaults |
| 204 | } |