blob: aa977793f8570e1e06c43f21ce75ff03eb759acd [file] [log] [blame]
Samuli Silvius9e9afd72018-12-21 14:23:51 +02001#! /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
35set -e
36
37# Nexus repository location
38NEXUS_DOMAIN="nexus"
39NPM_REGISTRY="http://${NEXUS_DOMAIN}:8081/repository/npm-private/"
40DOCKER_REGISTRY="${NEXUS_DOMAIN}:8082"
41
42# Nexus repository credentials
43NEXUS_USERNAME=admin
44NEXUS_PASSWORD=admin123
45NEXUS_EMAIL=admin@example.org
46
47# Setup simulated domain names to be able to push all in private Nexus repository
48SIMUL_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
51NEXUS_CONFIG_GROOVY='import org.sonatype.nexus.security.realm.RealmManager
52import org.sonatype.nexus.repository.attributes.AttributesFacet
53import org.sonatype.nexus.security.user.UserManager
54import org.sonatype.nexus.repository.manager.RepositoryManager
55import org.sonatype.nexus.security.user.UserNotFoundException
56/* Use the container to look up some services. */
57realmManager = container.lookup(RealmManager.class)
58userManager = container.lookup(UserManager.class, "default") //default user manager
59repositoryManager = 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. */
62userManager.listUserIds().each({ id ->
63 if (id != "anonymous" && id != "admin")
64 userManager.deleteUser(id)
65})
66repositoryManager.browse().each {
67 repositoryManager.delete(it.getName())
68}
69/* Add bearer token realms at the end of realm lists... */
70realmManager.enableRealm("NpmToken")
71realmManager.enableRealm("DockerToken")
72/* Create the docker user. */
73security.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. */
75repository.createNpmHosted("npm-private")
76def r = repository.createDockerHosted("onap", 8082, 0)
77/* force basic authentication true by default, must set to false for docker repo. */
78conf=r.getConfiguration()
79conf.attributes("docker").set("forceBasicAuth", false)
80repositoryManager.update(conf)'
81
82usage () {
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
112if [ "${1}" == "-h" ] || [ -z "${1}" ]; then
113 usage
114elif [ -f ${1} ]; then
115 . ${1}
116else
117 echo "Missing mandatory configuration file!"
118 usage
119 exit 1
120fi
121
122if [ -n "${2}" ]; then
123 NEXUS_DATA_TAR="${2}"
124fi
125
126for 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
133done
134
135if [ "${FAIL}" == "1" ]; then
136 echo "One or more mandatory variables are not set"
137 exit 1
138fi
139
140# Check the dependencies in the beginning
141
142# Install Node.js
143if yum list installed "nodejs" >/dev/null 2>&1; then
144 echo "Node.js is already installed"
145else
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
149fi
150
151# Install curl if necessary
152if yum list installed "curl" >/dev/null 2>&1; then
153 echo "curl is already installed"
154else
155 yum install -y --setopt=skip_missing_names_on_install=False curl
156fi
157
158# Install expect if necessary
159if yum list installed "expect" >/dev/null 2>&1; then
160 echo "expect is already installed"
161else
162 yum install -y --setopt=skip_missing_names_on_install=False expect
163fi
164
165# Install Docker (docker-ce in version 17.03 for RHEL) from online repositories if no version installed
166if yum list installed "docker-ce" >/dev/null 2>&1 || which docker>/dev/null 2>&1; then
167 echo "Docker is already installed"
168else
169 curl https://releases.rancher.com/install-docker/17.03.sh | sh
170fi
171
172# Prepare the Nexus configuration
173NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc '{"name":"configure", "type":"groovy", "content":.}')
174
175# Add simulated domain names to /etc/hosts
176cp /etc/hosts /etc/$(date +"%Y-%m-%d_%H-%M-%S")_hosts.bk
177for DNS in ${SIMUL_HOSTS}; do
178 echo "127.0.0.1 ${DNS}" >> /etc/hosts
179done
180
181# Backup the current docker registry settings
182if [ -f /root/.docker/config.json ]; then
183 mv /root/.docker/config.json /root/.docker/$(date +"%Y-%m-%d_%H-%M-%S")config.json.bk
184fi
185
186#################################
187# Docker repository preparation #
188#################################
189
190# Load all necessary images
191for 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}
193done
194
195for 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}
197done
198
199# Tag docker images from default repository to simulated repository to be able to upload it to our private registry
200for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
201 docker tag ${IMAGE} ${DOCKER_REGISTRY}/${IMAGE}
202done
203
204
205################################
206# Nexus repository preparation #
207################################
208
209# Load predefined Nexus image
210docker load -i ${NEXUS_IMAGE}
211
212# Prepare nexus-data directory
213if [ -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
219fi
220
221mkdir -p ${NEXUS_DATA_DIR}
222chown 200:200 ${NEXUS_DATA_DIR}
223chmod 777 ${NEXUS_DATA_DIR}
224
225# Save Nexus version to prevent/catch data incompatibility
226docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver
227
228# Start the Nexus
229NEXUS_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)
230echo "Waiting for Nexus to fully start"
231until curl -su admin:admin123 http://${NEXUS_DOMAIN}:8081/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do
232 printf "."
233 sleep 3
234done
235echo -e "\nNexus started"
236
237# Configure the nexus repository
238curl -X POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script
239curl -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
246npm config set registry ${NPM_REGISTRY}
247
248# Login to NPM registry
249/usr/bin/expect <<EOF
250spawn npm login
251expect "Username:"
252send "${NEXUS_USERNAME}\n"
253expect "Password:"
254send "${NEXUS_PASSWORD}\n"
255expect Email:
256send "${NEXUS_EMAIL}\n"
257expect eof
258EOF
259
260# Patch problematic package
261cd ${NXS_SRC_NPM_DIR}
262tar xvzf tsscmp-1.0.5.tgz
263rm -f tsscmp-1.0.5.tgz
264sed -i "s|https://registry.npmjs.org|http://${NEXUS_DOMAIN}:8081|g" package/package.json
265sed -i "s|https://nexus.onap-me.novalocal|http://${NEXUS_DOMAIN}:8081|g" package/package.json
266tar -zcvf tsscmp-1.0.5.tgz package
267rm -rf package
268
269# Push NPM packages to Nexus repository
270for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST} | awk '{ print $1 }'); do
271 npm publish --access public ${ARCHIVE}
272done
273
274##############################
275# Populate Docker repository #
276##############################
277
278for 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
280done
281
282for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
283 docker push ${DOCKER_REGISTRY}/${IMAGE}
284done
285
286for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do
287 docker push ${IMAGE}
288done
289
290##############################
291# Stop the Nexus and cleanup #
292##############################
293
294# Stop the Nexus
295docker stop ${NEXUS_CONT_ID}
296
297# Create the nexus-data package
298cd ${NEXUS_DATA_DIR}/..
299echo "Packing the ${NEXUS_DATA_DIR} dir"
300until tar -cf ${NEXUS_DATA_TAR} $(basename ${NEXUS_DATA_DIR}); do
301 printf "."
302 sleep 5
303done
304echo "${NEXUS_DATA_TAR} has been created"
305
306# Return the previous version of /etc/hosts back to its place
307mv -f $(ls -tr /etc/*hosts.bk | tail -1) /etc/hosts
308
309exit 0