summaryrefslogtreecommitdiffstats
path: root/utils/calculate_version.sh
blob: 4bfd13f7a9090def124768977a696a8c0da75da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2016 Ericsson AB and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

# Calculates and generates the version tag for the OPNFV objects:
#     - Docker images
#     - ISOs
#     - Artifacts

info ()  {
    logger -s -t "Calculate_version.info" "$*"
}


error () {
    logger -s -t "Calculate_version.error" "$*"
    exit 1
}


#Functions which calculate the version
function docker_version() {
    docker_image=$1
    url_repo="https://registry.hub.docker.com/v2/repositories/${docker_image}/"
    url_tag="https://registry.hub.docker.com/v2/repositories/${docker_image}/tags/"
    status=$(curl -s --head -w %{http_code} ${url_repo} -o /dev/null)
    if [ "${status}" != "200" ]; then
        error "Cannot access ${url_repo}. Does the image ${docker_image} exist?"
    fi
    tag_json=$(curl $url_tag 2>/dev/null | python -mjson.tool | grep ${BASE_VERSION} | head -1)
    #e.g. tag_json= "name": "brahmaputra.0.2",
    if [ "${tag_json}" == "" ]; then
        error "The Docker Image ${docker_image} does not have a TAG with base version ${BASE_VERSION}"
    fi
    tag=$(echo $tag_json | awk '{print $2}' | sed 's/\,//' | sed 's/\"//g')
    #e.g.: tag=brahmaputra.0.2
    tag_current_version=$(echo $tag | sed 's/.*\.//')
    tag_new_version=$(($tag_current_version+1))
    #e.g.: tag=brahmaputra.0.3
    echo ${BASE_VERSION}.${tag_new_version}
}


function artifact_version() {
    # To be done
    error "Not supported yet..."
}


STORAGE_TYPES=(docker artifactrepo)
TYPE=""
NAME=""


usage="Calculates the version text of one of the following objects.

usage:
    bash $(basename "$0") [-h|--help] -t|--type docker|artifactrepo -n|--name <object_name>

where:
    -h|--help      show this help text
    -t|--type      specify the storage location
    -n|--name      name of the repository/object

examples:
    $(basename "$0") -t docker -n opnfv/functest
    $(basename "$0") -t artifactrepo -n fuel"




# Parse parameters
while [[ $# > 0 ]]
    do
    key="$1"
    case $key in
        -h|--help)
            echo "$usage"
            exit 0
            shift
        ;;
        -t|--type)
            TYPE="$2"
            shift
        ;;
        -n|--name)
            NAME="$2"
            shift
        ;;
        *)
            error "unknown option $1"
            exit 1
        ;;
    esac
    shift # past argument or value
done

if [ -z "$BASE_VERSION" ]; then
    error "Base version must be specified as environment variable. Ex.: export BASE_VERSION='brahmaputra.0'"
fi

if [ "${TYPE}" == "" ]; then
    error "Please specify the type of object to get the version from. $usage"
fi

if [ "${NAME}" == "" ]; then
    error "Please specify the name for the given storage type. $usage"
fi

not_in=1
for i in "${STORAGE_TYPES[@]}"; do
    if [[ "${TYPE}" == "$i" ]]; then
        not_in=0
    fi
done
if [ ${not_in} == 1 ]; then
    error "Unknown type: ${TYPE}. Available storage types are: [${STORAGE_TYPES[@]}]"
fi


#info "Calculating version for object '${TYPE}' with arguments '${INFO}'..."
if [ "${TYPE}" == "docker" ]; then
    docker_version $NAME

elif [ "${TYPE}" == "artifactrepo" ]; then
    artifact_version $NAME
fi