Skip to content

Commit 3bfbc87

Browse files
committed
Restart daemon on update and add hook args
Bump version to v0.1.51. Improve auto-update flow to detect if the konta daemon is running and automatically restart it via systemctl, with logging and a fallback instruction if restart fails. Propagate context into hook invocations: pass reconciled project list to success hooks and include formatted error messages to failure hooks. Update hooks Runner API to accept arguments (RunSuccess(apps []string), RunFailure(errorMessage string)) and make the internal run() variadic so hook scripts receive these values as command-line arguments.
1 parent 5c754bc commit 3bfbc87

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

cmd/konta/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/talyguryn/konta/internal/logger"
1010
)
1111

12-
const Version = "0.1.50"
12+
const Version = "0.1.51"
1313

1414
func main() {
1515
if len(os.Args) < 2 {

internal/cmd/cmd.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,26 @@ func autoUpdate(currentVersion string, release *githubRelease) error {
815815

816816
runPostUpdateHook()
817817

818-
logger.Info("Auto-update complete: v%s installed. Restart the daemon to apply.", latestVersion)
818+
// Check if daemon is running
819+
statusCmd := exec.Command("systemctl", "is-active", "konta")
820+
err := statusCmd.Run()
821+
isDaemonRunning := err == nil
822+
823+
if isDaemonRunning {
824+
logger.Info("Auto-update complete: v%s installed. Restarting daemon...", latestVersion)
825+
826+
// Restart the daemon automatically
827+
restartCmd := exec.Command("systemctl", "restart", "konta")
828+
if err := restartCmd.Run(); err != nil {
829+
logger.Warn("Failed to restart daemon after auto-update: %v", err)
830+
logger.Info("Please restart manually: sudo konta restart")
831+
} else {
832+
logger.Info("Daemon restarted successfully with new version")
833+
}
834+
} else {
835+
logger.Info("Auto-update complete: v%s installed. Daemon is not running.", latestVersion)
836+
}
837+
819838
return nil
820839
}
821840

@@ -1128,7 +1147,7 @@ func reconcileOnce(dryRun bool, version string) error {
11281147
// Run pre-hook
11291148
if err := hookRunner.RunPre(); err != nil {
11301149
logger.Error("Pre-hook failed: %v", err)
1131-
_ = hookRunner.RunFailure()
1150+
_ = hookRunner.RunFailure(fmt.Sprintf("Pre-hook failed: %v", err))
11321151
return err
11331152
}
11341153

@@ -1138,15 +1157,15 @@ func reconcileOnce(dryRun bool, version string) error {
11381157
reconciledProjects, err := reconciler.Reconcile()
11391158
if err != nil {
11401159
logger.Error("Reconciliation failed: %v", err)
1141-
_ = hookRunner.RunFailure()
1160+
_ = hookRunner.RunFailure(fmt.Sprintf("Reconciliation failed: %v", err))
11421161
return err
11431162
}
11441163

11451164
// Atomic switch (only if not dry-run)
11461165
if !dryRun {
11471166
if err := atomicSwitch(newCommit, releaseDir); err != nil {
11481167
logger.Error("Atomic switch failed: %v", err)
1149-
_ = hookRunner.RunFailure()
1168+
_ = hookRunner.RunFailure(fmt.Sprintf("Atomic switch failed: %v", err))
11501169
return err
11511170
}
11521171

@@ -1163,10 +1182,10 @@ func reconcileOnce(dryRun bool, version string) error {
11631182
if !dryRun {
11641183
currentLink := state.GetCurrentLink()
11651184
successHookRunner := hooks.New(currentLink, cfg.Hooks.StartedAbs, cfg.Hooks.PreAbs, cfg.Hooks.SuccessAbs, cfg.Hooks.FailureAbs, cfg.Hooks.PostUpdateAbs)
1166-
if err := successHookRunner.RunSuccess(); err != nil {
1185+
if err := successHookRunner.RunSuccess(reconciledProjects); err != nil {
11671186
logger.Error("Success hook failed: %v", err)
11681187
}
1169-
} else if err := hookRunner.RunSuccess(); err != nil {
1188+
} else if err := hookRunner.RunSuccess(reconciledProjects); err != nil {
11701189
logger.Error("Success hook failed: %v", err)
11711190
}
11721191

internal/hooks/hooks.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,23 @@ func (r *Runner) RunPre() error {
4040
}
4141

4242
// RunSuccess runs the success hook
43-
func (r *Runner) RunSuccess() error {
44-
return r.run("success")
43+
// apps: list of applications that were successfully updated
44+
func (r *Runner) RunSuccess(apps []string) error {
45+
return r.run("success", apps...)
4546
}
4647

4748
// RunFailure runs the failure hook
48-
func (r *Runner) RunFailure() error {
49-
return r.run("failure")
49+
// errorMessage: the error message that caused the failure
50+
func (r *Runner) RunFailure(errorMessage string) error {
51+
return r.run("failure", errorMessage)
5052
}
5153

5254
// RunPostUpdate runs the post-update hook (executed after konta binary update)
5355
func (r *Runner) RunPostUpdate() error {
5456
return r.run("post_update")
5557
}
5658

57-
func (r *Runner) run(hookType string) error {
59+
func (r *Runner) run(hookType string, args ...string) error {
5860
hookPath := r.hookPaths[hookType]
5961
if hookPath == "" {
6062
logger.Debug("No %s hook configured", hookType)
@@ -74,7 +76,9 @@ func (r *Runner) run(hookType string) error {
7476

7577
logger.Debug("Running %s hook: %s", hookType, hookPath)
7678

77-
cmd := exec.Command("bash", hookPath)
79+
// Prepare command arguments: bash hook_script.sh [arg1] [arg2] ...
80+
cmdArgs := append([]string{hookPath}, args...)
81+
cmd := exec.Command("bash", cmdArgs...)
7882
cmd.Dir = r.repoDir
7983

8084
// Suppress output for post_update hook, show output for other hooks

0 commit comments

Comments
 (0)