summaryrefslogtreecommitdiffstats
path: root/config.py
blob: 7a3adcc553b8fa86f9efa22bb48eff7fce9e4189 (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
# Abstracts out the config file format/access and holds
# reasonable default settings.
#
# Michael DeHaan <mdehaan@redhat.com>

import api 
import util
from msg import *

import os
import yaml
import traceback

class BootConfig:

    """
    Constructor.  This class maintains both the logical
    configuration for Boot and the file representation thereof.
    Creating the config object only loads the default values,
    users of this class need to call deserialize() to load config
    file values.
    """
    def __init__(self,api):
        self.api = api
        self.config_file    = "/etc/bootconf.conf"
        self.set_defaults()
        self.clear()

    """
    Establish an empty list of groups, distros, and systems.
    """
    def clear(self):
        self.groups         = api.Groups(self.api,None)
        self.distros        = api.Distros(self.api,None)
        self.systems        = api.Systems(self.api,None)

    """
    Set some reasonable defaults in case no values are available
    """
    def set_defaults(self):
        self.servername     = "your_server_ip"
        self.kickstart_root = "/var/www/bootconf"
        self.kickstart_url  = "http://%s/kickstart" % (self.servername)
        self.kernel_root    = "/var/www/bootconf"
        self.tftpboot       = "/tftpboot"
        self.dhcpd_conf     = "/etc/dhcpd.conf"
        self.tftpd_conf     = "/etc/xinetd.d/tftp"
        self.pxelinux       = "/usr/lib/syslinux/pxelinux.0"    
        self.tftpd_bin      = "/usr/sbin/in.tftpd"
        self.dhcpd_bin      = "/usr/sbin/dhcpd"

    """
    Access the current groups list
    """
    def get_groups(self):
        return self.groups

    """
    Access the current distros list
    """
    def get_distros(self):
        return self.distros

    """
    Access the current systems list
    """
    def get_systems(self):
        return self.systems

    """
    Save all global config options in hash form (for serialization)
    """
    def config_to_hash(self):
        # FIXME: this duplication NEEDS to go away.
        # idea: add list of properties to self.properties
        # and use method_missing to write accessors???
        data = {}
        data['servername']     = self.servername
        data['kickstart_root'] = self.kickstart_root
        data['kickstart_url']  = self.kickstart_url
        data['kernel_root']    = self.kernel_root
        data['tftpboot']       = self.tftpboot
        data['dhcpd_conf']     = self.dhcpd_conf
        data['tftpd_conf']     = self.tftpd_conf
        data['pxelinux']       = self.pxelinux
        data['tftpd_bin']      = self.tftpd_bin
        data['dhcpd_bin']      = self.dhcpd_bin
        return data
    
    """
    Load all global config options from hash form (for deserialization)
    """
    def config_from_hash(self,hash):
        try:
            self.servername      = hash['servername']
            self.kickstart_root  = hash['kickstart_root']
            self.kickstart_url   = hash['kickstart_url']
            self.kernel_root     = hash['kernel_root']
            self.tftpboot        = hash['tftpboot']
            self.dhcpd_conf      = hash['dhcpd_conf']
            self.tftpd_conf      = hash['tftpd_conf']
            self.pxelinux        = hash['pxelinux']
            self.tftpd_bin       = hash['tftpd_bin']
            self.dhcpd_bin       = hash['dhcpd_bin']
        except:
            self.set_defaults()
    """
    Convert *everything* Boot knows about to a nested hash
    """
    def to_hash(self):
        world = {} 
        world['config']  = self.config_to_hash()
        world['distros'] = self.get_distros().to_datastruct()
        world['groups']  = self.get_groups().to_datastruct()
        world['systems'] = self.get_systems().to_datastruct()
        return world  


    """
    Convert a hash representation of a Boot config to 'reality'
    """
    def from_hash(self,hash):
        self.config_from_hash(hash['config'])
        self.distros = api.Distros(self.api, hash['distros'])
        self.groups  = api.Groups(self.api,  hash['groups'])
        self.systems = api.Systems(self.api, hash['systems'])

    # ------------------------------------------------------
    # we don't care about file formats until below this line

    """
    Save everything to the config file.
    This goes through an intermediate data format so we
    could use YAML later if we wanted.
    """
    def serialize(self):
        try:
            conf = open(self.config_file,"w+")
        except IOError:
            self.api.last_error = m("cant_create: %s" % self.config_file)
            return False
        data = self.to_hash()
        conf.write(yaml.dump(data))
        return True

    """
    Load everything from the config file.
    This goes through an intermediate data structure format so we
    could use YAML later if we wanted.
    """
    def deserialize(self):
        try:
            conf = yaml.loadFile(self.config_file)
            raw_data = conf.next()
            if raw_data is not None:
                self.from_hash(raw_data)
            return True
        except:
            self.api.last_error = m("parse_error")
            return False