-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpykickxe.py
executable file
·125 lines (111 loc) · 3.72 KB
/
pykickxe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import argparse
import os
import ConfigParser
def listConfig(): # TODO: Implement listConfig #p3
return
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
# TODO: Check if file already present #p1
# TODO: Check filesum #p2
return filePath
def copyFiles(args, config):
os.system('rsync --progress -av ./tftproot/* ' + args.tftp_root)
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(configFIle):
"""Parser for cli arguements"""
config = ConfigParser.SafeConfigParser()
config.read(configFile)
parser = argparse.ArgumentParser(
prog='Pykickxe',
description='a Python PXE and autoinstall tool',
epilog=config.get('DEFAULT', 'help_text', 1))
parser.add_argument(
'distro',
type=str,
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(
'-l',
'--list',
help='list some default options',
action='store_true')
parser.add_argument(
'-d',
'--download',
help='Download (not really working)',
action='store_true')
parser.add_argument(
'-v',
'--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=config.get('DEFAULT', 'ftp_root', 0),
help='pass an absolute path to tftp root directory')
parser.add_argument(
'--auto_cfg',
type=str,
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')
args = parser.parse_args()
return args, config
# pykickxe main
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)