Skip to content

Instantly share code, notes, and snippets.

@niedbalski
Created August 20, 2019 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niedbalski/e0a01c06b930f7378d2601cb21604f1f to your computer and use it in GitHub Desktop.
Save niedbalski/e0a01c06b930f7378d2601cb21604f1f to your computer and use it in GitHub Desktop.
update-hosts-juju.py
#!/usr/bin/env python
import subprocess
import yaml
import sys
def juju_status(juju_env=None):
cmd = ['juju', 'status', '--format=yaml']
status_raw = subprocess.check_output(cmd)
return yaml.safe_load(status_raw)
def juju_run(machine, command):
print("running %s - machine %s" % (command, machine))
return subprocess.check_call(["juju", "run", "--machine", machine, "--", command])
def get_entries():
status = juju_status()
for k, m in status.get('machines').items():
yield m['dns-name'], m['ip-addresses'][0]
def set_etc_hosts():
entries = [x for x in get_entries()]
for machine_id in juju_status().get('machines').keys():
map(lambda x: juju_run_all(machine_id, "sudo echo '%s %s' | sudo tee -a /etc/hosts" % (x[0], x[1])), entries)
if __name__ == "__main__":
set_etc_hosts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment