|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ------------------------------------------------------------------------------ |
| 3 | +# Copyright 2012-2015 Aerospike, Inc. |
| 4 | +# |
| 5 | +# Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 6 | +# license agreements. |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 9 | +# use this file except in compliance with the License. You may obtain a copy of |
| 10 | +# the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | +# License for the specific language governing permissions and limitations under |
| 16 | +# the License. |
| 17 | +# ------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +OPT_LONG=0 |
| 20 | + |
| 21 | +if [ "$1" = "-long" ] |
| 22 | +then |
| 23 | + OPT_LONG=1 |
| 24 | +fi |
| 25 | + |
| 26 | +error() { |
| 27 | + echo 'error:' $* >&2 |
| 28 | +} |
| 29 | + |
| 30 | +main() { |
| 31 | + |
| 32 | + local kernel='' |
| 33 | + local distro_id='' |
| 34 | + local distro_version='' |
| 35 | + local distro_long='' |
| 36 | + local distro_short='' |
| 37 | + |
| 38 | + # Make sure this script is running on Linux |
| 39 | + # The script is not designed to work on non-Linux |
| 40 | + # operating systems. |
| 41 | + kernel=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 42 | + if [ "$kernel" != 'linux' ] |
| 43 | + then |
| 44 | + error "$kernel is not supported." |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + |
| 48 | + if [ -f /etc/os-release ] |
| 49 | + then |
| 50 | + . /etc/os-release |
| 51 | + distro_id=${ID,,} |
| 52 | + distro_version=${VERSION_ID} |
| 53 | + elif [ -f /etc/issue ] |
| 54 | + then |
| 55 | + issue=$(cat /etc/issue | tr '[:upper:]' '[:lower:]') |
| 56 | + case "$issue" in |
| 57 | + *'centos'* ) |
| 58 | + distro_id='centos' |
| 59 | + ;; |
| 60 | + *'redhat'* | *'rhel'* ) |
| 61 | + distro_id='rhel' |
| 62 | + ;; |
| 63 | + *'debian'* ) |
| 64 | + distro_id='debian' |
| 65 | + ;; |
| 66 | + * ) |
| 67 | + error "/etc/issue contained an unsupported linux distibution: $issue" |
| 68 | + exit 1 |
| 69 | + ;; |
| 70 | + esac |
| 71 | + |
| 72 | + case "$distro_id" in |
| 73 | + 'centos' | 'rhel' ) |
| 74 | + local release='' |
| 75 | + if [ -f /etc/centos-release ]; then |
| 76 | + release=$(cat /etc/centos-release | tr '[:upper:]' '[:lower:]') |
| 77 | + elif [ -f /etc/redhat-release ]; then |
| 78 | + release=$(cat /etc/redhat-release | tr '[:upper:]' '[:lower:]') |
| 79 | + fi |
| 80 | + release_version=${release##*release} |
| 81 | + distro_version=${release_version%.*} |
| 82 | + ;; |
| 83 | + 'debian' ) |
| 84 | + debian_version=$(cat /etc/debian_version | tr '[:upper:]' '[:lower:]') |
| 85 | + distro_version=${debian_version%%.*} |
| 86 | + ;; |
| 87 | + * ) |
| 88 | + error "/etc/issue contained an unsupported linux distibution: $issue" |
| 89 | + exit 1 |
| 90 | + ;; |
| 91 | + esac |
| 92 | + fi |
| 93 | + |
| 94 | + distro_id=${distro_id//[[:space:]]/} |
| 95 | + distro_version=${distro_version//[[:space:]]/} |
| 96 | + |
| 97 | + case "$distro_id" in |
| 98 | + 'centos' ) |
| 99 | + distro_long="${distro_id}${distro_version%%.*}" |
| 100 | + distro_short="el${distro_version%%.*}" |
| 101 | + ;; |
| 102 | + 'rhel' | 'redhat' | 'red hat' ) |
| 103 | + distro_long="${distro_id}${distro_version%%.*}" |
| 104 | + distro_short="el${distro_version%%.*}" |
| 105 | + ;; |
| 106 | + 'fedora' ) |
| 107 | + if [ "$distro_version" -gt "15" ] |
| 108 | + then |
| 109 | + distro_version=7 |
| 110 | + elif [ "$distro_version" -gt "10" ] |
| 111 | + then |
| 112 | + distro_version=6 |
| 113 | + else |
| 114 | + error "Unsupported linux distibution: $distro_id $distro_version" |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + distro_long="centos${distro_version}" |
| 118 | + distro_short="el${distro_version}" |
| 119 | + ;; |
| 120 | + 'amzn' ) |
| 121 | + distro_long="ami" |
| 122 | + distro_short="ami" |
| 123 | + ;; |
| 124 | + * ) |
| 125 | + distro_long="${distro_id}${distro_version}" |
| 126 | + distro_short="${distro_id}${distro_version}" |
| 127 | + ;; |
| 128 | + esac |
| 129 | + |
| 130 | + if [ "$OPT_LONG" = "1" ] |
| 131 | + then |
| 132 | + echo "${distro_long}" |
| 133 | + else |
| 134 | + echo "${distro_short}" |
| 135 | + fi |
| 136 | + exit 0 |
| 137 | +} |
| 138 | + |
| 139 | +main |
0 commit comments