1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import os
18 import sys
19 import tempfile
20 import time
21 from stat import ST_SIZE
22 from optparse import Option, OptionParser
23 from spacewalk.common.rhnConfig import PRODUCT_NAME
24
25 MOUNT_POINT = '/tmp'
26 IMAGE_SIZE = "630M"
27 DVD_IMAGE_SIZE = "4380M"
28
29
31 optionsTable = [
32 Option('-m', '--mountpoint', action='store',
33 help="mount point"),
34 Option('-s', '--size', action='store',
35 help="image size (eg. 630M)"),
36 Option('-p', '--file-prefix', action='store',
37 help='Filename prefix'),
38 Option('-o', '--output', action='store',
39 help='output directory'),
40 Option('-v', '--version', action='store',
41 help='version string'),
42 Option('-r', '--release', action='store',
43 help='release string'),
44 Option('--copy-iso-dir', action='store',
45 help='directory to copy the isos to after they have been generated.'),
46 Option('-t', '--type', action='store',
47 help='the type of iso being generated.\
48 this flag is optional, but can be set to spanning, non-spanning, or base.'),
49 ]
50 parser = OptionParser(option_list=optionsTable)
51 options, _args = parser.parse_args(arglist)
52
53
54 if not os.path.exists('/usr/bin/mkisofs'):
55 print("ERROR:: mkisofs is not Installed. Cannot Proceed iso build. " \
56 + "Please install mkisofs and rerun this command.")
57 return
58
59 mountPoint = options.mountpoint or MOUNT_POINT
60 if options.type == "dvd":
61 print("Building DVD Iso ...")
62 sizeStr = options.size or DVD_IMAGE_SIZE
63 else:
64 sizeStr = options.size or IMAGE_SIZE
65 imageSize = sizeStrToInt(sizeStr)
66 if imageSize == 0:
67 print("Unknown size %s" % sizeStr)
68 return
69
70 if options.version is None:
71 options.version = time.strftime("%Y%m%d", time.gmtime(time.time()))
72
73 if options.release is None:
74 options.release = '0'
75
76 if options.output is None:
77 options.output = "/tmp/satellite-isos"
78
79 file_prefix = options.file_prefix or "rhn-satellite"
80 if not os.path.isdir(options.output):
81 os.makedirs(options.output)
82
83
84 for f in os.listdir(options.output):
85 os.unlink(os.path.join(options.output, f))
86
87
88 mountPoint = os.path.normpath(mountPoint)
89
90
91 files = findFiles(mountPoint)
92 cds = []
93 while files:
94 cd = []
95 sz = 0
96 while files:
97 filePath, fileSize = files[0]
98 if sz + fileSize > imageSize:
99
100 break
101
102 cd.append(filePath)
103 sz = sz + fileSize
104
105 del files[0]
106 cds.append(cd)
107
108
109 cdcount = len(cds)
110
111
112 fd, empty_file_path = tempfile.mkstemp(dir='/tmp', prefix='empty.file-')
113 os.close(fd)
114
115
116 mkisofsTemplate = "mkisofs -r -J -D -file-mode 0444 -new-dir-mode 0555 -dir-mode 0555 " \
117 + "-graft-points %s -o %s /DISK_%s_OF_%s=%s"
118 for i in range(cdcount):
119 print("---------- %s/%s" % (i + 1, cdcount))
120
121
122 filename = "%s/%s-%s.%s-%02d.iso" % (options.output, file_prefix,
123 options.version, options.release, i + 1)
124
125
126
127
128
129 pathfiles_fd, pathfiles = tempfile.mkstemp(dir='/tmp', prefix='geniso-')
130
131
132 opts = {
133 'preparer': PRODUCT_NAME,
134 'publisher': PRODUCT_NAME,
135 'volid': "RHNSAT_%s/%s" % (i + 1, cdcount),
136 'path-list': pathfiles,
137 }
138 opts = ['-%s "%s"' % x for x in list(opts.items())]
139
140
141
142
143 grafts = []
144 for f in cds[i]:
145
146 relpath = os.path.relpath(f, mountPoint)
147
148 relpath = os.path.dirname(relpath)
149 grafts.append("%s/=%s" % (relpath, f))
150
151
152 cmd = mkisofsTemplate % (' '.join(opts), filename, i + 1, cdcount,
153 empty_file_path)
154
155
156 for graft in grafts:
157 os.write(pathfiles_fd, graft)
158 os.write(pathfiles_fd, "\n")
159 os.close(pathfiles_fd)
160
161 print("Creating %s" % filename)
162
163 fd = os.popen(cmd, "r")
164 print(fd.read())
165
166 if options.copy_iso_dir is not None:
167 copy_iso_path = os.path.join(options.copy_iso_dir, os.path.basename(os.path.dirname(filename)))
168 if not os.path.exists(copy_iso_path):
169 os.mkdir(copy_iso_path)
170 fd = os.popen("mv %s %s" % (filename, copy_iso_path), "r")
171 print(fd.read())
172 fd = os.popen("rm %s" % filename)
173 print(fd.read())
174
175
176 os.unlink(pathfiles)
177
178
179 os.unlink(empty_file_path)
180
181
183
184 if s is None or s == "":
185
186 return 0
187
188 s = str(s)
189
190 while s and s[0] == '-':
191 s = s[1:]
192
193 try:
194 return int(s)
195 except ValueError:
196
197 pass
198
199 if s[-1] in ('k', 'K', 'm', 'M'):
200
201 if s[-1].lower() == 'k':
202 mult = 1024
203 else:
204 mult = 1024 * 1024
205
206 try:
207 return mult * int(s[:-1])
208 except ValueError:
209 pass
210
211
212 return 0
213
214
215
216
227
228
229
230
231
236
237
238 if __name__ == '__main__':
239 main(sys.argv)
240