~ubuntu-branches/ubuntu/hardy/bcfg2/hardy-updates

« back to all changes in this revision

Viewing changes to tools/batchadd.py

  • Committer: Bazaar Package Importer
  • Author(s): Sami Haahtinen
  • Date: 2006-11-16 22:39:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061116223916-8dtn3t86cz58vg2x
Tags: 0.8.6.1-1
* New Upstream Release
* Replaced faulty if clause in bcfg2.postrm (Closes: #398772)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import sys, os
 
4
from datetime import date
 
5
os.environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings'
 
6
from Hostbase.hostbase.models import *
 
7
from Hostbase.settings import DEFAULT_MX, PRIORITY
 
8
import Hostbase.regex
 
9
 
 
10
host_attribs = ['hostname', 'whatami', 'netgroup', 'security_class', 'support',
 
11
                'csi', 'printq', 'dhcp', 'outbound_smtp', 'primary_user',
 
12
                'administrator', 'location', 'expiration_date', 'comments']
 
13
 
 
14
def handle_error(field):
 
15
    if '-f' in sys.argv:
 
16
        return
 
17
    print "Error: %s is already defined in hostbase" % field
 
18
    if '-s' in sys.argv:
 
19
        sys.exit(1)
 
20
 
 
21
def checkformat(values, indices):
 
22
    """Ensures file contains all necessary attributes in order """
 
23
    filelist = [pair[0] for pair in values]
 
24
 
 
25
#    lines = len(filelist)
 
26
 
 
27
    for index in indices:
 
28
        filelist = filelist[index:]
 
29
        if filelist[0:14] != host_attribs:
 
30
            # figure out what to do here
 
31
            return False
 
32
        else:
 
33
            # process rest of host attributes
 
34
            try:
 
35
                next = filelist[1:].index('hostname')
 
36
                remaining = filelist[14:next+1]
 
37
            except:
 
38
                remaining = filelist[14:]
 
39
            needfields = ['mac_addr', 'hdwr_type', 'ip_addr']
 
40
            if [item for item in needfields if item not in remaining]:
 
41
                return False
 
42
    return True
 
43
 
 
44
 
 
45
if __name__ == '__main__':
 
46
 
 
47
    # argument handling for batchadd
 
48
    try:
 
49
        fd = open(sys.argv[1], 'r')
 
50
    except (IndexError, IOError):
 
51
        print "\nUsage: batchadd.py filename\n"
 
52
        sys.exit()
 
53
 
 
54
    lines = fd.readlines()
 
55
    # splits and strips lines into (attribute, value)
 
56
    info = [[item.strip() for item in line.split("->")] for line in lines
 
57
            if line.lstrip(' ')[0] != '#' and line != '\n']
 
58
 
 
59
    if info[0][0] == 'mx' and info[1][0] == 'priority':
 
60
        mx, created = MX.objects.get_or_create(mx=info[0][1], priority=info[1][1])
 
61
        info = info[2:]
 
62
    
 
63
    else:
 
64
        mx, created = MX.objects.get_or_create(mx=DEFAULT_MX, priority=PRIORITY)
 
65
    if created:
 
66
        mx.save()
 
67
 
 
68
    hostindices = [num for num in range(0, len(info)) if info[num][0] == 'hostname']
 
69
 
 
70
    if not checkformat(info, hostindices):
 
71
        print "Error: file format"
 
72
        sys.exit()
 
73
 
 
74
#################
 
75
 
 
76
    for host in hostindices:
 
77
        try:
 
78
            host = Host.objects.get(hostname=info[host][1])
 
79
            handle_error(info[host][1])
 
80
        except:
 
81
            # do something here
 
82
            pass
 
83
 
 
84
    macindices = [num for num in range(0, len(info)) if info[num][0] == 'mac_addr']
 
85
    for mac_addr in macindices:
 
86
        try:
 
87
            host = Interface.objects.get(mac_addr=info[mac_addr][1])
 
88
            handle_error(info[mac_addr][1])
 
89
        except:
 
90
            # do something here
 
91
            pass
 
92
 
 
93
    for host in hostindices:
 
94
        blank = Host()
 
95
        for attrib in host_attribs:
 
96
            pair = info.pop(0)
 
97
            if pair[0] == 'dhcp' or pair[0] == 'outbound_smtp':
 
98
                if pair[1] == 'y':
 
99
                    blank.__dict__[pair[0]] = 1
 
100
                else:
 
101
                    blank.__dict__[pair[0]] = 0
 
102
            elif pair[0] == 'expiration_date':
 
103
                (year, month, day) = pair[1].split("-")
 
104
                blank.expiration_date = date(int(year), int(month), int(day))
 
105
            else:
 
106
                blank.__dict__[pair[0]] = pair[1]
 
107
        blank.status = 'active'
 
108
        blank.save()
 
109
        newhostname = blank.hostname.split(".")[0]
 
110
        newdomain = blank.hostname.split(".", 1)[1]
 
111
        while info and info[0][0] != 'hostname':
 
112
            if info[0][0] == 'mac_addr':
 
113
                pair = info.pop(0)
 
114
                inter = Interface.objects.create(host=blank, mac_addr=pair[1], hdwr_type='eth')
 
115
                inter.save()
 
116
            elif info[0][0] == 'hdwr_type':
 
117
                pair = info.pop(0)
 
118
                inter.hdwr_type = pair[1]
 
119
                inter.save()
 
120
            elif info[0][0] == 'ip_addr':
 
121
                pair = info.pop(0)
 
122
                ip = IP.objects.create(interface=inter, ip_addr=pair[1], num=1)
 
123
                hostnamenode = Name(ip=ip, name=blank.hostname, dns_view='global', only=False)
 
124
                hostnamenode.save()
 
125
                namenode = Name(ip=ip, name=".".join([newhostname + "-" + inter.hdwr_type,
 
126
                                                          newdomain]),
 
127
                                dns_view="global", only=False)
 
128
                namenode.save()
 
129
                subnetnode = Name(ip=ip, name=newhostname + "-" + 
 
130
                                  ip.ip_addr.split(".")[2] + "." +
 
131
                                  newdomain, dns_view="global", only=False)
 
132
                subnetnode.save()
 
133
                hostnamenode.mxs.add(mx)
 
134
                namenode.mxs.add(mx)
 
135
                subnetnode.mxs.add(mx)
 
136
            elif info[0][0] == 'cname':
 
137
                pair = info.pop(0)
 
138
                cname = CName.objects.create(name=hostnamenode, cname=pair[1])
 
139
                cname.save()
 
140