3 # COPYRIGHT NOTICE STARTS HERE
5 # Copyright 2018 © Samsung Electronics Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 # COPYRIGHT NOTICE ENDS HERE
22 ### This script prepares Nexus repositories data blobs for ONAP
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
37 # Nexus repository location
39 NPM_REGISTRY="http://${NEXUS_DOMAIN}:8081/repository/npm-private/"
40 DOCKER_REGISTRY="${NEXUS_DOMAIN}:8082"
42 # Nexus repository credentials
44 NEXUS_PASSWORD=admin123
45 NEXUS_EMAIL=admin@example.org
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}"
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)
66 repositoryManager.browse().each {
67 repositoryManager.delete(it.getName())
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)'
83 echo " This script is preparing Nexus data blob from docker images and npm packages"
85 echo " ./$(basename $0) <config_file> [<target>]"
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"
91 echo " Example: ./$(basename $0) ./package.conf /root/nexus_data.tar"
93 echo " Parameters need to be defined in configuration file:"
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"
107 #################################
108 # Prepare the local environment #
109 #################################
111 # Load the config file
112 if [ "${1}" == "-h" ] || [ -z "${1}" ]; then
114 elif [ -f ${1} ]; then
117 echo "Missing mandatory configuration file!"
122 if [ -n "${2}" ]; then
123 NEXUS_DATA_TAR="${2}"
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}"
130 echo "${VAR} is not set and it is mandatory"
135 if [ "${FAIL}" == "1" ]; then
136 echo "One or more mandatory variables are not set"
140 # Check the dependencies in the beginning
143 if yum list installed "nodejs" >/dev/null 2>&1; then
144 echo "Node.js is already installed"
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
151 # Install curl if necessary
152 if yum list installed "curl" >/dev/null 2>&1; then
153 echo "curl is already installed"
155 yum install -y --setopt=skip_missing_names_on_install=False curl
158 # Install expect if necessary
159 if yum list installed "expect" >/dev/null 2>&1; then
160 echo "expect is already installed"
162 yum install -y --setopt=skip_missing_names_on_install=False expect
165 # Install Docker (docker-ce in version 17.03 for RHEL) from online repositories if no version installed
166 if yum list installed "docker-ce" >/dev/null 2>&1 || which docker>/dev/null 2>&1; then
167 echo "Docker is already installed"
169 curl https://releases.rancher.com/install-docker/17.03.sh | sh
172 # Prepare the Nexus configuration
173 NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc '{"name":"configure", "type":"groovy", "content":.}')
175 # Add simulated domain names to /etc/hosts
176 cp /etc/hosts /etc/$(date +"%Y-%m-%d_%H-%M-%S")_hosts.bk
177 for DNS in ${SIMUL_HOSTS}; do
178 echo "127.0.0.1 ${DNS}" >> /etc/hosts
181 # Backup the current docker registry settings
182 if [ -f /root/.docker/config.json ]; then
183 mv /root/.docker/config.json /root/.docker/$(date +"%Y-%m-%d_%H-%M-%S")config.json.bk
186 #################################
187 # Docker repository preparation #
188 #################################
190 # Load all necessary images
191 for 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}
195 for 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}
199 # Tag docker images from default repository to simulated repository to be able to upload it to our private registry
200 for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
201 docker tag ${IMAGE} ${DOCKER_REGISTRY}/${IMAGE}
205 ################################
206 # Nexus repository preparation #
207 ################################
209 # Load predefined Nexus image
210 docker load -i ${NEXUS_IMAGE}
212 # Prepare nexus-data directory
213 if [ -d ${NEXUS_DATA_DIR} ]; then
214 if [ "$(docker ps -q -f name=nexus)" ]; then
215 docker rm -f $(docker ps -aq -f name=nexus)
217 cd ${NEXUS_DATA_DIR}/..
218 mv ${NEXUS_DATA_DIR} $(date +"%Y-%m-%d_%H-%M-%S")_$(basename ${NEXUS_DATA_DIR})_bk
221 mkdir -p ${NEXUS_DATA_DIR}
222 chown 200:200 ${NEXUS_DATA_DIR}
223 chmod 777 ${NEXUS_DATA_DIR}
225 # Save Nexus version to prevent/catch data incompatibility
226 docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver
229 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)
230 echo "Waiting for Nexus to fully start"
231 until curl -su admin:admin123 http://${NEXUS_DOMAIN}:8081/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do
235 echo -e "\nNexus started"
237 # Configure the nexus repository
238 curl -X POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script
239 curl -X POST --header "Content-Type: text/plain" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script/configure/run
241 ###########################
242 # Populate NPM repository #
243 ###########################
245 # Configure NPM registry to our Nexus repository
246 npm config set registry ${NPM_REGISTRY}
248 # Login to NPM registry
249 /usr/bin/expect <<EOF
252 send "${NEXUS_USERNAME}\n"
254 send "${NEXUS_PASSWORD}\n"
256 send "${NEXUS_EMAIL}\n"
260 # Patch problematic package
261 cd ${NXS_SRC_NPM_DIR}
262 tar xvzf tsscmp-1.0.5.tgz
263 rm -f tsscmp-1.0.5.tgz
264 sed -i "s|https://registry.npmjs.org|http://${NEXUS_DOMAIN}:8081|g" package/package.json
265 sed -i "s|https://nexus.onap-me.novalocal|http://${NEXUS_DOMAIN}:8081|g" package/package.json
266 tar -zcvf tsscmp-1.0.5.tgz package
269 # Push NPM packages to Nexus repository
270 for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST} | awk '{ print $1 }'); do
271 npm publish --access public ${ARCHIVE}
274 ##############################
275 # Populate Docker repository #
276 ##############################
278 for 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
282 for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
283 docker push ${DOCKER_REGISTRY}/${IMAGE}
286 for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do
290 ##############################
291 # Stop the Nexus and cleanup #
292 ##############################
295 docker stop ${NEXUS_CONT_ID}
297 # Create the nexus-data package
298 cd ${NEXUS_DATA_DIR}/..
299 echo "Packing the ${NEXUS_DATA_DIR} dir"
300 until tar -cf ${NEXUS_DATA_TAR} $(basename ${NEXUS_DATA_DIR}); do
304 echo "${NEXUS_DATA_TAR} has been created"
306 # Return the previous version of /etc/hosts back to its place
307 mv -f $(ls -tr /etc/*hosts.bk | tail -1) /etc/hosts