add some basic logging output to certmaster
[certmaster.git] / certmaster / overlord / groups.py
1 #!/usr/bin/python
2
3 ## func command line interface & client lib
4 ##
5 ## Copyright 2007,2008 Red Hat, Inc
6 ## Adrian Likins <alikins@redhat.com>
7 ## +AUTHORS
8 ##
9 ## This software may be freely redistributed under the terms of the GNU
10 ## general public license.
11 ##
12 ## You should have received a copy of the GNU General Public License
13 ## along with this program; if not, write to the Free Software
14 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 ##
16
17
18 # this module lets you define groups of systems to work with from the
19 # commandline. It uses an "ini" style config parser like:
20
21 #[groupname]
22 #host = foobar, baz, blip
23 #subgroup = blippy
24
25
26 import ConfigParser
27 import os
28
29
30 class Groups(object):
31
32 def __init__(self, filename="/etc/func/groups"):
33 self.filename = filename
34 self.group_names = {}
35 self.groups = {}
36 self.__parse()
37
38 def __parse(self):
39
40 self.cp = ConfigParser.SafeConfigParser()
41 self.cp.read(self.filename)
42
43 for section in self.cp.sections():
44 self.add_group(section)
45 options = self.cp.options(section)
46 for option in options:
47 if option == "host":
48 self.add_hosts_to_group(section, self.cp.get(section, option))
49 if option == "subgroup":
50 pass
51
52
53 def show(self):
54 print self.cp.sections()
55 print self.groups
56
57 def add_group(self, group):
58 pass
59
60 def __parse_hoststrings(self, hoststring):
61 hosts = []
62 bits = hoststring.split(';')
63 for bit in bits:
64 blip = bit.strip().split(' ')
65 for host in blip:
66 if host not in hosts:
67 hosts.append(host.strip())
68
69 return hosts
70
71 def add_hosts_to_group(self, group, hoststring):
72 hosts = self.__parse_hoststrings(hoststring)
73 for host in hosts:
74 self.add_host_to_group(group, host)
75
76
77
78 def add_host_to_group(self, group, host):
79 if not self.groups.has_key(group):
80 self.groups[group] = []
81 self.groups[group].append(host)
82
83 def get_groups(self):
84 return self.groups
85
86
87
88 def main():
89 g = Groups("/tmp/testgroups")
90 print g.show()
91
92
93
94 if __name__ == "__main__":
95 main()