summaryrefslogtreecommitdiffstats
path: root/build/build_nexus_blob.sh
blob: dc4b1dd30661d1ade11c1ee09326075484a3a32c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#! /usr/bin/env bash

#   COPYRIGHT NOTICE STARTS HERE
#
#   Copyright 2018 © Samsung Electronics Co., Ltd.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#   COPYRIGHT NOTICE ENDS HERE


### This script prepares Nexus repositories data blobs for ONAP

# Mandatory variables need to be set in configuration file:
#   NXS_SRC_DOCKER_IMG_DIR  - resource directory of docker images
#   NXS_SRC_NPM_DIR         - resource directory of npm packages
#   NXS_SRC_PYPI_DIR        - resource directory of pypi packages
#   NXS_DOCKER_IMG_LIST     - list of docker images to be pushed to Nexus repository
#   NXS_DOCKER_WO_LIST      - list of docker images which uses default repository
#   NXS_NPM_LIST            - list of npm packages to be published to Nexus repository
#   NXS_PYPI_LIST           - list of pypi packages to be uploaded to Nexus repository
#   NEXUS_DATA_TAR          - target tarball of Nexus data path/name
#   NEXUS_DATA_DIR          - directory used for the Nexus blob build
#   NEXUS_IMAGE             - Sonatype/Nexus3 docker image which will be used for data blob creation

# Fail fast settings
set -e

# Nexus repository location
NEXUS_DOMAIN="nexus"
NPM_REGISTRY="http://${NEXUS_DOMAIN}:8081/repository/npm-private/"
PYPI_REGISTRY="http://${NEXUS_DOMAIN}:8081/repository/pypi-private/"
DOCKER_REGISTRY="${NEXUS_DOMAIN}:8082"

# Nexus repository credentials
NEXUS_USERNAME=admin
NEXUS_PASSWORD=admin123
NEXUS_EMAIL=admin@example.org

# Setup simulated domain names to be able to push all in private Nexus repository
SIMUL_HOSTS="docker.elastic.co gcr.io hub.docker.com nexus3.onap.org nexus.onap.org registry.hub.docker.com ${NEXUS_DOMAIN}"

# Nexus repository configuration setup
NEXUS_CONFIG_GROOVY='import org.sonatype.nexus.security.realm.RealmManager
import org.sonatype.nexus.repository.attributes.AttributesFacet
import org.sonatype.nexus.security.user.UserManager
import org.sonatype.nexus.repository.manager.RepositoryManager
import org.sonatype.nexus.security.user.UserNotFoundException
/* Use the container to look up some services. */
realmManager = container.lookup(RealmManager.class)
userManager = container.lookup(UserManager.class, "default") //default user manager
repositoryManager = container.lookup(RepositoryManager.class)
/* Managers are used when scripting api cannot. Note that scripting api can only create mostly, and that creation methods return objects of created entities. */
/* Perform cleanup by removing all repos and users. Realms do not need to be re-disabled, admin and anonymous user will not be removed. */
userManager.listUserIds().each({ id ->
    if (id != "anonymous" && id != "admin")
        userManager.deleteUser(id)
})
repositoryManager.browse().each {
    repositoryManager.delete(it.getName())
}
/* Add bearer token realms at the end of realm lists... */
realmManager.enableRealm("NpmToken")
realmManager.enableRealm("DockerToken")
realmManager.enableRealm("PypiToken")
/* Create the docker user. */
security.addUser("docker", "docker", "docker", "docker@example.com", true, "docker", ["nx-anonymous"])
/* Create docker, npm and pypi repositories. Their default configuration should be compliant with our requirements, except the docker registry creation. */
repository.createNpmHosted("npm-private")
repository.createPyPiHosted("pypi-private")
def r = repository.createDockerHosted("onap", 8082, 0)
/* force basic authentication true by default, must set to false for docker repo. */
conf=r.getConfiguration()
conf.attributes("docker").set("forceBasicAuth", false)
repositoryManager.update(conf)'

usage () {
    echo "  This script is preparing Nexus data blob from docker images and npm packages"
    echo "      Usage:"
    echo "        ./$(basename $0) <config_file> [<target>]"
    echo "      "
    echo "      config_file is a file with defined variables, which are mandatory for this script"
    echo "      target is optional parameter where you can specify full path/name of resulted package"
    echo "      which replaces the value specified in configuration file"
    echo "      "
    echo "      Example: ./$(basename $0) ./package.conf  /root/nexus_data.tar"
    echo "      "
    echo "      Parameters need to be defined in configuration file:"
    echo "      "
    echo "      NXS_SRC_DOCKER_IMG_DIR    - directory of resource docker images"
    echo "      NXS_SRC_NPM_DIR           - directory of resource npm packages"
    echo "      NXS_SRC_PYPI_DIR          - directory of resource pypi packages"
    echo "      NXS_DOCKER_IMG_LIST       - list of docker images to be pushed to Nexus repository"
    echo "      NXS_DOCKER_WO_LIST        - list of docker images which uses default repository"
    echo "      NXS_NPM_LIST              - list of npm packages to be published to Nexus repository"
    echo "      NXS_PYPI_LIST             - list of pypi packages to be uploaded to Nexus repository"
    echo "      NEXUS_DATA_TAR            - target tarball of Nexus data path/name"
    echo "      NEXUS_DATA_DIR            - directory used for the Nexus blob build"
    echo "      NEXUS_IMAGE               - Sonatype/Nexus3 docker image which will be used for data blob creation"
    exit 1
}


#################################
# Prepare the local environment #
#################################

# Load the config file
if [ "${1}" == "-h" ] || [ -z "${1}" ]; then
    usage
elif [ -f ${1} ]; then
    . ${1}
else
    echo "Missing mandatory configuration file!"
    usage
    exit 1
fi

if [ -n "${2}" ]; then
    NEXUS_DATA_TAR="${2}"
fi

for VAR in NXS_SRC_DOCKER_IMG_DIR NXS_SRC_NPM_DIR NXS_SRC_PYPI_DIR NXS_DOCKER_IMG_LIST NXS_DOCKER_WO_LIST NXS_NPM_LIST NXS_PYPI_LIST NEXUS_DATA_TAR NEXUS_DATA_DIR NEXUS_IMAGE; do
    if [ -n "${!VAR}" ] ; then
        echo "${VAR} is set to ${!VAR}"
    else
        echo "${VAR} is not set and it is mandatory"
        FAIL="1"
    fi
done

if [ "${FAIL}" == "1" ]; then
    echo "One or more mandatory variables are not set"
    exit 1
fi

# Check the dependencies in the beginning

# Install jq
if yum list installed "jq" >/dev/null 2>&1; then
    echo "jq is already installed"
else
    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
fi

# Install curl if necessary
if yum list installed "curl" >/dev/null 2>&1; then
    echo "curl is already installed"
else
    yum install -y --setopt=skip_missing_names_on_install=False curl
fi

# Install expect if necessary
if yum list installed "expect" >/dev/null 2>&1; then
    echo "expect is already installed"
else
    yum install -y --setopt=skip_missing_names_on_install=False expect
fi

# Install Docker (docker-ce in version 17.03 for RHEL) from online repositories if no version installed
if yum list installed "docker-ce" >/dev/null 2>&1 || which docker>/dev/null 2>&1; then
    echo "Docker is already installed"
else
    curl https://releases.rancher.com/install-docker/17.03.sh | sh
fi

# Prepare the Nexus configuration
NEXUS_CONFIG=$(echo "${NEXUS_CONFIG_GROOVY}" | jq -Rsc  '{"name":"configure", "type":"groovy", "content":.}')

# Add simulated domain names to /etc/hosts
cp /etc/hosts /etc/$(date +"%Y-%m-%d_%H-%M-%S")_hosts.bk
for DNS in ${SIMUL_HOSTS}; do
    echo "127.0.0.1 ${DNS}" >> /etc/hosts
done

# Backup the current docker registry settings
if [ -f /root/.docker/config.json ]; then
    mv /root/.docker/config.json /root/.docker/$(date +"%Y-%m-%d_%H-%M-%S")config.json.bk
fi

#################################
# Docker repository preparation #
#################################

# Load all necessary images
for ARCHIVE in $(sed $'s/\r// ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do
    docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE}
done

for ARCHIVE in $(sed $'s/\r// ; s/\:/\_/g ; s/\//\_/g ; s/$/\.tar/g' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
    docker load -i ${NXS_SRC_DOCKER_IMG_DIR}/${ARCHIVE}
done

# Tag docker images from default repository to simulated repository to be able to upload it to our private registry
for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
    docker tag ${IMAGE} ${DOCKER_REGISTRY}/${IMAGE}
done


################################
# Nexus repository preparation #
################################

# Load predefined Nexus image
docker load -i ${NEXUS_IMAGE}

# Prepare nexus-data directory
if [ -d ${NEXUS_DATA_DIR} ]; then
    if [ "$(docker ps -q -f name=nexus)" ]; then
        docker rm -f $(docker ps -aq -f name=nexus)
    fi
    cd ${NEXUS_DATA_DIR}/..
    mv ${NEXUS_DATA_DIR} $(date +"%Y-%m-%d_%H-%M-%S")_$(basename ${NEXUS_DATA_DIR})_bk
fi

mkdir -p ${NEXUS_DATA_DIR}
chown 200:200 ${NEXUS_DATA_DIR}
chmod 777 ${NEXUS_DATA_DIR}

# Save Nexus version to prevent/catch data incompatibility
docker images --no-trunc | grep sonatype/nexus3 | awk '{ print $1":"$2" "$3}' > ${NEXUS_DATA_DIR}/nexus.ver

# Start the Nexus
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)
echo "Waiting for Nexus to fully start"
until curl -su admin:admin123 http://${NEXUS_DOMAIN}:8081/service/metrics/healthcheck | grep '"healthy":true' > /dev/null ; do
    printf "."
    sleep 3
done
echo -e "\nNexus started"

# Configure the nexus repository
curl -X POST --header 'Content-Type: application/json' --data-binary "${NEXUS_CONFIG}" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script
curl -X POST --header "Content-Type: text/plain" http://admin:admin123@${NEXUS_DOMAIN}:8081/service/rest/v1/script/configure/run

###########################
# Populate NPM repository #
###########################

# Configure NPM registry to our Nexus repository
npm config set registry ${NPM_REGISTRY}

# Login to NPM registry
/usr/bin/expect <<EOF
spawn npm login
expect "Username:"
send "${NEXUS_USERNAME}\n"
expect "Password:"
send "${NEXUS_PASSWORD}\n"
expect Email:
send "${NEXUS_EMAIL}\n"
expect eof
EOF

# Patch problematic package
pushd ${NXS_SRC_NPM_DIR}
tar xvzf tsscmp-1.0.5.tgz
rm -f tsscmp-1.0.5.tgz
sed -i 's|\"registry\":\ \".*\"|\"registry\":\ \"'"${NPM_REGISTRY}"'\"|g' package/package.json
tar -zcvf tsscmp-1.0.5.tgz package
rm -rf package

# Push NPM packages to Nexus repository
for ARCHIVE in $(sed $'s/\r// ; s/\\@/\-/g ; s/$/\.tgz/g' ${NXS_NPM_LIST} | awk '{ print $1 }'); do
    npm publish --access public ${ARCHIVE}
done
popd

##############################
#  Populate PyPi repository  #
##############################

pushd ${NXS_SRC_PYPI_DIR}
for PACKAGE in $(sed $'s/\r//; s/==/-/' ${NXS_PYPI_LIST}); do
    twine upload -u ${NEXUS_USERNAME} -p ${NEXUS_PASSWORD} --repository-url ${PYPI_REGISTRY} ./${PACKAGE}*
done
popd

##############################
# Populate Docker repository #
##############################

for REGISTRY in $(sed 's/\/.*//' ${NXS_DOCKER_IMG_LIST} | uniq) ${NEXUS_DOMAIN}:8082; do
    docker login -u "${NEXUS_USERNAME}" -p "${NEXUS_PASSWORD}" ${REGISTRY} > /dev/null
done

for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_WO_LIST} | awk '{ print $1 }'); do
    docker push ${DOCKER_REGISTRY}/${IMAGE}
done

for IMAGE in $(sed $'s/\r//' ${NXS_DOCKER_IMG_LIST} | awk '{ print $1 }'); do
    docker push ${IMAGE}
done

##############################
# Stop the Nexus and cleanup #
##############################

# Stop the Nexus
docker stop ${NEXUS_CONT_ID}

# Create the nexus-data package
cd ${NEXUS_DATA_DIR}/..
echo "Packing the ${NEXUS_DATA_DIR} dir"
until tar -cf ${NEXUS_DATA_TAR} $(basename ${NEXUS_DATA_DIR}); do
    printf "."
    sleep 5
done
echo "${NEXUS_DATA_TAR} has been created"

# Return the previous version of /etc/hosts back to its place
mv -f $(ls -tr /etc/*hosts.bk | tail -1) /etc/hosts

exit 0