File: extract-native-package-install-steps.sh

package info (click to toggle)
snoopy 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,988 kB
  • sloc: ansic: 7,167; sh: 4,514; makefile: 1,095
file content (283 lines) | stat: -rwxr-xr-x 8,106 bytes parent folder | download | duplicates (2)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash



### Shell configuration and script bootstrap
#
set -e
set -u
set -o pipefail
. `dirname $0`/../_bootstrap.sh



### Define the help method
#
_showHelp()
{
    cat <<EOF
Purpose:

    Extract Snoopy native package installation steps from doc/install-from-repo.md

Supported CLI arguments:

    -a          Adjust install commands to run in a non-interactive mode (i.e. in GitHub Actions)
    -c          Same as -c, but get the version from ./dev-tools/libexec/get-release-version.sh
    -C VERSION  Check installed Snoopy version (only applicable in combination with -e flag)
    -d DIST     Distribution to extract steps for [default: use host system distribution]
    -e          Execute extracted steps too
    -p          Same as -p, but get the package version from ./dev-tools/build-native-package -v
    -P VERSION  Check installed Snoopy _package_ version (only applicable in combination with -e flag)
    -P
    -r          Switch repository URL to "raw":
                - From: https://a2o.github.io/snoopy-packages/repo/...
                - To:   https://raw.githubusercontent.com/a2o/snoopy-packages/master/repo/...
                The purpose of this switch is workflow-based package release testing - GitHub Pages
                sometimes take a bit to get refreshed.
    -t          Switch from 'stable' to 'testing' repositories
    -v VERSION  Distribution version to extract steps for [default: use host system distribution version]

    -h/--help   Show this help.

Usage:

    Just show the steps for a distribution running this script:
        $0

    Extract and execute:
        $0 -e

EOF
}

_showHelpAndExit()
{
    _showHelp
    exit
}



### Parse the CLI arguments
#
if [[ $@ =~ [-][-]help ]]; then
    _showHelpAndExit
fi

MODE="extract-only"
CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION=""
CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION=""
OS_ID_OVERRIDE=""
OS_VERSION_OVERRIDE=""
REPO_URL_SWITCH_TO_RAW="false"
REPO_CHANNEL_SWITCH_TO_TESTING="false"
ADJUST_COMMANDS_TO_NON_INTERACTIVE=false

while getopts ":acC:d:epP:rtv:h" opt; do
    case "$opt" in
        a)
            ADJUST_COMMANDS_TO_NON_INTERACTIVE="true"
            ;;

        c)
            CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION=`./dev-tools/libexec/get-release-version.sh`
            ;;

        C)
            CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION="$OPTARG"
            ;;

        d)
            OS_ID_OVERRIDE="$OPTARG"
            ;;

        e)
            MODE="extract-and-execute"
            ;;

        p)
            CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION="get-it-from-script"
            ;;

        P)
            CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION="$OPTARG"
            ;;

        r)
            REPO_URL_SWITCH_TO_RAW="true"
            ;;

        t)
            REPO_CHANNEL_SWITCH_TO_TESTING="true"
            ;;

        v)
            OS_VERSION_OVERRIDE="$OPTARG"
            ;;

        h)
            _showHelpAndExit
            ;;

        ?)
            _fatalError "Unsupported argument: '-$OPTARG'. Run '$0 -h' to list supported arguments." $LINENO
            ;;

        *)
            _fatalError "Internal error (opt=$opt)" $LINENO
            ;;
    esac
done

# "Format" the expected package version
if [ "$CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION" == "get-it-from-script" ]; then
    if [ "$CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION" != "" ]; then
        CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION=`./dev-tools/build-native-package.sh -v -V $CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION`
    else
        CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION=`./dev-tools/build-native-package.sh -v`
    fi
fi



### Detect operating system, or use the overrides
#
OS_ID=""
OS_VERSION=""
_detectOperatingSystem

if [ "$OS_ID_OVERRIDE" != "" ]; then
    _echo "Using OS id override: $OS_ID_OVERRIDE"
    OS_ID="$OS_ID_OVERRIDE"
fi

if [ "$OS_VERSION_OVERRIDE" != "" ]; then
    _echo "Using OS version override: $OS_VERSION_OVERRIDE"
    OS_VERSION="$OS_VERSION_OVERRIDE"
fi



### Generate search key
#
if [ "$OS_VERSION" == "" ]; then
    SEARCH_KEY="$OS_ID"
else
    SEARCH_KEY="$OS_ID-$OS_VERSION"
fi
_echo "Using OS search key: $SEARCH_KEY"



### Extract the install steps
#
SEARCH_STRING="[search-key:$SEARCH_KEY]:"
SEARCH_FILE="doc/install-from-repo.md"
if ! grep -F "$SEARCH_STRING" "$SEARCH_FILE" > /dev/null ; then
    _fatalError "Unable to find package installation steps for string '$SEARCH_STRING' in file $SEARCH_FILE."
fi

INSTALL_STEPS=`cat $SEARCH_FILE | grep -F "$SEARCH_STRING" -A20 | sed -e '1,/^\`\`\`shell$/d' | sed -e '/^\`\`\`$/,$d' | sed -e 's/^sudo //'`
if [ "$INSTALL_STEPS" == "" ]; then
    _fatalError "Unable to extract package installation steps for search key '$SEARCH_KEY' from file $SEARCH_FILE."
fi



### Adjust if requested
#
if [ "$REPO_URL_SWITCH_TO_RAW" == "true" ]; then
    _echo "NOTICE: Switching repository URLs from GitHub Pages to raw.githubusercontent..."
    INSTALL_STEPS=`echo "$INSTALL_STEPS" | sed -e 's#https://a2o.github.io/snoopy-packages/repo/#https://raw.githubusercontent.com/a2o/snoopy-packages/master/repo/#g'`
fi

if [ "$REPO_CHANNEL_SWITCH_TO_TESTING" == "true" ]; then
    _echo "NOTICE: Switching instructions from 'stable' to 'testing'..."
    INSTALL_STEPS=`echo "$INSTALL_STEPS" | sed -e 's/stable/testing/g'`
fi



### Adjust to non-interactive
#
if [ "$ADJUST_COMMANDS_TO_NON_INTERACTIVE" == "true" ]; then
    _echo "NOTICE: Adjusting steps to non-interactive mode..."
    INSTALL_STEPS=`echo "$INSTALL_STEPS" \
    | sed -e 's/^apt install snoopy/DEBIAN_FRONTEND=noninteractive apt install -y snoopy/' \
    | sed -e 's/^pacman -Sy snoopy/pacman -Sy --noconfirm snoopy/' \
    | sed -e 's/^yum install snoopy/yum install -y snoopy/' \
    | sed -e 's/^zypper install snoopy/zypper -n install snoopy/'`
fi



### Show extracted steps
#
echo "Extracted package installation steps ('sudo' was removed, not needed for CI):"
echo "============================================================================="
echo "$INSTALL_STEPS"
echo "============================================================================="



### Run the install steps if requested
#
if [ "$MODE" == "extract-and-execute" ]; then
    _echo "Executing extracted install steps:"
    eval "$INSTALL_STEPS"
fi



### Run the Snoopy library version check
#
if [ "$CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION" != "" ]; then
    INSTALLED_SNOOPY_LIBRARY_VERSION=`snoopyctl version | grep '^Snoopy library version:' | awk '{print $4}'`
    if [ "$CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION" != "$INSTALLED_SNOOPY_LIBRARY_VERSION" ]; then
        _fatalError "Installed Snoopy library version is not the same as expected (expected=$CHECK_INSTALLED_SNOOPY_LIBRARY_VERSION, installed=$INSTALLED_SNOOPY_LIBRARY_VERSION)"
    fi
fi

### FUNCTION: Get installed package's version
#
# Usage:
#       INSTALLED_PACKAGE_VERSION=""
#       _getInstalledPackageVersion "your-package-name-here"
#
_getInstalledPackageVersion() {
    PACKAGE_NAME="$1"

    OS_ID=""
    _detectOperatingSystem

    INSTALLED_PACKAGE_VERSION=""
    case "$OS_ID" in
        arch)
            INSTALLED_PACKAGE_VERSION=`pacman -Q "$PACKAGE_NAME" | awk '{print $2}'`
            ;;

        debian|ubuntu)
            INSTALLED_PACKAGE_VERSION=`dpkg-query --showformat='${Version}' --show "$PACKAGE_NAME"`
            ;;

        rhel|fedora|centos|almalinux|\
        sles|opensuse-leap|opensuse-tumbleweed)
            INSTALLED_PACKAGE_VERSION=`rpm -q "$PACKAGE_NAME" --qf '%{VERSION}-%{RELEASE}'`
            ;;

        *)
            _fatalError "Unsupported OS: '$OS_ID'. Install Snoopy from source instead."
            ;;
    esac
}

### Run the Snoopy package version check
#
if [ "$CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION" != "" ]; then
    INSTALLED_PACKAGE_VERSION=""
    _getInstalledPackageVersion "snoopy"
    if [ "$CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION" != "$INSTALLED_PACKAGE_VERSION" ]; then
        _fatalError "Installed Snoopy package version is not the same as expected (expected=$CHECK_INSTALLED_SNOOPY_PACKAGE_VERSION, installed=$INSTALLED_PACKAGE_VERSION)"
    fi
fi