608a3bd7331c7c2437e1e0f012539d13b5fa36a5
[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     if [ "${tag_json}" == "" ]; then
39         echo ${BASE_VERSION}.0
40     else
41         tag=$(echo $tag_json | awk '{print $2}' | sed 's/\,//' | sed 's/\"//g')
42         #e.g.: tag=brahmaputra.0.2
43         tag_current_version=$(echo $tag | sed 's/.*\.//')
44         tag_new_version=$(($tag_current_version+1))
45         #e.g.: tag=brahmaputra.0.3
46         echo ${BASE_VERSION}.${tag_new_version}
47     fi
48 }
49
50
51 function artifact_version() {
52     # To be done
53     error "Not supported yet..."
54 }
55
56
57 STORAGE_TYPES=(docker artifactrepo)
58 TYPE=""
59 NAME=""
60
61
62 usage="Calculates the version text of one of the following objects.
63
64 usage:
65     bash $(basename "$0") [-h|--help] -t|--type docker|artifactrepo -n|--name <object_name>
66
67 where:
68     -h|--help      show this help text
69     -t|--type      specify the storage location
70     -n|--name      name of the repository/object
71
72 examples:
73     $(basename "$0") -t docker -n opnfv/functest
74     $(basename "$0") -t artifactrepo -n fuel"
75
76
77
78
79 # Parse parameters
80 while [[ $# > 0 ]]
81     do
82     key="$1"
83     case $key in
84         -h|--help)
85             echo "$usage"
86             exit 0
87             shift
88         ;;
89         -t|--type)
90             TYPE="$2"
91             shift
92         ;;
93         -n|--name)
94             NAME="$2"
95             shift
96         ;;
97         *)
98             error "unknown option $1"
99             exit 1
100         ;;
101     esac
102     shift # past argument or value
103 done
104
105 if [ -z "$BASE_VERSION" ]; then
106     error "Base version must be specified as environment variable. Ex.: export BASE_VERSION='brahmaputra.0'"
107 fi
108
109 if [ "${TYPE}" == "" ]; then
110     error "Please specify the type of object to get the version from. $usage"
111 fi
112
113 if [ "${NAME}" == "" ]; then
114     error "Please specify the name for the given storage type. $usage"
115 fi
116
117 not_in=1
118 for i in "${STORAGE_TYPES[@]}"; do
119     if [[ "${TYPE}" == "$i" ]]; then
120         not_in=0
121     fi
122 done
123 if [ ${not_in} == 1 ]; then
124     error "Unknown type: ${TYPE}. Available storage types are: [${STORAGE_TYPES[@]}]"
125 fi
126
127
128 #info "Calculating version for object '${TYPE}' with arguments '${INFO}'..."
129 if [ "${TYPE}" == "docker" ]; then
130     docker_version $NAME
131
132 elif [ "${TYPE}" == "artifactrepo" ]; then
133     artifact_version $NAME
134 fi