-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect_os
executable file
·226 lines (192 loc) · 5.35 KB
/
detect_os
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
#!/bin/bash
#
# Copyright (C) 2023 Keith Valin [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
config="$(dirname $(realpath $0))/configs/detect_os.yml"
os_release=1 # /etc/os-release more reliable, so use it by default
os_release_file="/etc/os-release"
get_version=0 # Fetch OS name by default
usage()
{
echo -e "detect_os"
echo -e "\t Detects the OS that the system is currently using and prints it out"
echo -e "Options"
echo -e "\t -c/--config"
echo -e "\t\t The YAML configuration file used to help identify an OS (default is $(dirname $(realpath $0))/configs/detect_os.yml)"
echo -e "\t --uname"
echo -e "\t\t Determine the current OS using 'uname -a' output"
echo -e "\t\t Cannot be combined with --os-release"
echo -e "\t --os-release"
echo -e "\t\t Determine the current OS using the /etc/os-release file"
echo -e "\t\t Cannot be combined with --uname"
echo -e "\t --os-version"
echo -e "\t\t Determine the current OS version"
echo -e "\t\t Works with --uname and --os-release"
echo -e "\t -h/--help/--usage"
echo -e "\t\t Displays this message"
}
install_deps()
{
sys_arch=`arch`
if [ "$sys_arch" == "x86_64" ]; then
sys_arch="amd64"
elif [ "$sys_arch" == "aarch64" ]; then
sys_arch="arm64"
fi
if ! command -v yq > /dev/null 2>&1; then
if ! command -v wget > /dev/null 2>&1; then
if command -v yum > /dev/null 2>&1; then
yum install -y wget
else
apt install -y wget
fi
fi
version="v4.35.1"
bin_name="yq_linux_$sys_arch"
wget --quiet https://github.com/mikefarah/yq/releases/download/$version/$bin_name -O /usr/bin/yq
chmod +x /usr/bin/yq
fi
}
echo_stderr()
{
echo ${@:1} > /dev/stderr
}
parse_os_release()
{
if [ ! -f $os_release_file ];then
echo_stderr Could not find file $os_release_file, aborting
exit 1
fi
local os_id=`grep '^ID=' $os_release_file | cut -d'=' -f2 | sed -e "s/\"\|'//g"`
local version=`grep '^VERSION_ID=' $os_release_file | cut -d'=' -f2 | sed -e "s/\"\|'//g"`
local config_len=`yq '. | length' $config`
for i in `seq 0 $config_len`;do
local identifier=`yq -r ".[$i].identifier" $config`
if [ $identifier = $os_id ]; then
if [ $get_version -eq 0 ];then
echo $identifier
else
echo $version
fi
exit 0
fi
done
echo_stderr Unknown os \"$os_id\", check $config to make sure it exists
exit 1
}
parse_uname()
{
local kinfo=`uname -a`
local config_len=`yq '. | length' $config`
for i in `seq 0 $config_len`;do
local pattern=`yq -r ".[$i].uname_pattern" $config`
local identifier=`yq -r ".[$i].identifier" $config`
local ver_pattern=`yq -r ".[$i].uname_version_pattern" $config`
if [ -z ver_pattern ]; then
echo $identifier has no uname_version_pattern, skipping
continue
fi
local os_match=`echo $kinfo | grep -Eo $pattern`
if [ ! -z $os_match ]; then
if [ $get_version -eq 1 ];then
echo $os_match | grep -Eo $ver_pattern
else
echo $identifier
fi
exit 0
fi
done
echo_stderr No known uname pattern matches, check $config to ensure that a pattern matches the following uname line
echo_stderr $kinfo
exit 1
}
NOARG_OPTS=(
"uname"
"h"
"help"
"os-version"
"usage"
"os-release"
)
ARG_OPTS=(
"c"
"config"
)
opts=$(getopt \
--longoptions "$(printf "%s," "${NOARG_OPTS[@]}")" \
--longoptions "$(printf "%s:," "${ARG_OPTS[@]}")" \
--name "$(basename "$0")" \
--options "hc:" \
-- "$@"
)
if [ $? -ne 0 ]; then
exit 1
fi
eval set --$opts
flag_set=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --usage | --help)
usage
exit 0
;;
--uname)
if [ $flag_set -eq 1 ];then
usage
exit 1
fi
flag_set=1
os_release=0
shift 1
;;
--os-release)
if [ $flag_set -eq 1 ];then
usage
exit 1
fi
flag_set=1
os_release=1
shift 1
;;
--os-version)
get_version=1
shift 1
;;
-c | --config)
config=$2
shift 2
;;
--)
break
;;
*)
echo_stderr Unknown option $1
exit 1
;;
esac
done
if [ ! -f $config ]; then
echo_stderr Cannot access $config, aborting
exit 1
fi
install_deps
if [ $os_release -eq 1 ]; then
parse_os_release
else
parse_uname
fi
exit 0