Created
August 20, 2019 15:53
-
-
Save niedbalski/e0a01c06b930f7378d2601cb21604f1f to your computer and use it in GitHub Desktop.
update-hosts-juju.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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