ansible/filter_plugins/fedmsg.py
Kevin Fenzi 74591e19b5 filter_plugins / fedmsg: try and make staging only talk to staging hosts
I think the only reason this worked in phx2 was because we also had a
firewall rule that was blocking the staging subnet on all prod machines.
Instead of doing that here, lets just make fedmsg only deal with the
staging hosts in staging. The only thing this might break is things that
aren't in staging_friendly that should be.

Signed-off-by: Kevin Fenzi <kevin@scrye.com>
2021-03-01 14:34:41 -08:00

39 lines
1.2 KiB
Python

import operator
def invert_fedmsg_policy(groups, vars, env):
""" Given hostvars that map hosts -> topics, invert that
and return a dict that maps topics -> hosts.
Really, returns a list of tuples -- not a dict.
"""
if env == 'staging':
hosts = groups['staging']
else:
hosts = [h for h in groups['all'] if h not in groups['staging']]
inverted = {}
for host in hosts:
prefix = '.'.join([vars[host]['fedmsg_prefix'],
vars[host]['fedmsg_env']])
fqdn = vars[host].get('fedmsg_fqdn', host)
for cert in vars[host]['fedmsg_certs']:
for topic in cert.get('can_send', []):
key = prefix + '.' + topic
inverted[key] = inverted.get(key, [])
inverted[key].append(cert['service'] + '-' + fqdn)
result = list(inverted.items())
# Sort things so they come out in a reliable order (idempotence)
[inverted[key].sort() for key in inverted]
result.sort(key=operator.itemgetter(0))
return result
class FilterModule(object):
def filters(self):
return {
"invert_fedmsg_policy": invert_fedmsg_policy,
}