MCPcopy Create free account
hub / github.com/apache/cloudstack / compare

Method compare

systemvm/debian/opt/cloud/bin/cs/CsNetfilter.py:133–187  ·  view source on GitHub ↗

Compare reality with what is needed

(self, list)

Source from the content-addressed store, hash-verified

131 logging.info("Delete rule %s from table %s", r.to_str(True), r.get_table())
132
133 def compare(self, list):
134 """ Compare reality with what is needed """
135 # PASS 1: Ensure all chains are present
136 for fw in list:
137 new_rule = CsNetfilter()
138 new_rule.parse(fw[2])
139 new_rule.set_table(fw[0])
140 self.add_chain(new_rule)
141
142 ruleSet = set()
143 # PASS 2: Create rules
144 for fw in list:
145 tupledFw = tuple(fw)
146 if tupledFw in ruleSet:
147 logging.debug("Already processed : %s", tupledFw)
148 continue
149
150 new_rule = CsNetfilter()
151 new_rule.parse(fw[2])
152 new_rule.set_table(fw[0])
153 if isinstance(fw[1], int):
154 new_rule.set_count(fw[1])
155
156 rule_chain = new_rule.get_chain()
157
158 logging.debug("Checking if the rule already exists: rule=%s table=%s chain=%s", new_rule.get_rule(), new_rule.get_table(), new_rule.get_chain())
159 if self.has_rule(new_rule):
160 logging.debug("Exists: rule=%s table=%s", fw[2], new_rule.get_table())
161 else:
162 # print "Add rule %s in table %s" % ( fw[2], new_rule.get_table())
163 logging.info("Add: rule=%s table=%s", fw[2], new_rule.get_table())
164 # front means insert instead of append
165 cpy = fw[2]
166 if fw[1] == "front":
167 cpy = cpy.replace('-A', '-I')
168 if isinstance(fw[1], int):
169 # if the rule is for ACLs, we want to insert them in order, right before the DROP all
170 if rule_chain.startswith("ACL_INBOUND") or rule_chain.startswith("ACL_OUTBOUND"):
171 rule_count = self.chain.get_count(rule_chain) if self.chain.get_count(rule_chain) > 0 else 1
172 cpy = cpy.replace("-A %s" % new_rule.get_chain(), '-I %s %s' % (new_rule.get_chain(), rule_count))
173 else:
174 cpy = cpy.replace("-A %s" % new_rule.get_chain(), '-I %s %s' % (new_rule.get_chain(), fw[1]))
175 ret = CsHelper.execute2("iptables -t %s %s" % (new_rule.get_table(), cpy))
176 # There are some issues in this framework causing failures .. like adding a chain without checking it is present causing
177 # the failures. Also some of the rule like removeFromLoadBalancerRule is deleting rule and deleteLoadBalancerRule
178 # trying to delete which causes the failure.
179 # For now raising the log.
180 # TODO: Need to fix in the framework.
181 if ret.returncode != 0:
182 error = ret.communicate()[0].decode()
183 logging.debug("iptables command got failed ... continuing")
184 ruleSet.add(tupledFw)
185 self.chain.add_rule(rule_chain)
186 self.del_standard()
187 self.get_unseen()
188
189 def add_chain(self, rule):
190 """ Add the given chain if it is not already present """

Callers 1

processMethod · 0.95

Calls 15

parseMethod · 0.95
set_tableMethod · 0.95
add_chainMethod · 0.95
set_countMethod · 0.95
get_chainMethod · 0.95
get_ruleMethod · 0.95
get_tableMethod · 0.95
has_ruleMethod · 0.95
del_standardMethod · 0.95
get_unseenMethod · 0.95
CsNetfilterClass · 0.85
debugMethod · 0.80

Tested by

no test coverage detected