From b9e96d13412aed064d96f34268ab270e5b44f02e Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Mon, 29 Dec 2025 09:02:03 +0000 Subject: [PATCH] Add get_passwords.py, pythonised the bash script This has more features, with better handling of errors --- get_passwords.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 get_passwords.py diff --git a/get_passwords.py b/get_passwords.py new file mode 100755 index 0000000..0cf0834 --- /dev/null +++ b/get_passwords.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 + +import argparse +import asyncio +import logging +import sys + +from juju.model import Model + +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) +sh = logging.StreamHandler(sys.stdout) +logger.addHandler(sh) + + +async def main(args): + + ACTIONS = { + 'innodb_admin': { + 'app': 'mysql-innodb-cluster', + 'action': 'leader-get mysql.passwd', + }, + 'innodb_cluster': { + 'app': 'mysql-innodb-cluster', + 'action': 'leader-get cluster-password', + }, + 'percona': { + 'app': 'mysql', + 'action': 'leader-get root-password', + }, + 'nagios': { + 'app': 'nagios', + 'action': 'sudo cat /var/lib/juju/nagios.passwd', + }, + 'grafana': { + 'app': 'grafana', + 'cmd': "action", + 'action': "get-admin-password", + 'key': 'password', + }, + 'graylog': { + 'app': 'graylog', + 'cmd': "action", + 'action': "show-admin-password", + 'key': 'admin-password', + }, + 'keystone': { + 'app': 'keystone', + 'action': 'leader-get admin_passwd', + }, + } + + passwords = args.passwords + the_model = args.model + + if args.verbose: + logger.setLevel(logging.DEBUG) + + actions = [ + (k, v) for k, v in ACTIONS.items() + if passwords == "all" or k == passwords + ] + + model = Model() + if the_model == "local": + await model.connect_current() + else: + await model.connect(the_model) + + try: + for key, action in actions: + if action['app'] in model.applications: + units = model.applications[action['app']].units + cmd = 'run' if 'cmd' not in action else action['cmd'] + for unit in units: + if (await unit.is_leader_from_status()): + logger.info( + f'Getting {key} password from {unit.name}...') + await _run_action( + unit, action['action'], cmd, + action['key'] if 'key' in action else None) + finally: + await model.disconnect() + + +async def _run_action(unit, action, cmd, key=None): + + if cmd == "run": + action_output = await unit.run(action) + await action_output.fetch_output() + logger.debug(action_output.results) + if 'stdout' in action_output.results: + print(f"{unit.name}: {action_output.results['stdout'].strip()}") + elif cmd == "action": + action_output = await unit.run_action(action) + action_output = await action_output.wait() + + logger.debug(action_output.results) + if key in action_output.results: + print(f"{unit.name}: {action_output.results[key].strip()}") + + +def _parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('-m', '--model', metavar="", + help="The to use to get the passwords", + dest="model", default="local") + parser.add_argument('-v', '--verbose', + help="The to use to get the passwords", + dest="verbose", action='store_true') + parser.add_argument('-p', '--passwords', metavar="", + help="The to get from the model", + choices=['keystone', 'innodb_admin', 'grafana', + 'graylog', 'nagios', 'percona', + 'innodb_cluster', 'all'], + dest="passwords", default="all") + return parser.parse_args() + + +if __name__ == '__main__': + asyncio.run(main(_parse_args()))