--- cobbler/kickgen.py
+++ cobbler/kickgen.py	2010/09/02 08:32:59
@@ -41,6 +41,9 @@
 
 from utils import _
 
+import xml.dom.minidom
+import xml.dom.ext
+from StringIO import StringIO
 
 class KickGen:
     """
@@ -60,6 +63,96 @@
         self.repos       = config.repos()
         self.templar     = templar.Templar(config)
 
+    def createAutoYaSTScript( self, document, script, name ):
+        newScript = document.createElement("script")
+        newScriptSource = document.createElement("source")
+        newScriptSourceText = document.createTextNode(script)
+        newScript.appendChild(newScriptSource)
+
+        newScriptFile = document.createElement("filename")
+        newScriptFileText = document.createTextNode(name)
+        newScript.appendChild(newScriptFile)
+
+        newScriptSource.appendChild(newScriptSourceText)
+        newScriptFile.appendChild(newScriptFileText)
+        return newScript
+
+    def addAutoYaSTScript( self, document, type, source ):
+        scripts = document.getElementsByTagName("scripts")
+        if scripts.length == 0:
+            newScripts = document.createElement("scripts")
+            document.documentElement.appendChild( newScripts )
+            scripts = document.getElementsByTagName("scripts")
+        added = 0
+        for stype in scripts[0].childNodes:
+            if stype.nodeType == stype.ELEMENT_NODE and stype.tagName == type:
+                stype.appendChild( self.createAutoYaSTScript( document, source, type+"_cobbler" ) )
+                added = 1
+        if added == 0:
+            newChrootScripts = document.createElement( type )
+            newChrootScripts.setAttribute( "config:type", "list" )
+            newChrootScripts.appendChild( self.createAutoYaSTScript( document, source, type+"_cobbler" ) )
+            scripts[0].appendChild( newChrootScripts )
+
+    def generate_autoyast(self, profile=None, system=None, raw_data=None):
+        self.api.logger.info("autoyast XML file found. Checkpoint: profile=%s system=%s" % (profile,system) )
+        nopxe = "\nwget \"http://%s/cblr/svc/op/nopxe/system/%s\" -O /dev/null"
+        runpost = "\ncurl \"http://%s/cblr/svc/op/trig/mode/post/%s/%s\" > /dev/null"
+        runpre  = "\nwget \"http://%s/cblr/svc/op/trig/mode/pre/%s/%s\" -O /dev/null"
+
+        what = "profile"
+        blend_this = profile
+        if system:
+            what = "system"
+            blend_this = system
+        blended = utils.blender(self.api, False, blend_this)
+        srv = blended["http_server"]
+
+        document = xml.dom.minidom.parseString(raw_data)
+
+        # do we already have the #raw comment in the XML?
+        addComment = 1
+        for node in document.childNodes[1].childNodes:
+            if node.nodeType == node.COMMENT_NODE and node.data.find("#end raw") != -1:
+                addComment = 0
+                break
+
+        # if not, add the "#raw" comment + some useful data for autoyast or scripts parsing
+        if addComment == 1:
+            startComment = document.createComment("\ncobbler_system_name=$system_name\ncobbler_server=$server\n#raw\n")
+            endComment = document.createComment("\n#end raw\n")
+            document.childNodes[1].insertBefore( startComment, document.childNodes[1].childNodes[1])
+            document.childNodes[1].appendChild( endComment )
+            cobblerElement = document.createElement("cobbler")
+            cobblerElementSystem = xml.dom.minidom.Element("system_name")
+            cobblerTextSystem    = document.createTextNode("$system_name")
+            cobblerElementSystem.appendChild( cobblerTextSystem )
+
+            cobblerElementServer = document.createElement("server")
+            cobblerTextServer     = document.createTextNode("$server")
+            cobblerElementServer.appendChild( cobblerTextServer )
+
+            cobblerElement.appendChild( cobblerElementServer )
+            cobblerElement.appendChild( cobblerElementSystem )
+
+            document.childNodes[1].insertBefore( cobblerElement, document.childNodes[1].childNodes[1])
+
+        name = profile.name
+        if system is not None:
+            name = system.name
+
+        if str(self.settings.pxe_just_once).upper() in [ "1", "Y", "YES", "TRUE" ]:
+            self.addAutoYaSTScript( document, "chroot-scripts", nopxe % (srv, name) )
+        if self.settings.run_install_triggers:
+            # notify cobblerd when we start/finished the installation
+            self.addAutoYaSTScript( document, "pre-scripts", runpre % ( srv, what, name ) )
+            self.addAutoYaSTScript( document, "init-scripts", runpost % ( srv, what, name ) )
+
+        raw_result = StringIO();
+        xml.dom.ext.PrettyPrint(document,raw_result)
+        return raw_result.getvalue()
+
+
     def generate_kickstart_signal(self, is_pre=0, profile=None, system=None):
         """
         Do things that we do at the start/end of kickstarts...
@@ -83,6 +176,7 @@
         blended = utils.blender(self.api, False, blend_this)
         kickstart = blended.get("kickstart",None)
 
+
         buf = ""
         srv = blended["http_server"]
         if system is not None:
@@ -224,6 +318,13 @@
                     self.settings.template_remote_kickstarts)
             if raw_data is None:
                 return "# kickstart is sourced externally: %s" % meta["kickstart"]
+            distro = profile.get_conceptual_parent()
+            if system is not None:
+                distro = system.get_conceptual_parent().get_conceptual_parent()
+            if distro.breed == "suse":
+                # AutoYaST profile
+                raw_data = self.generate_autoyast(profile,system,raw_data)
+
             data = self.templar.render(raw_data, meta, None, obj)
             return data
         except FileNotFoundException:
