blob: f3edb482946a6de72b143fc995823dc2420046ed [file] [log] [blame]
Samuli Silvius9e9afd72018-12-21 14:23:51 +02001#! /usr/bin/env bash
2
3# COPYRIGHT NOTICE STARTS HERE
4#
Tomáš Levora8d272bd2019-03-12 15:06:35 +01005# Copyright 2018-2019 © Samsung Electronics Co., Ltd.
Samuli Silvius9e9afd72018-12-21 14:23:51 +02006#
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 Silvius9e9afd72018-12-21 14:23:51 +020021### This script prepares Nexus repositories data blobs for ONAP
22
Tomáš Levorafeaa6b42019-05-29 09:45:29 +020023## The script requires following dependencies are installed: nodejs, jq, docker, twine
Mateusz Pilatb12b4402019-05-23 15:55:56 +020024## 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 Silvius9e9afd72018-12-21 14:23:51 +020028
29# Fail fast settings
30set -e
31
Tomáš Levora8d272bd2019-03-12 15:06:35 +010032TIMESTAMP="date +'%Y-%m-%d_%H-%M-%S'"
33SCRIPT_LOG="/tmp/$(basename $0)_$(eval ${TIMESTAMP}).log"
34
35# Log everything
36exec &> >(tee -a "${SCRIPT_LOG}")
37
Samuli Silvius9e9afd72018-12-21 14:23:51 +020038# Nexus repository location
39NEXUS_DOMAIN="nexus"
Tomáš Levora8d272bd2019-03-12 15:06:35 +010040NEXUS_PORT="8081"
41NEXUS_DOCKER_PORT="8082"
42NPM_REGISTRY="http://${NEXUS_DOMAIN}:${NEXUS_PORT}/repository/npm-private/"
43PYPI_REGISTRY="http://${NEXUS_DOMAIN}:${NEXUS_PORT}/repository/pypi-private/"
44DOCKER_REGISTRY="${NEXUS_DOMAIN}:${NEXUS_DOCKER_PORT}"
45DEFAULT_REGISTRY="docker.io"
Samuli Silvius9e9afd72018-12-21 14:23:51 +020046
47# Nexus repository credentials
48NEXUS_USERNAME=admin
49NEXUS_PASSWORD=admin123
50NEXUS_EMAIL=admin@example.org
51
Tomáš Levora8d272bd2019-03-12 15:06:35 +010052# Setting paths
53LOCAL_PATH="$(readlink -f $(dirname ${0}))"
Mateusz Pilatb12b4402019-05-23 15:55:56 +020054
55#Defaults
Tomáš Levora8d272bd2019-03-12 15:06:35 +010056DATA_DIR="$(realpath ${LOCAL_PATH}/../../resources)"
Mateusz Pilatb12b4402019-05-23 15:55:56 +020057NEXUS_DATA_DIR="${DATA_DIR}/nexus_data"
Tomáš Levora8d272bd2019-03-12 15:06:35 +010058LISTS_DIR="${LOCAL_PATH}/data_lists"
59
Mateusz Pilatb12b4402019-05-23 15:55:56 +020060usage () {
Tomáš Levorafeaa6b42019-05-29 09:45:29 +020061 echo " Example usage: build_nexus_blob.sh --input-directory </path/to/downloaded/files/dir> --output-directory
Mateusz Pilatb12b4402019-05-23 15:55:56 +020062 </path/to/output/dir> --resource-list-directory </path/to/dir/with/resource/list>
63
Mateusz Pilatb12b4402019-05-23 15:55:56 +020064 -i | --input-directory directory containing file needed to create nexus blob. The structure of this directory must organized as described in build guide
65 -o | --output-directory
Tomáš Levora63433522019-05-28 16:18:24 +020066 -rl | --resource-list-directory directory with files containing docker, pypi and npm lists
Mateusz Pilatb12b4402019-05-23 15:55:56 +020067 "
68 exit 1
69}
70
71while [ "$1" != "" ]; do
72 case $1 in
Mateusz Pilatb12b4402019-05-23 15:55:56 +020073 -i | --input-directory ) shift
74 DATA_DIR=$1
75 ;;
76 -o | --output-directory ) shift
77 NEXUS_DATA_DIR=$1
78 ;;
79 -rl | --resource-list-directory ) shift
80 LISTS_DIR=$1
81 ;;
82 -h | --help ) usage
83 ;;
84 *) usage
85 esac
86 shift
87done
88
Tomáš Levora8d272bd2019-03-12 15:06:35 +010089# Setup directories with resources for docker, npm and pypi
90NXS_SRC_DOCKER_IMG_DIR="${DATA_DIR}/offline_data/docker_images_for_nexus"
91NXS_SRC_NPM_DIR="${DATA_DIR}/offline_data/npm_tar"
92NXS_SRC_PYPI_DIR="${DATA_DIR}/offline_data/pypi"
93
Tomáš Levorafeaa6b42019-05-29 09:45:29 +020094# Setup specific resources lists
95NXS_DOCKER_IMG_LIST="${LISTS_DIR}/onap_docker_images.list"
96NXS_NPM_LIST="${LISTS_DIR}/onap_npm.list"
97NXS_PYPI_LIST="${LISTS_DIR}/onap_pip_packages.list"
Tomáš Levora8d272bd2019-03-12 15:06:35 +010098
99# Setup Nexus image used for build and install infra
100INFRA_LIST="${LISTS_DIR}/infra_docker_images.list"
101NEXUS_IMAGE="$(grep sonatype/nexus3 ${INFRA_LIST})"
102NEXUS_IMAGE_TAR="${DATA_DIR}/offline_data/docker_images_infra/$(sed 's/\//\_/ ; s/$/\.tar/ ; s/\:/\_/' <<< ${NEXUS_IMAGE})"
103
104# Setup default ports published to host as docker registry
105PUBLISHED_PORTS="-p ${NEXUS_PORT}:${NEXUS_PORT} -p ${NEXUS_DOCKER_PORT}:${NEXUS_DOCKER_PORT}"
106
107# Setup additional ports published to host based on simulated docker registries
108for REGISTRY in $(sed -n '/\.[^/].*\//p' ${NXS_DOCKER_IMG_LIST} | sed -e 's/\/.*$//' | sort -u | grep -v ${DEFAULT_REGISTRY} || true); do
109 if [[ ${REGISTRY} != *":"* ]]; then
110 if [[ ${PUBLISHED_PORTS} != *"80:${NEXUS_DOCKER_PORT}"* ]]; then
111 PUBLISHED_PORTS="${PUBLISHED_PORTS} -p 80:${NEXUS_DOCKER_PORT}"
112 fi
113 else
114 REGISTRY_PORT="$(sed 's/^.*\:\([[:digit:]]*\)$/\1/' <<< ${REGISTRY})"
115 if [[ ${PUBLISHED_PORTS} != *"${REGISTRY_PORT}:${NEXUS_DOCKER_PORT}"* ]]; then
116 PUBLISHED_PORTS="${PUBLISHED_PORTS} -p ${REGISTRY_PORT}:${NEXUS_DOCKER_PORT}"
117 fi
118 fi
119done
120
121# Setup simulated domain names to be able to push all to private Nexus repository
122SIMUL_HOSTS="$(sed -n '/\.[^/].*\//p' ${NXS_DOCKER_IMG_LIST} | sed -e 's/\/.*$// ; s/:.*$//' | sort -u | grep -v ${DEFAULT_REGISTRY} || true) ${NEXUS_DOMAIN}"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200123
124# Nexus repository configuration setup
125NEXUS_CONFIG_GROOVY='import org.sonatype.nexus.security.realm.RealmManager
126import org.sonatype.nexus.repository.attributes.AttributesFacet
127import org.sonatype.nexus.security.user.UserManager
128import org.sonatype.nexus.repository.manager.RepositoryManager
129import org.sonatype.nexus.security.user.UserNotFoundException
130/* Use the container to look up some services. */
131realmManager = container.lookup(RealmManager.class)
132userManager = container.lookup(UserManager.class, "default") //default user manager
133repositoryManager = container.lookup(RepositoryManager.class)
134/* Managers are used when scripting api cannot. Note that scripting api can only create mostly, and that creation methods return objects of created entities. */
135/* Perform cleanup by removing all repos and users. Realms do not need to be re-disabled, admin and anonymous user will not be removed. */
136userManager.listUserIds().each({ id ->
137 if (id != "anonymous" && id != "admin")
138 userManager.deleteUser(id)
139})
140repositoryManager.browse().each {
141 repositoryManager.delete(it.getName())
142}
143/* Add bearer token realms at the end of realm lists... */
144realmManager.enableRealm("NpmToken")
145realmManager.enableRealm("DockerToken")
Tomáš Levora1d902342019-02-05 10:01:43 +0100146realmManager.enableRealm("PypiToken")
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200147/* Create the docker user. */
148security.addUser("docker", "docker", "docker", "docker@example.com", true, "docker", ["nx-anonymous"])
Tomáš Levora1d902342019-02-05 10:01:43 +0100149/* Create docker, npm and pypi repositories. Their default configuration should be compliant with our requirements, except the docker registry creation. */
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200150repository.createNpmHosted("npm-private")
Tomáš Levora1d902342019-02-05 10:01:43 +0100151repository.createPyPiHosted("pypi-private")
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200152def r = repository.createDockerHosted("onap", 8082, 0)
153/* force basic authentication true by default, must set to false for docker repo. */
154conf=r.getConfiguration()
155conf.attributes("docker").set("forceBasicAuth", false)
156repositoryManager.update(conf)'
157
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100158# Prepare the Nexus configuration
159NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc '{"name":"configure", "type":"groovy", "content":.}')
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200160
161#################################
162# Prepare the local environment #
163#################################
164
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200165# Add simulated domain names to /etc/hosts
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100166HOSTS_BACKUP="$(eval ${TIMESTAMP}_hosts.bk)"
167cp /etc/hosts "/etc/${HOSTS_BACKUP}"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200168for DNS in ${SIMUL_HOSTS}; do
169 echo "127.0.0.1 ${DNS}" >> /etc/hosts
170done
171
172# Backup the current docker registry settings
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100173if [ -f ~/.docker/config.json ]; then
174 DOCKER_CONF_BACKUP="$(eval ${TIMESTAMP}_config.json.bk)"
175 mv ~/.docker/config.json "~/.docker/${DOCKER_CONF_BACKUP}"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200176fi
177
178#################################
179# Docker repository preparation #
180#################################
181
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100182# Load predefined Nexus image
183docker load -i ${NEXUS_IMAGE_TAR}
184
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200185# Load all necessary images
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100186for ARCHIVE in $(sed $'s/\r// ; /^#/d ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do
187 docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE}
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200188done
189
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200190################################
191# Nexus repository preparation #
192################################
193
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200194# Prepare nexus-data directory
195if [ -d ${NEXUS_DATA_DIR} ]; then
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100196 if [ "$(docker ps -q -f name="${NEXUS_DOMAIN}")" ]; then
197 echo "Removing container ${NEXUS_DOMAIN}"
198 docker rm -f $(docker ps -aq -f name="${NEXUS_DOMAIN}")
199 fi
200 pushd ${NEXUS_DATA_DIR}/..
201 NXS_BACKUP="$(eval ${TIMESTAMP})_$(basename ${NEXUS_DATA_DIR})_bk"
202 mv ${NEXUS_DATA_DIR} "${NXS_BACKUP}"
203 echo "${NEXUS_DATA_DIR} already exists - backing up to ${NXS_BACKUP}"
204 popd
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200205fi
206
207mkdir -p ${NEXUS_DATA_DIR}
208chown 200:200 ${NEXUS_DATA_DIR}
209chmod 777 ${NEXUS_DATA_DIR}
210
211# Save Nexus version to prevent/catch data incompatibility
212docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver
213
214# Start the Nexus
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100215NEXUS_CONT_ID=$(docker run -d --rm -v ${NEXUS_DATA_DIR}:/nexus-data:rw --name ${NEXUS_DOMAIN} ${PUBLISHED_PORTS} ${NEXUS_IMAGE})
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200216echo "Waiting for Nexus to fully start"
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100217until curl -su ${NEXUS_USERNAME}:${NEXUS_PASSWORD} http://${NEXUS_DOMAIN}:${NEXUS_PORT}/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200218 printf "."
219 sleep 3
220done
221echo -e "\nNexus started"
222
223# Configure the nexus repository
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100224curl -sX POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://${NEXUS_USERNAME}:${NEXUS_PASSWORD}@${NEXUS_DOMAIN}:${NEXUS_PORT}/service/rest/v1/script
225curl -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 Silvius9e9afd72018-12-21 14:23:51 +0200226
227###########################
228# Populate NPM repository #
229###########################
230
231# Configure NPM registry to our Nexus repository
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100232echo "Configure NPM registry to ${NPM_REGISTRY}"
233npm config set registry "${NPM_REGISTRY}"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200234
235# Login to NPM registry
236/usr/bin/expect <<EOF
237spawn npm login
238expect "Username:"
239send "${NEXUS_USERNAME}\n"
240expect "Password:"
241send "${NEXUS_PASSWORD}\n"
242expect Email:
243send "${NEXUS_EMAIL}\n"
244expect eof
245EOF
246
247# Patch problematic package
Tomáš Levora1d902342019-02-05 10:01:43 +0100248pushd ${NXS_SRC_NPM_DIR}
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100249PATCHED_NPM="$(grep tsscmp ${NXS_NPM_LIST} | sed $'s/\r// ; s/\\@/\-/ ; s/$/\.tgz/')"
250if [[ ! -z "${PATCHED_NPM}" ]] && ! zgrep -aq "${NPM_REGISTRY}" "${PATCHED_NPM}" 2>/dev/null; then
251 tar xzf "${PATCHED_NPM}"
252 rm -f "${PATCHED_NPM}"
253 sed -i 's|\"registry\":\ \".*\"|\"registry\":\ \"'"${NPM_REGISTRY}"'\"|g' package/package.json
254 tar -zcf "${PATCHED_NPM}" package
255 rm -rf package
256fi
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200257
258# Push NPM packages to Nexus repository
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100259for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST});do
260 npm publish --access public ${ARCHIVE} > /dev/null
261 echo "NPM ${ARCHIVE} pushed to Nexus"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200262done
Tomáš Levora1d902342019-02-05 10:01:43 +0100263popd
264
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100265###############################
266## Populate PyPi repository #
267###############################
Tomáš Levora1d902342019-02-05 10:01:43 +0100268
269pushd ${NXS_SRC_PYPI_DIR}
270for PACKAGE in $(sed $'s/\r//; s/==/-/' ${NXS_PYPI_LIST}); do
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100271 twine upload -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" --repository-url ${PYPI_REGISTRY} ${PACKAGE}*
272 echo "PYPI ${PACKAGE} pushed to Nexus"
Tomáš Levora1d902342019-02-05 10:01:43 +0100273done
274popd
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200275
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100276###############################
277## Populate Docker repository #
278###############################
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200279
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100280# Login to simulated docker registries
281for REGISTRY in $(sed -n '/\.[^/].*\//p' ${NXS_DOCKER_IMG_LIST} | sed -e 's/\/.*$//' | sort -u | grep -v ${DEFAULT_REGISTRY}) ${DOCKER_REGISTRY}; do
282 echo "Docker login to ${REGISTRY}"
283 docker login -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" ${REGISTRY} > /dev/null
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200284done
285
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100286# Push images to private nexus based on the list
287# Images from default registry need to be tagged to private registry
288# and those without defined repository in tag uses default repository 'library'
289for IMAGE in $(sed $'s/\r// ; /^#/d' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do
290 PUSH=""
291 if [[ ${IMAGE} != *"/"* ]]; then
292 PUSH="${DOCKER_REGISTRY}/library/${IMAGE}"
293 elif [[ ${IMAGE} == *"${DEFAULT_REGISTRY}"* ]]; then
Mateusz Pilatb12b4402019-05-23 15:55:56 +0200294 if [[ ${IMAGE} == *"/"*"/"* ]]; then
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100295 PUSH="$(sed 's/'"${DEFAULT_REGISTRY}"'/'"${DOCKER_REGISTRY}"'/' <<< ${IMAGE})"
296 else
297 PUSH="$(sed 's/'"${DEFAULT_REGISTRY}"'/'"${DOCKER_REGISTRY}"'\/library/' <<< ${IMAGE})"
298 fi
299 elif [[ -z $(sed -n '/\.[^/].*\//p' <<< ${IMAGE}) ]]; then
300 PUSH="${DOCKER_REGISTRY}/${IMAGE}"
301 fi
302 if [[ ! -z ${PUSH} ]]; then
303 docker tag ${IMAGE} ${PUSH}
304 else
305 PUSH="${IMAGE}"
306 fi
307 docker push ${PUSH}
308 echo "${IMAGE} pushed as ${PUSH} to Nexus"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200309done
310
311##############################
312# Stop the Nexus and cleanup #
313##############################
314
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100315echo "Stopping Nexus and returning backups"
316
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200317# Stop the Nexus
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100318docker stop ${NEXUS_CONT_ID} > /dev/null
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200319
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100320# Return backed up configuration files
Tomáš Levorafd1f0e72019-05-23 12:25:44 +0200321mv -f "/etc/${HOSTS_BACKUP}" /etc/hosts
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200322
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100323if [ -f "~/.docker/${DOCKER_CONF_BACKUP}" ]; then
Tomáš Levorafd1f0e72019-05-23 12:25:44 +0200324 mv -f "~/.docker/${DOCKER_CONF_BACKUP}" ~/.docker/config.json
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100325fi
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200326
Tomáš Levora8d272bd2019-03-12 15:06:35 +0100327# Return default settings
328npm config set registry "https://registry.npmjs.org"
329
330echo "Nexus blob is built"
Samuli Silvius9e9afd72018-12-21 14:23:51 +0200331exit 0