From a60d4780ba130dcc4ae63ae78faf5c0744c0a060 Mon Sep 17 00:00:00 2001 From: brent s Date: Thu, 27 Apr 2017 05:08:39 -0400 Subject: [PATCH] checkin for progress --- TODO | 14 +++------ aif.xsd | 12 +++++++- aifclient.py | 86 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/TODO b/TODO index ab1ff76..031f1d9 100644 --- a/TODO +++ b/TODO @@ -1,13 +1,11 @@ - config layout -- need to apply defaults and annotate/document -find out where to run aif-pre.sh (runs on host) (rename to aif-pre.script) +SCRATCH: find out where to run aif-pre.sh (runs on host) (rename to aif-pre.script) and get a way to insert that and aif-post(.script) from the kernel params, etc. remember to uncomment the functions in main() when ready to test +INSTEAD: scripts['setup']? -- use sgdisk? scripting (generated by python) for disk partitioning (part types listed at http://www.rodsbooks.com/gdisk/walkthrough.html ) --- actually, might want to use parted --script instead? then we can do percentages. https://www.gnu.org/software/parted/manual/parted.html - https://unix.stackexchange.com/questions/200582/scripteable-gpt-partitions-using-parted - add mkfs-ing - how to support mdadm, lvm? @@ -19,16 +17,12 @@ would yield the *client* sending info via URL params, e.g. parser: make sure to use https://mikeknoop.com/lxml-xxe-exploit/ fix -left off at network config- i think i just have software/packages/etc. next, unless i already did that - convert use of confobj or whatever to maybe be suitable to use webFetch instead. LOTS of duplicated code there. need to write docs -make sure you call install.scripts or whatever it's called in main() - -update aif.xsd for auth stuff in scripts - +need to double-check aif.xsd spec for the packaging command- can i specify a single element? +finish up software/packages section docs: http://lxml.de/parsing.html diff --git a/aif.xsd b/aif.xsd index cbb98ef..0fc0420 100644 --- a/aif.xsd +++ b/aif.xsd @@ -114,6 +114,12 @@ + + + + + + @@ -262,6 +268,7 @@ + @@ -319,9 +326,12 @@ - + + + + diff --git a/aifclient.py b/aifclient.py index 9f97d1a..6f82440 100755 --- a/aifclient.py +++ b/aifclient.py @@ -717,57 +717,77 @@ class archInstall(object): return(bootcmds) def scriptcmds(self): - if xmlobj.find('scripts') is not None: - self.scripts['pre'] = [] - self.scripts['post'] = [] - tempscriptdict = {'pre': {}, 'post': {}} - for x in xmlobj.find('scripts'): - if all(keyname in list(x.attrib.keys()) for keyname in ('user', 'password')): - auth = {} - auth['user'] = x.attrib['user'] - auth['password'] = x.attrib['password'] - if 'realm' in x.attrib.keys(): - auth['realm'] = x.attrib['realm'] - if 'authtype' in x.attrib.keys(): - auth['type'] = x.attrib['authtype'] - scriptcontents = self.webFetch(x.attrib['uri']).decode('utf-8') - else: - scriptcontents = self.webFetch(x.attrib['uri']).decode('utf-8') - if x.attrib['bootstrap'].lower() in ('true', '1'): - tempscriptdict['pre'][x.attrib['order']] = scriptcontents - else: - tempscriptdict['post'][x.attrib['order']] = scriptcontents - for d in ('pre', 'post'): - keylst = list(tempscriptdict[d].keys()) - keylst.sort() - for s in keylst: - aifdict['scripts'][d].append(tempscriptdict[d][s]) + if xmlobj.find('scripts') is not None: + self.scripts['pre'] = [] + self.scripts['post'] = [] + tempscriptdict = {'pre': {}, 'post': {}} + for x in xmlobj.find('scripts'): + if all(keyname in list(x.attrib.keys()) for keyname in ('user', 'password')): + auth = {} + auth['user'] = x.attrib['user'] + auth['password'] = x.attrib['password'] + if 'realm' in x.attrib.keys(): + auth['realm'] = x.attrib['realm'] + if 'authtype' in x.attrib.keys(): + auth['type'] = x.attrib['authtype'] + scriptcontents = self.webFetch(x.attrib['uri'], auth).decode('utf-8') + else: + scriptcontents = self.webFetch(x.attrib['uri']).decode('utf-8') + if x.attrib['bootstrap'].lower() in ('true', '1'): + tempscriptdict['pre'][x.attrib['order']] = scriptcontents + else: + tempscriptdict['post'][x.attrib['order']] = scriptcontents + for d in ('pre', 'post'): + keylst = list(tempscriptdict[d].keys()) + keylst.sort() + for s in keylst: + self.scripts[d].append(tempscriptdict[d][s]) - def chroot(self, chrootcmds = False, bootcmds = False): + def packagecmds(self): + pass + + def chroot(self, chrootcmds = False, bootcmds = False, scriptcmds = False): if not chrootcmds: chrootcmds = self.setup() if not bootcmds: bootcmds = self.bootloader() + if not scriptcmds: + scriptcmds = self.scripts # We don't need this currently, but we might down the road. #chrootscript = '#!/bin/bash\n# https://aif.square-r00t.net/\n\n' #with open('{0}/root/aif.sh'.format(self.system['chrootpath']), 'w') as f: # f.write(chrootscript) #os.chmod('{0}/root/aif.sh'.format(self.system['chrootpath']), 0o700) - with open('{0}/root/aif-pre.sh'.format(self.system['chrootpath']), 'w') as f: - f.write(self.scripts['pre']) - with open('{0}/root/aif-post.sh'.format(self.system['chrootpath']), 'w') as f: - f.write(self.scripts['post']) + for t in self.scripts.keys(): + os.makedirs('{0}/root/scripts/{1}'.format(self.system['chrootpath'], t), exist_ok = True) + cnt = 0 + for s in self.scripts[t]: + with open('{0}/root/scripts/{1}/{2}'.format(self.system['chrootpath'], + t, + cnt), 'w') as f: + f.write(self.scripts[t][cnt]) + os.chmod('{0}/root/scripts/{1}/{2}'.format(self.system['chrootpath'], + t, + cnt), 0o700) + cnt += 1 real_root = os.open("/", os.O_RDONLY) os.chroot(self.system['chrootpath']) # Does this even work with an os.chroot()? Let's hope so! with open(os.devnull, 'w') as DEVNULL: + if scriptcmds['pre']: + for s in len(scriptcmds['pre']): + script = '/root/scripts/pre/{0}'.format(s - 1) + subprocess.call(script, stdout = DEVNULL, stderr = subprocess.STDOUT) for c in chrootcmds: subprocess.call(c, stdout = DEVNULL, stderr = subprocess.STDOUT) for b in bootcmds: subprocess.call(b, stdout = DEVNULL, stderr = subprocess.STDOUT) - os.system('{0}/root/aif-pre.sh'.format(self.system['chrootpath'])) - #os.system('{0}/root/aif.sh'.format(self.system['chrootpath'])) - os.system('{0}/root/aif-post.sh'.format(self.system['chrootpath'])) + if scriptcmds['post']: + for s in len(scriptcmds['post']): + script = '/root/scripts/post/{0}'.format(s - 1) + subprocess.call(script, stdout = DEVNULL, stderr = subprocess.STDOUT) + #os.system('{0}/root/aif-pre.sh'.format(self.system['chrootpath'])) + #os.system('{0}/root/aif-post.sh'.format(self.system['chrootpath'])) os.fchdir(real_root) os.chroot('.') os.close(real_root)