From 4c6c657c881f9d381d53ed72b720162dfaf8f89a Mon Sep 17 00:00:00 2001 From: Esgariot Date: Mon, 9 Jan 2017 06:36:21 +0100 Subject: [PATCH] fingers crossed --- README.md | 2 +- ks.cfg | 71 ++++++------ pykickxe.conf | 43 ++++++++ pykickxe.py | 104 ++++++++++++------ pykickxe_conf.py | 15 +-- tftproot/pxelinux.cfg/autoinstall.menu | 10 -- tftproot/pxelinux.cfg/autoinstall.menu.source | 10 ++ tftproot/pxelinux.cfg/default | 8 +- 8 files changed, 159 insertions(+), 104 deletions(-) create mode 100644 pykickxe.conf create mode 100644 tftproot/pxelinux.cfg/autoinstall.menu.source diff --git a/README.md b/README.md index 552e390..331a6ad 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ This will become a beautiful parser and generator for preseed file for debian an 2. Po pobraniu uruchomieniu kernela i załadowaniu ramdisku z serwera tftp uruchomionego przez dnsmasq system szuka drzewa katalogów związanych z dystrybucją. Znajduje wszystko, czego potrzebuje dzięki serwerowi vsftp (z jakiegoś powodu dnsmasqowy tftp nie udostępnia plików jako "normalny" ftp, tylko dla PXE). - W korzeniu serwera ftp jest katalog ```///``` z którego pobiera pliki host na którym jest uruchomiona instalacja. Można też przeprowadzić instalację przez Internet i nie udostępniać całej dystrybucji na serwerze, ale ma to bardzo małe szanse powodzenia przez duże opóźnienie, które nie jest brane pod uwagę przez dracut + W korzeniu serwera ftp jest katalog ```//``` z którego pobiera pliki host na którym jest uruchomiona instalacja. Można też przeprowadzić instalację przez Internet i nie udostępniać całej dystrybucji na serwerze, ale ma to bardzo małe szanse powodzenia przez duże opóźnienie, które nie jest brane pod uwagę przez dracut 3. Żeby to wszystko zautomatyzować do parametrów kernela RedHatowych dystrybucji dodaję ```ks= <ścieżka-do-pliku-kickstart>``` 4. Wszystko powyższe można zrobić manualnie, mój skrypt pykickxe.py dąży do zautomatyzowania tego ( z różną skutecznością ). Np. wybór pliku kickstart, kernela i parametrów kernela odbywa się na zasadzie złożenia pliku kickstart, znalezieniu znaczników do zastąpienia zaczynających się od ```$``` w ```/pxelinux.cfg/autoinstall.menu``` i skopiowania pliku kickstart do ```/util/``` diff --git a/ks.cfg b/ks.cfg index c8b87ac..44ad3bb 100644 --- a/ks.cfg +++ b/ks.cfg @@ -1,41 +1,34 @@ -#Generated by Kickstart Configurator -#platform=AMD64 or Intel EM64T +#platform=x86, AMD64, or Intel EM64T +#version=DEVEL +# Install OS instead of upgrade +install +# Keyboard layouts +keyboard 'pl2' +# Root password +rootpw --iscrypted $1$ZIgQqPR3$OY8ivoaWxmw56pWomI9Xl/ +# Use network installation +url --url="ftp://192.168.0.1/centos" +# System language +lang pl_PL +# Firewall configuration +firewall --disabled +# System authorization information +auth --useshadow --passalgo=sha512 +# Use text mode install +text +firstboot --disable +# SELinux configuration +selinux --enforcing -#System language -lang en_US -#Language modules to install -langsupport pl_PL --default=en_US -#System keyboard -keyboard us -#System mouse -mouse -#System timezone -timezone --utc Europe/Warsaw -#Root password -rootpw --iscrypted $1$z0BY0NkL$Necy0Mqj6AYqatrm9Pi.Y. -#Initial user -user justuser --fullname "delete me later" --iscrypted --password $1$tmQA0Jxc$/QUItyxu8npyxjoKgEgBv0 -#Reboot after installation +# Network information +network --bootproto=dhcp --device=enp0s3 +# Reboot after installation reboot -#Use text mode install -text -#Install OS instead of upgrade -install -#Use Web installation -url --url ftp://192.168.0.1/debian/kickstart -#System bootloader configuration -bootloader --location=mbr -#Clear the Master Boot Record -zerombr yes -#Partition clearing information -clearpart --all --initlabel -#System authorization infomation -auth --useshadow --enablemd5 -#Network information -network --bootproto=dhcp --device=eth0 -#Firewall configuration -firewall --disabled -#X Window System configuration information -xconfig --depth=32 --resolution=1024x768 --defaultdesktop=KDE --startxonboot -#Run the Setup Agent on first boot -firstboot --enable \ No newline at end of file +# System timezone +timezone Europe/Warsaw --isUtc +# System bootloader configuration +bootloader --location=none +# Clear the Master Boot Record +zerombr +# Partition clearing information +clearpart --all --initlabel \ No newline at end of file diff --git a/pykickxe.conf b/pykickxe.conf new file mode 100644 index 0000000..5d37d48 --- /dev/null +++ b/pykickxe.conf @@ -0,0 +1,43 @@ +# Pykickxe config file that will be parsed using python's 2.7 ConfigParser + +[DEFAULT] +last_updated = 09.01.17 +ftp_root = /srv/tftp/ +ftp_url = ftp://192.168.0.1 +default_distro = centos +default_autocfg = %(default_distro)s/ks.cfg +default_kernel = %(default_distro)s/images/vmlinuz +default_append = +pxe_cfgPath = ./pxeappend.cfg + +# remember to use helpText with raw parser! +help_text = This program requires that the root from where pxe gets files has following structure: /<~copied content of iso image~> for everything, especially /images/pxeboot for kernel and ramdisk, kickstart file in /ks.cfg or passed manually, (will get copied to util/ks.cfg) + + +[debian] +name = Debian +dir = debian +auto_file = +auto_param = preseed\= +kernel = %(dir)s/linux +append = initrd\=%(dir)/initrd.gz +url = + + +[fedora] +name = Fedora +dir = fedora +auto_file = %(dir)s/ks.cfg +auto_param = ks= +kernel = %(dir)s/images/pxeboot/vmlinuz +append = initrd=%(dir)s/initrd.img inst.repo=%(ftp_url)s/%(dir)s devfs=nomount +url = + +[centos] +name = CentOS +dir = centos +auto_file = %(dir)s/ks.cfg +auto_param = ks= +kernel = %(dir)s/images/pxeboot/vmlinuz +append = initrd=%(dir)s/initrd.img method=%(ftp_url)s/%(dir)s devfs=nomount ip=dhcp +url = \ No newline at end of file diff --git a/pykickxe.py b/pykickxe.py index edf6e18..876945a 100644 --- a/pykickxe.py +++ b/pykickxe.py @@ -1,24 +1,15 @@ import argparse -import pykickxe_conf import os +import ConfigParser def listConfig(): # TODO: Implement listConfig #p3 return -def downloadFromURL(args): # TODO: Implement download #p1 - distroURL = '' - if 'debian' == args.distro: - distroURL = pykickxe_conf.debianURL - elif 'fedora' == args.distro: - distroURL = pykickxe_conf.fedoraURL - else: - print args.distro + ' is not supported' - return - if args.verbose is True: - print 'downloading ' + args.distro + ' from ' + distroURL +def downloadFromURL(args, config): # TODO: Implement download #p1 # TODO: check if wget present #p2 + distroURL = config.get(args.distro, 'url', 1) os.system('wget ' + distroURL) filePath = os.path.split(distroURL) # urllib.urlretrieve(distroURL) # TODO: Decide on this or wget #p3 @@ -27,23 +18,63 @@ def downloadFromURL(args): # TODO: Implement download #p1 return filePath -def copyToTftp(): - os.system('cp ./tftproot/. ' + pykickxe_conf.tftpRoot) +def copyFiles(args, config): + os.system('rsync --progress -av ./tftproot/* ' + args.tftp_root) -def writeToMenu(): - menuPath = pykickxe_conf.tftpRoot + 'pxelinux.cfg/autoinstall.menu' +def replaceLine(filePath, srcText, destText): + workingFile = open(filePath, 'r+') + original = workingFile.read() + modified = original.replace(srcText, destText) + workingFile.seek(0) + workingFile.write(modified) + workingFile.close() + return + + +def writeToMenu(args, config): + menuPath = './tftproot/pxelinux.cfg/' + # menuFile = open(menuPath + 'autoinstall.menu.source', 'r') + # targetMenuFile = open(menuPath + 'autoinstall.menu', 'w') + autoconfig_file = config.get(args.distro, 'auto_file', 0) + if args.auto_cfg is not None: + autoconfig_file = args.auto_cfg + os.system('cp ' + autoconfig_file + ' ./tftproot/util/auto.cfg') + autoconfig_file = '/util/auto.cfg' + menuIn = menuPath + 'autoinstall.menu.source' + menuOut = menuPath + 'autoinstall.menu' + os.system('cp ' + menuIn + ' ' + menuOut) + replaceLine( + menuOut, + '$DISTRO_NAME', + config.get(args.distro, 'name', 0)) + replaceLine( + menuOut, + '$KERNEL', + config.get(args.distro, 'kernel', 0)) + replaceLine( + menuOut, + '$APPEND', + config.get(args.distro, 'append', 0)) + replaceLine( + menuOut, + '$KS', + config.get(args.distro, 'auto_param', 0) + autoconfig_file) + return -def parseArgs(): +def parseArgs(configFIle): """Parser for cli arguements""" + config = ConfigParser.SafeConfigParser() + config.read(configFile) parser = argparse.ArgumentParser( prog='Pykickxe', - description='a Python PXE and autoinstall tool') + description='a Python PXE and autoinstall tool', + epilog=config.get('DEFAULT', 'help_text', 1)) parser.add_argument( 'distro', type=str, - choices=pykickxe_conf.distroList, + choices=config.sections(), help='pick a linux distro to download, unpack and append ' 'the PXE config to (Debian or Fedora for now)') parser.add_argument( @@ -58,33 +89,36 @@ def parseArgs(): action='store_true') parser.add_argument( '-v', - '--verbose', + '--verbose (also not really working)', help='print some of what this is doing at any moment', action='store_true') parser.add_argument( '-t', '--tftp_root', type=str, - default=pykickxe_conf.tftpRoot, + default=config.get('DEFAULT', 'ftp_root', 0), help='pass an absolute path to tftp root directory') parser.add_argument( - '--ks_cfg', + '--auto_cfg', type=str, - default=pykickxe_conf.ks_cfgPath, help='pass an absolute path to Kickstart file') - parser.add_argument( - '--pxe_cfg', - type=str, - default=pykickxe_conf.pxe_cfgPath, - help='pass an absolute path to pxe file with entry to append') + # parser.add_argument( + # '--pxe_cfg', + # type=str, + # default=pykickxe_conf.pxe_cfgPath, + # help='pass an absolute path to pxe file with entry to append') args = parser.parse_args() - return args + return args, config # pykickxe main -args = parseArgs() # TODO: Add sensible options and check exclusivity #p3 -if args.list is True: - listConfig() -if args.download is True: - filePath = downloadFromURL(args) -copyToTftp() \ No newline at end of file +if __name__ == "__main__": + configFile = 'pykickxe.conf' + # TODO: Add sensible options and check exclusivity #p3 + args, config = parseArgs(configFile) + if args.list is True: + listConfig() + if args.download is True: + filePath = downloadFromURL(args) + writeToMenu(args, config) + copyFiles(args, config) diff --git a/pykickxe_conf.py b/pykickxe_conf.py index d293e28..1b17394 100644 --- a/pykickxe_conf.py +++ b/pykickxe_conf.py @@ -1,14 +1,3 @@ -# TODO: WRewrite #p1 -lastCheckedDate = '03.01.17' -tftpRoot = '/srv/tftp/' -pxe_cfgPath = './pxeappend.cfg' -ks_cfgPath = './ks.cfg' +import ConfigParser -distroList = ['debian', 'fedora'] -fedoraURL = ( - "http://ftp.icm.edu.pl/pub/Linux/fedora/" - "linux/releases/25/Workstation/x86_64/os/images/pxeboot/*") # doesnt work -debianURL = ( - "http://ftp.nl.debian.org/debian/dists/jessie/" - "main/installer-amd64/current/images/netboot/" - "netboot.tar.gz") +cfg = ConfigParser.SafeConfigParser() diff --git a/tftproot/pxelinux.cfg/autoinstall.menu b/tftproot/pxelinux.cfg/autoinstall.menu index e744711..e69de29 100644 --- a/tftproot/pxelinux.cfg/autoinstall.menu +++ b/tftproot/pxelinux.cfg/autoinstall.menu @@ -1,10 +0,0 @@ -MENU TITLE Autoinstall -PATH util/ -DEFAULT util/menu.c32 -TIMEOUT 20 -ONTIMEOUT Proceeding -ONERROR COM32 reboot.c32 - -LABEL Proceed - KERNEL centos/vmlinuz - APPEND initrd=centos/initrd.img method=ftp://192.168.0.1/centos/ISO devfs=nomount ip=dhcp diff --git a/tftproot/pxelinux.cfg/autoinstall.menu.source b/tftproot/pxelinux.cfg/autoinstall.menu.source new file mode 100644 index 0000000..4358cdf --- /dev/null +++ b/tftproot/pxelinux.cfg/autoinstall.menu.source @@ -0,0 +1,10 @@ +MENU TITLE Autoinstall +PATH util/ +DEFAULT util/menu.c32 +TIMEOUT 20 +ONTIMEOUT Proceeding +ONERROR COM32 reboot.c32 + +LABEL Automatic $DISTRO_NAME Installer + KERNEL $KERNEL + APPEND $APPEND $KS \ No newline at end of file diff --git a/tftproot/pxelinux.cfg/default b/tftproot/pxelinux.cfg/default index cf2c96a..4e3df95 100644 --- a/tftproot/pxelinux.cfg/default +++ b/tftproot/pxelinux.cfg/default @@ -14,12 +14,8 @@ LABEL Debian LABEL Fedora KERNEL fedora/vmlinuz - APPEND initrd=fedora/initrd.img inst.repo=ftp://192.168.0.1/fedora/ISO devfs=nomount + APPEND initrd=fedora/initrd.img inst.repo=ftp://192.168.0.1/fedora/ devfs=nomount LABEL CentOS KERNEL centos/vmlinuz - APPEND initrd=centos/initrd.img method=ftp://192.168.0.1/centos/ISO devfs=nomount ip=dhcp - -LABEL OpenSUSE - KERNEL opensuse/? - APPEND ? \ No newline at end of file + APPEND initrd=centos/initrd.img method=ftp://192.168.0.1/centos/ devfs=nomount ip=dhcp