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
|
#!/usr/bin/python -tt
# (c) 2012, Red Hat, Inc
# Written by Seth Vidal <skvidal at fedoraproject.org>
#
# This file is part of Ansible
#
# Ansible 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 3 of the License, or
# (at your option) any later version.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
import yum
import subprocess
import datetime
import shlex
import re
import traceback
try:
import json
except ImportError:
import simplejson as json
def yum_base(conf_file=None, cachedir=False):
my = yum.YumBase()
my.preconf.debuglevel=0
if conf_file and os.path.exists(conf_file):
my.preconf.fn = conf_file
if cachedir:
my.setCacheDir()
return my
def pkg_to_dict(po):
d = {
'name':po.name,
'arch':po.arch,
'epoch':po.epoch,
'release':po.release,
'version':po.version,
'repo':po.ui_from_repo,
'_nevra':po.ui_nevra,
}
if type(po) == yum.rpmsack.RPMInstalledPackage:
d['yumstate'] = 'installed'
else:
d['yumstate'] = 'available'
return d
def list_stuff(my, stuff):
if stuff == 'installed':
return [ pkg_to_dict(po) for po in my.rpmdb ]
elif stuff == 'updates':
return [ pkg_to_dict(po) for
po in my.doPackageLists(pkgnarrow='updates').updates ]
elif stuff == 'available':
return [ pkg_to_dict(po) for po in my.pkgSack ]
elif stuff == 'repos':
r = []
for repo in my.repos.repos.values():
t = {}
s = 'disabled'
if repo.enabled:
s = 'enabled'
t[repo.id] = s
r.append(t)
return r
else:
e,m,u = my.rpmdb.matchPackageNames([stuff])
p = e + m
e,m,u = my.pkgSack.matchPackageNames([stuff])
p = p + e + m
return [ pkg_to_dict(po) for po in p ]
def run_yum(command):
try:
cmd = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
except (OSError, IOError), e:
rc = 1
err = str(e)
out = ''
except:
rc = 1
err = traceback.format_exc()
out = ''
if out is None:
out = ''
if err is None:
err = ''
else:
rc = cmd.returncode
return rc, out, err
def ensure(my, state, pkgspec):
yumconf = my.conf.config_file_path
if state == 'installed':
pkg = None
# check if pkgspec is installed
if re.search('[></=]',pkgspec):
try:
pkgs = my.returnInstalledPackagesByDep(pkgspec)
except yum.Errors.YumBaseError:
pkgs = None
else:
e,m,u = my.rpmdb.matchPackageNames([pkgspec])
pkgs = e +m
if pkgs:
return { 'changed':False, 'failed':False, 'results':'', 'errors':'' }
# if not - try to install it
my.close()
del my
cmd = 'yum -c %s -d1 -y install %s' % (yumconf, pkgspec)
rc, out, err = run_yum(cmd)
if rc:
changed = False
failed = True
else:
changed = True
failed = False
return {'changed': changed,
'failed': failed,
'results':out,
'errors': err }
if state == 'removed':
# check if pkgspec is installed
# if not return {changed: False, failed=False }
# if so try to remove it:
# if successfull {changed:True, failed=False, results=stdout, errors=stderr }
# if fail {'changed':False, failed='True', results=stdout, errors=stderr }
yumconf = my.conf.config_file_path
if re.search('[></=]',pkgspec):
try:
pkgs = my.returnInstalledPackagesByDep(pkgspec)
except yum.Errors.YumBaseError:
pkgs = None
else:
e,m,u = my.rpmdb.matchPackageNames([pkgspec])
pkgs = e +m
if not pkgs:
return { 'changed':False, 'failed':False, 'results':'', 'errors':'' }
my.close()
del my
cmd = 'yum -c %s -d1 -y remove %s' % (yumconf, pkgspec)
rc, out, err = run_yum(cmd)
if rc:
changed = False
failed = True
else:
changed = True
failed = False
return {'changed': changed,
'failed': failed,
'results':out,
'errors': err }
#if state == 'latest':
# check to see if this pkg is in an update
# if it is - update it and check to see if it applied
# if it is not - then return
# return { 'changed':False, 'failed':False, 'results':'', 'errors':'' }
return {'changed': False,
'failed': True,
'results':'',
'errors': 'Ensure state %r unknown' % state }
def update(args):
#generic update routine
pass
def remove_only(pkgspec):
# remove this pkg and only this pkg - fail if it will require more to remove
pass
def main():
# state=installed pkg=pkgspec
# state=removed pkg=pkgspec
# list=installed
# list=updates
# list=available
# list=repos
# list=pkgspec
# update="args"?
#
args = " ".join(sys.argv[1:])
items = shlex.split(args)
# if nothing else changes - it fails
results = { 'changed':False, 'failed':True, 'results':'', 'errors':args }
params = {}
for x in items:
(k, v) = x.split("=", 1)
params[k] = v
if 'conf_file' not in params:
params['conf_file'] = None
if 'list' in params:
my = yum_base(conf_file=params['conf_file'], cachedir=True)
results = list_stuff(my, params['list'])
elif 'state' in params:
my = yum_base(conf_file=params['conf_file'], cachedir=True)
state = params['state']
pkgspec = params['pkg']
results = ensure(my, state, pkgspec)
print json.dumps(results)
if __name__ == "__main__":
main()
|