Skip to content

Instantly share code, notes, and snippets.

@kapilt
Last active September 15, 2020 20:29
Show Gist options
  • Save kapilt/a61efcb4eaef9e685397 to your computer and use it in GitHub Desktop.
Save kapilt/a61efcb4eaef9e685397 to your computer and use it in GitHub Desktop.
Workaround for juju bug http://pad.lv/1215579
#!/usr/bin/python
"""
Juju fails to update addresses in relations when those addresses
change. This script can be used to propagate address changes on a
unit to all of its relations and related units.
This is a workaround for http://pad.lv/1215579
You need to transfer this file to all the units with changed addresses
and make it executable. Then you need to execute this from a client machine
via juju run.
ie. say i have units x/0 and y/1 that have changed addresses::
juju scp update-address x/0:/tmp/update-address
juju scp update-address y/1:/tmp/update-address
juju run --unit=x/0 --unit=y/0 "chmod +x /tmp/update-address"
juju run --unit=x/0 --unit=y/0 "/tmp/update-address"
"""
import json
import logging
import itertools
import os
import subprocess
# not in stdlib but in cloud-image
import yaml
def main():
md_path = os.path.join(os.environ['CHARM_DIR'], 'metadata.yaml')
with open(md_path) as fh:
md = yaml.safe_load(fh.read())
addr = subprocess.check_output(['unit-get', 'private-address']).strip()
rel_names = []
for rel_type in ('provides', 'requires', 'peers'):
rel_names.extend(md.get(kind, ()))
for name in rel_names:
rels = json.loads(
subprocess.check_output(['relation-ids', '--format=json', name]))
for r in rels:
subprocess.check_output(['relation-set', '-r', r, 'private-address=%s' % addr])
print "updated", os.environ['JUJU_UNIT_NAME'], name
if __name__ == '__main__':
main()
@bellini666
Copy link

Hey man,

Thank you so much for this! I forked this gist to fix some problems... If you can and want, pull it ;)

@kapilt
Copy link
Author

kapilt commented May 27, 2014

fixed, cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment