Merge "Fix packagecloud wget schema url"
[releng/global-jjb.git] / shell / release-job.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> release-job.sh"
12 set -eu -o pipefail
13
14 echo "INFO: creating virtual environment"
15 virtualenv -p python3 /tmp/venv
16 PATH=/tmp/venv/bin:$PATH
17 pipup="python -m pip install -q --upgrade pip lftools[nexus] jsonschema niet twine yq"
18 echo "INFO: $pipup"
19 $pipup
20
21 #Functions.
22
23 set_variables_common(){
24     echo "INFO: Setting all common variables"
25     LOGS_SERVER="${LOGS_SERVER:-None}"
26     if [ "${LOGS_SERVER}" == 'None' ]; then
27         echo "ERROR: log server not found"
28         exit 1
29     fi
30     NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
31     # Verify if using release file or parameters
32     if $USE_RELEASE_FILE ; then
33         release_files=$(git diff-tree --no-commit-id -r "$GIT_COMMIT" --name-only -- "releases/" ".releases/")
34         if (( $(grep -c . <<<"$release_files") > 1 )); then
35           echo "INFO: RELEASE FILES ARE AS FOLLOWS: $release_files"
36           echo "ERROR: Committing multiple release files in the same commit OR rename/amend of existing files is not supported."
37           exit 1
38         else
39           release_file="$release_files"
40           echo "INFO: RELEASE FILE: $release_files"
41         fi
42     else
43         echo "INFO: This job is built with parameters, no release file needed. Continuing..."
44         release_file="None"
45     fi
46
47     DISTRIBUTION_TYPE="${DISTRIBUTION_TYPE:-None}"
48     if [[ $DISTRIBUTION_TYPE == "None" ]]; then
49         DISTRIBUTION_TYPE="$(niet ".distribution_type" "$release_file")"
50     fi
51
52     PATCH_DIR="$(mktemp -d)"
53
54     # Displaying Release Information (Common variables)
55     printf "\t%-30s\n" RELEASE_ENVIRONMENT_INFO:
56     printf "\t%-30s %s\n" RELEASE_FILE: $release_file
57     printf "\t%-30s %s\n" LOGS_SERVER: $LOGS_SERVER
58     printf "\t%-30s %s\n" NEXUS_PATH: $NEXUS_PATH
59     printf "\t%-30s %s\n" JENKINS_HOSTNAME: $JENKINS_HOSTNAME
60     printf "\t%-30s %s\n" SILO: $SILO
61     printf "\t%-30s %s\n" PROJECT: $PROJECT
62     printf "\t%-30s %s\n" PROJECT-DASHED: ${PROJECT//\//-}
63     printf "\t%-30s %s\n" DISTRIBUTION_TYPE: $DISTRIBUTION_TYPE
64 }
65
66 set_variables_maven(){
67     VERSION="${VERSION:-None}"
68     if [[ $VERSION == "None" ]]; then
69         VERSION="$(niet ".version" "$release_file")"
70     fi
71     LOG_DIR="${LOG_DIR:-None}"
72     if [[ $LOG_DIR == "None" ]]; then
73         LOG_DIR="$(niet ".log_dir" "$release_file")"
74     fi
75     LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
76     LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
77
78     # Continuing displaying Release Information (Maven)
79     printf "\t%-30s\n" RELEASE_MAVEN_INFO:
80     printf "\t%-30s %s\n" VERSION: $VERSION
81     printf "\t%-30s %s\n" LOG_DIR: $LOG_DIR
82     printf "\t%-30s %s\n" LOGS_URL: $LOGS_URL
83 }
84
85 set_variables_container(){
86     VERSION="${VERSION:-None}"
87     if [[ $VERSION == "None" ]]; then
88         VERSION="$(niet ".container_release_tag" "$release_file")"
89     fi
90     if grep -q "container_pull_registry" "$release_file" ; then
91         CONTAINER_PULL_REGISTRY="$(niet ".container_pull_registry" "$release_file")"
92     fi
93     if grep -q "container_push_registry" "$release_file" ; then
94         CONTAINER_PUSH_REGISTRY="$(niet ".container_push_registry" "$release_file")"
95     fi
96     # Make sure both pull and push registries are defined
97     if [ -z ${CONTAINER_PULL_REGISTRY+x} ] || [ -z ${CONTAINER_PUSH_REGISTRY+x} ]; then
98         echo "ERROR: CONTAINER_PULL_REGISTRY and CONTAINER_PUSH_REGISTRY need to be defined"
99         exit 1
100     fi
101     ref="$(niet ".ref" "$release_file")"
102
103     # Continuing displaying Release Information (Container)
104     printf "\t%-30s\n" RELEASE_CONTAINER_INFO:
105     printf "\t%-30s %s\n" CONTAINER_RELEASE_TAG: $VERSION
106     printf "\t%-30s %s\n" CONTAINER_PULL_REGISTRY: $CONTAINER_PULL_REGISTRY
107     printf "\t%-30s %s\n" CONTAINER_PUSH_REGISTRY: $CONTAINER_PUSH_REGISTRY
108     printf "\t%-30s %s\n" GERRIT_REF_TO_TAG: $ref
109 }
110
111 set_variables_pypi(){
112     # use Jenkins parameter if set; else get value from release file
113     echo "INFO: Setting pypi variables"
114     LOG_DIR="${LOG_DIR:-None}"
115     if [[ $LOG_DIR == "None" ]]; then
116         LOG_DIR="$(yq -er .log_dir "$release_file")"
117     fi
118     LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
119     LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
120     PYPI_PROJECT="${PYPI_PROJECT:-None}"
121     if [[ $PYPI_PROJECT == "None" ]]; then
122         PYPI_PROJECT="$(yq -er .pypi_project "$release_file")"
123     fi
124     PYTHON_VERSION="${PYTHON_VERSION:-None}"
125     if [[ $PYTHON_VERSION == "None" ]]; then
126         PYTHON_VERSION="$(yq -er .python_version "$release_file")"
127     fi
128     VERSION="${VERSION:-None}"
129     if [[ $VERSION == "None" ]]; then
130         VERSION="$(yq -er .version "$release_file")"
131     fi
132
133     # Continuing displaying Release Information (pypi)
134     printf "\t%-30s\n" RELEASE_PYPI_INFO:
135     printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
136     printf "\t%-30s %s\n" LOGS_URL: "$LOGS_URL"
137     printf "\t%-30s %s\n" PYPI_INDEX: "$PYPI_INDEX" # from job configuration
138     printf "\t%-30s %s\n" PYPI_PROJECT: "$PYPI_PROJECT"
139     printf "\t%-30s %s\n" PYTHON_VERSION: "$PYTHON_VERSION"
140     printf "\t%-30s %s\n" VERSION: "$VERSION"
141 }
142
143 verify_schema(){
144     echo "INFO: Verifying $release_file schema."
145     lftools schema verify "$release_file" "$RELEASE_SCHEMA"
146 }
147
148 verify_version(){
149     # Verify allowed patterns "v#.#.#" or "#.#.#" aka SemVer
150     echo "INFO: Verifying version string $VERSION"
151     allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
152     if [[ $VERSION =~ $allowed_version_regex ]]; then
153         echo "INFO: The version $VERSION is a valid semantic version"
154     else
155         echo "INFO: The version $VERSION is not a semantic valid version"
156         echo "INFO: Allowed versions are \"v#.#.#\" or \"#.#.#\" aka SemVer"
157         echo "INFO: See https://semver.org/ for more details on SemVer"
158         exit 1
159     fi
160 }
161
162 verify_version_match_release(){
163     wget -P /tmp "${LOGS_URL}/"console.log.gz
164     echo "INFO: Comparing version $VERSION with log snippet from maven-stage:"
165     if zgrep "Successfully uploaded" /tmp/console.log.gz | grep "$VERSION"; then
166         echo "INFO: version $VERSION matches maven-stage artifacts"
167     else
168         echo "ERROR: Defined version in release file does not match staging repo artifacts version to be released"
169         echo "       Please make sure maven-stage job selected as candidate and release version are correct"
170         exit 1
171     fi
172 }
173
174 # check prerequisites to detect mistakes in the release YAML file
175 verify_pypi_match_release(){
176     wget -q -P /tmp "${LOGS_URL}/"console.log.gz
177     echo "INFO: Searching for strings >$PYPI_PROJECT< and >$VERSION< in job log"
178     # pypi-upload.sh generates success message with file list
179     if zgrep -i "uploaded" /tmp/console.log.gz | grep "$PYPI_PROJECT" | grep "$VERSION" ; then
180         echo "INFO: found expected strings in job log"
181     else
182         echo "ERROR: failed to find expected strings in job log"
183         exit 1
184     fi
185 }
186
187 # sigul is only available on Centos
188 # TODO: write tag_github function
189 tag(){
190     # Import public signing key
191     gpg --import "$SIGNING_PUBKEY"
192     if git tag -v "$VERSION"; then
193         echo "OK: Repo already tagged $VERSION Continuting to release"
194     else
195         echo "INFO: Repo has not yet been tagged $VERSION"
196         git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
197         sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
198         echo "INFO: Showing latest signature for $PROJECT:"
199         echo "INFO: git tag -v $VERSION"
200         git tag -v "$VERSION"
201
202         ########## Merge Part ##############
203         if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
204             echo "INFO: Running merge, pushing tag"
205             gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
206             git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
207             git config user.name "$RELEASE_USERNAME"
208             git config user.email "$RELEASE_EMAIL"
209             echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
210             chmod 600 ~/.ssh/config
211             git push origin "$VERSION"
212         fi
213     fi
214 }
215
216 nexus_release(){
217     for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
218         # extract the domain name from URL
219         NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
220         echo "INFO: NEXUS_URL: $NEXUS_URL"
221         # extract the staging repo from URL
222         STAGING_REPO=${staging_url#*repositories/}
223         echo "INFO: Running Nexus Verify"
224         lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
225         echo "INFO: Merge will run:"
226         echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
227     done
228
229     #Run the loop twice, to catch errors on either nexus repo
230     if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
231         for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
232           NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
233           STAGING_REPO=${staging_url#*repositories/}
234           echo "INFO: Promoting $STAGING_REPO on $NEXUS_URL."
235           lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
236         done
237     fi
238 }
239
240 container_release_file(){
241     echo "INFO: Processing container release"
242     local lfn_umbrella
243     lfn_umbrella="$(echo "$GERRIT_HOST" | awk -F"." '{print $2}')"
244
245     for namequoted in $(cat $release_file | yq '.containers[].name'); do
246         versionquoted=$(cat $release_file | yq ".containers[] |select(.name==$namequoted) |.version")
247
248         #Remove extra yaml quotes
249         name="${namequoted#\"}"
250         name="${name%\"}"
251         version="${versionquoted#\"}"
252         version="${version%\"}"
253
254         echo "$name"
255         echo "$version"
256         echo "INFO: Merge will release $name $version as $VERSION"
257         # Attempt to pull from releases registry to see if the image has been released.
258         if docker pull "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"; then
259             echo "OK: $VERSION is already released for image $name, Continuing..."
260         else
261             echo "OK: $VERSION not found in releases, release will be prepared. Continuing..."
262             docker pull "$CONTAINER_PULL_REGISTRY"/"$lfn_umbrella"/"$name":"$version"
263             container_image_id="$(docker images | grep $name | grep $version | awk '{print $3}')"
264             echo "INFO: Merge will run the following commands:"
265             echo "docker tag $container_image_id $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
266             echo "docker push $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
267             if [[ "$JOB_NAME" =~ "merge" ]]; then
268                 docker tag "$container_image_id" "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
269                 docker push "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
270             fi
271             echo "#########################"
272         fi
273     done
274
275     echo "INFO: Merge will tag ref: $ref"
276     git checkout "$ref"
277     tag
278 }
279
280 maven_release_file(){
281     echo "INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
282     wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
283     pushd "$PATCH_DIR"
284         echo "INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
285         wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
286         gunzip taglist.log.gz
287         cat "$PATCH_DIR"/taglist.log
288     popd
289     git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
290     git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
291     git merge --ff-only FETCH_HEAD
292     nexus_release
293     tag
294 }
295
296 # calls pip to download binary and source distributions from the specified index,
297 # which requires a recent-in-2019 version.  Uploads the files it received.
298 pypi_release_file(){
299     tgtdir=dist
300     mkdir $tgtdir
301     pip_pfx="pip download -d $tgtdir --no-deps --python-version $PYTHON_VERSION -i $PYPI_INDEX"
302     module="$PYPI_PROJECT==$VERSION"
303     pip_bin="$pip_pfx $module"
304     echo "INFO: downloading binary: $pip_bin"
305     if ! $pip_bin ; then
306         echo "WARN: failed to download binary distribution"
307     fi
308     pip_src="$pip_pfx --no-binary=:all: $module"
309     echo "INFO: downloading source: $pip_src"
310     if ! $pip_src ; then
311         echo "WARN: failed to download source distribution"
312     fi
313     echo "INFO: Checking files in $tgtdir"
314     filecount=$(ls $tgtdir | wc -l)
315     if [[ $filecount = 0 ]] ; then
316         echo "ERROR: no files downloaded"
317         exit 1
318     else
319         # shellcheck disable=SC2046
320         echo "INFO: downloaded $filecount distributions: " $(ls $tgtdir)
321     fi
322
323     if [[ ! "$JOB_NAME" =~ "merge" ]] ; then
324         echo "INFO: not a merge job, not uploading files"
325         return
326     fi
327
328     cmd="twine upload -r $REPOSITORY $tgtdir/*"
329     if $DRY_RUN; then
330         echo "INFO: dry-run is set, echoing command only"
331         echo "$cmd"
332     else
333         echo "INFO: uploading $filecount distributions to repo $REPOSITORY"
334         $cmd
335     fi
336     tag
337 }
338
339 packagecloud_verify(){
340     echo "INFO: ---> Verifying $1 exists in staging..."
341     if [[ $1 == $(curl --netrc-file ~/packagecloud_api --silent \
342         https://packagecloud.io/api/v1/repos/"$2"/search?q="$1" \
343         | yq -r .[].filename) ]]; then
344         echo "INFO: $1 exists in staging!"
345         echo "INFO: Existing package location: https://packagecloud.io$(curl \
346             --netrc-file ~/packagecloud_api --silent \
347             https://packagecloud.io/api/v1/repos/"$2"/search?q="$1" \
348             | yq -r .[].package_html_url)"
349     else
350         echo "ERROR: $package_name does not exist in staging"
351         exit 1
352     fi
353 }
354
355 packagecloud_promote(){
356     echo "INFO: ---> Preparing to promote $1..."
357     promote_url="https://packagecloud.io$(curl --netrc-file ~/packagecloud_api \
358         --silent https://packagecloud.io/api/v1/repos/"$2"/search?q="$1" \
359         | yq -r .[].promote_url)"
360     echo "INFO: Promoting $1 from staging to release"
361     curl --netrc-file ~/packagecloud_api -X POST -F \
362         destination=o-ran-sc/release "$promote_url" \
363         | echo "INFO: Promoted package location: \
364         https://packagecloud.io$(yq -r .package_html_url)"
365 }
366
367 ##############################  End Function Declarations  ################################
368
369 # Set common environment variables
370 set_variables_common
371
372 # Determine the type of release:
373 #   - container, release-container-schema.yaml
374 #   - maven, release-schema.yaml
375 #   - pypi,  release-pypi-schema.yaml
376
377 if [[ "$DISTRIBUTION_TYPE" == "maven" ]]; then
378     if $USE_RELEASE_FILE ; then
379         RELEASE_SCHEMA="release-schema.yaml"
380         echo "INFO: Fetching schema $RELEASE_SCHEMA"
381         wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
382         verify_schema
383     fi
384     set_variables_maven
385     verify_version
386     verify_version_match_release
387     maven_release_file
388 elif [[ "$DISTRIBUTION_TYPE" == "container" ]]; then
389     if $USE_RELEASE_FILE ; then
390         RELEASE_SCHEMA="release-container-schema.yaml"
391         echo "INFO: Fetching schema $RELEASE_SCHEMA"
392         wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${RELEASE_SCHEMA}
393         verify_schema
394     fi
395     set_variables_container
396     verify_version
397     container_release_file
398 elif [[ "$DISTRIBUTION_TYPE" == "pypi" ]]; then
399     if $USE_RELEASE_FILE ; then
400         RELEASE_SCHEMA="release-pypi-schema.yaml"
401         echo "INFO: Fetching schema $RELEASE_SCHEMA"
402         wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${RELEASE_SCHEMA}
403         verify_schema
404     fi
405     set_variables_pypi
406     verify_version
407     verify_pypi_match_release
408     pypi_release_file
409 elif [[ "$DISTRIBUTION_TYPE" == "packagecloud" ]]; then
410     RELEASE_SCHEMA="release-packagecloud-schema.yaml"
411     package_name=$(yq -r '.package_name' $release_file)
412     username_repo="o-ran-sc/staging"
413     echo "INFO: Fetching schema $RELEASE_SCHEMA"
414     wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${RELEASE_SCHEMA}
415     verify_schema
416     for name in $(yq -r '.package_name[].name' $release_file); do
417         package_name=$name
418         packagecloud_verify "$package_name" "$username_repo"
419         if [[ "$JOB_NAME" =~ "merge" ]] && ! $DRY_RUN; then
420             packagecloud_promote "$package_name" "$username_repo"
421         fi
422     done
423 else
424     echo "ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
425     exit 1
426 fi
427
428 echo "---> release-job.sh ends"