summaryrefslogtreecommitdiffstats
path: root/todo.py
blob: c03d657ef665c7bdf8e8dba5cb96fe114201e9aa (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# For an install to proceed, the following todo fields must be filled in
#
#	mount list (unless todo.runLive)		addMount()
#	lilo boot.b installation (may be None)		liloLocation()

import rpm, os
import util, isys
from lilo import LiloConfiguration
from syslog import Syslog
import string
import socket
import crypt
import whrandom

class SimpleConfigFile:
    def __str__ (self):
        s = ""
        keys = self.info.keys ()
        keys.sort ()
        for key in keys:
            s = s + key + "=" + self.info[key] + "\n"
        return s
            
    def __init__ (self):
        self.info = {}

    def set (self, *args):
        for (key, data) in args:
            self.info[string.upper (key)] = data

    def unset (self, *keys):
        for key in keys:
            key = string.upper (key)
            if self.info.has_key (key):
               del self.info[key] 

    def get (self, key):
        key = string.upper (key)
        if self.info.has_key (key):
            return self.info[key]
        else:
            return ""


class NetworkDevice (SimpleConfigFile):
    def __str__ (self):
        s = ""
        s = s + "DEVICE=" + self.info["DEVICE"] + "\n"
        keys = self.info.keys ()
        keys.sort ()
        keys.remove ("DEVICE")
        for key in keys:
            s = s + key + "=" + self.info[key] + "\n"
        return s

    def __init__ (self, dev):
        self.info = { "DEVICE" : dev }
        self.hostname = ""

class Network:
    def __init__ (self):
        self.netdevices = {}
        self.gateway = ""
        self.primaryNS = ""
        self.secondaryNS = ""
        self.ternaryNS = ""
        self.domains = []
    
    def available (self):
        if self.netdevices:
            return self.netdevices
        f = open ("/proc/net/dev")
        lines = f.readlines()
        f.close ()
        # skip first two lines, they are header
        lines = lines[2:]
        for line in lines:
            dev = string.strip (line[0:6])
            if dev != "lo":
                self.netdevices[dev] = NetworkDevice (dev)
        return self.netdevices

    def guessHostnames (self):
        # guess the hostname for the first device with an IP
        # XXX fixme - need to set up resolv.conf
        self.domains = []
        for dev in self.netdevices.values ():
            ip = dev.get ("ipaddr")
            if ip:
                try:
                    (hostname, aliases, ipaddrs) = socket.gethostbyaddr (ip)
                except socket.error:
                    hostname = ""
                if hostname:
                    dev.hostname = hostname
                    self.domains.append (string.joinfields (string.splitfields (hostname, '.')[1:], '.'))
            else:
                dev.hostname = "localhost.localdomain"
        if not self.domains:
            self.domains = [ "localdomain" ]

    def nameservers (self):
        return [ self.primaryNS, self.secondaryNS, self.ternaryNS ]

class Password:
    def __init__ (self):
        self.crypt = ""

    def set (self, password, isCrypted = 0):
        if not isCrypted:
            salt = (whrandom.choice (string.letters +
                                     string.digits + './') + 
                    whrandom.choice (string.letters +
                                     string.digits + './'))
            self.crypt = crypt.crypt (password, salt)
        else:
            self.crypt = password

    def get (self):
        return self.crypt
            
class Language (SimpleConfigFile):
    def __init__ (self):
        self.info = {}
        self.lang = None
        self.langs = {
            "English" : "C",
            "German" : "de",
            }

    def available (self):
        return self.langs
    
    def set (self, lang):
        self.lang = lang
        self.info["LANG"] = self.langs[lang]
        self.info["LINGUAS"] = self.langs[lang]
        self.info["LC_ALL"] = self.langs[lang]
        
    def get (self):
        return self.lang

class Mouse (SimpleConfigFile):
    # XXX fixme - externalize
    def __init__ (self):
        self.info = {}
        self.mice = {
            "PS/2" :
                    ("ps/2", "PS/2", "psaux"),
            "ALPS GlidePoint (PS/2)" :
                    ("ps/2", "GlidePointPS/2", "psaux"),
            "ASCII MieMouse (serial)" :
                    ("ms3", "IntelliMouse", "ttyS"),
            "ASCII MieMouse (PS/2)" : 
                    ("ps/2", "NetMousePS/2", "psaux"),
            "ATI Bus Mouse" :
                    ("Busmouse", "BusMouse", "atibm"),
            "Generic Mouse (serial)" :
                    ("Microsoft", "Microsoft", "ttyS"),
            "Generic 3 Button Mouse (serial)" :
                    ("Microsoft", "Microsoft", "ttyS"),
            "Generic Mouse (PS/2)" :
                    ("ps/2", "PS/2", "psaux"),
            "Generic 3 Button Mouse (PS/2)" :
	            ("ps/2", "PS/2", "psaux"),
            "Genius NetMouse (serial)" :
        	   ("ms3", "IntelliMouse", "ttyS"),
            "Genius NetMouse (PS/2)" :
	            ("netmouse", "NetMousePS/2", "psaux"),
            "Genius NetMouse Pro (PS/2)" :
	            ("netmouse", "NetMousePS/2", "psaux"),
            "Genius NetScroll (PS/2)" :
	            ("netmouse", "NetScrollPS/2", "psaux"),
            "Kensington Thinking Mouse (PS/2)" :
            	    ("ps/2", "ThinkingMousePS/2", "psaux"),
            "Logitech Mouse (serial, old C7 type)" :
            	    ("Logitech", "Logitech", "ttyS"),
            "Logitech CC Series (serial)" :
	            ("logim", "MouseMan", "ttyS"),
            "Logitech Bus Mouse" :
            	    ("Busmouse", "BusMouse", "logibm"),
            "Logitech MouseMan/FirstMouse (serial)" :
            	    ("MouseMan", "MouseMan", "ttyS"),
            "Logitech MouseMan/FirstMouse (ps/2)" :
            	    ("ps/2", "PS/2", "psaux"),
            "Logitech MouseMan+/FirstMouse+ (serial)" :
	            ("pnp", "IntelliMouse", "ttyS"),
            "Logitech MouseMan+/FirstMouse+ (PS/2)" :
	            ("ps/2", "MouseManPlusPS/2", "psaux"),
            "Microsoft compatible (serial)" :
            	    ("Microsoft",    "Microsoft", "ttyS"),
            "Microsoft Rev 2.1A or higher (serial)" :
                    ("pnp", "Auto", "ttyS"),
            "Microsoft IntelliMouse (serial)" :
                    ("ms3", "IntelliMouse", "ttyS"),
            "Microsoft IntelliMouse (PS/2)" :
            	    ("imps2", "IMPS/2", "psaux"), 
            "Microsoft Bus Mouse" :
	            ("Busmouse", "BusMouse", "inportbm"),
            "Mouse Systems (serial)" :
            	    ("MouseSystems", "MouseSystems", "ttyS"), 
            "MM Series (serial)" :
	            ("MMSeries", "MMSeries", "ttyS"),
            "MM HitTablet (serial)" :
	            ("MMHitTab", "MMHittab", "ttyS"),
            }
            

    def available (self):
        return self.mice.keys ()

    def get (self):
        if self.info.has_key ("FULLNAME"):
            return self.info ("FULLNAME")

    def set (self, mouse):
        (gpm, x11, dev) = self.mice[mouse]
        self.info["MOUSETYPE"] = gpm
        self.info["XMOUSETYPE"] = x11
        self.info["FULLNAME"] = mouse

class Authentication:
    def __init__ (self):
        self.domain = ""
        self.useBroadcast = 0
        self.server = ""
        self.useNis = 0
        self.useShadow = 1
        self.useMD5 = 1
        
class ToDo:
    def __init__(self, intf, method, rootPath, setupFilesystems = 1,
		 installSystem = 1):
	self.intf = intf
	self.method = method
	self.mounts = []
	self.hdList = None
	self.comps = None
	self.instPath = rootPath
	self.setupFilesystems = setupFilesystems
	self.installSystem = installSystem
        self.language = Language ()
        self.network = Network ()
        self.rootpassword = Password ()
        self.mouse = Mouse ()
        self.auth = Authentication ()

    def umountFilesystems(self):
	if (not self.setupFilesystems): return 

	self.mounts.sort(mountListCmp)
	self.mounts.reverse()
	for n in self.mounts:
	    isys.makeDevInode(n, '/tmp/' + n)
	    isys.umount(n)
            os.remove('/tmp/' + n)

    def mountFilesystems(self):
	if (not self.setupFilesystems): return 

	for n in self.mounts:
	    (device, mntpoint, format) = n
            isys.makeDevInode(device, '/tmp/' + device)
	    isys.mount( '/tmp/' + device, self.instPath + mntpoint)
	    os.remove( '/tmp/' + device);

    def makeFilesystems(self):
	if (not self.setupFilesystems): return 

	self.mounts.sort(mountListCmp)
	for n in self.mounts:
	    (device, mntpoint, format) = n
	    if not format: continue
	    w = self.intf.waitWindow("Formatting", 
			"Formatting %s filesystem..." % (mntpoint,))
	    isys.makeDevInode(device, '/tmp/' + device)
	    util.execWithRedirect("mke2fs", [ "mke2fs", '/tmp/' + device ],
				  stdout = None, stderr = None, searchPath = 1)
            os.remove('/tmp/' + device)
	    w.pop()

    def addMount(self, device, location, reformat = 1):
	self.mounts.append((device, location, reformat))

    def writeFstab(self):
	format = "%-23s %-23s %-7s %-15s %d %d\n";

	f = open(self.instPath + "/etc/fstab", "w")
	self.mounts.sort(mountListCmp)
	for n in self.mounts: 
	    (dev, fs, reformat) = n
	    if (fs == '/'):
		f.write(format % ( '/dev/' + dev, fs, 'ext2', 'defaults', 1, 1))
	    else:
		f.write(format % ( '/dev/' + dev, fs, 'ext2', 'defaults', 1, 2))
	f.write(format % ("/mnt/floppy", "/dev/fd0", 'ext', 'noauto', 0, 0))
	f.write(format % ("none", "/proc", 'proc', 'defaults', 0, 0))
	f.write(format % ("none", "/dev/pts", 'devpts', 'gid=5,mode=620', 0, 0))
	f.close()

    def writeLanguage(self):
	f = open(self.instPath + "/etc/sysconfig/i18n", "w")
	f.write(str (self.language))
	f.close()

    def writeMouse(self):
	f = open(self.instPath + "/etc/sysconfig/mouse", "w")
	f.write(str (self.mouse))
	f.close()

    def installLilo(self):
	if not self.liloDevice: return

	# FIXME: make an initrd here

	l = LiloConfiguration()
	l.addEntry("boot", '/dev/' + self.liloDevice)
	l.addEntry("map", "/boot/map")
	l.addEntry("install", "/boot/boot.b")
	l.addEntry("prompt")
	l.addEntry("timeout", "50")

	sl = LiloConfiguration()
	sl.addEntry("label", "linux")

	for n in self.mounts:
	    (dev, fs, reformat) = n
	    if fs == '/':
		sl.addEntry("root", '/dev/' + dev)
	sl.addEntry("read-only")

	kernelFile = '/boot/vmlinuz-' +  \
		str(self.kernelPackage[rpm.RPMTAG_VERSION]) + "-" + \
		str(self.kernelPackage[rpm.RPMTAG_RELEASE])
	    
	l.addImage(kernelFile, sl)
	l.write(self.instPath + "/etc/lilo.conf")

	util.execWithRedirect(self.instPath + '/sbin/lilo' , [ "lilo", 
				"-r", self.instPath ], stdout = None)

    def freeHeaderList(self):
	if (self.hdList):
	    self.hdList = None

    def getHeaderList(self):
	if (not self.hdList):
	    w = self.intf.waitWindow("Reading",
                                     "Reading package information...")
	    self.hdList = self.method.readHeaders()
	    w.pop()
	return self.hdList

    def setLiloLocation(self, device):
	self.liloDevice = device

    def getCompsList(self):
	if (not self.comps):
	    self.getHeaderList()
	    self.comps = self.method.readComps(self.hdList)
	self.comps['Base'].select(1)
	self.kernelPackage = self.hdList['kernel']

	if (self.hdList.has_key('kernel-smp') and isys.smpAvailable()):
	    self.hdList['kernel-smp'].selected = 1
	    self.kernelPackage = self.hdList['kernel-smp']

	return self.comps

    def writeNetworkConfig (self):
        # /etc/sysconfig/network-scripts/ifcfg-*
        for dev in self.network.netdevices.values ():
            device = dev.get ("device")
            f = open (self.instPath + "/etc/sysconfig/network-scripts/ifcfg-" + device, "w")
            f.write (str (dev))
            f.close ()

        # /etc/sysconfig/network
        f = open (self.instPath + "/etc/sysconfig/network", "w")
        f.write ("NETWORKING=yes\n"
                 "FORWARD_IPV4=false\n"
                 "HOSTNAME=localhost.localdomain\n"
                 "GATEWAY=" + self.network.gateway + "\n")
        f.close ()

        # /etc/hosts
        f = open (self.instPath + "/etc/hosts", "w")
        f.write ("127.0.0.1\t\tlocalhost.localdomain\n")
        for dev in self.network.netdevices.values ():
            ip = dev.get ("ipaddr")
            if dev.hostname and ip:
                f.write ("%s\t\t%s\n" % (ip, dev.hostname))
        f.close ()

        # /etc/resolv.conf
        f = open (self.instPath + "/etc/resolv.conf", "w")
        f.write ("search " + string.joinfields (self.network.domains, ' ') + "\n")
        for ns in self.network.nameservers ():
            if ns:
                f.write ("nameserver " + ns + "\n")
        f.close ()

    def writeRootPassword (self):
        f = open (self.instPath + "/etc/passwd", "r")
        lines = f.readlines ()
        f.close ()
        index = 0
        for line in lines:
            if line[0:4] == "root":
                entry = string.splitfields (line, ':')
                entry[1] = self.rootpassword.get ()
                lines[index] = string.joinfields (entry, ':')
                break
            index = index + 1
        f = open (self.instPath + "/etc/passwd", "w")
        f.writelines (lines)
        f.close ()

    def doInstall(self, intf):
	# make sure we have the header list and comps file
	self.getHeaderList()
	self.getCompsList()

        # make sure that all comps that include other comps are
        # selected (i.e. - recurse down the selected comps and turn
        # on the children

        for comp in self.comps:
            if comp.selected:
                comp.select(1)

	self.makeFilesystems()
	self.mountFilesystems()

	if not self.installSystem: 
	    return

	for i in [ '/var', '/var/lib', '/var/lib/rpm', '/tmp', '/dev' ]:
	    try:
	        os.mkdir(self.instPath + i)
	    except os.error, (errno, msg):
                intf.messageWindow("Error", "Error making directory %s: %s" % (i, msg))

	db = rpm.opendb(1, self.instPath)
	ts = rpm.TransactionSet(self.instPath, db)

        total = 0
	totalSize = 0
	for p in self.hdList.selected():
	    ts.add(p.h, (p.h, self.method))
	    total = total + 1
	    totalSize = totalSize + p.h[rpm.RPMTAG_SIZE]

	ts.order()

	instLog = open(self.instPath + '/tmp/install.log', "w+")
	syslog = Syslog(root = self.instPath, output = instLog)

	instLogFd = os.open(self.instPath + '/tmp/install.log', os.O_RDWR)
	ts.scriptFd = instLogFd
	# the transaction set dup()s the file descriptor and will close the
	# dup'd when we go out of scope
	os.close(instLogFd)	

	p = self.intf.packageProgressWindow(total, totalSize)

        def instCallback(what, amount, total, key, data):
            if (what == rpm.RPMCALLBACK_INST_OPEN_FILE):
                (h, method) = key
                data.setPackage(h)
                data.setPackageScale(0, 1)
                fn = method.getFilename(h)
                d = os.open(fn, os.O_RDONLY)
                return d
            elif (what == rpm.RPMCALLBACK_INST_PROGRESS):
                data.setPackageScale(amount, total)
            elif (what == rpm.RPMCALLBACK_INST_CLOSE_FILE):
                (h, method) = key
                data.completePackage(h)

	ts.run(0, 0, instCallback, p)

	del syslog
        del p

        w = self.intf.waitWindow("Post Install", 
                                 "Performing post install configuration")
        
	self.writeFstab ()
        self.writeLanguage ()
        self.writeMouse ()
        self.writeNetworkConfig ()
        self.writeRootPassword ()
	self.installLilo ()

        w.pop ()

def mountListCmp(first, second):
    mnt1 = first[1]
    mnt2 = first[2]
    if (first < second):
	return -1
    elif (first == second):
	return 0
    return 1