Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> 

# 

# This file is part of Ansible 

# 

# Ansible is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# Ansible is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 

from __future__ import (absolute_import, division, print_function) 

__metaclass__ = type 

 

from ansible.errors import AnsibleError 

 

 

class Group: 

''' a group of ansible hosts ''' 

 

# __slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth', '_hosts_cache' ] 

 

def __init__(self, name=None): 

 

self.depth = 0 

self.name = name 

self.hosts = [] 

self._hosts = None 

self.vars = {} 

self.child_groups = [] 

self.parent_groups = [] 

self._hosts_cache = None 

self.priority = 1 

 

def __repr__(self): 

return self.get_name() 

 

def __str__(self): 

return self.get_name() 

 

def __getstate__(self): 

return self.serialize() 

 

def __setstate__(self, data): 

return self.deserialize(data) 

 

def serialize(self): 

parent_groups = [] 

for parent in self.parent_groups: 

parent_groups.append(parent.serialize()) 

 

self._hosts = None 

 

result = dict( 

name=self.name, 

vars=self.vars.copy(), 

parent_groups=parent_groups, 

depth=self.depth, 

hosts=self.hosts, 

) 

 

return result 

 

def deserialize(self, data): 

self.__init__() 

self.name = data.get('name') 

self.vars = data.get('vars', dict()) 

self.depth = data.get('depth', 0) 

self.hosts = data.get('hosts', []) 

self._hosts = None 

 

parent_groups = data.get('parent_groups', []) 

for parent_data in parent_groups: 

g = Group() 

g.deserialize(parent_data) 

self.parent_groups.append(g) 

 

@property 

def host_names(self): 

85 ↛ 87line 85 didn't jump to line 87, because the condition on line 85 was never false if self._hosts is None: 

self._hosts = set(self.hosts) 

return self._hosts 

 

def get_name(self): 

return self.name 

 

def add_child_group(self, group): 

 

94 ↛ 95line 94 didn't jump to line 95, because the condition on line 94 was never true if self == group: 

raise Exception("can't add group to itself") 

 

# don't add if it's already there 

98 ↛ exitline 98 didn't return from function 'add_child_group', because the condition on line 98 was never false if group not in self.child_groups: 

self.child_groups.append(group) 

 

# update the depth of the child 

group.depth = max([self.depth + 1, group.depth]) 

 

# update the depth of the grandchildren 

group._check_children_depth() 

 

# now add self to child's parent_groups list, but only if there 

# isn't already a group with the same name 

109 ↛ 114line 109 didn't jump to line 114, because the condition on line 109 was never false if self.name not in [g.name for g in group.parent_groups]: 

group.parent_groups.append(self) 

for h in group.get_hosts(): 

h.populate_ancestors() 

 

self.clear_hosts_cache() 

 

def _check_children_depth(self): 

 

try: 

119 ↛ 120line 119 didn't jump to line 120, because the loop on line 119 never started for group in self.child_groups: 

group.depth = max([self.depth + 1, group.depth]) 

group._check_children_depth() 

except RuntimeError: 

raise AnsibleError("The group named '%s' has a recursive dependency loop." % self.name) 

 

def add_host(self, host): 

126 ↛ exitline 126 didn't return from function 'add_host', because the condition on line 126 was never false if host.name not in self.host_names: 

self.hosts.append(host) 

self._hosts.add(host.name) 

host.add_group(self) 

self.clear_hosts_cache() 

 

def remove_host(self, host): 

 

if host.name in self.host_names: 

self.hosts.remove(host) 

self._hosts.remove(host.name) 

host.remove_group(self) 

self.clear_hosts_cache() 

 

def set_variable(self, key, value): 

 

142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true if key == 'ansible_group_priority': 

self.set_priority(int(value)) 

else: 

self.vars[key] = value 

 

def clear_hosts_cache(self): 

 

self._hosts_cache = None 

150 ↛ 151line 150 didn't jump to line 151, because the loop on line 150 never started for g in self.parent_groups: 

g.clear_hosts_cache() 

 

def get_hosts(self): 

 

if self._hosts_cache is None: 

self._hosts_cache = self._get_hosts() 

return self._hosts_cache 

 

def _get_hosts(self): 

 

hosts = [] 

seen = {} 

for kid in self.child_groups: 

kid_hosts = kid.get_hosts() 

for kk in kid_hosts: 

166 ↛ 165line 166 didn't jump to line 165, because the condition on line 166 was never false if kk not in seen: 

seen[kk] = 1 

168 ↛ 169line 168 didn't jump to line 169, because the condition on line 168 was never true if self.name == 'all' and kk.implicit: 

continue 

hosts.append(kk) 

for mine in self.hosts: 

172 ↛ 171line 172 didn't jump to line 171, because the condition on line 172 was never false if mine not in seen: 

seen[mine] = 1 

174 ↛ 175line 174 didn't jump to line 175, because the condition on line 174 was never true if self.name == 'all' and mine.implicit: 

continue 

hosts.append(mine) 

return hosts 

 

def get_vars(self): 

return self.vars.copy() 

 

def _get_ancestors(self): 

 

results = {} 

for g in self.parent_groups: 

results[g.name] = g 

results.update(g._get_ancestors()) 

return results 

 

def get_ancestors(self): 

 

return self._get_ancestors().values() 

 

def set_priority(self, priority): 

try: 

self.priority = int(priority) 

except TypeError: 

# FIXME: warn about invalid priority 

pass