1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import logging
17
18
19 import pulp.server.auth.cert_generator as cert_generator
20 from pulp.server.agent import Agent
21 from pulp.server.api.base import BaseApi
22 from pulp.server.api.consumer_history import ConsumerHistoryApi
23 from pulp.server.api.errata import ErrataApi
24 from pulp.server.api.package import PackageApi
25 from pulp.server.api.repo import RepoApi
26 from pulp.server.auditing import audit
27 from pulp.server.db import model
28 from pulp.server.db.connection import get_object_db
29 from pulp.server.pexceptions import PulpException
30 from pulp.server.util import chunks, compare_packages
31
32
33 log = logging.getLogger(__name__)
34
35 consumer_fields = model.Consumer(None, None).keys()
39
46
51
52 @property
55
56 @property
58 return ["package_profile.name", "repoids"]
59
60 @audit(params=['id'])
61 - def create(self, id, description):
72
73 @audit()
80
81 @audit()
92
93 @audit()
95 """
96 Create a set of Consumer objects in a bulk manner
97 @type consumers: list of dictionaries
98 @param consumers: dictionaries representing new consumers
99 """
100
101
102 chunksize = 50
103 chunked = chunks(consumers, chunksize)
104 inserted = 0
105 for chunk in chunked:
106 self.objectdb.insert(chunk, check_keys=False, safe=False)
107 inserted = inserted + chunksize
108
109 - def consumers(self, spec=None, fields=None):
110 """
111 List all consumers. Can be quite large
112 """
113 return list(self.objectdb.find(spec=spec, fields=fields))
114
123
129
139
140 @audit()
141 - def bind(self, id, repoid):
142 """
143 Bind (subscribe) a consumer to a repo.
144 @param id: A consumer id.
145 @type id: str
146 @param repoid: A repo id to bind.
147 @type repoid: str
148 @raise PulpException: When consumer not found.
149 """
150 consumer = self.consumer(id)
151 if consumer is None:
152 raise PulpException('Consumer [%s] not found', id)
153 repoids = consumer.setdefault('repoids', [])
154 if repoid in repoids:
155 return
156 repoids.append(repoid)
157 self.update(consumer)
158 self.consumer_history_api.repo_bound(id, repoid)
159
160 @audit()
161 - def unbind(self, id, repoid):
162 """
163 Unbind (unsubscribe) a consumer to a repo.
164 @param id: A consumer id.
165 @type id: str
166 @param repoid: A repo id to unbind.
167 @type repoid: str
168 @raise PulpException: When consumer not found.
169 """
170 consumer = self.consumer(id)
171 if consumer is None:
172 raise PulpException('Consumer [%s] not found', id)
173 repoids = consumer.setdefault('repoids', [])
174 if repoid not in repoids:
175 return
176 repoids.remove(repoid)
177 self.update(consumer)
178 self.consumer_history_api.repo_unbound(id, repoid)
179
180 @audit(params=['id'])
190
191 @audit()
193 """
194 Install packages on the consumer.
195 @param id: A consumer id.
196 @type id: str
197 @param packagenames: The package names to install.
198 @type packagenames: [str,..]
199 """
200 agent = Agent(id)
201 data = []
202 for pkg in packagenames:
203 info = pkg.split('.')
204 if len(info) > 1:
205 data.append(('.'.join(info[:-1]), info[-1]))
206 else:
207 data.append(pkg)
208 log.debug("Packages to Install: %s" % data)
209 installed_packages = agent.packages.install(data)
210 self.consumer_history_api.packages_installed(id, installed_packages)
211 return installed_packages
212
213 @audit()
215 """
216 Install package groups on the consumer.
217 @param id: A consumer id.
218 @type id: str
219 @param packageids: The package ids to install.
220 @type packageids: [str,..]
221 """
222 agent = Agent(id)
223 agent.packagegroups.install(packageids)
224 return packageids
225
227 """
228 Install errata on the consumer.
229 @param id: A consumer id.
230 @type id: str
231 @param errataids: The errata ids to install.
232 @type errataids: [str,..]
233 @param types: Errata type filter
234 @type types: str
235 """
236 agent = Agent(id)
237 consumer = self.consumer(id)
238 pkgs = []
239 if errataids:
240 applicable_errata = self._applicable_errata(consumer, types)
241 for eid in errataids:
242 for pobj in applicable_errata[eid]:
243 if pobj["arch"] != "src":
244 pkgs.append(pobj["name"])
245 else:
246
247 pkgobjs = self.list_package_updates(id, types)
248 for pobj in pkgobjs:
249 if pobj["arch"] != "src":
250 pkgs.append(pobj["name"])
251 log.error("Packages to install [%s]" % pkgs)
252 agent.packages.install(pkgs)
253 return pkgs
254
261
269
271 """
272 Logic to filter applicable errata for a consumer
273 """
274 applicable_errata = {}
275 pkg_profile = consumer["package_profile"]
276
277 pkg_profile_dict = [dict(pkg) for pkg in pkg_profile]
278 pkg_profile_names = [pkg['name'] for pkg in pkg_profile]
279
280
281 errataids = [eid for repoid in consumer["repoids"] \
282 for eid in self.repoapi.errata(repoid, types) ]
283 for erratumid in errataids:
284
285
286 erratum = self.errataapi.erratum(erratumid)
287 for epkg in erratum["pkglist"]:
288 for pkg in epkg["packages"]:
289 epkg_info = {
290 'name': pkg["name"],
291 'version': pkg["version"],
292 'arch': pkg["arch"],
293 'epoch': pkg["epoch"] or 0,
294 'release': pkg["release"],
295 }
296 if epkg_info['name'] not in pkg_profile_names:
297
298
299 continue
300 for ppkg in pkg_profile_dict:
301 if epkg_info['name'] != ppkg['name']:
302 continue
303
304 status = compare_packages(epkg_info, ppkg)
305 if status == 1:
306
307 if not applicable_errata.has_key(erratumid):
308 applicable_errata[erratumid] = []
309 applicable_errata[erratumid].append(pkg)
310 return applicable_errata
311