github.com/canonical/ubuntu-image@v0.0.0-20240430122802-2202fe98b290/tests/lib/external/snapd-testing-tools/tools/os.query (about) 1 #!/bin/bash 2 3 show_help() { 4 echo "usage: os.query is-core, is-classic" 5 echo " os.query is-core16, is-core18, is-core20, is-core22, is-core24" 6 echo " os.query is-core-gt, is-core-ge, is-core-lt, is-core-le" 7 echo " os.query is-trusty, is-xenial, is-bionic, is-focal, is-jammy" 8 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]" 9 echo " os.query is-ubuntu-gt [ID], is-ubuntu-ge [ID], is-ubuntu-lt [ID], is-ubuntu-le [ID]" 10 echo " os.query is-pc-amd64, is-pc-i386, is-arm, is-armhf, is-arm64, is-s390x" 11 echo "" 12 echo "Get general information about the current system" 13 } 14 15 is_core() { 16 # We need to check $SPREAD_SYSTEM var because in snapd the os-release file does 17 # not contain the ubuntu-core info while the system is being prepared 18 if [ -n "$SPREAD_SYSTEM" ]; then 19 [[ "$SPREAD_SYSTEM" == ubuntu-core-* ]] 20 else 21 grep -qFx 'ID=ubuntu-core' /etc/os-release 22 fi 23 } 24 25 is_core16() { 26 # We need to check $SPREAD_SYSTEM var because in snapd the os-release file does 27 # not contain the ubuntu-core info while the system is being prepared 28 if [ -n "$SPREAD_SYSTEM" ]; then 29 [[ "$SPREAD_SYSTEM" == ubuntu-core-16-* ]] 30 else 31 grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="16"' /etc/os-release 32 fi 33 } 34 35 is_core18() { 36 # We need to check $SPREAD_SYSTEM var because in snapd the os-release file does 37 # not contain the ubuntu-core info while the system is being prepared 38 if [ -n "$SPREAD_SYSTEM" ]; then 39 [[ "$SPREAD_SYSTEM" == ubuntu-core-18-* ]] 40 else 41 grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="18"' /etc/os-release 42 fi 43 } 44 45 is_core20() { 46 # We need to check $SPREAD_SYSTEM var because in snapd the os-release file does 47 # not contain the ubuntu-core info while the system is being prepared 48 if [ -n "$SPREAD_SYSTEM" ]; then 49 [[ "$SPREAD_SYSTEM" == ubuntu-core-20-* ]] 50 else 51 grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="20"' /etc/os-release 52 fi 53 } 54 55 is_core22() { 56 # We need to check $SPREAD_SYSTEM var because in snapd the os-release file does 57 # not contain the ubuntu-core info while the system is being prepared 58 if [ -n "$SPREAD_SYSTEM" ]; then 59 [[ "$SPREAD_SYSTEM" == ubuntu-core-22-* ]] 60 else 61 grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="22"' /etc/os-release 62 fi 63 } 64 65 is_core24() { 66 # We need to check $SPREAD_SYSTEM var because in snapd the os-release file does 67 # not contain the ubuntu-core info while the system is being prepared 68 if [ -n "$SPREAD_SYSTEM" ]; then 69 [[ "$SPREAD_SYSTEM" == ubuntu-core-24-* ]] 70 else 71 grep -qFx 'ID=ubuntu-core' /etc/os-release && grep -qFx 'VERSION_ID="24"' /etc/os-release 72 fi 73 } 74 75 is_core_gt() { 76 compare_ubuntu "${1:-}" "-gt" 77 } 78 79 is_core_ge() { 80 compare_ubuntu "${1:-}" "-ge" 81 } 82 83 is_core_lt() { 84 compare_ubuntu "${1:-}" "-lt" 85 } 86 87 is_core_le() { 88 compare_ubuntu "${1:-}" "-le" 89 } 90 91 is_classic() { 92 ! is_core 93 } 94 95 is_trusty() { 96 grep -qFx 'ID=ubuntu' /etc/os-release && grep -qFx 'VERSION_ID="14.04"' /etc/os-release 97 } 98 99 is_xenial() { 100 grep -qFx 'UBUNTU_CODENAME=xenial' /etc/os-release 101 } 102 103 is_bionic() { 104 grep -qFx 'UBUNTU_CODENAME=bionic' /etc/os-release 105 } 106 107 is_focal() { 108 grep -qFx 'UBUNTU_CODENAME=focal' /etc/os-release 109 } 110 111 is_jammy() { 112 grep -qFx 'UBUNTU_CODENAME=jammy' /etc/os-release 113 } 114 115 is_ubuntu() { 116 VERSION=$1 117 if [ -z "$VERSION" ]; then 118 grep -qFx 'ID=ubuntu' /etc/os-release || grep -qFx 'ID=ubuntu-core' /etc/os-release 119 else 120 grep -qFx 'ID=ubuntu' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release 121 fi 122 } 123 124 is_ubuntu_gt() { 125 compare_ubuntu "${1:-}" "-gt" 126 } 127 128 is_ubuntu_ge() { 129 compare_ubuntu "${1:-}" "-ge" 130 } 131 132 is_ubuntu_lt() { 133 compare_ubuntu "${1:-}" "-lt" 134 } 135 136 is_ubuntu_le() { 137 compare_ubuntu "${1:-}" "-le" 138 } 139 140 compare_ubuntu() { 141 VERSION=$1 142 OPERAND=$2 143 144 if [ -z "$VERSION" ]; then 145 echo "os.query: version id is expected" 146 exit 1 147 fi 148 149 if ! grep -q 'ID=ubuntu' /etc/os-release; then 150 echo "os.query: comparing non ubuntu system" 151 return 1 152 fi 153 154 NUM_RE='^[0-9]+$' 155 NUM_VERSION="$(echo "$VERSION" | tr -d '".')" 156 if ! [[ $NUM_VERSION =~ $NUM_RE ]] ; then 157 echo "os.query: invalid version format \"$VERSION\" provided" 158 exit 1 159 fi 160 161 SYS_VERSION="$(grep 'VERSION_ID' /etc/os-release)" 162 SYS_VERSION="$(echo "${SYS_VERSION#*=}" | tr -d '".')" 163 if ! [[ $SYS_VERSION =~ $NUM_RE ]] ; then 164 echo "os.query: invalid version format \"$SYS_VERSION\" retrieved from system" 165 exit 1 166 fi 167 168 test "$SYS_VERSION" "$OPERAND" "$NUM_VERSION" 169 } 170 171 172 is_debian() { 173 VERSION=$1 174 if [ -z "$VERSION" ]; then 175 grep -qFx 'ID=debian' /etc/os-release 176 elif [ "$VERSION" == "sid" ]; then 177 if [ -n "$SPREAD_SYSTEM" ]; then 178 [[ "$SPREAD_SYSTEM" == debian-sid-* ]] 179 else 180 grep -qFx 'ID=debian' /etc/os-release && grep -qE '^PRETTY_NAME=.*/sid"$' /etc/os-release 181 fi 182 else 183 grep -qFx 'ID=debian' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release 184 fi 185 } 186 187 is_fedora() { 188 VERSION=$1 189 if [ -z "$VERSION" ]; then 190 grep -qFx 'ID=fedora' /etc/os-release 191 elif [ "$VERSION" == "rawhide" ]; then 192 if [ -n "$SPREAD_SYSTEM" ]; then 193 [[ "$SPREAD_SYSTEM" == fedora-rawhide-* ]] 194 else 195 grep -qFx 'ID=fedora' /etc/os-release && grep -qFx "REDHAT_BUGZILLA_PRODUCT_VERSION=rawhide" /etc/os-release 196 fi 197 else 198 grep -qFx 'ID=fedora' /etc/os-release && grep -qFx "VERSION_ID=$VERSION" /etc/os-release 199 fi 200 } 201 202 is_amazon_linux() { 203 VERSION=$1 204 if [ -z "$VERSION" ]; then 205 grep -qFx 'ID="amzn"' /etc/os-release 206 else 207 grep -qFx 'ID="amzn"' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release 208 fi 209 } 210 211 is_centos() { 212 VERSION=$1 213 if [ -z "$VERSION" ]; then 214 grep -qFx 'ID="centos"' /etc/os-release 215 else 216 grep -qFx 'ID="centos"' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release 217 fi 218 } 219 220 is_arch_linux() { 221 grep -qFx 'ID=arch' /etc/os-release 222 } 223 224 is_opensuse() { 225 VERSION=$1 226 if [ -z "$VERSION" ]; then 227 grep -qFx 'ID="opensuse-leap"' /etc/os-release || grep -qFx 'ID="opensuse-tumbleweed"' /etc/os-release 228 elif [ "$VERSION" == "tumbleweed" ]; then 229 grep -qFx 'ID="opensuse-tumbleweed"' /etc/os-release 230 else 231 grep -qFx 'ID="opensuse-leap"' /etc/os-release && grep -qFx "VERSION_ID=\"$VERSION\"" /etc/os-release 232 fi 233 } 234 235 is_pc_amd64() { 236 uname -m | grep -qFx 'x86_64' 237 } 238 239 is_pc_i386() { 240 uname -m | grep -Eq '(i686|i386)' 241 } 242 243 is_arm() { 244 uname -m | grep -Eq '(^arm.*|^aarch*)' 245 } 246 247 is_armhf() { 248 uname -m | grep -qx 'armv7.*' 249 } 250 251 is_arm64() { 252 uname -m | grep -Eq '(aarch64.*|armv8.*)' 253 } 254 255 is_s390x() { 256 uname -m | grep -qFx 's390x' 257 } 258 259 260 main() { 261 if [ $# -eq 0 ]; then 262 show_help 263 exit 0 264 fi 265 266 local subcommand="$1" 267 local action= 268 while [ $# -gt 0 ]; do 269 case "$1" in 270 -h|--help) 271 show_help 272 exit 0 273 ;; 274 *) 275 action=$(echo "$subcommand" | tr '-' '_') 276 shift 277 break 278 ;; 279 esac 280 done 281 282 if [ -z "$(declare -f "$action")" ]; then 283 echo "os.query: no such command: $subcommand" >&2 284 show_help 285 exit 1 286 fi 287 288 "$action" "$@" 289 } 290 291 main "$@"