File: os.query

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (286 lines) | stat: -rwxr-xr-x 6,903 bytes parent folder | download
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
284
285
286
#!/bin/bash

show_help() {
    echo "usage: os.query is-core, is-classic"
    echo "       os.query is-core16, is-core18, is-core20, is-core22, is-core24"
    echo "       os.query is-core-gt, is-core-ge, is-core-lt, is-core-le"
    echo "       os.query is-trusty, is-xenial, is-bionic, is-focal, is-jammy, is-noble"
    echo "       os.query is-ubuntu [ID], is-debian [ID], is-fedora [ID], is-amazon-linux [ID], is-arch-linux, is-centos [ID], is-opensuse [ID]"
    echo "       os.query is-ubuntu-gt [ID], is-ubuntu-ge [ID], is-ubuntu-lt [ID], is-ubuntu-le [ID]"
    echo "       os.query is-pc-amd64, is-pc-i386, is-arm, is-armhf, is-arm64, is-s390x"
    echo ""
    echo "Get general information about the current system"
}

is_core() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release
}

is_core16() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="16"' /etc/os-release
}

is_core18() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="18"' /etc/os-release
}

is_core20() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="20"' /etc/os-release
}

is_core22() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="22"' /etc/os-release
}

is_core24() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="24"' /etc/os-release
}

is_core26() {
    grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="26"' /etc/os-release
}

is_core_gt() {
    local VERSION=$1
    if [ -z "$VERSION" ]; then
        echo "os.query: version id is expected"
        exit 1
    fi

    is_core && compare_ubuntu "$VERSION" "-gt"
}

is_core_ge() {
    local VERSION=$1
    if [ -z "$VERSION" ]; then
        echo "os.query: version id is expected"
        exit 1
    fi

    is_core && compare_ubuntu "$VERSION" "-ge"
}

is_core_lt() {
    local VERSION=$1
    if [ -z "$VERSION" ]; then
        echo "os.query: version id is expected"
        exit 1
    fi

    is_core && compare_ubuntu "$VERSION" "-lt"
}

is_core_le() {
    local VERSION=$1
    if [ -z "$VERSION" ]; then
        echo "os.query: version id is expected"
        exit 1
    fi

    is_core && compare_ubuntu "$VERSION" "-le"
}

is_classic() {
    ! is_core
}

is_trusty() {
    grep -qFx 'ID=ubuntu' /etc/os-release && grep -qFx 'VERSION_ID="14.04"' /etc/os-release
}

is_xenial() {
    grep -qFx 'UBUNTU_CODENAME=xenial' /etc/os-release
}

is_bionic() {
    grep -qFx 'UBUNTU_CODENAME=bionic' /etc/os-release
}

is_focal() {
    grep -qFx 'UBUNTU_CODENAME=focal' /etc/os-release
}

is_jammy() {
    grep -qFx 'UBUNTU_CODENAME=jammy' /etc/os-release
}

is_noble() {
    grep -qFx 'UBUNTU_CODENAME=noble' /etc/os-release
}

is_ubuntu() {
    VERSION=$1
    if [ -z "$VERSION" ]; then
        grep -qFx 'ID=ubuntu' /etc/os-release || grep -qFx 'ID=ubuntu-core' /etc/os-release
    else
        grep -qFx 'ID=ubuntu' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release
    fi
}

is_ubuntu_gt() {
    is_classic && compare_ubuntu "${1:-}" "-gt"
}

is_ubuntu_ge() {
    is_classic && compare_ubuntu "${1:-}" "-ge"
}

is_ubuntu_lt() {
    is_classic && compare_ubuntu "${1:-}" "-lt"
}

is_ubuntu_le() {
    is_classic && compare_ubuntu "${1:-}" "-le"
}

compare_ubuntu() {
    VERSION=$1
    OPERAND=$2

    if [ -z "$VERSION" ]; then
        echo "os.query: version id is expected"
        exit 1
    fi

    if ! grep -q 'ID=ubuntu' /etc/os-release; then
        echo "os.query: comparing non ubuntu system"
        return 1
    fi

    NUM_RE='^[0-9]+$'
    NUM_VERSION="$(echo "$VERSION" | tr -d '".')"
    if ! [[ $NUM_VERSION =~ $NUM_RE ]] ; then
       echo "os.query: invalid version format \"$VERSION\" provided"
       exit 1
    fi

    SYS_VERSION="$(grep 'VERSION_ID' /etc/os-release)"
    SYS_VERSION="$(echo "${SYS_VERSION#*=}" | tr -d '".')"
    if ! [[ $SYS_VERSION =~ $NUM_RE ]] ; then
       echo "os.query: invalid version format \"$SYS_VERSION\" retrieved from system"
       exit 1
    fi

    test "$SYS_VERSION" "$OPERAND" "$NUM_VERSION"
}

is_debian() {
    VERSION=$1
    if [ -z "$VERSION" ]; then
        grep -qFx 'ID=debian' /etc/os-release
    elif [ "$VERSION" == "sid" ]; then
        if [ -n "$SPREAD_SYSTEM" ]; then
            [[ "$SPREAD_SYSTEM" == debian-sid-* ]]
        else
            grep -qFx 'ID=debian' /etc/os-release && grep -qE '^PRETTY_NAME=.*/sid"$' /etc/os-release
        fi
    else
        grep -qFx 'ID=debian' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release
    fi
}

is_fedora() {
    VERSION=$1
    if [ -z "$VERSION" ]; then
        grep -qFx 'ID=fedora' /etc/os-release
    elif [ "$VERSION" == "rawhide" ]; then
        if [ -n "$SPREAD_SYSTEM" ]; then
            [[ "$SPREAD_SYSTEM" == fedora-rawhide-* ]]
        else
            grep -qFx 'ID=fedora' /etc/os-release && grep -qFx "REDHAT_BUGZILLA_PRODUCT_VERSION=rawhide" /etc/os-release
        fi
    else
        grep -qFx 'ID=fedora' /etc/os-release && grep -qFx "VERSION_ID=$VERSION" /etc/os-release
    fi
}

is_amazon_linux() {
    VERSION=$1
    if [ -z "$VERSION" ]; then
        grep -qFx 'ID="amzn"' /etc/os-release
    else
        grep -qFx 'ID="amzn"' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release
    fi
}

is_centos() {
    VERSION=$1
    if [ -z "$VERSION" ]; then
        grep -qFx 'ID="centos"' /etc/os-release
    else
        grep -qFx 'ID="centos"' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release
    fi
}

is_arch_linux() {
    grep -qFx 'ID=arch' /etc/os-release
}

is_opensuse() {
    VERSION=$1
    if [ -z "$VERSION" ]; then
        grep -qFx 'ID="opensuse-leap"' /etc/os-release || grep -qFx 'ID="opensuse-tumbleweed"' /etc/os-release
    elif [ "$VERSION" == "tumbleweed" ]; then
        grep -qFx 'ID="opensuse-tumbleweed"' /etc/os-release
    else
        grep -qFx 'ID="opensuse-leap"' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release
    fi
}

is_pc_amd64() {
    uname -m | grep -qFx 'x86_64'
}

is_pc_i386() {
    uname -m | grep -Eq '(i686|i386)'
}

is_arm() {
    uname -m | grep -Eq '(^arm.*|^aarch*)'
}

is_armhf() {
    uname -m | grep -qx 'armv7.*'
}

is_arm64() {
    uname -m | grep -Eq '(aarch64.*|armv8.*)'
}

is_s390x() {
    uname -m | grep -qFx 's390x'
}


main() {
    if [ $# -eq 0 ]; then
        show_help
        exit 0
    fi

    local subcommand="$1"
    local action=
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)
                show_help
                exit 0
                ;;
            *)
                action=$(echo "$subcommand" | tr '-' '_')
                shift
                break
                ;;
        esac
    done

    if [ -z "$(declare -f "$action")" ]; then
        echo "os.query: no such command: $subcommand" >&2
        show_help
        exit 1
    fi

    "$action" "$@"
}

main "$@"