From d24d66423a93123242dda24cca4622b5060267bd Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Sun, 19 May 2024 19:29:49 +0100 Subject: [PATCH] [sunbeam] Update plugin to collet more details * Add juju related info for sunbeam * Add logs from the sunbeam snap home user Resolves: SET-682 Signed-off-by: Arif Ali --- sos/report/plugins/sunbeam.py | 84 ++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/sos/report/plugins/sunbeam.py b/sos/report/plugins/sunbeam.py index 433b2a3e6a..6ab1931588 100644 --- a/sos/report/plugins/sunbeam.py +++ b/sos/report/plugins/sunbeam.py @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Canonical Ltd., Arif Ali +# # This file is part of the sos project: https://github.com/sosreport/sos # # This copyrighted material is made available to anyone wishing to use, @@ -6,7 +8,9 @@ # # See the LICENSE file in the source distribution for further information. -from sos.report.plugins import Plugin, UbuntuPlugin +import json +import pwd +from sos.report.plugins import Plugin, UbuntuPlugin, PluginOpt class Sunbeam(Plugin, UbuntuPlugin): @@ -19,6 +23,13 @@ class Sunbeam(Plugin, UbuntuPlugin): common_dir = '/var/snap/openstack/common' + option_list = [ + PluginOpt('sunbeam-user', default='ubuntu', val_type=str, + desc='The user used for sunbeam installation'), + PluginOpt('juju-allow-login', default=False, val_type=bool, + desc='Allow sos to login to juju'), + ] + def setup(self): self.add_service_status('snap.openstack.*') @@ -37,6 +48,77 @@ def setup(self): 'sunbeam cluster list --format yaml', ]) + sunbeam_user = self.get_option("sunbeam-user") + + user_pwd = pwd.getpwnam(sunbeam_user) + if user_pwd: + sb_snap_homedir = f'{user_pwd.pw_dir}/snap/openstack/common' + + self.add_copy_spec([ + f"{sb_snap_homedir}/*.log", + f"{sb_snap_homedir}/etc/*/*.log", + f"{sb_snap_homedir}/logs/*.log", + ]) + + juju_allow_login = self.get_option("juju-allow-login") + + if juju_allow_login and user_pwd: + res = self.collect_cmd_output( + f'"su -u {sunbeam_user} -c "sunbeam utils juju-login"') + if res['status'] == 1: + account_file = (f'{user_pwd.pw_dir}/snap/openstack/current/' + 'account.yaml') + try: + with open(account_file, 'r', encoding='UTF-8') as afile: + for line in afile.read().splitlines(): + words = line.split() + if words[0].strip() == 'password:': + juju_pass = words[1].strip() + if words[0].strip() == 'user:': + juju_user = words[1].strip() + if juju_user and juju_pass: + self.collect_cmd_output( + fr'su - {sunbeam_user} "echo \"{juju_pass}\" | ' + f'juju login -u {juju_user}"') + except IOError as error: + self._log_error(f'Could not open conf file {account_file}:' + f' {error}') + self._get_juju_cmd_details(sunbeam_user) + + def _get_juju_cmd_details(self, user): + self.add_cmd_output("juju controllers", runas=user) + juju_controllers = self.collect_cmd_output( + "juju controllers --format json", runas=user) + + if juju_controllers['status'] == 0: + juju_ctrl_json = json.loads(juju_controllers['output']) + for controller in juju_ctrl_json['controllers'].keys(): + + self.add_cmd_output([ + f'juju models -c {controller}', + f'juju model-defaults -c {controller}', + f'juju controller-config -c {controller}', + f'juju controller-config -c {controller} --format json', + ], runas=user) + + juju_models = self.collect_cmd_output( + f'juju models -c {controller} --format json', + runas=user) + + if juju_models['status'] == 0: + juju_status_json = json.loads(juju_models['output']) + + for model in juju_status_json['models']: + + model_name = f'{controller}:{model["name"]}' + + self.add_cmd_output([ + f'juju status -m {model_name}', + f'juju status -m {model_name} --format json', + f'juju model-config -m {model_name}', + f'juju model-config -m {model_name} --format json', + ], runas=user) + def postproc(self): self.do_file_private_sub(