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