Thanks - this is precisely the sort of example I was looking for 

Chris

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Glenn Bailey
Sent: 30 April 2010 16:21
To: cobbler mailing list
Subject: RE: Including python in kickstarts

> Has anyone got any documentation, hints or samples of python inside a
> kickstart?  Specifically I need to connect to a MySQL db when
> displaying the kickstart to pull some information from a database that
> will be used to configure the servers settings, and I'm having some
> trouble getting to grips with it.

Sure, I use purley python in all my kickstarts, he's an excerpt from the 
beginning of my "pre" section:

%pre --interpreter /usr/bin/python

# Begin KickstartConfigMenu.py snippet

"""
KickstartConfigMenu.py

A cobbler snippet to parse user input pre build to later be parsed by a 
kickstart file during or post. 

[email protected]
"""

import os, sys
from snack import *
import ConfigParser

sys.path.append('/usr/lib/anaconda')

def set_tty(n):
    f = open('/dev/tty%d' % n, 'a')
    os.dup2(f.fileno(), sys.stdin.fileno())
    os.dup2(f.fileno(), sys.stdout.fileno())
    os.dup2(f.fileno(), sys.stderr.fileno())

class KickstartConfigMenu:
    """
    Example usage:
    
    x = KickstartConfigMenu("entry_box")
    x.title = "Cobbler Network Config"
    x.text = "eth0 Configuration"
    x.items = ["Main", "IP Address", "Netmask", "Gateway"]
    x.gen_menu()

    This will then create a file in /tmp called "eth0_configuration.conf" with
    some options similar to this:

    [main]
    netmask = 255.255.255.0
    hostname = mordor
    gatway = 192.168.0.1
    address = 192.168.0.10

    The class will replace all spaces with an underscore and make all characters
    lowerclass. The filename is based on the "text" option passed to the class.

    Another example:

    x = KickstartConfigMenu("list_box")
    x.title = "Cobbler Swap Config"
    x.text = "Select Swap Size"
    x.items = ["1024", "2048", "4096", "8192"]
    x.gen_menu()

    Will generate a file in /tmp called "select_swap_size.conf" looking like
    this:

    [main]
    result = 4096

    """
    
    def __init__(self,type):
        self.type = type 

        self.file = None # file to write to
        self.title = None # Title of the window
        self.text = None # Text description of the items to gather
        self.items = [] # items to gather results for
        
        self.complete = None
        self.screen = None
        
    def list_box(self):
        self.results = []
        self.display_results = None

        x = ListboxChoiceWindow(self.screen, self.title, self.text, self.items, 
['Continue'], 40, 1, -1)
        self.results.append(self.items[int(x[1])])
        self.display_results = self.text + " : " + self.items[int(x[1])] + "\n"

    def entry_box(self):
        self.results = []
        self.display_results = None

        x = EntryWindow(self.screen, self.title, self.text, self.items, 1, 40, 
20, ['Continue'], "None")
        self.results = x[1]
        y = 0
        for result in x[1]:
            if self.display_results:
                self.display_results = self.display_results + self.items[y] + " 
: " + result + "\n"
            else:
                self.display_results = self.items[y] + " : " + result + "\n"
            y += 1
    
    def confirm_box(self):
        self.display_results = "Confirm Options\n\n" + self.display_results
        x = ButtonChoiceWindow(self.screen, self.title, self.display_results, 
['Continue', 'Re-input Items'], 40, 0, 0, "None")
        if x == "continue":
            self.complete = 1
    
    def error_box(self):
        ButtonChoiceWindow(self.screen, self.title, self.error_string, 
['Exit'], 40, 0, 0, "None")
        self.screen.finish()
        sys.exit()

    def write_config(self):
        config = ConfigParser.ConfigParser()
        config.add_section("main")

        self.file = "/tmp/" + self.text.replace(" ","_").lower() + ".conf"

        if self.type == "list_box":
            self.items = ['result']

        x = 0
        for result in self.results:
            config_item = self.items[x].replace(" ","_").lower()
            config.set("main",config_item,result)
            x += 1

        try:
            f = open(self.file,'w')
        except:
            self.error_string = "Unable to open " + self.file + " for writing, 
Exiting"
            self.error_box()

        config.write(f)
        f.close()    

    def gen_menu(self):
        self.screen = SnackScreen()
        try:
            while not self.complete:
                if self.type == "list_box":
                    self.list_box()
                elif self.type == "entry_box":
                    self.entry_box()
                self.confirm_box()
                self.display_results = None 

        except: 
            self.error_string = "Something went wrong, check your params"
            self.error_box()

        self.write_config()
        self.screen.finish()
_______________________________________________
cobbler mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/cobbler
_______________________________________________
cobbler mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/cobbler

Reply via email to