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