summaryrefslogtreecommitdiffstats
path: root/client/Debian.py
blob: e5e1c3bab2e16ee4f51e68abc65ea08f9b69bf78 (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
# This is the bcfg2 support for debian
# $Id $
from glob import glob
from os import environ, stat, system
from popen2 import Popen4
from string import split
from sys import argv

import apt_pkg

from Toolset import Toolset

def Detect():
    try:
        stat('/etc/debian_version')
        return True
    except OSError:
        return False

class Debian(Toolset):
    __important__ = ["/etc/apt/sources.list", "/var/cache/debconf/config.dat", \
                     "/var/cache/debconf/templates.dat"]

    def __init__(self, cfg, setup):
        Toolset.__init__(self, cfg, setup)
        #system("dpkg --configure -a")
        if not self.setup['build']:
            system("dpkg-reconfigure -f noninteractive debconf")
        system("apt-get -q=2 -y update")
        environ["DEBIAN_FRONTEND"]='noninteractive'
        self.Refresh()

    def Refresh(self):
        apt_pkg.init()
        self.cache = apt_pkg.GetCache()
        self.installed = {}
        for pkg in self.cache.Packages:
            if pkg.CurrentVer:
                self.installed[pkg.Name] = pkg.CurrentVer.VerStr

    def VerifyService(self, entry):
        if entry.attrib['status'] == 'off':
            cmd = Popen4("/usr/sbin/update-rc.d -n -f %s remove"%(entry.attrib['name']))
            num = 1
        else:
            cmd = Popen4("/usr/sbin/update-rc.d -n -f %s remove"%(entry.attrib['name']))
            num = 2
        cstat = cmd.poll()
        output = ''
        while cstat == -1:
            output += cmd.fromchild.read()
            cstat = cmd.poll() >> 8
        if len(filter(lambda x:x, split(output, '\n'))) > num:
            return False
        return True

    def InstallService(self, entry):
        if entry.attrib['status'] == 'off':
            if self.setup['dryrun']:
                print "Disabling service %s"%(entry.attrib['name'])
                rc = 1
            else:
                rc = system("update-rc.d -f %s remove"%entry.attrib['name'])
        else:
            if self.setup['dryrun']:
                print "Enabling service %s"%(entry.attrib['name'])
                rc = 1
            else:
                rc = system("update-rc.d %s defaults"%(entry.attrib['name']))
        if rc:
            return False
        return True

    def VerifyPackage(self, entry, modlist=[]):
        if self.installed.has_key(entry.attrib['name']):
            if self.installed[entry.attrib['name']] == entry.attrib['version']:
                if not self.setup['quick']:
                    cmd = Popen4("debsums -s %s"%(entry.attrib['name']))
                    cstat = cmd.poll()
                    output = cmd.fromchild.read()
                    while cstat == -1:
                        output += cmd.fromchild.read()
                        cstat = cmd.poll()
                    output = filter(lambda x:x, split(output, '\n'))
                    if [x for x in output if x not in modlist]:
                        return False
                return True
        return False

    def InstallPackages(self, entries):
        for e in entries:
            system("apt-get --reinstall -q=2 -y install %s=%s"%(e.attrib['name'],e.attrib['version']))

    def InstallPackage(self, entry):
        if self.setup['dryrun'] or self.setup['verbose']:
            print "Installing package %s %s"%(entry.attrib['name'], entry.attrib['version'])
            if self.setup['dryrun']:
                return False
        else:
            # implement package installation here
            return False

    def GetInstalledConfigs(self):
        # returns a list of installed config files
        ret = []
        for a in map(lambda x:split(open(x).read(),"\n"),glob("/var/lib/dpkg/info/*.conffiles")):
            ret += a
        return ret