Skip to content

Commit

Permalink
Better debug and auto update-database when charm upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
alithethird committed Aug 2, 2024
1 parent fc22478 commit c8573c4
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,25 @@ def _on_rotate_wordpress_secrets_action(self, event: ActionEvent):
self._reconciliation(event)
event.set_results({"result": "ok"})

def _update_database(self, dry_run: bool = False) -> types_.ExecResult:

cmd = ["wp", "core", "update-db"]
if dry_run:
cmd.append("--dry-run")
logger.info("Starting Database update process.")
result = self._run_wp_cli(cmd, timeout=600)
if result.return_code != 0:
return types_.ExecResult(
success=False,
result=None,
message=str(result.stderr) if result.stderr else "Database update failed",
)
logger.info("Finished Database update process.")

return types_.ExecResult(
success=True, result=None, message=str(result.stdout) if result.stdout else "ok"
)

def _on_update_database_action(self, event: ActionEvent):
"""Handle the update-database action.
Expand All @@ -276,17 +295,12 @@ def _on_update_database_action(self, event: ActionEvent):
Args:
event: Used for returning result or failure of action.
"""
cmd = ["wp", "core", "update-db"]
event.params.get("dry-run")
if event.params.get("dry-run"):
cmd.append("--dry-run")
result = self._update_database(event.params.get("dry-run"))

result = self._run_wp_cli(cmd, timeout=600)
if result.return_code != 0:
err_msg = str(result.stderr) if result.stderr else "Database update failed"
event.fail(err_msg)
if not result.success:
event.fail(result.message)
return
event.set_results({"result": "ok"})
event.set_results({"result": result.message})

@staticmethod
def _wordpress_secret_key_fields():
Expand Down Expand Up @@ -389,6 +403,7 @@ def _on_upgrade_charm(self, _event: UpgradeCharmEvent):
_event: required by ops framework, not used.
"""
self._setup_replica_data(_event)
self._update_database()

def _gen_wp_config(self):
"""Generate the wp-config.php file WordPress needs based on charm config and relations.
Expand Down

0 comments on commit c8573c4

Please sign in to comment.