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 | |
Tomáš Levora | d204853 | 2019-01-16 16:14:43 +0100 | [diff] [blame] | 142 | # Install jq |
| 143 | if yum list installed "jq" >/dev/null 2>&1; then |
| 144 | echo "jq is already installed" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 145 | else |
Tomáš Levora | d204853 | 2019-01-16 16:14:43 +0100 | [diff] [blame] | 146 | yum install -y --setopt=skip_missing_names_on_install=False http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/j/jq-1.5-1.el7.x86_64.rpm |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 147 | fi |
| 148 | |
| 149 | # Install curl if necessary |
| 150 | if yum list installed "curl" >/dev/null 2>&1; then |
| 151 | echo "curl is already installed" |
| 152 | else |
| 153 | yum install -y --setopt=skip_missing_names_on_install=False curl |
| 154 | fi |
| 155 | |
| 156 | # Install expect if necessary |
| 157 | if yum list installed "expect" >/dev/null 2>&1; then |
| 158 | echo "expect is already installed" |
| 159 | else |
| 160 | yum install -y --setopt=skip_missing_names_on_install=False expect |
| 161 | fi |
| 162 | |
| 163 | # Install Docker (docker-ce in version 17.03 for RHEL) from online repositories if no version installed |
| 164 | if yum list installed "docker-ce" >/dev/null 2>&1 || which docker>/dev/null 2>&1; then |
| 165 | echo "Docker is already installed" |
| 166 | else |
| 167 | curl https://releases.rancher.com/install-docker/17.03.sh | sh |
| 168 | fi |
| 169 | |
| 170 | # Prepare the Nexus configuration |
| 171 | NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc '{"name":"configure", "type":"groovy", "content":.}') |
| 172 | |
| 173 | # Add simulated domain names to /etc/hosts |
| 174 | cp /etc/hosts /etc/$(date +"%Y-%m-%d_%H-%M-%S")_hosts.bk |
| 175 | for DNS in ${SIMUL_HOSTS}; do |
| 176 | echo "127.0.0.1 ${DNS}" >> /etc/hosts |
| 177 | done |
| 178 | |
| 179 | # Backup the current docker registry settings |
| 180 | if [ -f /root/.docker/config.json ]; then |
| 181 | mv /root/.docker/config.json /root/.docker/$(date +"%Y-%m-%d_%H-%M-%S")config.json.bk |
| 182 | fi |
| 183 | |
| 184 | ################################# |
| 185 | # Docker repository preparation # |
| 186 | ################################# |
| 187 | |
| 188 | # Load all necessary images |
| 189 | for ARCHIVE in $(sed $'s/\r// ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do |
| 190 | docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE} |
| 191 | done |
| 192 | |
| 193 | for ARCHIVE in $(sed $'s/\r// ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do |
| 194 | docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE} |
| 195 | done |
| 196 | |
| 197 | # Tag docker images from default repository to simulated repository to be able to upload it to our private registry |
| 198 | for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do |
| 199 | docker tag ${IMAGE} ${DOCKER_REGISTRY}/${IMAGE} |
| 200 | done |
| 201 | |
| 202 | |
| 203 | ################################ |
| 204 | # Nexus repository preparation # |
| 205 | ################################ |
| 206 | |
| 207 | # Load predefined Nexus image |
| 208 | docker load -i ${NEXUS_IMAGE} |
| 209 | |
| 210 | # Prepare nexus-data directory |
| 211 | if [ -d ${NEXUS_DATA_DIR} ]; then |
| 212 | if [ "$(docker ps -q -f name=nexus)" ]; then |
| 213 | docker rm -f $(docker ps -aq -f name=nexus) |
| 214 | fi |
| 215 | cd ${NEXUS_DATA_DIR}/.. |
| 216 | mv ${NEXUS_DATA_DIR} $(date +"%Y-%m-%d_%H-%M-%S")_$(basename ${NEXUS_DATA_DIR})_bk |
| 217 | fi |
| 218 | |
| 219 | mkdir -p ${NEXUS_DATA_DIR} |
| 220 | chown 200:200 ${NEXUS_DATA_DIR} |
| 221 | chmod 777 ${NEXUS_DATA_DIR} |
| 222 | |
| 223 | # Save Nexus version to prevent/catch data incompatibility |
| 224 | docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver |
| 225 | |
| 226 | # Start the Nexus |
| 227 | 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) |
| 228 | echo "Waiting for Nexus to fully start" |
| 229 | until curl -su admin:admin123 http://${NEXUS_DOMAIN}:8081/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do |
| 230 | printf "." |
| 231 | sleep 3 |
| 232 | done |
| 233 | echo -e "\nNexus started" |
| 234 | |
| 235 | # Configure the nexus repository |
| 236 | curl -X POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script |
| 237 | curl -X POST --header "Content-Type: text/plain" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script/configure/run |
| 238 | |
| 239 | ########################### |
| 240 | # Populate NPM repository # |
| 241 | ########################### |
| 242 | |
| 243 | # Configure NPM registry to our Nexus repository |
| 244 | npm config set registry ${NPM_REGISTRY} |
| 245 | |
| 246 | # Login to NPM registry |
| 247 | /usr/bin/expect <<EOF |
| 248 | spawn npm login |
| 249 | expect "Username:" |
| 250 | send "${NEXUS_USERNAME}\n" |
| 251 | expect "Password:" |
| 252 | send "${NEXUS_PASSWORD}\n" |
| 253 | expect Email: |
| 254 | send "${NEXUS_EMAIL}\n" |
| 255 | expect eof |
| 256 | EOF |
| 257 | |
| 258 | # Patch problematic package |
| 259 | cd ${NXS_SRC_NPM_DIR} |
| 260 | tar xvzf tsscmp-1.0.5.tgz |
| 261 | rm -f tsscmp-1.0.5.tgz |
| 262 | sed -i "s|https://registry.npmjs.org|http://${NEXUS_DOMAIN}:8081|g" package/package.json |
| 263 | sed -i "s|https://nexus.onap-me.novalocal|http://${NEXUS_DOMAIN}:8081|g" package/package.json |
| 264 | tar -zcvf tsscmp-1.0.5.tgz package |
| 265 | rm -rf package |
| 266 | |
| 267 | # Push NPM packages to Nexus repository |
| 268 | for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST} | awk '{ print $1 }'); do |
| 269 | npm publish --access public ${ARCHIVE} |
| 270 | done |
| 271 | |
| 272 | ############################## |
| 273 | # Populate Docker repository # |
| 274 | ############################## |
| 275 | |
| 276 | for REGISTRY in $(sed 's/\/.*//' ${NXS_DOCKER_IMG_LIST} | uniq) ${NEXUS_DOMAIN}:8082; do |
| 277 | docker login -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" ${REGISTRY} > /dev/null |
| 278 | done |
| 279 | |
| 280 | for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do |
| 281 | docker push ${DOCKER_REGISTRY}/${IMAGE} |
| 282 | done |
| 283 | |
| 284 | for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do |
| 285 | docker push ${IMAGE} |
| 286 | done |
| 287 | |
| 288 | ############################## |
| 289 | # Stop the Nexus and cleanup # |
| 290 | ############################## |
| 291 | |
| 292 | # Stop the Nexus |
| 293 | docker stop ${NEXUS_CONT_ID} |
| 294 | |
| 295 | # Create the nexus-data package |
| 296 | cd ${NEXUS_DATA_DIR}/.. |
| 297 | echo "Packing the ${NEXUS_DATA_DIR} dir" |
| 298 | until tar -cf ${NEXUS_DATA_TAR} $(basename ${NEXUS_DATA_DIR}); do |
| 299 | printf "." |
| 300 | sleep 5 |
| 301 | done |
| 302 | echo "${NEXUS_DATA_TAR} has been created" |
| 303 | |
| 304 | # Return the previous version of /etc/hosts back to its place |
| 305 | mv -f $(ls -tr /etc/*hosts.bk | tail -1) /etc/hosts |
| 306 | |
| 307 | exit 0 |