Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 1 | #! /usr/bin/env bash |
| 2 | |
| 3 | # COPYRIGHT NOTICE STARTS HERE |
| 4 | # |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 5 | # Copyright 2018-2019 © Samsung Electronics Co., Ltd. |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 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 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 21 | ### This script prepares Nexus repositories data blobs for ONAP |
| 22 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 23 | ## The script requires following dependencies are installed: nodejs, jq, docker |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 24 | ## All required resources are expected in the upper directory created during |
| 25 | ## download procedure as DATA_DIR or in the directory given as --input-directory |
| 26 | ## All lists used must be in project data_lists directory or in the directory given |
| 27 | ## as --resource-list-directory |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 28 | |
| 29 | # Fail fast settings |
| 30 | set -e |
| 31 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 32 | TIMESTAMP="date +'%Y-%m-%d_%H-%M-%S'" |
| 33 | SCRIPT_LOG="/tmp/$(basename $0)_$(eval ${TIMESTAMP}).log" |
| 34 | |
| 35 | # Log everything |
| 36 | exec &> >(tee -a "${SCRIPT_LOG}") |
| 37 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 38 | # Nexus repository location |
| 39 | NEXUS_DOMAIN="nexus" |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 40 | NEXUS_PORT="8081" |
| 41 | NEXUS_DOCKER_PORT="8082" |
| 42 | NPM_REGISTRY="http://${NEXUS_DOMAIN}:${NEXUS_PORT}/repository/npm-private/" |
| 43 | PYPI_REGISTRY="http://${NEXUS_DOMAIN}:${NEXUS_PORT}/repository/pypi-private/" |
| 44 | DOCKER_REGISTRY="${NEXUS_DOMAIN}:${NEXUS_DOCKER_PORT}" |
| 45 | DEFAULT_REGISTRY="docker.io" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 46 | |
| 47 | # Nexus repository credentials |
| 48 | NEXUS_USERNAME=admin |
| 49 | NEXUS_PASSWORD=admin123 |
| 50 | NEXUS_EMAIL=admin@example.org |
| 51 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 52 | # Setting paths |
| 53 | LOCAL_PATH="$(readlink -f $(dirname ${0}))" |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 54 | |
| 55 | #Defaults |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 56 | DATA_DIR="$(realpath ${LOCAL_PATH}/../../resources)" |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 57 | NEXUS_DATA_DIR="${DATA_DIR}/nexus_data" |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 58 | LISTS_DIR="${LOCAL_PATH}/data_lists" |
| 59 | |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 60 | usage () { |
| 61 | echo " Example usage: build_nexus_blob.sh -t <tag> --input-directory </path/to/downloaded/files/dir> --output-directory |
| 62 | </path/to/output/dir> --resource-list-directory </path/to/dir/with/resource/list> |
| 63 | |
Tomáš Levora | 6343352 | 2019-05-28 16:18:24 +0200 | [diff] [blame^] | 64 | -t | --tag release tag, taken from available on git or placed by data generating script (mandatory) must follow scheme onap_<semver> |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 65 | -i | --input-directory directory containing file needed to create nexus blob. The structure of this directory must organized as described in build guide |
| 66 | -o | --output-directory |
Tomáš Levora | 6343352 | 2019-05-28 16:18:24 +0200 | [diff] [blame^] | 67 | -rl | --resource-list-directory directory with files containing docker, pypi and npm lists |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 68 | " |
| 69 | exit 1 |
| 70 | } |
| 71 | |
| 72 | while [ "$1" != "" ]; do |
| 73 | case $1 in |
| 74 | -t | --tag ) shift |
| 75 | TAG=$1 |
| 76 | ;; |
| 77 | -i | --input-directory ) shift |
| 78 | DATA_DIR=$1 |
| 79 | ;; |
| 80 | -o | --output-directory ) shift |
| 81 | NEXUS_DATA_DIR=$1 |
| 82 | ;; |
| 83 | -rl | --resource-list-directory ) shift |
| 84 | LISTS_DIR=$1 |
| 85 | ;; |
| 86 | -h | --help ) usage |
| 87 | ;; |
| 88 | *) usage |
| 89 | esac |
| 90 | shift |
| 91 | done |
| 92 | |
| 93 | |
| 94 | # exit if no tag given |
| 95 | if [ -z ${TAG} ]; then |
| 96 | usage |
| 97 | exit 1 |
| 98 | fi |
| 99 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 100 | # Setup directories with resources for docker, npm and pypi |
| 101 | NXS_SRC_DOCKER_IMG_DIR="${DATA_DIR}/offline_data/docker_images_for_nexus" |
| 102 | NXS_SRC_NPM_DIR="${DATA_DIR}/offline_data/npm_tar" |
| 103 | NXS_SRC_PYPI_DIR="${DATA_DIR}/offline_data/pypi" |
| 104 | |
| 105 | # Setup specific resources list based on the tag provided |
| 106 | NXS_DOCKER_IMG_LIST="${LISTS_DIR}/${TAG}-docker_images.list" |
| 107 | NXS_NPM_LIST="${LISTS_DIR}/$(sed 's/.$/x/' <<< ${TAG})-npm.list" |
| 108 | NXS_PYPI_LIST="${LISTS_DIR}/$(sed 's/.$/x/' <<< ${TAG})-pip_packages.list" |
| 109 | |
| 110 | # Setup Nexus image used for build and install infra |
| 111 | INFRA_LIST="${LISTS_DIR}/infra_docker_images.list" |
| 112 | NEXUS_IMAGE="$(grep sonatype/nexus3 ${INFRA_LIST})" |
| 113 | NEXUS_IMAGE_TAR="${DATA_DIR}/offline_data/docker_images_infra/$(sed 's/\//\_/ ; s/$/\.tar/ ; s/\:/\_/' <<< ${NEXUS_IMAGE})" |
| 114 | |
| 115 | # Setup default ports published to host as docker registry |
| 116 | PUBLISHED_PORTS="-p ${NEXUS_PORT}:${NEXUS_PORT} -p ${NEXUS_DOCKER_PORT}:${NEXUS_DOCKER_PORT}" |
| 117 | |
| 118 | # Setup additional ports published to host based on simulated docker registries |
| 119 | for REGISTRY in $(sed -n '/\.[^/].*\//p' ${NXS_DOCKER_IMG_LIST} | sed -e 's/\/.*$//' | sort -u | grep -v ${DEFAULT_REGISTRY} || true); do |
| 120 | if [[ ${REGISTRY} != *":"* ]]; then |
| 121 | if [[ ${PUBLISHED_PORTS} != *"80:${NEXUS_DOCKER_PORT}"* ]]; then |
| 122 | PUBLISHED_PORTS="${PUBLISHED_PORTS} -p 80:${NEXUS_DOCKER_PORT}" |
| 123 | fi |
| 124 | else |
| 125 | REGISTRY_PORT="$(sed 's/^.*\:\([[:digit:]]*\)$/\1/' <<< ${REGISTRY})" |
| 126 | if [[ ${PUBLISHED_PORTS} != *"${REGISTRY_PORT}:${NEXUS_DOCKER_PORT}"* ]]; then |
| 127 | PUBLISHED_PORTS="${PUBLISHED_PORTS} -p ${REGISTRY_PORT}:${NEXUS_DOCKER_PORT}" |
| 128 | fi |
| 129 | fi |
| 130 | done |
| 131 | |
| 132 | # Setup simulated domain names to be able to push all to private Nexus repository |
| 133 | SIMUL_HOSTS="$(sed -n '/\.[^/].*\//p' ${NXS_DOCKER_IMG_LIST} | sed -e 's/\/.*$// ; s/:.*$//' | sort -u | grep -v ${DEFAULT_REGISTRY} || true) ${NEXUS_DOMAIN}" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 134 | |
| 135 | # Nexus repository configuration setup |
| 136 | NEXUS_CONFIG_GROOVY='import org.sonatype.nexus.security.realm.RealmManager |
| 137 | import org.sonatype.nexus.repository.attributes.AttributesFacet |
| 138 | import org.sonatype.nexus.security.user.UserManager |
| 139 | import org.sonatype.nexus.repository.manager.RepositoryManager |
| 140 | import org.sonatype.nexus.security.user.UserNotFoundException |
| 141 | /* Use the container to look up some services. */ |
| 142 | realmManager = container.lookup(RealmManager.class) |
| 143 | userManager = container.lookup(UserManager.class, "default") //default user manager |
| 144 | repositoryManager = container.lookup(RepositoryManager.class) |
| 145 | /* Managers are used when scripting api cannot. Note that scripting api can only create mostly, and that creation methods return objects of created entities. */ |
| 146 | /* Perform cleanup by removing all repos and users. Realms do not need to be re-disabled, admin and anonymous user will not be removed. */ |
| 147 | userManager.listUserIds().each({ id -> |
| 148 | if (id != "anonymous" && id != "admin") |
| 149 | userManager.deleteUser(id) |
| 150 | }) |
| 151 | repositoryManager.browse().each { |
| 152 | repositoryManager.delete(it.getName()) |
| 153 | } |
| 154 | /* Add bearer token realms at the end of realm lists... */ |
| 155 | realmManager.enableRealm("NpmToken") |
| 156 | realmManager.enableRealm("DockerToken") |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 157 | realmManager.enableRealm("PypiToken") |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 158 | /* Create the docker user. */ |
| 159 | security.addUser("docker", "docker", "docker", "docker@example.com", true, "docker", ["nx-anonymous"]) |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 160 | /* Create docker, npm and pypi repositories. Their default configuration should be compliant with our requirements, except the docker registry creation. */ |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 161 | repository.createNpmHosted("npm-private") |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 162 | repository.createPyPiHosted("pypi-private") |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 163 | def r = repository.createDockerHosted("onap", 8082, 0) |
| 164 | /* force basic authentication true by default, must set to false for docker repo. */ |
| 165 | conf=r.getConfiguration() |
| 166 | conf.attributes("docker").set("forceBasicAuth", false) |
| 167 | repositoryManager.update(conf)' |
| 168 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 169 | # Prepare the Nexus configuration |
| 170 | NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc '{"name":"configure", "type":"groovy", "content":.}') |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 171 | |
| 172 | ################################# |
| 173 | # Prepare the local environment # |
| 174 | ################################# |
| 175 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 176 | # Add simulated domain names to /etc/hosts |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 177 | HOSTS_BACKUP="$(eval ${TIMESTAMP}_hosts.bk)" |
| 178 | cp /etc/hosts "/etc/${HOSTS_BACKUP}" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 179 | for DNS in ${SIMUL_HOSTS}; do |
| 180 | echo "127.0.0.1 ${DNS}" >> /etc/hosts |
| 181 | done |
| 182 | |
| 183 | # Backup the current docker registry settings |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 184 | if [ -f ~/.docker/config.json ]; then |
| 185 | DOCKER_CONF_BACKUP="$(eval ${TIMESTAMP}_config.json.bk)" |
| 186 | mv ~/.docker/config.json "~/.docker/${DOCKER_CONF_BACKUP}" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 187 | fi |
| 188 | |
| 189 | ################################# |
| 190 | # Docker repository preparation # |
| 191 | ################################# |
| 192 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 193 | # Load predefined Nexus image |
| 194 | docker load -i ${NEXUS_IMAGE_TAR} |
| 195 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 196 | # Load all necessary images |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 197 | for ARCHIVE in $(sed $'s/\r// ; /^#/d ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do |
| 198 | docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE} |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 199 | done |
| 200 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 201 | ################################ |
| 202 | # Nexus repository preparation # |
| 203 | ################################ |
| 204 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 205 | # Prepare nexus-data directory |
| 206 | if [ -d ${NEXUS_DATA_DIR} ]; then |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 207 | if [ "$(docker ps -q -f name="${NEXUS_DOMAIN}")" ]; then |
| 208 | echo "Removing container ${NEXUS_DOMAIN}" |
| 209 | docker rm -f $(docker ps -aq -f name="${NEXUS_DOMAIN}") |
| 210 | fi |
| 211 | pushd ${NEXUS_DATA_DIR}/.. |
| 212 | NXS_BACKUP="$(eval ${TIMESTAMP})_$(basename ${NEXUS_DATA_DIR})_bk" |
| 213 | mv ${NEXUS_DATA_DIR} "${NXS_BACKUP}" |
| 214 | echo "${NEXUS_DATA_DIR} already exists - backing up to ${NXS_BACKUP}" |
| 215 | popd |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 216 | fi |
| 217 | |
| 218 | mkdir -p ${NEXUS_DATA_DIR} |
| 219 | chown 200:200 ${NEXUS_DATA_DIR} |
| 220 | chmod 777 ${NEXUS_DATA_DIR} |
| 221 | |
| 222 | # Save Nexus version to prevent/catch data incompatibility |
| 223 | docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver |
| 224 | |
| 225 | # Start the Nexus |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 226 | NEXUS_CONT_ID=$(docker run -d --rm -v ${NEXUS_DATA_DIR}:/nexus-data:rw --name ${NEXUS_DOMAIN} ${PUBLISHED_PORTS} ${NEXUS_IMAGE}) |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 227 | echo "Waiting for Nexus to fully start" |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 228 | until curl -su ${NEXUS_USERNAME}:${NEXUS_PASSWORD} http://${NEXUS_DOMAIN}:${NEXUS_PORT}/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 229 | printf "." |
| 230 | sleep 3 |
| 231 | done |
| 232 | echo -e "\nNexus started" |
| 233 | |
| 234 | # Configure the nexus repository |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 235 | curl -sX POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_DOMAIN}:${NEXUS_PORT}/service/rest/v1/script |
| 236 | curl -sX POST --header "Content-Type: text/plain" http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_DOMAIN}:${NEXUS_PORT}/service/rest/v1/script/configure/run > /dev/null |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 237 | |
| 238 | ########################### |
| 239 | # Populate NPM repository # |
| 240 | ########################### |
| 241 | |
| 242 | # Configure NPM registry to our Nexus repository |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 243 | echo "Configure NPM registry to ${NPM_REGISTRY}" |
| 244 | npm config set registry "${NPM_REGISTRY}" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 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 |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 259 | pushd ${NXS_SRC_NPM_DIR} |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 260 | PATCHED_NPM="$(grep tsscmp ${NXS_NPM_LIST} | sed $'s/\r// ; s/\\@/\-/ ; s/$/\.tgz/')" |
| 261 | if [[ ! -z "${PATCHED_NPM}" ]] && ! zgrep -aq "${NPM_REGISTRY}" "${PATCHED_NPM}" 2>/dev/null; then |
| 262 | tar xzf "${PATCHED_NPM}" |
| 263 | rm -f "${PATCHED_NPM}" |
| 264 | sed -i 's|\"registry\":\ \".*\"|\"registry\":\ \"'"${NPM_REGISTRY}"'\"|g' package/package.json |
| 265 | tar -zcf "${PATCHED_NPM}" package |
| 266 | rm -rf package |
| 267 | fi |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 268 | |
| 269 | # Push NPM packages to Nexus repository |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 270 | for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST});do |
| 271 | npm publish --access public ${ARCHIVE} > /dev/null |
| 272 | echo "NPM ${ARCHIVE} pushed to Nexus" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 273 | done |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 274 | popd |
| 275 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 276 | ############################### |
| 277 | ## Populate PyPi repository # |
| 278 | ############################### |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 279 | |
| 280 | pushd ${NXS_SRC_PYPI_DIR} |
| 281 | for PACKAGE in $(sed $'s/\r//; s/==/-/' ${NXS_PYPI_LIST}); do |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 282 | twine upload -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" --repository-url ${PYPI_REGISTRY} ${PACKAGE}* |
| 283 | echo "PYPI ${PACKAGE} pushed to Nexus" |
Tomáš Levora | 1d90234 | 2019-02-05 10:01:43 +0100 | [diff] [blame] | 284 | done |
| 285 | popd |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 286 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 287 | ############################### |
| 288 | ## Populate Docker repository # |
| 289 | ############################### |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 290 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 291 | # Login to simulated docker registries |
| 292 | for REGISTRY in $(sed -n '/\.[^/].*\//p' ${NXS_DOCKER_IMG_LIST} | sed -e 's/\/.*$//' | sort -u | grep -v ${DEFAULT_REGISTRY}) ${DOCKER_REGISTRY}; do |
| 293 | echo "Docker login to ${REGISTRY}" |
| 294 | docker login -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" ${REGISTRY} > /dev/null |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 295 | done |
| 296 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 297 | # Push images to private nexus based on the list |
| 298 | # Images from default registry need to be tagged to private registry |
| 299 | # and those without defined repository in tag uses default repository 'library' |
| 300 | for IMAGE in $(sed $'s/\r// ; /^#/d' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do |
| 301 | PUSH="" |
| 302 | if [[ ${IMAGE} != *"/"* ]]; then |
| 303 | PUSH="${DOCKER_REGISTRY}/library/${IMAGE}" |
| 304 | elif [[ ${IMAGE} == *"${DEFAULT_REGISTRY}"* ]]; then |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 305 | if [[ ${IMAGE} == *"/"*"/"* ]]; then |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 306 | PUSH="$(sed 's/'"${DEFAULT_REGISTRY}"'/'"${DOCKER_REGISTRY}"'/' <<< ${IMAGE})" |
| 307 | else |
| 308 | PUSH="$(sed 's/'"${DEFAULT_REGISTRY}"'/'"${DOCKER_REGISTRY}"'\/library/' <<< ${IMAGE})" |
| 309 | fi |
| 310 | elif [[ -z $(sed -n '/\.[^/].*\//p' <<< ${IMAGE}) ]]; then |
| 311 | PUSH="${DOCKER_REGISTRY}/${IMAGE}" |
| 312 | fi |
| 313 | if [[ ! -z ${PUSH} ]]; then |
| 314 | docker tag ${IMAGE} ${PUSH} |
| 315 | else |
| 316 | PUSH="${IMAGE}" |
| 317 | fi |
| 318 | docker push ${PUSH} |
| 319 | echo "${IMAGE} pushed as ${PUSH} to Nexus" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 320 | done |
| 321 | |
| 322 | ############################## |
| 323 | # Stop the Nexus and cleanup # |
| 324 | ############################## |
| 325 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 326 | echo "Stopping Nexus and returning backups" |
| 327 | |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 328 | # Stop the Nexus |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 329 | docker stop ${NEXUS_CONT_ID} > /dev/null |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 330 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 331 | # Return backed up configuration files |
Tomáš Levora | fd1f0e7 | 2019-05-23 12:25:44 +0200 | [diff] [blame] | 332 | mv -f "/etc/${HOSTS_BACKUP}" /etc/hosts |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 333 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 334 | if [ -f "~/.docker/${DOCKER_CONF_BACKUP}" ]; then |
Tomáš Levora | fd1f0e7 | 2019-05-23 12:25:44 +0200 | [diff] [blame] | 335 | mv -f "~/.docker/${DOCKER_CONF_BACKUP}" ~/.docker/config.json |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 336 | fi |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 337 | |
Tomáš Levora | 8d272bd | 2019-03-12 15:06:35 +0100 | [diff] [blame] | 338 | # Return default settings |
| 339 | npm config set registry "https://registry.npmjs.org" |
| 340 | |
| 341 | echo "Nexus blob is built" |
Samuli Silvius | 9e9afd7 | 2018-12-21 14:23:51 +0200 | [diff] [blame] | 342 | exit 0 |
Mateusz Pilat | b12b440 | 2019-05-23 15:55:56 +0200 | [diff] [blame] | 343 | |