Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame^] | 1 | #! /usr/bin/env bash |
| 2 | |
| 3 | # COPYRIGHT NOTICE STARTS HERE |
| 4 | # |
| 5 | # Copyright 2018 © Samsung Electronics Co., Ltd. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # COPYRIGHT NOTICE ENDS HERE |
| 20 | |
| 21 | |
| 22 | ### This script prepares Nexus repositories data blobs for ONAP |
| 23 | |
| 24 | # Mandatory variables need to be set in configuration file: |
| 25 | # NXS_SRC_DOCKER_IMG_DIR - resource directory of docker images |
| 26 | # NXS_SRC_NPM_DIR - resource directory of npm packages |
| 27 | # NXS_DOCKER_IMG_LIST - list of docker images to be pushed to Nexus repository |
| 28 | # NXS_DOCKER_WO_LIST - list of docker images which uses default repository |
| 29 | # NXS_NPM_LIST - list of npm packages to be published to Nexus repository |
| 30 | # NEXUS_DATA_TAR - target tarball of Nexus data path/name |
| 31 | # NEXUS_DATA_DIR - directory used for the Nexus blob build |
| 32 | # NEXUS_IMAGE - Sonatype/Nexus3 docker image which will be used for data blob creation |
| 33 | |
| 34 | # Fail fast settings |
| 35 | set -e |
| 36 | |
| 37 | # Nexus repository location |
| 38 | NEXUS_DOMAIN="nexus" |
| 39 | NPM_REGISTRY="http://${NEXUS_DOMAIN}:8081/repository/npm-private/" |
| 40 | DOCKER_REGISTRY="${NEXUS_DOMAIN}:8082" |
| 41 | |
| 42 | # Nexus repository credentials |
| 43 | NEXUS_USERNAME=admin |
| 44 | NEXUS_PASSWORD=admin123 |
| 45 | NEXUS_EMAIL=admin@example.org |
| 46 | |
| 47 | # Setup simulated domain names to be able to push all in private Nexus repository |
| 48 | SIMUL_HOSTS="docker.elastic.co gcr.io hub.docker.com nexus3.onap.org nexus.onap.org registry.hub.docker.com ${NEXUS_DOMAIN}" |
| 49 | |
| 50 | # Nexus repository configuration setup |
| 51 | NEXUS_CONFIG_GROOVY='import org.sonatype.nexus.security.realm.RealmManager |
| 52 | import org.sonatype.nexus.repository.attributes.AttributesFacet |
| 53 | import org.sonatype.nexus.security.user.UserManager |
| 54 | import org.sonatype.nexus.repository.manager.RepositoryManager |
| 55 | import org.sonatype.nexus.security.user.UserNotFoundException |
| 56 | /* Use the container to look up some services. */ |
| 57 | realmManager = container.lookup(RealmManager.class) |
| 58 | userManager = container.lookup(UserManager.class, "default") //default user manager |
| 59 | repositoryManager = container.lookup(RepositoryManager.class) |
| 60 | /* Managers are used when scripting api cannot. Note that scripting api can only create mostly, and that creation methods return objects of created entities. */ |
| 61 | /* Perform cleanup by removing all repos and users. Realms do not need to be re-disabled, admin and anonymous user will not be removed. */ |
| 62 | userManager.listUserIds().each({ id -> |
| 63 | if (id != "anonymous" && id != "admin") |
| 64 | userManager.deleteUser(id) |
| 65 | }) |
| 66 | repositoryManager.browse().each { |
| 67 | repositoryManager.delete(it.getName()) |
| 68 | } |
| 69 | /* Add bearer token realms at the end of realm lists... */ |
| 70 | realmManager.enableRealm("NpmToken") |
| 71 | realmManager.enableRealm("DockerToken") |
| 72 | /* Create the docker user. */ |
| 73 | security.addUser("docker", "docker", "docker", "docker@example.com", true, "docker", ["nx-anonymous"]) |
| 74 | /* Create docker and npm repositories. Their default configuration should be compliant with our requirements, except the docker registry creation. */ |
| 75 | repository.createNpmHosted("npm-private") |
| 76 | def r = repository.createDockerHosted("onap", 8082, 0) |
| 77 | /* force basic authentication true by default, must set to false for docker repo. */ |
| 78 | conf=r.getConfiguration() |
| 79 | conf.attributes("docker").set("forceBasicAuth", false) |
| 80 | repositoryManager.update(conf)' |
| 81 | |
| 82 | usage () { |
| 83 | echo " This script is preparing Nexus data blob from docker images and npm packages" |
| 84 | echo " Usage:" |
| 85 | echo " ./$(basename $0) <config_file> [<target>]" |
| 86 | echo " " |
| 87 | echo " config_file is a file with defined variables, which are mandatory for this script" |
| 88 | echo " target is optional parameter where you can specify full path/name of resulted package" |
| 89 | echo " which replaces the value specified in configuration file" |
| 90 | echo " " |
| 91 | echo " Example: ./$(basename $0) ./package.conf /root/nexus_data.tar" |
| 92 | echo " " |
| 93 | echo " Parameters need to be defined in configuration file:" |
| 94 | echo " " |
| 95 | echo " NXS_SRC_DOCKER_IMG_DIR - directory of resource docker images" |
| 96 | echo " NXS_SRC_NPM_DIR - directory of resource npm packages" |
| 97 | echo " NXS_DOCKER_IMG_LIST - list of docker images to be pushed to Nexus repository" |
| 98 | echo " NXS_DOCKER_WO_LIST - list of docker images which uses default repository" |
| 99 | echo " NXS_NPM_LIST - list of npm packages to be published to Nexus repository" |
| 100 | echo " NEXUS_DATA_TAR - target tarball of Nexus data path/name" |
| 101 | echo " NEXUS_DATA_DIR - directory used for the Nexus blob build" |
| 102 | echo " NEXUS_IMAGE - Sonatype/Nexus3 docker image which will be used for data blob creation" |
| 103 | exit 1 |
| 104 | } |
| 105 | |
| 106 | |
| 107 | ################################# |
| 108 | # Prepare the local environment # |
| 109 | ################################# |
| 110 | |
| 111 | # Load the config file |
| 112 | if [ "${1}" == "-h" ] || [ -z "${1}" ]; then |
| 113 | usage |
| 114 | elif [ -f ${1} ]; then |
| 115 | . ${1} |
| 116 | else |
| 117 | echo "Missing mandatory configuration file!" |
| 118 | usage |
| 119 | exit 1 |
| 120 | fi |
| 121 | |
| 122 | if [ -n "${2}" ]; then |
| 123 | NEXUS_DATA_TAR="${2}" |
| 124 | fi |
| 125 | |
| 126 | for VAR in NXS_SRC_DOCKER_IMG_DIR NXS_SRC_NPM_DIR NXS_DOCKER_IMG_LIST NXS_DOCKER_WO_LIST NXS_NPM_LIST NEXUS_DATA_TAR NEXUS_DATA_DIR NEXUS_IMAGE; do |
| 127 | if [ -n "${!VAR}" ] ; then |
| 128 | echo "${VAR} is set to ${!VAR}" |
| 129 | else |
| 130 | echo "${VAR} is not set and it is mandatory" |
| 131 | FAIL="1" |
| 132 | fi |
| 133 | done |
| 134 | |
| 135 | if [ "${FAIL}" == "1" ]; then |
| 136 | echo "One or more mandatory variables are not set" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | # Check the dependencies in the beginning |
| 141 | |
| 142 | # Install Node.js |
| 143 | if yum list installed "nodejs" >/dev/null 2>&1; then |
| 144 | echo "Node.js is already installed" |
| 145 | else |
| 146 | yum install -y --setopt=skip_missing_names_on_install=False epel-release |
| 147 | curl --silent --location https://rpm.nodesource.com/setup_6.x | bash - |
| 148 | yum install -y --setopt=skip_missing_names_on_install=False nodejs |
| 149 | fi |
| 150 | |
| 151 | # Install curl if necessary |
| 152 | if yum list installed "curl" >/dev/null 2>&1; then |
| 153 | echo "curl is already installed" |
| 154 | else |
| 155 | yum install -y --setopt=skip_missing_names_on_install=False curl |
| 156 | fi |
| 157 | |
| 158 | # Install expect if necessary |
| 159 | if yum list installed "expect" >/dev/null 2>&1; then |
| 160 | echo "expect is already installed" |
| 161 | else |
| 162 | yum install -y --setopt=skip_missing_names_on_install=False expect |
| 163 | fi |
| 164 | |
| 165 | # Install Docker (docker-ce in version 17.03 for RHEL) from online repositories if no version installed |
| 166 | if yum list installed "docker-ce" >/dev/null 2>&1 || which docker>/dev/null 2>&1; then |
| 167 | echo "Docker is already installed" |
| 168 | else |
| 169 | curl https://releases.rancher.com/install-docker/17.03.sh | sh |
| 170 | fi |
| 171 | |
| 172 | # Prepare the Nexus configuration |
| 173 | NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc '{"name":"configure", "type":"groovy", "content":.}') |
| 174 | |
| 175 | # Add simulated domain names to /etc/hosts |
| 176 | cp /etc/hosts /etc/$(date +"%Y-%m-%d_%H-%M-%S")_hosts.bk |
| 177 | for DNS in ${SIMUL_HOSTS}; do |
| 178 | echo "127.0.0.1 ${DNS}" >> /etc/hosts |
| 179 | done |
| 180 | |
| 181 | # Backup the current docker registry settings |
| 182 | if [ -f /root/.docker/config.json ]; then |
| 183 | mv /root/.docker/config.json /root/.docker/$(date +"%Y-%m-%d_%H-%M-%S")config.json.bk |
| 184 | fi |
| 185 | |
| 186 | ################################# |
| 187 | # Docker repository preparation # |
| 188 | ################################# |
| 189 | |
| 190 | # Load all necessary images |
| 191 | for ARCHIVE in $(sed $'s/\r// ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do |
| 192 | docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE} |
| 193 | done |
| 194 | |
| 195 | for ARCHIVE in $(sed $'s/\r// ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do |
| 196 | docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE} |
| 197 | done |
| 198 | |
| 199 | # Tag docker images from default repository to simulated repository to be able to upload it to our private registry |
| 200 | for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do |
| 201 | docker tag ${IMAGE} ${DOCKER_REGISTRY}/${IMAGE} |
| 202 | done |
| 203 | |
| 204 | |
| 205 | ################################ |
| 206 | # Nexus repository preparation # |
| 207 | ################################ |
| 208 | |
| 209 | # Load predefined Nexus image |
| 210 | docker load -i ${NEXUS_IMAGE} |
| 211 | |
| 212 | # Prepare nexus-data directory |
| 213 | if [ -d ${NEXUS_DATA_DIR} ]; then |
| 214 | if [ "$(docker ps -q -f name=nexus)" ]; then |
| 215 | docker rm -f $(docker ps -aq -f name=nexus) |
| 216 | fi |
| 217 | cd ${NEXUS_DATA_DIR}/.. |
| 218 | mv ${NEXUS_DATA_DIR} $(date +"%Y-%m-%d_%H-%M-%S")_$(basename ${NEXUS_DATA_DIR})_bk |
| 219 | fi |
| 220 | |
| 221 | mkdir -p ${NEXUS_DATA_DIR} |
| 222 | chown 200:200 ${NEXUS_DATA_DIR} |
| 223 | chmod 777 ${NEXUS_DATA_DIR} |
| 224 | |
| 225 | # Save Nexus version to prevent/catch data incompatibility |
| 226 | docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver |
| 227 | |
| 228 | # Start the Nexus |
| 229 | NEXUS_CONT_ID=$(docker run -d --rm -v ${NEXUS_DATA_DIR}:/nexus-data:rw --name nexus -p 8081:8081 -p 8082:8082 -p 80:8082 -p 10001:8082 sonatype/nexus3) |
| 230 | echo "Waiting for Nexus to fully start" |
| 231 | until curl -su admin:admin123 http://${NEXUS_DOMAIN}:8081/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do |
| 232 | printf "." |
| 233 | sleep 3 |
| 234 | done |
| 235 | echo -e "\nNexus started" |
| 236 | |
| 237 | # Configure the nexus repository |
| 238 | curl -X POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script |
| 239 | curl -X POST --header "Content-Type: text/plain" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script/configure/run |
| 240 | |
| 241 | ########################### |
| 242 | # Populate NPM repository # |
| 243 | ########################### |
| 244 | |
| 245 | # Configure NPM registry to our Nexus repository |
| 246 | npm config set registry ${NPM_REGISTRY} |
| 247 | |
| 248 | # Login to NPM registry |
| 249 | /usr/bin/expect <<EOF |
| 250 | spawn npm login |
| 251 | expect "Username:" |
| 252 | send "${NEXUS_USERNAME}\n" |
| 253 | expect "Password:" |
| 254 | send "${NEXUS_PASSWORD}\n" |
| 255 | expect Email: |
| 256 | send "${NEXUS_EMAIL}\n" |
| 257 | expect eof |
| 258 | EOF |
| 259 | |
| 260 | # Patch problematic package |
| 261 | cd ${NXS_SRC_NPM_DIR} |
| 262 | tar xvzf tsscmp-1.0.5.tgz |
| 263 | rm -f tsscmp-1.0.5.tgz |
| 264 | sed -i "s|https://registry.npmjs.org|http://${NEXUS_DOMAIN}:8081|g" package/package.json |
| 265 | sed -i "s|https://nexus.onap-me.novalocal|http://${NEXUS_DOMAIN}:8081|g" package/package.json |
| 266 | tar -zcvf tsscmp-1.0.5.tgz package |
| 267 | rm -rf package |
| 268 | |
| 269 | # Push NPM packages to Nexus repository |
| 270 | for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST} | awk '{ print $1 }'); do |
| 271 | npm publish --access public ${ARCHIVE} |
| 272 | done |
| 273 | |
| 274 | ############################## |
| 275 | # Populate Docker repository # |
| 276 | ############################## |
| 277 | |
| 278 | for REGISTRY in $(sed 's/\/.*//' ${NXS_DOCKER_IMG_LIST} | uniq) ${NEXUS_DOMAIN}:8082; do |
| 279 | docker login -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" ${REGISTRY} > /dev/null |
| 280 | done |
| 281 | |
| 282 | for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do |
| 283 | docker push ${DOCKER_REGISTRY}/${IMAGE} |
| 284 | done |
| 285 | |
| 286 | for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do |
| 287 | docker push ${IMAGE} |
| 288 | done |
| 289 | |
| 290 | ############################## |
| 291 | # Stop the Nexus and cleanup # |
| 292 | ############################## |
| 293 | |
| 294 | # Stop the Nexus |
| 295 | docker stop ${NEXUS_CONT_ID} |
| 296 | |
| 297 | # Create the nexus-data package |
| 298 | cd ${NEXUS_DATA_DIR}/.. |
| 299 | echo "Packing the ${NEXUS_DATA_DIR} dir" |
| 300 | until tar -cf ${NEXUS_DATA_TAR} $(basename ${NEXUS_DATA_DIR}); do |
| 301 | printf "." |
| 302 | sleep 5 |
| 303 | done |
| 304 | echo "${NEXUS_DATA_TAR} has been created" |
| 305 | |
| 306 | # Return the previous version of /etc/hosts back to its place |
| 307 | mv -f $(ls -tr /etc/*hosts.bk | tail -1) /etc/hosts |
| 308 | |
| 309 | exit 0 |