126 lines
3.8 KiB
Python
Executable File
126 lines
3.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import asyncio
|
|
import logging
|
|
import sys
|
|
|
|
from juju.model import Model
|
|
from juju.errors import JujuConnectionError
|
|
|
|
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:
|
|
try:
|
|
await model.connect(the_model)
|
|
except JujuConnectionError as e:
|
|
logger.error(e)
|
|
|
|
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="<model>",
|
|
help="The <model> to use to get the passwords",
|
|
dest="model", default="local")
|
|
parser.add_argument('-v', '--verbose',
|
|
help="The <model> to use to get the passwords",
|
|
dest="verbose", action='store_true')
|
|
parser.add_argument('-p', '--passwords', metavar="<passwords>",
|
|
help="The <passwords> 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()))
|