Ejemplo n.º 1
0
def Channel_02_UpdateArchive(*args):
    """       create or update archive channels according to the settings
       in the server config to the appointed channel.
       if test is given only shows what will be done.
       Errata are always cloned for Archives.
  """
    from __main__ import dbg, prgargs, data, modlog
    dbg.entersub()
    dbg.dprint(2, "ARGS", args)
    ses = data['conn']['ses']
    key = data['conn']['key']
    chmap = data['chmap']['02_datefreeze']
    chlist = data['sumastate']['channels']
    testonly = False
    printout = False
    clone = True
    ###### Check args and do it ################################################
    if len(args) > 0:
        if "help" in args:
            print(f"Usage: {dbg.myself().__name__} [test] [-v]")
            print(f"{dbg.myself().__doc__}")
            dbg.dprint(64, "current config from data.chmap.02_datefreeze",
                       chmap, "End data['chmap']['02_datefreeze']")
            print("")
            dbg.leavesub()
            return
        elif "test" in args:
            testonly = 1
        else:
            pass
    modlog.info(f"----> {dbg.myself().__name__}")
    for (source, target, parent) in chmap:
        modlog.info(f"{source} -> {target}")
        ok = uyuni_channels.Merge_Channel(source,
                                          target,
                                          parent,
                                          test=testonly,
                                          clone=clone)
        if not ok:
            dbg.dprint(256, F"ArchiveUpdate to {target} did not succeed")
            modlog.error(F"ArchiveUpdate to {target} did not succeed")
    modlog.info(f"<---- {dbg.myself().__name__}")
    dbg.leavesub()
    return
Ejemplo n.º 2
0
def Channel_03_MergeArchive(*args):
    """       merge the latest archive channel into the test channel.
       See server config data['chmap']['03_arcmerge']
       if test is given only shows what will be done.
  """
    from __main__ import dbg, prgargs, data, modlog
    dbg.entersub()
    dbg.dprint(2, "ARGS", args)
    ses = data['conn']['ses']
    key = data['conn']['key']
    clone = False
    testonly = False
    if len(args) > 0:
        if "help" in args:
            print(f"Usage: {dbg.myself().__name__} [test] [-v]")
            print(f"{dbg.myself().__doc__}")
            dbg.leavesub()
            return
        if "test" in args:
            testonly = True

    chmap = data['chmap']['03_arcmerge']
    modlog.info(f"----> {dbg.myself().__name__}")
    for (source, target, parent) in chmap:
        modlog.info(f"{source} -> {target}")
        ok = uyuni_channels.Merge_Channel(source,
                                          target,
                                          parent,
                                          test=testonly,
                                          clone=clone)
        if not ok:
            line = F"Merging {source} to {target} did not succeed"
            dbg.dprint(256, line)
            modlog.error(line)
    modlog.info(f"<---- {dbg.myself().__name__}")
    dbg.leavesub()
    return
def Merge_Channel(source,target,parent,test=False,clone=False):
  """ Merge content and errata from source channel to target channel. All 
      channel parameters are label strings. parent is the label of target's parent.
      In addition the kwargs test and clone are accepted with boolean values.
      Returns 0 on failure, in case of test or success returns 1
  """    
  from __main__ import dbg,prgargs,data,modlog
  dbg.entersub()
  dbg.dprint(2,"verbose :",dbg.setlvl(),", test :", test,",clone :",clone)
  #dbg.dprint(256,dbg.myname())
  ses = data['conn']['ses']
  key = data['conn']['key']
  ##### Falls test angegeben wurde nur die Aktionen ausgeben
  if test:
    dbg.setlvl(64)
    #dbg.dprint\(64,"Would merge",source, "to", target)
  ##### Check if channels exist
  clist = ses.channel.listSoftwareChannels(key)
  source_exists = next((True for channel in clist if channel['label'] == source),False)
  target_exists = next((True for channel in clist if channel['label'] == target),False)
  clone_stat = 0
  ##### stop if no source
  if not source_exists:
    dbg.dprint(0,"Cannot merge non existing",source)
    modlog.error(f"Cannot merge non existing {source}")
    dbg.setlvl()
    dbg.leavesub()
    return clone_stat
  ##### If clone is set, clone channel completely if channel does not yet exist, 
  ##### otherwise merge packages, but clone errata
  if clone :
    dbg.dprint(64,"Clone:",clone, "   ",source, "->", target)
    ##### Clone is set, but channel already exists 
    if target_exists:
      dbg.dprint(64,"Channel exists, clone pkgs + errata")
      if test :
        dbg.dprint(64,"  - OK - Would merge packages and clone errata...")
        clone_stat = 1
      else:  
        ### get the missing errata 
        srcerrata = ses.channel.software.listErrata(key,source)
        tgterrata = ses.channel.software.listErrata(key,target)
        srcnames = set(d['advisory_name'] for d in srcerrata)
        tgtnames = set(d['advisory_name'].replace('CL-','',1) for d in tgterrata)
        srconly  = sorted(list(srcnames.difference(tgtnames))) 
        dbg.dprint(64,"Missing Errata:",srconly)
        failure = 0
        try:
          resultMP = ses.channel.software.mergePackages(key,source,target)
          dbg.dprint(64, "Packages merged :", len(resultMP))
          modlog.info(f"Packages merged : {len(resultMP)}")
        except:
          dbg.dprint(256,"Merging Packages did not succeed")
          modlog.error("Merging Packages did not succeed")
          failure += 1
        ### break patchlist into chunks
        chunknum = 0
        chunk = []
        for errnum in range(0,len(srconly)):
          if errnum // 10 == chunknum:
            chunk.append(srconly[errnum])
          else :  
            try:
              resultCE = ses.errata.clone(key,target,chunk)
              dbg.dprint(64, "Errata chunk ",chunknum, "cloned   :", len(resultCE))
              #modlog.info(f"Errata chunk {chunknum} cloned   : {len(resultCE)}")
            except: 
              dbg.dprint(256, "Errata chunk ",chunknum, "could not be cloned")
              modlog.error(f"Errata chunk {chunknum} could not be cloned") 
              dbg.dprint(0, "chunk", chunknum, chunk)
              failure += 1
            chunk = []
            chunknum += 1
            chunk.append(srconly[errnum])  
        ### proces last chunk 
        if len(chunk):
          try:
            resultCE = ses.errata.clone(key,target,chunk)
            dbg.dprint(64, "Errata chunk ",chunknum, "cloned   :", len(resultCE))
            modlog.info(f"Errata chunk {chunknum} cloned   : {len(resultCE)}")
          except: 
            dbg.dprint(256, "Errata chunk ",chunknum, "could not be cloned")
            modlog.error(f"Errata chunk {chunknum} could not be cloned") 
            dbg.dprint(0, "chunk", chunknum, chunk)
            failure += 1
        if failure:  
          dbg.dprint(256,"Merging packages or cloning errata did not succeed")
          modlog.error(f"Merging packages or cloning errata did not succeed") 
          clone_stat = 0
        else:
          result = f"Errata cloned   : {len(srconly)}" 
          dbg.dprint(64,result)
          modlog.info(f"{result}")
          clone_stat = 1
          
    ##### clone is set, target does not exist
    else:
      srcdetails = ses.channel.software.getDetails(key,source)
      dbg.dprint(0,"Arch_label of source", srcdetails['arch_label'])
      ### only if parent is not "" and not existing create an empty parent
      if parent:
        parent_exists = next((True for channel in clist if channel['label'] == parent),False)
        if not parent_exists:
          dbg.dprint(0,"Targets parent does not exist")
          psumm       = 'custom parent channel'
          parch       = srcdetails['arch_label']
          try:
            result = ses.channel.software.create(key,parent,parent,psumm,parch,"")
            dbg.dprint(64,"Parent Channel", parent, "created",result)
            modlog.info(f"Parent Channel {parent} created")
          except:
            err = "error exit: \"{0}\" in {1}".format(sys.exc_info()[1],sys.exc_info()[2:])
            dbg.dprint(256,err)
            modlog.error(f"{err}")
            dbg.setlvl()
            dbg.leavesub()
            return clone_stat
      ### non empty parent was created
      details = {
              'label'        : target,
              'name'         : target,
              'summary'      : "Clone of "+source,
              'description'  : "Created at "+datetime.date.today().strftime("%Y-%m-%d"),
              'parent_label' : parent,
              'checksum'     : srcdetails['checksum_label'],
      }  
      try:
        result = ses.channel.software.clone(key,source,details,False)
        dbg.dprint(64,"Channel geclont mit der ID ",result)
        modlog.info(f"Channel geclont mit der ID {result}")
        clone_stat = 1
      except: 
        err = "error exit: \"{0}\" in {1}".format(sys.exc_info()[1],sys.exc_info()[2:])
        dbg.dprint(256,err)
        modlog.error(f"{err}")
        dbg.dprint(256,"Could not clone",source,"to",target,"with Error")

  ##### Clone ist nicht gesetzt => nur mergen
  else:
    dbg.dprint(64,"Merge:",source,"->", target)
    if target_exists:
      dbg.dprint(64,"Channels ok, merge pkgs + errata:" )
      if test:
        dbg.dprint(64,"  - OK - Would merge packages and errata...")
        clone_stat = 1
      else:
        try: 
          resultMP = ses.channel.software.mergePackages(key,source,target)
          dbg.dprint(64, "Packages merged :", len(resultMP))
          modlog.info(f"Packages merged : {len(resultMP)}")
          resultME = ses.channel.software.mergeErrata(key,source,target) 
          dbg.dprint(64, "Errata merged   :", len(resultME))
          modlog.info(f"Errata merged   : {len(resultME)}")
          clone_stat = 1
        except:
          err = "error exit: \"{0}\" in {1}".format(sys.exc_info()[1],sys.exc_info()[2:])
          dbg.dprint(256,err)
          modlog.error(f"{err}")
          dbg.dprint(256,"Could not merge",target,"with Error")

    ##### Target does not exist 
    else:
      dbg.dprint(0,"Cannot merge",source,"to non existing",target)
      modlog.error(f"Cannot merge {source} to non existing {target}")
      if test:
        dbg.dprint(64,"Would try to create new channel",target)
        clone_stat = 1
      else:
        dbg.dprint(0,"Trying to create new channel:",target)  
        try:
          archLabel  = 'channel-' + target.split('-')[-1]
          summary    = "Created from "+source
          clone_stat = ses.channel.software.create(key,target,target,summary,archLabel,parent)
          if clone_stat:
            dbg.dprint(0,"Channel Creation succeeded. Run again to merge channels!") 
            modlog.warning("Channel Creation succeeded. Run again to merge channels!")
        except: 
          dbg.dprint(256,"Could not create Channel",target)
          modlog.error(f"Could not create Channel {target}")
      
  ##### Cleanup and return status
  dbg.dprint(64,"----------- Status: ",clone_stat, " --------------------")
  #modlog.info(f"--- Status: {clone_stat}")
  dbg.setlvl()
  dbg.leavesub()
  return clone_stat
Ejemplo n.º 4
0
def Channel_02_applyExcludes(*args):
    """       Apply the Content Exclude settings in the server configuration file
       to the named channels. 
       ->   'test' : only shows all packages and errata that will be removed
       ->   'yes'  : silently removes errata and all packages found by 'test'.
       -> Without args asks for confirmation of each pkg referend by an 
       erratum.

  Note: if you confirm the deletion of any pkg the referencing
        erratum will be removed as well without further confirmation.
  """
    # Note: If there is a cloned erratum in some other channel, but not in the
    #            requested one, packages are deleted nevertheless.
    from __main__ import dbg, prgargs, data, modlog
    import re
    dbg.entersub()
    dbg.dprint(2, "ARGS", args)
    ses = data['conn']['ses']
    key = data['conn']['key']
    confirm = True
    test = False
    rest = []
    ### Set default debug to show everyhing for interactive use
    dbg.setlvl(6)
    ###### Check args  #################################################
    modlog.info(f"----> {dbg.myself().__name__}")
    for i in range(0, len(args)):
        if args[i] == "help":
            print(f"Usage: {dbg.myself().__name__} [ test | yes ]")
            print(f"{dbg.myself().__doc__}")
            dbg.dprint(192, "current config from data.contentexcludes",
                       data['contentexcludes'], "End data['contentexcludes']")
            dbg.leavesub()
            return
        elif args[i] == "test":
            test = True
            # dbg.setlvl(6)
        elif args[i] == "yes":
            confirm = False
            dbg.setlvl(0)
        else:
            pass
    ###### Execute  #################################################

    for channel in data['contentexcludes'].keys():
        modlog.info(f"Working on {channel}")
        excludes = data['contentexcludes'][channel]
        fpats = {}
        fpkgs = {}
        regexes = {}
        names = list()
        for (name, rstring, vcompare, version) in excludes:
            if name not in regexes:
                regexes[name] = {}
                regexes[name]['regex'] = re.compile(rstring)
                regexes[name]['operator'] = vcompare
                regexes[name]['version'] = version
            names.append(rstring)
        channelregex = re.compile('|'.join(names))
        #    dbg.dprint(0,"Start regex", regex,"End regex" )
        dbg.dprint(2, "-----", channel)
        pkgs = ses.channel.software.listAllPackages(key, channel)
        for pkg in pkgs:
            ### If the name matches any excluderegex
            if channelregex.search(pkg['name']):
                pkgid = pkg['id']
                ### get the matching part of channelregex
                symname = ''
                for name in regexes:
                    if regexes[name]['regex'].search(pkg['name']):
                        symname = name
                        break
                ### prepare to compare version of this package
                p1 = packaging.version.parse(pkg['version'])
                p2 = packaging.version.parse(regexes[symname]['version'])
                compare = regexes[symname]['operator']
                cstring = repr(compare).replace("<built-in function ", "")
                cstring = cstring.replace(">", "")
                if compare(p1, p2):
                    dbg.dprint(
                        4,
                        f"{pkg['name']} Id: {pkgid}, v: {p1} found. excluding because {cstring} {p2}"
                    )
                    ##### get the errata of this package, only handle cloned ones and
                    ##### only remove them from this channel
                    errata = ses.packages.listProvidingErrata(key, pkgid)
                    if len(errata):
                        for err in errata:
                            advisory = err['advisory']
                            if advisory.startswith("CL-"):
                                dbg.dprint(4, " ", advisory, "found for pkg",
                                           pkg['name'])
                                if advisory not in fpats:
                                    fpats[advisory] = dict(
                                    )  #fpats.get(advisory,dict())
                                else:
                                    dbg.dprint(4, "  ", "is Duplicate erratum")
                                    break
                                pkgs_to_remove = ses.errata.listPackages(
                                    key, advisory)
                                for p in pkgs_to_remove:
                                    dbg.dprint(
                                        4,
                                        f"    remove {p['name']}, v: {p['version']}, id: {p['id']}"
                                    )
                                    fpats[advisory][
                                        p['id']] = fpats[advisory].get(
                                            p['id'], 0) + 1
                                    fpkgs[p['id']] = fpkgs.get(p['id'], dict())
                                    fpkgs[p['id']]['version'] = p['version']
                                    fpkgs[p['id']]['name'] = p['name']
                                    fpkgs[p['id']]['release'] = p['release']
                                    fpkgs[p['id']]['refby'] = fpkgs[
                                        p['id']].get('refby', list())
                                    fpkgs[p['id']]['refby'].append(advisory)
                    else:  # no erratum for this package
                        fpkgs[pkgid] = fpkgs.get(pkgid, dict())
                        fpkgs[pkgid]['version'] = pkg['version']
                        fpkgs[pkgid]['name'] = pkg['name']
                        fpkgs[pkgid]['release'] = pkg['release']
        pkgs_to_remove = list()
        pats_to_remove = list()
        ### show patches and packages and query for remove
        if test or confirm:
            dbg.dprint(2, "---------------------------------------------")
            dbg.dprint(2, "Would remove", len(fpats.keys()), "patches and",
                       len(fpkgs.keys()), "Packages")
            dbg.dprint(2, "------------  Patches to remove  ------------")
            pkglist = list()
            for patch in sorted(fpats):
                dbg.dprint(2, patch)
                for pkid in sorted(fpats[patch]):
                    pkglist.append(pkid)
                    dbg.dprint(2, "  ", pkid, fpkgs[pkid]['name'],
                               fpkgs[pkid]['version'], fpkgs[pkid]['release'])
                    if confirm:
                        remove = myinput.query_yes_no(
                            "            Mark this package for removal", "no")
                        if remove:
                            pkgs_to_remove.append(pkid)
                            if patch not in pats_to_remove:
                                pats_to_remove.append(patch)

            dbg.dprint(2, "------------  Packages to remove  -----------")
            for pkid in sorted(fpkgs):
                if pkid not in pkglist:
                    dbg.dprint(2, "  ", pkid, fpkgs[pkid]['name'],
                               fpkgs[pkid]['version'], fpkgs[pkid]['release'])
                    if confirm:
                        remove = myinput.query_yes_no(
                            "            Mark this package for removal", "no")
                        if remove:
                            pkgs_to_remove.append(pkid)
        ### yes is set, just take the complete lists
        else:
            pats_to_remove = list(fpats)
            pkgs_to_remove = list(fpkgs)

        if test:
            dbg.dprint(0, 'pats_to_remove', pats_to_remove,
                       "End pats_to_remove")
            dbg.dprint(0, 'pkgs_to_remove', pkgs_to_remove, "pkgs_to_remove")
        else:
            ### Remove patches and packages in the to_remove lists
            if len(pats_to_remove):
                dbg.dprint(2, "pats_to_remove", pats_to_remove,
                           "End pats_to_remove")
                try:
                    result = ses.channel.software.removeErrata(
                        key, channel, pats_to_remove, False)
                    modlog.info(f"Errata removed  : {len(pats_to_remove)}")
                except:
                    dbg.dprint(256, "Removing", pats_to_remove, "failed")
                    modlog.error(f"Removing {pats_to_remove} failed")
            if len(pkgs_to_remove):
                dbg.dprint(2, "pkgs_to_remove", pkgs_to_remove,
                           "pkgs_to_remove")
                try:
                    result = ses.channel.software.removePackages(
                        key, channel, pkgs_to_remove)
                    modlog.info(f"Packages removed: {len(pkgs_to_remove)}")
                except:
                    dbg.dprint(256, "Removing", pkgs_to_remove, "failed")
                    modlog.error(f"Removing {pkgs_to_remove} failed")

    modlog.info(f"<---- {dbg.myself().__name__}")
    dbg.setlvl()
    dbg.leavesub()
    return
Ejemplo n.º 5
0
def Channel_01_MergeVendor(*args):
    """       merge vendor channels according to the settings"
       in the server config to the appointed channel.
       if test is given only shows what will be done.
       if merge is given merges the errata instead of cloning.
       help -vv shows additional commandline hints
  """
    addontext = """
  ------------------- Additional Hints using commandline ----------------------
  A. clone a vendor tree:
    spacecmd -u user -p pwd -s osv9-suse-mgmt.voip.sen -- \\
      softwarechannel_clonetree  --source -channel sles12-sp4-pool-x86_64 -p "itg-"

  B. Add SuSE Channel
    mgr-sync -v add channel sles12-sp3-ltss-updates-x86

  C. Remove suse Channels
    spacewalk-remove-channel -c sles12-sp2-ltss-updates-x86_64
    + spacecmd repo_delete sles12-sp2-ltss-updates-x86_64

  D. remove custom channels 
    spacecmd softwarechannel_delete itg-sles12-sp2-ltss-updates-x86_64
  """
    from __main__ import dbg, prgargs, data, modlog
    #  import uyuni_channels
    dbg.entersub()
    dbg.dprint(2, "ARGS", args)
    ses = data['conn']['ses']
    key = data['conn']['key']
    chmap = data['chmap']['01_vendorclone']
    chlist = data['sumastate']['channels']
    t = False
    cl = True
    if len(args) > 0:
        if "help" in args:
            print(f"Usage: {dbg.myself().__name__} [test] [merge] [-v]")
            print(f"{dbg.myself().__doc__}")
            dbg.dprint(64, "current config from data.chmap.01_vendorclone",
                       data['chmap']['01_vendorclone'],
                       "End data['chmap']['01_vendorclone']")
            dbg.dprint(128, addontext)
            print("")
            dbg.leavesub()
            return
        if "test" in args:
            t = True
        if "merge" in args:
            cl = False

    #  dbg.dprint(256,t,p,cl)
    modlog.info(f"----> {dbg.myself().__name__}")
    for (source, target, parent) in chmap:
        modlog.info(f"{source} -> {target}")
        ok = uyuni_channels.Merge_Channel(source,
                                          target,
                                          parent,
                                          test=t,
                                          clone=cl)
        if not ok:
            dbg.dprint(256, F"MergeVendor to {target} did not succeed")
            modlog.error(F"MergeVendor to {target} did not succeed")
    modlog.info(f"<---- {dbg.myself().__name__}")
    dbg.leavesub()
    return