Skip to content

Commit

Permalink
Attempt a graceful exit on interrupt
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Feb 5, 2025
1 parent fd6d01a commit 043ae74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
30 changes: 30 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package cmd

import (
"context"
"os"
"os/signal"
"syscall"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

func trapSignals(ctx context.Context, cancel context.CancelFunc) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ch:
log.Warnf("received an interrupt signal, aborting operation")
cancel()
case <-ctx.Done():
return
}
}

// App is the main urfave/cli.App for k0sctl
var App = &cli.App{
Name: "k0sctl",
Expand Down Expand Up @@ -31,4 +49,16 @@ var App = &cli.App{
completionCommand,
},
EnableBashCompletion: true,
Before: func(ctx *cli.Context) error {
if globalCancel == nil {
cancelCtx, cancel := context.WithCancel(ctx.Context)
ctx.Context = cancelCtx
globalCancel = cancel
}
go trapSignals(ctx.Context, globalCancel)
return nil
},
After: func(ctx *cli.Context) error {
return cancelTimeout(ctx)
},
}
5 changes: 4 additions & 1 deletion phase/uploadfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ func (p *UploadFiles) Run(ctx context.Context) error {
return p.parallelDoUpload(ctx, p.Config.Spec.Hosts, p.uploadFiles)
}

func (p *UploadFiles) uploadFiles(_ context.Context, h *cluster.Host) error {
func (p *UploadFiles) uploadFiles(ctx context.Context, h *cluster.Host) error {
for _, f := range h.Files {
if ctx.Err() != nil {
return fmt.Errorf("upload canceled: %w", ctx.Err())
}
var err error
if f.IsURL() {
err = p.uploadURL(h, f)
Expand Down

0 comments on commit 043ae74

Please sign in to comment.