Hi all,

   I tried to fix the problem described in bz1058431 - Spacewalk-remove-channel 
does not remove the kickstart trees.
   You can find my patch enclosed.

Dimi

-- 
Dimitar Yordanov
Systems Management QA
#satellite-qa
From 50cd79f0607322aab39a3d34e48dd8712c87eadb Mon Sep 17 00:00:00 2001
From: Dimitar Yordanov <dyord...@redhat.com>
Date: Mon, 27 Jan 2014 20:09:53 +0100
Subject: [PATCH] 1058431 - sw-remove-channel does not rm ks trees.

---
 backend/satellite_tools/spacewalk-remove-channel | 71 ++++++++++++++++--------
 1 file changed, 47 insertions(+), 24 deletions(-)

diff --git a/backend/satellite_tools/spacewalk-remove-channel b/backend/satellite_tools/spacewalk-remove-channel
index f3b5d2e..1b9216c 100755
--- a/backend/satellite_tools/spacewalk-remove-channel
+++ b/backend/satellite_tools/spacewalk-remove-channel
@@ -11,10 +11,10 @@
 # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
 # along with this software; if not, see
 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
-# 
+#
 # Red Hat trademarks are not licensed under GPLv2. No permission is
 # granted to use or replicate Red Hat trademarks that are incorporated
-# in this software or its documentation. 
+# in this software or its documentation.
 #
 
 import sys
@@ -35,11 +35,11 @@ from spacewalk.server import rhnSQL
 from spacewalk.server.rhnPackage import unlink_package_file
 
 options_table = [
-    Option("-v", "--verbose",       action="count", 
+    Option("-v", "--verbose",       action="count",
            help="Increase verbosity"),
     Option("-l", "--list",          action="store_true",
            help="List defined channels and exit"),
-    Option("-c", "--channel",       action="append", 
+    Option("-c", "--channel",       action="append",
            help="Delete this channel (can be present multiple times)"),
     Option("-u", "--unsubscribe",   action="store_true",
            help="Unsubscribe systems registered to the specified channels"),
@@ -157,7 +157,7 @@ def main():
 def __serverCheck(labels, unsubscribe):
     sql = """
         select distinct S.org_id, S.id, S.name
-        from rhnChannel c inner join 
+        from rhnChannel c inner join
              rhnServerChannel sc on c.id = sc.channel_id inner join
              rhnServer s on s.id = sc.server_id
         where c.label in (%s)
@@ -187,13 +187,13 @@ def __serverCheck(labels, unsubscribe):
         print str(map['id']).ljust(14),
         print(map['name'])
 
-    return len(list) 
+    return len(list)
 
 
 def __unsubscribeServers(labels):
     sql = """
         select distinct sc.server_id as server_id, C.id as channel_id, c.parent_channel, c.label
-        from rhnChannel c inner join 
+        from rhnChannel c inner join
              rhnServerChannel sc on c.id = sc.channel_id
         where c.label in (%s) order by C.parent_channel
     """
@@ -231,7 +231,7 @@ def __unsubscribeServers(labels):
 def __kickstartCheck(labels):
     sql = """
         select K.org_id, K.label
-        from rhnKSData K inner join 
+        from rhnKSData K inner join
              rhnKickstartDefaults KD on KD.kickstart_id = K.id inner join
              rhnKickstartableTree KT on KT.id = KD.kstree_id inner join
              rhnChannel c on c.id = KT.channel_id
@@ -250,7 +250,7 @@ def __kickstartCheck(labels):
           "Please remove these or change their associated base channel.\n")
     print('org_id'.ljust(8)),
     print('label')
-    print("-"*20) 
+    print("-"*20)
     for map in list:
         print str(map['org_id']).ljust(8),
         print(map['label'])
@@ -260,7 +260,7 @@ def __kickstartCheck(labels):
 
 def __listChannels():
     sql = """
-        select c1.label, c2.label parent_channel 
+        select c1.label, c2.label parent_channel
         from rhnChannel c1 left outer join rhnChannel c2 on c1.parent_channel = c2.id
         order by c2.label desc, c1.label asc
     """
@@ -286,7 +286,7 @@ def __listChannels():
 def delete_channels(channelLabels, force=0, justdb=0, skip_packages=0, skip_channels=0):
     # Get the package ids
     if not channelLabels:
-        return 
+        return
 
     rpms_ids = list_packages(channelLabels, force=force, sources=0)
     rpms_paths = _get_package_paths(rpms_ids, sources=0)
@@ -297,14 +297,17 @@ def delete_channels(channelLabels, force=0, justdb=0, skip_packages=0, skip_chan
         _delete_srpms(srpms_ids)
         _delete_rpms(rpms_ids)
 
+    _delete_ks_files(channelLabels)
+
     if not justdb and not skip_packages:
         _delete_files(rpms_paths + srpms_paths)
 
+
     # Get the channel ids
     h = rhnSQL.prepare("""
-        select id, parent_channel 
-        from rhnChannel 
-        where label = :label 
+        select id, parent_channel
+        from rhnChannel
+        where label = :label
         order by parent_channel""")
     channel_ids = []
     for label in channelLabels:
@@ -327,7 +330,7 @@ def delete_channels(channelLabels, force=0, justdb=0, skip_packages=0, skip_chan
     ]
     query = """
         delete from %(table_2)s where %(link_field)s in (
-            select id 
+            select id
               from %(table_1)s
              where %(channel_field)s = :channel_id
         )
@@ -459,7 +462,7 @@ def _delete_srpms(srcPackageIds):
 def _delete_rpms(packageIds):
     if not packageIds:
         return
-    group = 300 
+    group = 300
     toDel = packageIds[:]
     print "Deleting package metadata (" + str(len(toDel)) + "):"
     pb = ProgressBar(prompt='Removing:         ', endTag=' - complete',
@@ -476,14 +479,14 @@ def _delete_rpms(packageIds):
 def _delete_rpm_group(packageIds):
 
     references = [
-        'rhnChannelPackage', 
-        'rhnErrataPackage', 
-        'rhnErrataPackageTMP', 
-        'rhnPackageChangelogRec', 
-        'rhnPackageConflicts', 
-        'rhnPackageFile', 
-        'rhnPackageObsoletes', 
-        'rhnPackageProvides', 
+        'rhnChannelPackage',
+        'rhnErrataPackage',
+        'rhnErrataPackageTMP',
+        'rhnPackageChangelogRec',
+        'rhnPackageConflicts',
+        'rhnPackageFile',
+        'rhnPackageObsoletes',
+        'rhnPackageProvides',
         'rhnPackageRequires',
         'rhnPackageRecommends',
         'rhnPackageSuggests',
@@ -540,6 +543,26 @@ def _get_package_paths(package_ids, sources=0):
 
     return pdict.keys()
 
+def _delete_ks_files(channel_lables):
+    sql = """
+        select kt.base_path
+        from rhnChannel c
+        left join  rhnKickstartableTree kt on c.id = kt.channel_id
+        where c.label in (%s);
+    """
+
+    params, bind_params = _bind_many(channel_lables)
+    bind_params = string.join(bind_params, ', ')
+    h = rhnSQL.prepare(sql % (bind_params))
+    h.execute(**params)
+    list = h.fetchall_dict()
+
+    for map in list:
+        path = os.path.join(CFG.MOUNT_POINT, str(map['base_path']))
+        if not os.path.exists(path):
+            log_debug(1, "Not removing %s: no such file" % path)
+            continue
+        shutil.rmtree(path)
 
 
 def releaseLOCK():
-- 
1.8.3.1

_______________________________________________
Spacewalk-devel mailing list
Spacewalk-devel@redhat.com
https://www.redhat.com/mailman/listinfo/spacewalk-devel

Reply via email to