-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions_os
287 lines (251 loc) · 8.51 KB
/
.functions_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
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
287
#----- header -----
[ "${0##*/}" != "${BASH_SOURCE##*/}" ] || { >&2 echo -e "ERROR\tfile must be sourced ($0)"; return 2; }
#------------------
function is_wsl() { uname -r | grep -q WSL; }
# fallback
declare -F is_windows &>/dev/null ||
function is_windows() { [[ "${OSTYPE:-`uname -o`}" =~ [cC]ygwin|[mM]sys ]]; }
#TODO is_linux, or is_os that can emit solaris, windows, cygwin, etc
function os.tree() {
local filter= type= reverse= flags=()
local -a cmd=( \find ) #alt: \ls -R -1 --quoting-style=shell
local -i OPTIND
local opt OPTARG
while getopts ':hdlL:P:x' opt 2>/dev/null; do
case "$opt" in
d) flags+=( '-type' d ) ;;
l) cmd+=( '-L' ) ;;
L) flags+=( '-maxdepth' "$OPTARG" ) ;;
P) flags+=( '-name' "$OPTARG" ) ;;
x) flags+=( '-xdev' ) ;;
:) log.error "missing argument (-$OPTARG)" ;;&
\?) log.error "unsupported option (-$OPTARG)" ;&
h|*) >&2 cat <<_EOF
Usage: $FUNCNAME [ options ] <dir> [<dir> ...]
-d list Directories only
-l follow symbolic links
-L <level> maximum traverse depth
-P <pattern> must escape any wildcards
-x stay on current filesystem
ref: http://mama.indstate.edu/users/ice/tree/tree.1.html
_EOF
return 2
;;
esac
done
shift $((OPTIND - 1))
while read item; do
out=$( sed -e 's/[^-][^\/]*\// |/g' -e 's/|\([^ ]\)/|- \1/' <<< "$item" )
#TODO handle filenames with spaces
[ -h "$item" ] && out+=" -> $( readlink -f "$item" )"
echo "$out"
done < <( ${DEBUG:+ runv} "${cmd[@]}" -H ${1:-.} ${flags[@]} )
echo
}
#WIP
return 0
#is_exec -q rpm apt || { >&2 echo -e "ERROR\tmissing required commands"; return 2; }
function detect_os() {
#TODO leverage facter2, chef equiv, and distro if available
#ref: https://github.com/nir0s/distro/blob/master/distro.py#L154
# old, rather brittle way
#if [[ -e /etc/redhat-release ]]; then
# RELEASE_RPM=$(rpm -qf /etc/redhat-release)
# RELEASE=$(rpm -q --qf '%{VERSION}' ${RELEASE_RPM})
# case ${RELEASE_RPM} in
# centos*)
# echo "detected CentOS ${RELEASE}"
# ;;
# redhat*)
local k
declare -A defaults=(
[OS_ID]=
[OS_NAME]=`uname -o`
[OS_KERNEL]=`uname -s`
[OS_ARCH]=`uname -m`
# Cygwin puts Build version inside () which is nonstandard, and RHEL/Amazon/CentOS tack on 'arch'
[OS_RELEASE]=$( uname -r | sed -e 's/(.*)$//' -e "s/\.`uname -m`$//" | awk -F . '{ print $NF }' )
[OS_FAMILY]=$( facter osfamily 2>/dev/null )
# or `lsb_release -i | awk '{ print $NF }' 2>/dev/null`
[OS_MAJOR]=$( facter operatingsystemmajrelease 2>/dev/null )
[OS_REPO_CMD]=
[OS_PKG_CMD]=
[OS_PKG_SUFFIX]=
[OS_LOCAL_DIR]=/usr/local
[OS_TEMP_DIR]=/tmp
# also `facter lsb*`
)
#TODO? define *_CMD of type array
# convirt MAP to individual OS_* variables
for k in "${!defaults[@]}"; do
[ -n "${!k}" ] || eval $k="${defaults[$k]}"
done
#TODO section only applies to Linux - break out into detect_${OS_KERNEL}
case $OS_RELEASE in
amzn?|el?)
: ${OS_FAMILY:=RedHat}
[ -n "$OS_REPO_CMD" ] || {
OS_REPO_CMD="yum ${QUIET:+$_QUIET}"
[ ${DEBUG:-0} -ge 2 ] && OS_REPO_CMD+=" --debuglevel=$DEBUG" || OS_REPO_CMD+=" --debuglevel=0"
[ ${VERBOSE:-0} -ge 2 ] && OS_REPO_CMD+=" --rpmverbosity=info" || OS_REPO_CMD+=" --rpmverbosity=warn"
}
: ${OS_PKG_CMD:='rpm'}
: ${OS_PKG_SUFFIX:='rpm'}
;;&
amzn1|el6)
OS_MAJOR=6
;;
amzn2|el7)
OS_MAJOR=7
;;
deb*)
: ${OS_FAMILY:=Debian}
: ${OS_REPO_CMD:="apt-get --verbose-versions
-o Dpkg::Options::='--force-confdef'
-o Dpkg::Options::='--force-confold'
-o Dpkg::Options::='--force-confmiss'"}
: ${OS_PKG_CMD:='dpkg'}
: ${OS_PKG_SUFFIX:='deb'}
;;&
*)
esac
for k in ${!OS_*}; do debug "$k=${!k}"; done
export ${!OS_*}
}
function package_name() {
local name= py{2,3}
while [ "${1:x}" ]; do
[ -n "$1" ] || { shift; continue; }
case "${OS_FAMILY:-$OS_NAME}" in
*Linux)
unset OS_FAMILY
;&
"") detect_os
;;&
RedHat)
case "$1" in
epel-release)
name="https://dl.fedoraproject.org/pub/epel/epel-release-latest-${OS_MAJOR:?}.noarch.${OS_PKG_SUFFIX:?}"
;;
ius-release)
# TODO also 'centos', use $OS_ID? `curl 'https://setup.ius.io/' | bash` is possible but weak
name="https://rhel${OS_MAJOR:?}.iuscommunity.org/ius-release.${OS_PKG_SUFFIX:?}"
;;
puppet-release)
name="https://yum.puppet.com/puppet/puppet-release-el-${OS_MAJOR:?}.noarch.${OS_PKG_SUFFIX:?}"
;;
python3|python3-*)
rpm -q epel-release &>/dev/null && py3=34
rpm -q ius-release &>/dev/null && py3=36u
name=${1/3/$py3}
;;
esac
;;
Debian)
;;
*) # unsupported
esac
echo "${name:-$1}"
shift
done
}
#---------------
#WIP - copy over latest from ami-factory/scripts/
os.detect
# misc notes
#if [ -f /etc/os-release ]; then
# # freedesktop.org and systemd
# . /etc/os-release
# OS=$NAME
# VER=$VERSION_ID
#elif type lsb_release >/dev/null 2>&1; then
# # linuxbase.org
# OS=$(lsb_release -si)
# VER=$(lsb_release -sr)
#elif [ -f /etc/lsb-release ]; then
# # For some versions of Debian/Ubuntu without lsb_release command
# . /etc/lsb-release
# OS=$DISTRIB_ID
# VER=$DISTRIB_RELEASE
#elif [ -f /etc/debian_version ]; then
# # Older Debian/Ubuntu/etc.
# OS=Debian
# VER=$(cat /etc/debian_version)
#elif [ -f /etc/SuSe-release ]; then
# # Older SuSE/etc.
# ...
#elif [ -f /etc/redhat-release ]; then
# # Older Red Hat, CentOS, etc.
# ...
#else
# # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
# OS=$(uname -s)
# VER=$(uname -r)
#fi
#
#case $(uname -m) in
#x86_64)
# ARCH=x64 # or AMD64 or Intel64 or whatever
# ;;
#i*86)
# ARCH=x86 # or IA32 or Intel32 or whatever
# ;;
#*)
# # leave ARCH as-is
# ;;
#esac
function os.file.replace() {
# replace file and optionally take backup
local orig="${1:?}"
local new="${2:?}"
shift 2
#FIXME use getopt() for '-b'. and break on '--' and set filename(s) from remainder
# see --* handling from ec2-validate.sh and packer wrapper.
#TODO? use _xxx format so can be overriden from cmdline?
local mode uid gid context flags
# TODO Selinux Context with '%C' and --context=$context. check with 'getenforce' first
#FIXME test for symlink and output %N ('link' -> 'real dest'), parse and strip quotes?
# non-priv can stat restricted files if can walk intervening paths
read -r mode uid gid context \
< <(stat --format='%a %U %G' "$orig" 2>/dev/null)
# clear parameters if our own
[ "$uid" = "`id -nu`" ] && unset uid
[ "$gid" = "`id -ng`" ] && unset gid
${DEBUG:+runv} ${uid:+$SUDO} install ${VERBOSE:+ -v} ${mode:+ -m $mode} \
${uid:+ -o $uid} ${gid:+ -g $gid} ${context:+ --context=$context} \
${flags:- -b} "$new" "$orig"
}
#TODO move to .functions_os
#function userinfo() {
# # analogous to /bin/usermod
# #while [ "${1+xxx}" ] && [[ "$1" =~ -* ]]; do
# local OPTIND
## FIXME use getopts like a sane person!
# [[ "$1" =~ -* ]] && { flag=${1:1:1}; shift; }
#
##FIXME define a HASH for lookups instead of this crap
# case $flag in
# l) field=1 ;;&
# u) field=2 ;;&
# g) field=3 ;;&
# d) field=6 ;;&
# L) field=lock ;&
# # TODO needs if/else
#XX p) getent shadow ${1:-`id -u`} | awk -F: -v field=$field '{ print $2 }' ;;
#XX '') id -nu $1 ;;
#XX *) getent passwd ${1:-`id -u`}
# esac
# # login:x:uid:gid:gecos:home:shell
# -c, --comment COMMENT new value of the GECOS field
# -d, --home HOME_DIR new home directory for the user account
# -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
# -f, --inactive INACTIVE set password inactive after expiration
# -g, --gid GROUP force use GROUP as new primary group
# -G, --groups GROUPS new list of supplementary GROUPS
# -l, --login NEW_LOGIN new value of the login name
# -L, --lock lock the user account
# -p, --password PASSWORD use encrypted password for the new password
# -s, --shell SHELL new login shell for the user account
# -u, --uid UID new UID for the user account
#}
# vim: expandtab:ts=4:sw=4