Adds command to export configchannel content to JSON format file
---
 spacecmd/src/lib/configchannel.py |  124 +++++++++++++++++++++++++++++++++++++
 1 files changed, 124 insertions(+), 0 deletions(-)

diff --git a/spacecmd/src/lib/configchannel.py 
b/spacecmd/src/lib/configchannel.py
index 6ecc369..0ab3734 100644
--- a/spacecmd/src/lib/configchannel.py
+++ b/spacecmd/src/lib/configchannel.py
@@ -708,4 +708,128 @@ def do_configchannel_verifyfile(self, args):
 
     logging.info('Action ID: %i' % action_id)
 
+####################
+
+def help_configchannel_export(self):
+    print 'configchannel_export: export config channel(s) to json format file'
+    print '''usage: configchannel_export <CHANNEL>... [options]
+options:
+    -f outfile.json : specify an output filename, defaults to <CHANNEL>.json
+                      if exporting a single channel, ccs.json for multiple
+                      channels, or cc_all.json if no CHANNEL specified
+                      e.g (export ALL)
+
+Note : CHANNEL list is optional, default is to export ALL'''
+
+def complete_configchannel_export(self, text, line, beg, end):
+    return tab_completer(self.do_configchannel_list('', True), text)
+
+def export_configchannel_getdetails(self, channel):
+    # Get the cc details
+    logging.info("Getting config channel details for %s" % channel)
+    details = self.client.configchannel.getDetails(self.session, channel)
+    files = self.client.configchannel.listFiles(self.session, channel)
+    details['files'] = []
+    paths = [f['path'] for f in files]
+    logging.debug("Found files %s for %s" % (paths, channel))
+    fileinfo = self.client.configchannel.lookupFileInfo(self.session, \
+                    channel, paths)
+    # Now we strip the datetime fields from the Info structs, as they
+    # are not JSON serializable with the default encoder, and we don't
+    # need them on import anyway
+    # We also strip some other fields which are not useful on import
+    # This is a bit complicated because the createOrUpdateFoo functions
+    # take two different struct formats, which are both different to 
+    # the format returned by lookupFileInfo, doh!
+    # We get:                         We need:
+    #                                 (file/dir)      (symlink)
+    # string "type"                   Y               Y
+    # string "path"                   Y               Y
+    # string "target_path"            N               Y
+    # string "channel"                N               N
+    # string "contents"               Y               N
+    # int "revision"                  N (auto)        N (auto)
+    # dateTime.iso8601 "creation"     N               N
+    # dateTime.iso8601 "modified"     N               N
+    # string "owner"                  Y               N
+    # string "group"                  Y               N
+    # int "permissions"               Y (as string!)  N
+    # string "permissions_mode"       N               N
+    # string "selinux_ctx"            Y               Y
+    # boolean "binary"                N               N
+    # string "md5"                    N               N
+    # string "macro-start-delimiter"  Y               N
+    # string "macro-end-delimiter"    Y               N
+    for f in fileinfo:
+        for k in [ 'channel', 'revision', 'creation', 'modified', \
+                        'permissions_mode', 'binary', 'md5' ]:
+            if f.has_key(k):
+                del f[k]
+        if f['type'] == 'symlink':
+            for k in [ 'contents', 'owner', 'group', 'permissions', \
+                            'macro-start-delimiter', 'macro-end-delimiter' ]:
+                if f.has_key(k):
+                    del f[k]
+        else:
+            if f.has_key('target_path'):
+                del f['target_path']
+            f['permissions'] = str(f['permissions'])
+
+    details['files'] = fileinfo
+    return details
+
+def do_configchannel_export(self, args):
+    options = [ Option('-f', '--file', action='store') ]
+    (args, options) = parse_arguments(args, options)
+
+    filename=""
+    if options.file != None:
+        logging.debug("Passed filename do_configchannel_export %s" % \
+            options.file)
+        filename=options.file
+
+    # Get the list of ccs to export and sort out the filename if required
+    ccs=[]
+    if not len(args):
+        if len(filename) == 0:
+            filename="cc_all.json"
+        logging.info("Exporting ALL config channels to %s" % filename)
+        ccs = self.do_configchannel_list('', True)
+    else:
+        # allow globbing of configchannel channel names
+        ccs = filter_results(self.do_configchannel_list('', True), args)
+        logging.debug("configchannel_export called with args %s, ccs=%s" % \
+            (args, ccs))
+        if (len(ccs) == 0):
+            logging.error("Error, no valid config channel passed, " + \
+                "check name is  correct with spacecmd configchannel_list")
+            return
+        if len(filename) == 0:
+            # No filename arg, so we try to do something sensible:
+            # If we are exporting exactly one cc, we default to ccname.json
+            # otherwise, generic ccs.json name
+            if len(ccs) == 1:
+                filename="%s.json" % ccs[0]
+            else:
+                filename="ccs.json"
+
+    # Dump as a list of dict
+    ccdetails_list=[]
+    for c in ccs:
+        logging.info("Exporting cc %s to %s" % (c, filename))
+        ccdetails_list.append(self.export_configchannel_getdetails(c))
+
+    logging.debug("About to dump %d ccs to %s" % \
+        (len(ccdetails_list), filename))
+    # Check if filepath exists, if it is an existing file
+    # we prompt the user for confirmation
+    if os.path.isfile(filename):
+        if not self.user_confirm("File %s exists, " % filename + \
+                    "confirm overwrite file? (y/n)"):
+            return
+    if json_dump_to_file(ccdetails_list, filename) != True:
+        logging.error("Error saving exported config channels to file" % \
+            filename)
+        return
+
 # vim:ts=4:expandtab:
-- 
1.7.1

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

Reply via email to