Skip to content

Commit

Permalink
[juju] Add plugin option for Juju state reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelThamm committed Oct 15, 2024
1 parent 32e9650 commit 55d9643
Showing 1 changed file with 29 additions and 37 deletions.
66 changes: 29 additions & 37 deletions sos/report/plugins/juju.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

class Juju(Plugin, UbuntuPlugin):

short_desc = "Juju orchestration tool"
short_desc = 'Juju orchestration tool'

plugin_name = "juju"
profiles = ("virt", "sysmgmt",)
plugin_name = 'juju'
profiles = ('virt', 'sysmgmt',)

# Using files instead of packages here because there is no identifying
# package on a juju machine.
Expand All @@ -29,7 +29,7 @@ class Juju(Plugin, UbuntuPlugin):
"juju-state",
default=False,
val_type=bool,
desc="Apply PR feature functionality",
desc="Include Juju state in the report",
),
PluginOpt(
"juju-user",
Expand Down Expand Up @@ -62,28 +62,27 @@ def setup(self):
self.add_copy_spec("/var/lib/juju/agents/*/agent.conf")

# Get a directory listing of /var/log/juju and /var/lib/juju
self.add_dir_listing(
["/var/log/juju*", "/var/lib/juju*"], recursive=True
)
self.add_dir_listing([
'/var/log/juju*',
'/var/lib/juju*'
], recursive=True)

if self.get_option("all_logs"):
# /var/lib/juju used to be in the default capture moving here
# because it usually was way to big. However, in most cases you
# want all logs you want this too.
self.add_copy_spec(
[
"/var/log/juju",
"/var/lib/juju",
"/var/lib/juju/**/.*",
]
)
self.add_copy_spec([
"/var/log/juju",
"/var/lib/juju",
"/var/lib/juju/**/.*",
])
self.add_forbidden_path("/var/lib/juju/kvm")
else:
# We need this because we want to collect to the limit of all
# logs in the directory.
self.add_copy_spec("/var/log/juju/*.log")

# Only run the feature code if this plugin option is set
# Only include the Juju state report if this plugin option is set
if not self.get_option("juju-state"):
return

Expand All @@ -92,15 +91,15 @@ def setup(self):
pwd.getpwnam(juju_user)
except KeyError:
self._log_warn(
f'User "{juju_user}" does not exist, will not collect Juju \
information.'
f'User "{juju_user}" does not exist, '
"will not collect Juju information."
)
return

if self.get_option("controllers") and self.get_option("models"):
self._log_warn(
"Options: controllers, models are mutually exclusive. Will \
not collect Juju information."
"Options: controllers, models are mutually exclusive. "
"Will not collect Juju information."
)
return

Expand All @@ -126,11 +125,8 @@ def setup(self):

# Specific models
if self.get_option("models"):
models = self.get_option("models").split(" ")
commands = [
f"juju status -m {model} --format=json" for model in models
]
for command in commands:
for model in self.get_option("models").split(" "):
command = f"juju status -m {model} --format=json"
self.collect_cmd_output(command, runas=juju_user)

# All controllers and all models OR specific controllers and all
Expand All @@ -142,17 +138,13 @@ def setup(self):
runas=juju_user,
)
if models_json["status"] == 0:
models = [
model["short-name"]
for model in json.loads(models_json["output"])[
"models"
]
]
commands = [
f"juju status -m {controller}:{model} --format=json"
for model in models
]
for command in commands:
models = json.loads(models_json["output"])["models"]
for model in models:
short_name = model["short-name"]
command = (
f"juju status -m {controller}:{short_name} "
f"--format=json"
)
self.collect_cmd_output(command, runas=juju_user)

def postproc(self):
Expand All @@ -165,11 +157,11 @@ def postproc(self):
]

# Redact simple yaml style "key: value".
keys_regex = rf"(^\s*({'|'.join(protect_keys)})\s*:\s*)(.*)"
keys_regex = fr"(^\s*({'|'.join(protect_keys)})\s*:\s*)(.*)"
sub_regex = r"\1*********"
self.do_path_regex_sub(agents_path, keys_regex, sub_regex)
# Redact certificates
self.do_file_private_sub(agents_path)

self.do_cmd_private_sub('juju controllers')

# vim: set et ts=4 sw=4 :

0 comments on commit 55d9643

Please sign in to comment.