dovetail docker job added
[releng.git] / utils / calculate_version.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Ericsson AB and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 # Calculates and generates the version tag for the OPNFV objects:
12 #     - Docker images
13 #     - ISOs
14 #     - Artifacts
15
16 info ()  {
17     logger -s -t "Calculate_version.info" "$*"
18 }
19
20
21 error () {
22     logger -s -t "Calculate_version.error" "$*"
23     exit 1
24 }
25
26
27 #Functions which calculate the version
28 function docker_version() {
29     docker_image=$1
30     url_repo="https://registry.hub.docker.com/v2/repositories/${docker_image}/"
31     url_tag="https://registry.hub.docker.com/v2/repositories/${docker_image}/tags/"
32     status=$(curl -s --head -w %{http_code} ${url_repo} -o /dev/null)
33     if [ "${status}" != "200" ]; then
34         error "Cannot access ${url_repo}. Does the image ${docker_image} exist?"
35     fi
36     tag_json=$(curl $url_tag 2>/dev/null | python -mjson.tool | grep ${BASE_VERSION} | head -1)
37     #e.g. tag_json= "name": "brahmaputra.0.2",
38     #special case, for dovetail, not sync with release, tag_json name not headed with arno, etc
39     if [ "${tag_json}" == "" ]; then
40         echo ${BASE_VERSION}.0
41     else
42         tag=$(echo $tag_json | awk '{print $2}' | sed 's/\,//' | sed 's/\"//g')
43         #e.g.: tag=brahmaputra.0.2
44         #special case, for dovetail, not sync with release
45         tag_current_version=$(echo $tag | sed 's/.*\.//')
46         tag_new_version=$(($tag_current_version+1))
47         #e.g.: tag=brahmaputra.0.3
48         echo ${BASE_VERSION}.${tag_new_version}
49     fi
50 }
51
52
53 function artifact_version() {
54     # To be done
55     error "Not supported yet..."
56 }
57
58
59 STORAGE_TYPES=(docker artifactrepo)
60 TYPE=""
61 NAME=""
62
63
64 usage="Calculates the version text of one of the following objects.
65
66 usage:
67     bash $(basename "$0") [-h|--help] -t|--type docker|artifactrepo -n|--name <object_name>
68
69 where:
70     -h|--help      show this help text
71     -t|--type      specify the storage location
72     -n|--name      name of the repository/object
73
74 examples:
75     $(basename "$0") -t docker -n opnfv/functest
76     $(basename "$0") -t artifactrepo -n fuel"
77
78
79
80
81 # Parse parameters
82 while [[ $# > 0 ]]
83     do
84     key="$1"
85     case $key in
86         -h|--help)
87             echo "$usage"
88             exit 0
89             shift
90         ;;
91         -t|--type)
92             TYPE="$2"
93             shift
94         ;;
95         -n|--name)
96             NAME="$2"
97             shift
98         ;;
99         *)
100             error "unknown option $1"
101             exit 1
102         ;;
103     esac
104     shift # past argument or value
105 done
106
107 if [ -z "$BASE_VERSION" ]; then
108     error "Base version must be specified as environment variable. Ex.: export BASE_VERSION='brahmaputra.0'"
109 fi
110
111 if [ "${TYPE}" == "" ]; then
112     error "Please specify the type of object to get the version from. $usage"
113 fi
114
115 if [ "${NAME}" == "" ]; then
116     error "Please specify the name for the given storage type. $usage"
117 fi
118
119 not_in=1
120 for i in "${STORAGE_TYPES[@]}"; do
121     if [[ "${TYPE}" == "$i" ]]; then
122         not_in=0
123     fi
124 done
125 if [ ${not_in} == 1 ]; then
126     error "Unknown type: ${TYPE}. Available storage types are: [${STORAGE_TYPES[@]}]"
127 fi
128
129
130 #info "Calculating version for object '${TYPE}' with arguments '${INFO}'..."
131 if [ "${TYPE}" == "docker" ]; then
132     docker_version $NAME
133
134 elif [ "${TYPE}" == "artifactrepo" ]; then
135     artifact_version $NAME
136 fi