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