This repository has been archived by the owner on Mar 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathos_helper.bash
197 lines (173 loc) · 4.55 KB
/
os_helper.bash
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
# vim: ft=sh:sw=2:et
tIsRedHatCompatible() {
[[ -f /etc/redhat-release ]]
}
tIsCentOSCompatible() {
[[ -f /etc/centos-release ]]
}
tIsFedoraCompatible() {
[[ -f /etc/redhat-release && -f /etc/fedora-release ]]
}
tIsDebianCompatible() {
[[ -f /etc/debian_version ]]
}
tIsUbuntuCompatible() {
[[ -f /etc/os-release ]] && grep -q ID=ubuntu /etc/os-release
}
tSetOSVersion() {
if [[ -z "$OS_VERSION" ]]; then
if tIsFedoraCompatible; then
OS_VERSION=$(rpm -q --queryformat '%{VERSION}' fedora-release)
elif tIsRedHatCompatible; then
_PKG=$(rpm -qa '(redhat|sl|centos|oraclelinux)-release(|-server|-workstation|-client|-computenode)')
OS_VERSION=$(rpm -q --queryformat '%{VERSION}' $_PKG | grep -o '^[0-9]*')
elif tIsUbuntuCompatible; then
tPackageExists lsb-release || tPackageInstall lsb-release
OS_VERSION=$(. /etc/os-release; echo $VERSION_ID)
OS_RELEASE=$(lsb_release -cs)
elif tIsDebianCompatible; then
tPackageExists lsb-release || tPackageInstall lsb-release
OS_VERSION=$(cut -d. -f1 /etc/debian_version)
OS_RELEASE=$(lsb_release -cs)
fi
fi
}
tIsFedora() {
if [ -z "$1" ]; then
tIsFedoraCompatible
else
tSetOSVersion
tIsFedoraCompatible && [[ "$1" -eq "$OS_VERSION" ]]
fi
}
tIsRHEL() {
if [ -z "$1" ]; then
tIsRedHatCompatible && ! tIsFedoraCompatible
else
tSetOSVersion
tIsRedHatCompatible && [[ "$1" -eq "$OS_VERSION" ]]
fi
}
tIsDebian() {
tIsDebianCompatible && ! tIsUbuntuCompatible
}
tIsUbuntu() {
tIsUbuntuCompatible
}
tPackageAvailable() {
if tIsRedHatCompatible; then
yum info "$1" >/dev/null 2>&1
elif tIsDebianCompatible; then
apt-cache show "$1" >/dev/null 2>&1
else
false # not implemented
fi
}
tPackageExists() {
if tIsRedHatCompatible; then
rpm -q "$1" >/dev/null
elif tIsDebianCompatible; then
dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -q '^i'
else
false # not implemented
fi
}
tPackageInstall() {
if tIsRedHatCompatible; then
yum -y install $*
elif tIsDebianCompatible; then
export DEBIAN_FRONTEND=noninteractive
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install $*
else
false # not implemented
fi
}
tPackageUpgrade() {
if tIsRedHatCompatible; then
yum -y upgrade $*
elif tIsDebianCompatible; then
export DEBIAN_FRONTEND=noninteractive
apt-get -y --only-upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install $*
else
false # not implemented
fi
}
tPackageVersion() {
if tIsRedHatCompatible; then
rpm -q --qf "%{VERSION}\n" "$1"
elif tIsDebianCompatible; then
dpkg -s "$1" | awk '/^Version:/ { print $2 }'
else
false # not implemented
fi
}
tServiceDisable() {
if tCommandExists systemctl; then
systemctl disable "$1"
else
if tIsRedHatCompatible; then
chkconfig "$1" off
elif tIsDebianCompatible; then
update-rc.d "$1" disable
else
false # not implemented
fi
fi
}
tServiceEnable() {
if tCommandExists systemctl; then
systemctl enable "$1"
else
if tIsRedHatCompatible; then
chkconfig "$1" on
elif tIsDebianCompatible; then
update-rc.d "$1" enable
else
false # not implemented
fi
fi
}
tServiceStart() {
if tCommandExists systemctl; then
systemctl start "$1"
else
service "$1" start
fi
}
tServiceStop() {
if tCommandExists systemctl; then
systemctl stop "$1"
else
service "$1" stop
fi
}
tCommandExists() {
type -p "$1" >/dev/null
}
tFileExists() {
[[ -f "$1" ]]
}
tRHSubscribeAttach() {
if tIsRHEL; then
[[ -z "$RHSM_USER" || -z "$RHSM_PASS" || -z "$RHSM_POOL" ]] && skip "No subscription-manager credentials and pool id"
tPackageExists subscription-manager || tPackageInstall subscription-manager
echo $RHSM_USER $RHSM_PASS $RHSM_POOL
subscription-manager register --username=$RHSM_USER --password=$RHSM_PASS
subscription-manager attach --pool=$RHSM_POOL
subscription-manager repos --enable rhel-server-rhscl-$OS_VERSION-rpms --enable rhel-$OS_VERSION-server-optional-rpms
else
skip "Not required"
fi
}
tRHEnableEPEL() {
tIsRHEL || skip "Not required"
tSetOSVersion
tPackageExists epel-release || \
rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-${OS_VERSION}.noarch.rpm
}
tEnableDebianBackports() {
tIsDebian || skip "Not applicable, non-Debian OS"
tSetOSVersion
awk -v dist=${OS_RELEASE}-backports '/^deb/ { $3 = dist; print; exit }' < /etc/apt/sources.list >> /etc/apt/sources.list
apt-get update
}