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
|
#!/usr/bin/python
## func command line interface & client lib
##
## Copyright 2007,2008 Red Hat, Inc
## Adrian Likins <alikins@redhat.com>
## +AUTHORS
##
## This software may be freely redistributed under the terms of the GNU
## general public license.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
# this module lets you define groups of systems to work with from the
# commandline. It uses an "ini" style config parser like:
#[groupname]
#host = foobar, baz, blip
#subgroup = blippy
import ConfigParser
class Groups(object):
def __init__(self, filename="/etc/func/groups"):
self.filename = "/etc/func/groups"
if filename:
self.filename = filename
self.group_names = {}
self.groups = {}
self.__parse()
def __parse(self):
self.cp = ConfigParser.SafeConfigParser()
self.cp.read(self.filename)
for section in self.cp.sections():
options = self.cp.options(section)
for option in options:
if option == "host":
self.add_hosts_to_group(section, self.cp.get(section, option))
if option == "subgroup":
pass
def show(self):
print self.cp.sections()
print self.groups
def __parse_hoststrings(self, hoststring):
hosts = []
bits = hoststring.split(';')
for bit in bits:
blip = bit.strip().split(' ')
for host in blip:
if host not in hosts:
hosts.append(host.strip())
return hosts
def add_hosts_to_group(self, group, hoststring):
hosts = self.__parse_hoststrings(hoststring)
for host in hosts:
self.add_host_to_group(group, host)
def add_host_to_group(self, group, host):
if not self.groups.has_key(group):
self.groups[group] = []
self.groups[group].append(host)
def get_groups(self):
return self.groups
def get_hosts_by_groupgoo(self, groupgoo):
group_gloobs = groupgoo.split(':')
hosts = []
for group_gloob in group_gloobs:
if not group_gloob[0] == "@":
continue
if self.groups.has_key(group_gloob[1:]):
hosts = hosts + self.groups[group_gloob[1:]]
else:
print "group %s not defined" % group_gloob
return hosts
def main():
g = Groups("/tmp/testgroups")
print g.show()
if __name__ == "__main__":
main()
|