summaryrefslogtreecommitdiffstats
path: root/jjb/kuberef/kuberef-run-linting.sh
blob: 084eac91caea5ded80b73c0b1d3af31c37dab673 (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
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2020 Samsung Electronics
# 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
##############################################################################

set -o nounset
set -o pipefail
set -o xtrace

# _vercmp() - Function that compares two versions
function _vercmp {
    local v1=$1
    local op=$2
    local v2=$3
    local result

    # sort the two numbers with sort's "-V" argument.  Based on if v2
    # swapped places with v1, we can determine ordering.
    result=$(echo -e "$v1\n$v2" | sort -V | head -1)

    case $op in
        "==")
            [ "$v1" = "$v2" ]
            return
            ;;
        ">")
            [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
            return
            ;;
        "<")
            [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
            return
            ;;
        ">=")
            [ "$result" = "$v2" ]
            return
            ;;
        "<=")
            [ "$result" = "$v1" ]
            return
            ;;
        *)
            die $LINENO "unrecognised op: $op"
            ;;
    esac
}

echo "Requirements validation"
# shellcheck disable=SC1091
source /etc/os-release || source /usr/lib/os-release

min_shellcheck_version=0.4.0
min_tox_version=3.5

pkgs=""
if ! command -v shellcheck; then
    case ${ID,,} in
        *suse*|rhel|centos|fedora)
            pkgs="ShellCheck"
        ;;
        ubuntu|debian)
            pkgs="shellcheck"
        ;;
    esac
fi
if ! command -v pip; then
    case ${ID,,} in
        *suse*|rhel|centos|fedora)
            pkgs+=" python3-pip python3-setuptools"
        ;;
        ubuntu|debian)
            if _vercmp "${VERSION_ID}" '<=' "18.04"; then
                pkgs+=" python-pip python-setuptools"
            else
                pkgs+=" python3-pip python3-setuptools"
            fi
        ;;
    esac
fi

if [ -n "$pkgs" ]; then
    echo "Requirements installation"
    case ${ID,,} in
        *suse*)
            sudo zypper install --gpg-auto-import-keys refresh
            # shellcheck disable=SC2086
            sudo -H -E zypper install -y --no-recommends $pkgs
        ;;
        ubuntu|debian)
            sudo apt-get update
            # shellcheck disable=SC2086
            sudo -H -E apt-get -y --no-install-recommends install $pkgs
        ;;
        rhel|centos|fedora)
            PKG_MANAGER=$(command -v dnf || command -v yum)
            if ! sudo "$PKG_MANAGER" repolist | grep "epel/"; then
                sudo -H -E "$PKG_MANAGER" -q -y install epel-release
            fi
            sudo "$PKG_MANAGER" updateinfo --assumeyes
            # shellcheck disable=SC2086
            sudo -H -E "$PKG_MANAGER" -y install $pkgs
        ;;
    esac
    if ! command -v pip && command -v pip3 ; then
        sudo ln -s "$(command -v pip3)" /usr/bin/pip
    fi
    sudo "$(command -v pip)" install --upgrade pip
fi

if ! command -v tox || _vercmp "$(tox --version | awk '{print $1}')" '<' "$min_tox_version"; then
    sudo "$(command -v pip)" install tox==$min_tox_version
fi

echo "Server tools information:"
python -V
tox --version
shellcheck -V

echo "Linting process execution"
tox -e lint
if _vercmp "$(shellcheck --version | awk 'FNR==2{print $2}')" '<' "$min_shellcheck_version"; then
    bash -c 'shopt -s globstar; shellcheck **/*.sh'
else
    bash -c 'shopt -s globstar; shellcheck -x **/*.sh'
fi