This repository was archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkelfs.py
270 lines (225 loc) · 15.5 KB
/
mkelfs.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
# mkelfs.py - script for downloading kickstart-relevant
# files for EL-like distros like CentOS, Fedora etc.
#
# 2015 By Christian Stankowic
# <info at stankowic hyphen development dot net>
# https://github.com/stdevel/mkelfs
#
from optparse import OptionParser
import sys
import os
import shutil
import xmlrpclib
import getpass
import stat
#minimum version of supported API level
supportedAPI = [11.1]
#TODO: try to create /var/satellite/kickstart_tree
#TODO: release 6. => 6 workaround?, try downloads!
#TODO: append download for better debug instrad of delete and re-download
#valid url cheatsheet - some URLs:
#EL2.1: http://vault.centos.org/2.1/final/i386/
#EL3: http://vault.centos.org/3.1/i386/
#EL4: http://vault.centos.org/4.0/os/i386/
#EL5: http://mirror.centos.org/centos/5.11/os/x86_64/
#EL6: http://mirror.centos.org/centos/6.6/os/x86_64/
#EL7: http://mirror.centos.org/centos/7/os/x86_64/
#F18: x
#F19: x
#...
#defining default mirrors
default_centos="http://mirror.centos.org/centos"
default_scientific="http://ftp.scientificlinux.org/linux/scientific"
default_fedora="http://mirrors.kernel.org/fedora"
default_folders=["images","isolinux","repodata"]
if __name__ == "__main__":
#define description, version and load parser
desc='''%prog is used to create kickstartable distribution trees of EL-like distros like CentOS, Fedora and ScientificLinux. Optionally you can also create kickstart distributions on Spacewalk, Red Hat Satellite and SUSE Manager. Login credentials are assigned using the following shell variables:
SATELLITE_LOGIN username
SATELLITE_PASSWORD password
It is also possible to create an authfile (permissions 0600) for usage with this script. The first line needs to contain the username, the second line should consist of the appropriate password.
If you're not defining variables or an authfile you will be prompted to enter your login information.
Checkout the GitHub page for updates: https://github.com/stdevel/mkelfs'''
parser = OptionParser(description=desc,version="%prog version 0.4")
#-r / --release
parser.add_option("-r", "--release", action="store", type="string", dest="release", help="define which release to use (e.g. 6.5)", metavar="RELEASE")
#-x / --arch
parser.add_option("-x", "--arch", action="store", type="string", dest="arch", help="define which architecture to use (e.g. x86_64)", metavar="ARCH")
#-t / --target
parser.add_option("-t", "--target", action="store", type="string", dest="target", default="/var/satellite/kickstart_tree", help="define where to store kickstart files. A subfolder will be created automatically. (default: /var/satellite/kickstart_tree)", metavar="DIR")
#-m / --mirror
parser.add_option("-m", "--mirror", dest="mirror", action="store", type="string", help="define a valid EL mirror to use - DON'T add the trailing slash! Have a loot at the EL mirror list (e.g. http://www.centos.org/download/mirrors) for alternatives", metavar="MIRROR")
#-o / --distribution
parser.add_option("-o", "--distro", dest="distro", default="centos", action="store", type="string", help="defines for which distro the files are downloaded (default: centos) - other possible values: fedora, scientific", metavar="DISTRO")
#-f / --force
parser.add_option("-f", "--force", dest="force", default=False, action="store_true", help="defines whether pre-existing kickstart files shall be overwritten")
#-i / --ignore-existing
parser.add_option("-i", "--ignore-existing", dest="ignoreExisting", default=False, action="store_true", help="don't throw errors if downloaded files are already existing (e.g. testing purposes)")
#-q / --quiet
parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout")
#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")
#-c / --create-distribution
parser.add_option("-c", "--create-distribution", dest="createDistribution", default=False, action="store_true", help="creates a kickstart distribution on the Spacewalk / Red Hat Satellite or SUSE Manager server")
#-b / --base-channel
parser.add_option("-b", "--base-channel", dest="baseChannel", type="string", default="", help="defines the name of the distro base-channel", metavar="CHANNEL")
#-a / --authfile
parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE", default="", help="defines an auth file to use instead of shell variables")
#-s / --server
parser.add_option("-s", "--server", dest="server", metavar="SERVER", default="localhost", help="defines the server to use")
#parse arguments
(options, args) = parser.parse_args()
#check whether all required options are given
if options.release is None and options.arch is None:
parser.error("missing values for release and arch!")
else:
#make options being lower-case in case you missed it
options.distro = str(options.distro).lower()
options.release = str(options.release).lower()
options.arch = str(options.arch).lower()
#setup default mirror URL (if no other defined) depending on selected distro
if options.mirror == None:
if str(options.distro).lower() == "scientific": options.mirror = default_scientific
if str(options.distro).lower() == "fedora": options.mirror = default_fedora
if str(options.distro).lower() == "centos": options.mirror = default_centos
if str(options.distro).lower() == "scientific": url = options.mirror+"/"+options.release+"/"+options.arch+"/os"
elif str(options.distro).lower() == "fedora": url = options.mirror+"/releases/"+options.release+"/Fedora/"+options.arch+"/os"
else: url = options.mirror+"/"+options.release+"/os/"+options.arch
#workaround for EL7
if options.release == "7" and options.distro.lower() in ["centos","scientific"]:
default_folders.append("LiveOS")
if options.verbose: print("INFO: EL7 detected, making sure to also download LiveOS")
#print debug output if required
if options.debug: print("release: " + options.release + "\narch: " + options.arch + "\ntarget: " + options.target + "\nmirror: " + options.mirror + "\nforce: " + `options.force` + "\nverbose: " + `options.verbose` + "\ndebug: " + `options.debug` + "\ndistro: " + options.distro + "\nURL: " + url)
#define URL and login information
SATELLITE_URL = "http://"+options.server+"/rpc/api"
#setup client and key depending on mode
client = xmlrpclib.Server(SATELLITE_URL, verbose=options.debug)
if options.authfile:
#use authfile
if options.debug: print "DEBUG: using authfile"
try:
#check filemode and read file
filemode = oct(stat.S_IMODE(os.lstat(options.authfile).st_mode))
if filemode == "0600":
if options.debug: print "DEBUG: file permission ("+filemode+") matches 0600"
fo = open(options.authfile, "r")
s_username=fo.readline().replace("\n", "")
s_password=fo.readline().replace("\n", "")
key = client.auth.login(s_username, s_password)
else:
if options.verbose: print "ERROR: file permission ("+filemode+") not matching 0600!"
exit(1)
except OSError:
print "ERROR: file non-existent or permissions not 0600!"
exit(1)
elif "SATELLITE_LOGIN" in os.environ and "SATELLITE_PASSWORD" in os.environ:
#shell variables
if options.debug: print "DEBUG: checking shell variables"
key = client.auth.login(os.environ["SATELLITE_LOGIN"], os.environ["SATELLITE_PASSWORD"])
else:
#prompt user
if options.debug: print "DEBUG: prompting for login credentials"
s_username = raw_input("Username: ")
s_password = getpass.getpass("Password: ")
key = client.auth.login(s_username, s_password)
#check whether the API version matches the minimum required
api_level = client.api.getVersion()
if not api_level >= supportedAPI:
print "ERROR: your API version ("+api_level+") does not support the required calls. You'll need API version 1.8 (11.1) or higher!"
exit(1)
else:
if options.debug: print "INFO: supported API version ("+api_level+") found."
#search for base-channel or check base-channel
listChannels = client.channel.listAllChannels(key)
if options.debug: print "INFO: all channels" + str(listChannels)
if options.baseChannel != "":
#check base-channel
if options.baseChannel not in str(listChannels):
print "ERROR: base-channel '" + options.baseChannel + "' does not exist!"
exit(1)
else:
for dict in listChannels:
if dict["label"] == options.baseChannel:
if options.arch == "i386":
if dict["arch_name"] != "IA-32":
print "ERROR: base-channel '" + options.baseChannel + "' has a different architecture!"
exit(1)
else:
if dict["arch_name"] != options.arch:
print "ERROR: base-channel '" + options.baseChannel + "' has a different architecture!"
exit(1)
else:
#search base-channel
for dict in listChannels:
#print dict
if dict["label"] == options.distro+options.release+"-"+options.arch:
if options.verbose: print "INFO: found matching base channel '" + dict["label"] + "'"
options.baseChannel = dict["label"]
#last check if we configured a base-channel
if options.baseChannel == "":
print "ERROR: unable to find a valid base-channel, please check your channels!"
exit(1)
#check whether target is writable
if os.access(options.target, os.W_OK):
if options.verbose: print "INFO: path exists and writable"
#switch to directory and create subfolder non-existent
os.chdir(options.target)
#check whether the directory already exists
if os.path.exists(options.distro+"-"+options.release+"-"+options.arch):
#delete content of directory if force given
if options.force == True:
shutil.rmtree(options.target+"/"+options.distro+"-"+options.release+"-"+options.arch)
if options.verbose: print "INFO: deleted directory ("+options.target+"/"+options.distro+"-"+options.release+"-"+options.arch+") because -f / --force given"
elif options.ignoreExisting == False:
#abort with error
print >> sys.stderr, "ERROR: kickstart tree directory ("+options.target+"/"+options.distro+"-"+options.release+"-"+options.arch+") already exists! Use -f / --force to overwrite!"
exit(1)
#create directory and change directory
if options.ignoreExisting == False: os.system("mkdir "+options.distro+"-"+options.release+"-"+options.arch)
os.chdir(options.target+"/"+options.distro+"-"+options.release+"-"+options.arch)
#download files
if options.ignoreExisting == False:
if options.verbose: print "INFO: about to download kickstart files for EL "+options.release+" "+options.arch+" from mirror "+options.mirror+"..."
for i in default_folders:
#setting offset based on mirror and distro
if options.distro == "fedora": dir_offset=6
elif "vault" in options.mirror: dir_offset=3
elif options.distro == "scientific": dir_offset=5
else: dir_offset=4
if options.debug: print "INFO: dir_offset: "+`dir_offset`
#run wget with or without quiet mode
cmd = "wget -e robots=off -r -nH --cut-dirs="+`dir_offset`+" --no-parent --reject 'index.html*' "+url+"/"+i+"/"
if options.verbose == False:
cmd = cmd+" --quiet"
retcode = os.system(cmd)
else:
retcode = os.system(cmd)
#print error if wget had a error
if retcode != 0:
print >> sys.stderr, "ERROR: some error occurred (see output above!) - hint: check URL ("+options.mirror+"/"+options.release+")"
exit(1)
else:
if options.verbose: print "INFO: successfully downloaded kickstart files for EL "+options.release+" "+options.arch+"!\nUse this file path for cobbler or the webui: "+options.target+"/"+options.distro+"-"+options.release+"-"+options.arch
else:
print >> sys.stderr, "ERROR: path non-existent or non-writable!"
#create kickstart distribution
if options.createDistribution:
if options.verbose: print "INFO: Creating kickstart distribution..."
#set install type
if options.distro == "fedora": installType = "fedora"
else:
if "2.1" in options.release: installType = "rhel_2.1"
if "3" in options.release: installType = "rhel_3"
if "4" in options.release: installType = "rhel_4"
if "5" in options.release: installType = "rhel_5"
if "6" in options.release: installType = "rhel_6"
if "7" in options.release: installType = "rhel_7"
if options.debug: print "DEBUG: install type is '" + installType + "'"
#create distribution
result = client.kickstart.tree.create(key,"KD-"+options.distro+"-"+options.release+"-"+options.arch,options.target+"/"+options.distro+"-"+options.release+"-"+options.arch,options.baseChannel,installType)
if result == 1:
if options.verbose: print "Successfully created kickstart distribution 'KD-" + options.distro+"-"+options.release+"-"+options.arch + "'"
#logout and exit
client.auth.logout(key)