summaryrefslogtreecommitdiffstats
path: root/jjb/ci-management/packagecloud-repo-cleanup.sh
blob: 8fdfe1187d800683f118a146a6e372dfb2e78fd1 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
set -euo pipefail

# Number of packages to keep.
N_PACKAGES=5

PACKAGECLOUD_REPO_DEB="https://packagecloud.io/install/repositories/fdio/${STREAM}/script.deb.sh"
PACKAGECLOUD_REPO_RPM="https://packagecloud.io/install/repositories/fdio/${STREAM}/script.rpm.sh"

FACTER_OS=$(/usr/bin/facter operatingsystem)
PACKAGE_LIST=""
VERSION_REGEX="[0-9]+.[0-9]+[-_][0-9]+[-_]release(-1)?|[0-9]+.[0-9]+[-_][0-9]+~g[[:graph:]]+"

declare -A FUNCTIONS

echo_err () {
    >&2 echo ${@}
    exit 1
}

contains() {
    [[ ${1} =~ (^|[[:space:]])${2}($|[[:space:]]) ]] && return 1 || return 0
}

check_version_whitelist() {
    if [[ ${1} =~ ([0-9]+).([0-9]+)[-_]([0-9]+).+ ]]; then
        MAJOR=${BASH_REMATCH[1]}
        MINOR=${BASH_REMATCH[2]}
        REVISION=${BASH_REMATCH[3]}
        VER="${MAJOR}.${MINOR}-${REVISION}"

        if contains "${VERSION_WHITELIST}" ${VER}; then
            return 1;
        fi
    fi

    return 0
}

# Params
# $1: Package list
build_package_blacklist_ubuntu () {
    PACKAGE_LIST=${@}
    OUTPUT_LIST=""
    ARCH=$(dpkg --print-architecture)

    for package in ${PACKAGE_LIST}; do
        OUTPUT=$(apt-cache policy ${package} 2> /dev/null)

        if [[ ${?} -ne 0 || -z "${OUTPUT}" ]]; then
            continue
        fi

        # N_PACKAGES + 2 is justified by the fact thathe output of apt-cache policy is in the form:
        # apt-cache policy libhicn
        # libhicn:
        #   Installed: (none)
        #   Candidate: 20.05-11-release
        #   Version table:
        #      20.05-11-release 500
        #
        # The first version candidate is printed twice.
        # So we need to exclude the "Candidate: 20.05-11-release" (+1).
        # Also `tail -n +M` starts to print from the Mth line, being 1 the first line.
        # So to exclude the Mth line an additional +1 is needed.
        VERSIONS="$(echo ${OUTPUT} | grep -E -o ${VERSION_REGEX} | tail -n +$((N_PACKAGES + 2)))"

        for version in ${VERSIONS}; do
            if ! check_version_whitelist ${version}; then
                OUTPUT_LIST+="${package}_${version}_${ARCH}.deb "
            fi
        done
    done

    echo ${OUTPUT_LIST}
}

# Params
# $1: Package list
build_package_blacklist_centos () {
    PACKAGE_LIST=${@}
    OUTPUT_LIST=""
    VERSIONS=""
    ARCH=$(uname -m)

    for package in ${PACKAGE_LIST}; do
        OUTPUT=$(yum --showduplicates list ${package} 2> /dev/null)
        if [[ ${?} -ne 0 || -z "${OUTPUT}" ]]; then
            continue
        fi

        VERSIONS="$(echo ${OUTPUT} | grep -Eo "${VERSION_REGEX}" | head -n -${N_PACKAGES})"

        for version in ${VERSIONS}; do
            if ! check_version_whitelist ${version}; then
                OUTPUT_LIST+="${package}-${version}.${ARCH}.rpm "
            fi
        done
    done

    echo ${OUTPUT_LIST}
}

promote_attic_repo_centos () {
    FACTER_OSMAJREL=$(/usr/bin/facter operatingsystemmajrelease)
    FACTER_ARCH=$(/usr/bin/facter architecture)

    for package in ${@}; do
        echo package_cloud promote \
            ${PCIO_CO}/${STREAM}/el/${FACTER_OSMAJREL}/os/${FACTER_ARCH}/ \
            ${package} ${PCIO_CO}/attic/el/${FACTER_OSMAJREL}/os/${FACTER_ARCH}/
        package_cloud promote \
            ${PCIO_CO}/${STREAM}/el/${FACTER_OSMAJREL}/os/${FACTER_ARCH}/ \
            ${package} ${PCIO_CO}/attic/el/${FACTER_OSMAJREL}/os/${FACTER_ARCH}/
    done
}

promote_attic_repo_ubuntu () {
    FACTER_LSBNAME=$(/usr/bin/facter lsbdistcodename)

    for package in ${@}; do
        echo package_cloud promote ${PCIO_CO}/${STREAM}/ubuntu/${FACTER_LSBNAME}/main/ \
            ${package} ${PCIO_CO}/attic/ubuntu/${FACTER_LSBNAME}/main/
        package_cloud promote ${PCIO_CO}/${STREAM}/ubuntu/${FACTER_LSBNAME}/main/ \
            ${package} ${PCIO_CO}/attic/ubuntu/${FACTER_LSBNAME}/main/
    done
}

promote_to_attic_repo () {
    ${FUNCTIONS["promote_attic_repo"]} ${@}
}

setup_fdio_repo () {
    case "${FACTER_OS}" in
      Ubuntu)
        curl -s ${PACKAGECLOUD_REPO_DEB} | sudo bash
        FUNCTIONS["package_blacklist"]="build_package_blacklist_ubuntu"
        FUNCTIONS["promote_attic_repo"]="promote_attic_repo_ubuntu"
        PACKAGE_LIST="${PACKAGE_LIST_COMMON} ${PACKAGE_LIST_UBUNTU}"
      ;;
      CentOS)
        curl -s ${PACKAGECLOUD_REPO_RPM} | sudo bash
        FUNCTIONS["package_blacklist"]="build_package_blacklist_centos"
        FUNCTIONS["promote_attic_repo"]="promote_attic_repo_centos"
        PACKAGE_LIST="${PACKAGE_LIST_COMMON} ${PACKAGE_LIST_CENTOS}"
      ;;
      *)
        echo_err "Distribution ${FACTER_OS} is not supported."
      ;;
    esac
}

# Params
# $1: Package list
build_package_blacklist () {
    ${FUNCTIONS["package_blacklist"]} ${@}
}

setup_fdio_repo
PACKAGES_TO_PROMOTE=$(build_package_blacklist ${PACKAGE_LIST})
promote_to_attic_repo ${PACKAGES_TO_PROMOTE}