-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathyum2
executable file
·249 lines (198 loc) · 5.54 KB
/
yum2
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
#!/bin/bash
#
# Mike Grozak <mike.grozak@gmail.com>
#
# - disablerepo
# required - no
# Repoid of repositories to disable for the install/update operation. These repos will not persist beyond the transaction. Multiple repos separated with a ',' (added in Ansible 0.9).
# - enablerepo
# required: no
# Repoid of repositories to enable for the install/update operation. These repos will not persist beyond the transaction multiple repos separated with a ',' (added in Ansible 0.9)
# - name
# requried: yes
# Package name, or package specifier with version, like name-1.0.
# - state
# required: no
# default: present
# choices: present latest absent
# Whether to install (present, latest), or remove (absent) a package.
# - options
# Various options to yum
# - gpgcheck
# required: no
# default: not specified
# choices: no
# Disable the gpg check of yum by suppliying --nogpgcheck CLI option
#
# Uncomment the following two strings in order to debug the module
source ${1}
if [[ -n "${debug}" && "${debug}" == "yes" ]]
exec 2>/tmp/yum2.log
set -x
fi
function exit_success() {
print_json 'msg' 'changed'
exit 0
}
function exit_failed() {
print_json 'failed' 'msg'
exit 1
}
function print_json() {
echo '{'
coma=""
for arg in $@
do
[[ -n "${!arg}" ]] && val="${!arg}" || continue
if [[ $val =~ ^\[ ]]; then
echo " ${coma} \"${arg}\": ${val}"
else
if [[ $arg == "changed" || $arg == "failed" ]]; then
echo " ${coma} \"${arg}\": ${val}"
else
echo " ${coma} \"${arg}\": \"${val}\""
fi
fi
[[ -z "${coma}" ]] && coma=","
done
echo '}'
}
YUM_REPOS=/etc/yum.repos.d
# Check the correctness of the input first
if [[ -z "${name}" ]]; then
failed="true"
msg="Please, specify the name of the package to deal with"
exit_failed
fi
[[ -z "${state}" ]] && state="present"
case $state in
'present'|'latest'|'absent'|'installed')
;;
*)
failed="true"
msg="Please, specify the correct state: present, absent or latest"
exit_failed
;;
esac
if [[ ! -z "${enablerepo}" || ! -z "${disablerepo}" ]]; then
for i in ${enablerepo//,/ } ${disablerepo//,/ }
do
if ! grep -q "\[$i\]" $YUM_REPOS/*.repo; then
failed="true"
failed_repos="$failed_repos $i"
fi
done
if [[ ! -z "${failed}" ]]; then
failed="true"
msg="The following repos could not be found in yum configuration in $YUM_REPOS directory:${failed_repos}"
exit_failed
fi
fi
# Do the actions, finally
if [[ ! -z ${name} ]]; then
name=${name//,/ }
fi
cmd="yum -q -y"
to_install=""
to_update=""
to_remove=""
[[ ! -z "${enablerepo}" && "${state}" != "absent" ]] && cmd="$cmd --enablerepo=${enablerepo}"
[[ ! -z "${disablerepo}" && "${state}" != "absent" ]] && cmd="$cmd --disablerepo=${disablerepo}"
[[ ! -z "${gpgcheck}" && "${gpgcheck}" == "no" ]] && cmd="$cmd --nogpgcheck"
case ${state} in
'present'|'installed')
# collect the names of the packages to install - do not install already installed
for i in ${name}
do
if [[ "$i" =~ ^.*/.*\.rpm$ ]]; then
real_name=$(rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" -p $i)
! rpmquery --quiet $real_name && to_install="${to_install} ${i}"
else
! rpmquery --quiet $i && to_install="${to_install} ${i}"
fi
done
;;
'latest')
# Collect the packages to install/update first
for i in ${name}
do
if ! rpmquery --quiet $i; then
to_install="${to_install} $i"
else
to_update="${to_update} $i"
fi
done
;;
'absent')
to_remove=''
for i in ${name}
do
rpmquery --quiet $i && to_remove="${to_remove} $i"
done
;;
esac
# remove leading space from our strings
to_install="${to_install# }"
to_update="${to_update# }"
to_remove="${to_remove# }"
if [[ ! -z "${to_install}" ]]; then
if [[ -z "${module}" ]]; then
cmd="${cmd} install"
${cmd} ${to_install} 2>&1 &>/dev/null
else
if [[ -z "${stream}" ]]; then
msg="please specify stream version in addition to a module name -- exit!"
exit_failed
fi
${cmd} module reset ${module} && \
${cmd} module enable ${module}:${stream} && \
${cmd} module install ${module}:${stream}
fi
# check, that the packages were installed sucessfully
x=""
for i in ${to_install}
do
# if it's local installation - normalize the name
if [[ "$i" =~ ^.*/.*\.rpm$ ]]; then
i=${i##*/}
i=${i/\.rpm/}
fi
! rpmquery --quiet $i && x="$x $i"
done
x="${x# }"
if [[ ! -z "$x" ]]; then
msg="$x packages has been failed for installation;"
[[ "${x}" != "${to_install}" ]] && changed="true" || failed="true"
else
changed="true"
fi
fi
if [[ ! -z "${to_update}" ]]; then
# this respect globs with version numbers
cmd2="${cmd} update-to"
version="$(rpmquery --queryformat=\"%{VERSION}-%{RELEASE}\" ${to_update})"
if ${cmd2} ${to_update} 2>&1 &>/dev/null; then
version_now="$(rpmquery --queryformat=\"%{VERSION}-%{RELEASE}\" ${to_update})"
[[ "${version}" != "${version_now}" ]] && changed="true"
fi
fi
if [[ ! -z "${to_remove}" ]]; then
cmd="$cmd remove"
x=""
${cmd} ${to_remove} 2>&1 &>/dev/null && changed="true"
for i in "${to_remove}"
do
rpmquery --quiet $i && x="$x $i"
if [[ ! -z "${x}" ]]; then
msg="$x packages has been failed to remove;"
[[ "${x}" == "${to_remove}" ]] && failed="true"
fi
done
fi
[[ -z "${msg}" ]] && msg="OK"
[[ -z "${changed}" ]] && changed="false"
if [[ -z ${failed} ]]; then
exit_success
else
exit_failed
fi