1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import os
21 import string
22 import sys
23 from spacewalk.common.usix import TupleType
24
25 from spacewalk.common.usix import raise_with_tb
26
27 from spacewalk.common.rhnLog import log_debug, log_error
28 from spacewalk.common.rhnConfig import CFG
29 from spacewalk.common.rhnException import rhnFault
30 from spacewalk.common.RPC_Base import RPC_Base
31
32 from spacewalk.server import rhnSQL, rhnPackageUpload, rhnUser, rhnSession
33
34 from spacewalk.server.importlib.backendOracle import SQLBackend
35 from spacewalk.server.importlib.importLib import Collection, IncompatibleArchError,\
36 Channel, IncompletePackage, InvalidChannelError
37 from spacewalk.server.importlib.packageImport import ChannelPackageSubscription
38
39 from spacewalk.server.importlib.packageUpload import uploadPackages, listChannels, listChannelsSource, listChannelsChecksum
40 from spacewalk.server.importlib.userAuth import UserAuth
41 from spacewalk.server.importlib.errataCache import schedule_errata_cache_update
42
43
44
45
46
47
48
49
50
51
52
53
55
57 log_debug(3)
58 RPC_Base.__init__(self)
59 self.functions.append('uploadPackageInfo')
60 self.functions.append('uploadPackageInfoBySession')
61 self.functions.append('uploadSourcePackageInfo')
62 self.functions.append('uploadSourcePackageInfoBySession')
63 self.functions.append('listChannel')
64 self.functions.append('listChannelBySession')
65 self.functions.append('listChannelChecksum')
66 self.functions.append('listChannelChecksumBySession')
67 self.functions.append('listChannelSource')
68 self.functions.append('listChannelSourceBySession')
69 self.functions.append('listMissingSourcePackages')
70 self.functions.append('listMissingSourcePackagesBySession')
71 self.functions.append('channelPackageSubscription')
72 self.functions.append('channelPackageSubscriptionBySession')
73 self.functions.append('no_op')
74 self.functions.append('test_login')
75 self.functions.append('test_new_login')
76 self.functions.append('test_check_session')
77 self.functions.append('login')
78 self.functions.append('check_session')
79 self.functions.append('getPackageChecksum')
80 self.functions.append('getPackageChecksumBySession')
81 self.functions.append('getSourcePackageChecksum')
82 self.functions.append('getSourcePackageChecksumBySession')
83
84 self.functions.append('getPackageMD5sum')
85 self.functions.append('getPackageMD5sumBySession')
86 self.functions.append('getSourcePackageMD5sum')
87 self.functions.append('getSourcePackageMD5sumBySession')
88
90 """ This is so the client can tell if the satellite supports session tokens or not.
91
92 This was used in rhnpush-5.5.26 and older. When there will be no such version of rhnpush in wild,
93 then this function can be safely removed."""
94 return 1
95
101
106
119
125
130
132
133 authobj.authzOrg(info)
134 force = 0
135 if 'force' in info:
136 force = info['force']
137 return uploadPackages(info, source=1, force=force,
138 caller="server.app.uploadSourcePackageInfo")
139
144
149
154
155 - def listChannel(self, channelList, username, password):
160
165
169
175
180
184
185 - def login(self, username, password):
198
206
214
219
221 """ rhnpush's --extended-test will call this function. """
222 log_debug(5, "testing check session")
223 return self.check_session(session)
224
225
231
236
238 authobj.authzChannels(channelList)
239
240 h = rhnSQL.prepare("""
241 select distinct sr.name source_rpm
242 from rhnChannel c
243 join rhnChannelNewestPackage cnp
244 on cnp.channel_id = c.id
245 join rhnPackage p
246 on cnp.package_id = p.id
247 join rhnSourceRPM sr
248 on p.source_rpm_id = sr.id
249 left join rhnPackageSource ps
250 on p.source_rpm_id = ps.source_rpm_id
251 and (p.org_id = ps.org_id or
252 (p.org_id is null and ps.org_id is null)
253 )
254 where c.label = :channel_label
255 and ps.source_rpm_id is null
256 """)
257 missing_packages = []
258 for c in channelList:
259 h.execute(channel_label=c)
260 while 1:
261 row = h.fetchone_dict()
262 if not row:
263 break
264
265 missing_packages.append([row['source_rpm'], c])
266
267 return missing_packages
268
274
279
281
282 authobj.authzOrg(info)
283
284 packageList = info.get('packages') or []
285 if not packageList:
286 log_debug(1, "No packages found; done")
287 return 0
288
289 if 'channels' not in info or not info['channels']:
290 log_debug(1, "No channels found; done")
291 return 0
292
293 channelList = info['channels']
294 authobj.authzChannels(channelList)
295
296
297 channelList = [Channel().populate({'label': x}) for x in channelList]
298
299
300
301
302 org_id = info.get('orgId')
303 if org_id == '':
304 org_id = None
305
306 batch = Collection()
307 package_keys = ['name', 'version', 'release', 'epoch', 'arch']
308 for package in packageList:
309 for k in package_keys:
310 if k not in package:
311 raise Exception("Missing key %s" % k)
312 if k == 'epoch':
313 if package[k] is not None:
314 if package[k] == '':
315 package[k] = None
316 else:
317 package[k] = str(package[k])
318 else:
319 package[k] = str(package[k])
320
321 if package['arch'] == 'src' or package['arch'] == 'nosrc':
322
323 continue
324 _checksum_sql_filter = ""
325 if 'md5sum' in package:
326 package['checksum_type'] = 'md5'
327 package['checksum'] = package['md5sum']
328
329 exec_args = {
330 'name': package['name'],
331 'pkg_epoch': package['epoch'],
332 'pkg_version': package['version'],
333 'pkg_rel': package['release'],
334 'pkg_arch': package['arch'],
335 'orgid': org_id
336 }
337
338 if 'checksum' in package and CFG.ENABLE_NVREA:
339 _checksum_sql_filter = """and c.checksum = :checksum
340 and c.checksum_type = :checksum_type"""
341 exec_args.update({'checksum_type': package['checksum_type'],
342 'checksum': package['checksum']})
343
344 h = rhnSQL.prepare(self._get_pkg_info_query %
345 _checksum_sql_filter)
346 h.execute(**exec_args)
347 row = h.fetchone_dict()
348
349 package['checksum_type'] = row['checksum_type']
350 package['checksum'] = row['checksum']
351 package['org_id'] = org_id
352 package['channels'] = channelList
353 batch.append(IncompletePackage().populate(package))
354
355 caller = "server.app.channelPackageSubscription"
356
357 backend = SQLBackend()
358 importer = ChannelPackageSubscription(batch, backend, caller=caller)
359 try:
360 importer.run()
361 except IncompatibleArchError:
362 e = sys.exc_info()[1]
363 raise_with_tb(rhnFault(50, string.join(e.args), explain=0), sys.exc_info()[2])
364 except InvalidChannelError:
365 e = sys.exc_info()[1]
366 raise_with_tb(rhnFault(50, str(e), explain=0), sys.exc_info()[2])
367
368 affected_channels = importer.affected_channels
369
370 log_debug(3, "Computing errata cache for systems affected by channels",
371 affected_channels)
372
373 schedule_errata_cache_update(affected_channels)
374 rhnSQL.commit()
375
376 return 0
377
378 - def getAnyChecksum(self, info, username=None, password=None, session=None, is_source=0):
379 """ returns checksum info of available packages
380 also does an existance check on the filesystem.
381 """
382 log_debug(3)
383
384 pkg_infos = info.get('packages')
385 channels = info.get('channels', [])
386 force = info.get('force', 0)
387 orgid = info.get('org_id')
388
389 if orgid == 'null':
390 null_org = 1
391 else:
392 null_org = None
393
394 if not session:
395 org_id, force = rhnPackageUpload.authenticate(username, password,
396 channels=channels,
397 null_org=null_org,
398 force=force)
399 else:
400 try:
401 org_id, force = rhnPackageUpload.authenticate_session(
402 session, channels=channels, null_org=null_org, force=force)
403 except rhnSession.InvalidSessionError:
404 raise_with_tb(rhnFault(33), sys.exc_info()[2])
405 except rhnSession.ExpiredSessionError:
406 raise_with_tb(rhnFault(34), sys.exc_info()[2])
407
408 if is_source:
409 ret = self._getSourcePackageChecksum(org_id, pkg_infos)
410 else:
411 ret = self._getPackageChecksum(org_id, pkg_infos)
412 return ret
413
416
425
428
434
435 _get_pkg_info_query = """
436 select
437 c.checksum_type,
438 c.checksum,
439 p.path path
440 from
441 rhnPackageEVR pe,
442 rhnPackageName pn,
443 rhnPackage p,
444 rhnPackageArch pa,
445 rhnChecksumView c
446 where
447 pn.name = :name
448 and ( pe.epoch = :pkg_epoch or
449 ( pe.epoch is null and :pkg_epoch is null )
450 )
451 and pe.version = :pkg_version
452 and pe.release = :pkg_rel
453 and ( p.org_id = :orgid or
454 ( p.org_id is null and :orgid is null )
455 )
456 and p.name_id = pn.id
457 and p.evr_id = pe.id
458 and p.package_arch_id = pa.id
459 and pa.label = :pkg_arch
460 and p.checksum_id = c.id
461 %s
462 """
463
465 log_debug(3)
466 row_list = {}
467 checksum_exists = 0
468 for pkg in pkg_infos.keys():
469
470 pkg_info = pkg_infos[pkg]
471 pkg_epoch = pkg_info['epoch']
472 if pkg_epoch is not None:
473
474 if pkg_epoch == '':
475 pkg_epoch = None
476
477 else:
478 pkg_epoch = str(pkg_epoch)
479
480 query_args = {
481 'name': pkg_info['name'],
482 'pkg_epoch': pkg_epoch,
483 'pkg_version': str(pkg_info['version']),
484 'pkg_rel': str(pkg_info['release']),
485 'pkg_arch': pkg_info['arch'],
486 'orgid': org_id,
487 }
488
489 _checksum_sql_filter = ""
490 if 'checksum' in pkg_info and CFG.ENABLE_NVREA:
491 _checksum_sql_filter = """and c.checksum = :checksum
492 and c.checksum_type = :checksum_type"""
493 query_args.update({
494 'checksum_type': pkg_info['checksum_type'],
495 'checksum': pkg_info['checksum'],
496 })
497
498 h = rhnSQL.prepare(self._get_pkg_info_query % _checksum_sql_filter)
499 row_list[pkg] = self._get_package_checksum(h, query_args)
500
501 return row_list
502
504 h.execute(**query_args)
505 row = h.fetchone_dict()
506 if not row:
507 ret = ''
508 elif row.get('path'):
509 filePath = os.path.join(CFG.MOUNT_POINT, row['path'])
510 if os.access(filePath, os.R_OK):
511 if 'checksum' in row:
512 ret = (row['checksum_type'], row['checksum'])
513 else:
514 ret = 'on-disk'
515 else:
516
517 log_error("Package not found", filePath)
518 ret = ''
519 else:
520 log_error("Package path null for package", query_args['name'])
521 ret = ''
522 return ret
523
525 log_debug(5)
526 pkg_infos = info.get('packages')
527 for pkg in pkg_infos.keys():
528 if 'md5sum' in pkg_infos[pkg]:
529 pkg_infos[pkg]['checksum_type'] = 'md5'
530 pkg_infos[pkg]['checksum'] = pkg_infos[pkg]['md5sum']
531 del(pkg_infos[pkg]['md5sum'])
532
534 log_debug(5)
535 row_list = {}
536 for k in checksum_list.keys():
537 if checksum_list[k] == '' or checksum_list[k] == 'on-disk':
538 row_list[k] = checksum_list[k]
539 elif type(checksum_list[k]) == TupleType and checksum_list[k][0] == 'md5':
540 row_list[k] = checksum_list[k][1]
541 else:
542 row_list[k] = ''
543 return row_list
544
547
553
556
562
564 """ Gives checksum info of available source packages.
565 Also does an existance check on the filesystem.
566 """
567
568 log_debug(3)
569
570 statement = """
571 select
572 ps.path path,
573 c.checksum,
574 c.checksum_type
575 from
576 rhnSourceRpm sr,
577 rhnPackageSource ps,
578 rhnChecksumView c
579 where
580 sr.name = :name
581 and ps.source_rpm_id = sr.id
582 and ( ps.org_id = :orgid or
583 ( ps.org_id is null and :orgid is null )
584 )
585 and ps.checksum_id = c.id
586 """
587 h = rhnSQL.prepare(statement)
588 row_list = {}
589 for pkg in pkg_infos.keys():
590 row_list[pkg] = self._get_package_checksum(h,
591 {'name': pkg, 'orgid': org_id})
592 return row_list
593
594
595 -def auth(login, password):
600
601
603 """ Authenticate based on a session. """
604 authobj = UserAuth()
605 authobj.auth_session(session_string)
606 return authobj
607