Skip to content

Commit

Permalink
Revert "Confirm cancelling when user cancels (Ctrl+C) CLI execution (#…
Browse files Browse the repository at this point in the history
…5207)" (#5724)

This reverts commit 045372f.
  • Loading branch information
chrisdoherty4 authored Apr 27, 2023
1 parent 5fa0222 commit b9c7528
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 267 deletions.
49 changes: 7 additions & 42 deletions cmd/eksctl-anywhere/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package cmd
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"syscall"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/aws/eks-anywhere/pkg/console"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/signals"
)

var rootCmd = &cobra.Command{
Expand All @@ -42,55 +38,24 @@ func init() {
}

func rootPersistentPreRun(cmd *cobra.Command, args []string) {
stdout := logger.NewPausableWriter(os.Stdout)

if err := initLogger(stdout); err != nil {
if err := initLogger(); err != nil {
log.Fatal(err)
}

signals.On(func() {
resume := stdout.Pause()

logger.Info("Warning: Terminating this operation may leave the cluster in an irrecoverable state.")
if console.Confirm("Are you sure you want to exit?", os.Stdout, os.Stdin) {
os.Exit(-1)
}

if err := resume(); err != nil {
// Logging the error may not alert the user because the logger may be borked.
// We can't terminate the program so instead we're writing to Stdout so the user is
// aware an issue has happened.
//
// We still log the error as other log sinks may be working.
logger.Error(err, "Resuming stdout logging")
fmt.Fprintf(os.Stderr, "Failed to resume stdout logging: %s", err)
}
}, syscall.SIGINT)

// We've historically performed a forceful process termination on SIGTERM. We should be able to
// improve this and trigger a graceful shutdown instead.
signals.On(func() {
logger.Info("Warning: Terminating may have left your cluster in an irrecoverable state.")
os.Exit(-1)
}, syscall.SIGTERM)
}

func initLogger(consoleWriter io.Writer) error {
func initLogger() error {
logsFolder := filepath.Join(".", "eksa-cli-logs")
err := os.MkdirAll(logsFolder, 0o750)
if err != nil {
return fmt.Errorf("failed to create logs folder: %v", err)
}
filename := fmt.Sprintf("%s.log", time.Now().Format("2006-01-02T15_04_05"))
logFilePath := filepath.Join(".", "eksa-cli-logs", filename)

err = logger.Init(logger.Options{
outputFilePath := filepath.Join(".", "eksa-cli-logs", fmt.Sprintf("%s.log", time.Now().Format("2006-01-02T15_04_05")))
if err = logger.Init(logger.Options{
Level: viper.GetInt("verbosity"),
OutputFilePath: logFilePath,
Console: consoleWriter,
})
if err != nil {
return fmt.Errorf("root cmd: init zap: %v", err)
OutputFilePath: outputFilePath,
}); err != nil {
return fmt.Errorf("root cmd: %v", err)
}

return nil
Expand Down
10 changes: 10 additions & 0 deletions cmd/eksctl-anywhere/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ package main
import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/aws/eks-anywhere/cmd/eksctl-anywhere/cmd"
"github.com/aws/eks-anywhere/pkg/eksctl"
"github.com/aws/eks-anywhere/pkg/logger"
)

func main() {
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChannel
logger.Info("Warning: Terminating this operation may leave the cluster in an irrecoverable state")
os.Exit(-1)
}()
if eksctl.Enabled() {
err := eksctl.ValidateVersion()
if err != nil {
Expand Down
23 changes: 0 additions & 23 deletions pkg/console/console.go

This file was deleted.

67 changes: 0 additions & 67 deletions pkg/console/console_test.go

This file was deleted.

12 changes: 2 additions & 10 deletions pkg/logger/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logger

import (
"fmt"
"io"
"os"
"time"

Expand Down Expand Up @@ -44,16 +43,12 @@ func Init(opts Options) error {
return err
}

// Default to os.Stdout if no writer is provided.
if opts.Console == nil {
opts.Console = os.Stdout
}

// Build the encoders and logger.

fileEncoder := zapcore.NewJSONEncoder(encoderCfg)
consoleEncoder := zapcore.NewConsoleEncoder(encoderCfg)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, zapcore.AddSync(opts.Console), logrAtomicLevel(opts.Level)),
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), logrAtomicLevel(opts.Level)),
zapcore.NewCore(fileEncoder, logFile, logrAtomicLevel(MaxLogLevel)),
)
logger := zap.New(core)
Expand All @@ -74,9 +69,6 @@ type Options struct {
// OutputFilePath is an absolute file path. The file will be created if it doesn't exist.
// All logs available at level 9 will be written to the file.
OutputFilePath string

// Console is a Writer that represents the console. It defaults to os.Stdout.
Console io.Writer
}

// logrAtomicLevel creates a zapcore.AtomicLevel compatible with go-logr.
Expand Down
51 changes: 0 additions & 51 deletions pkg/logger/pause.go

This file was deleted.

40 changes: 0 additions & 40 deletions pkg/logger/pause_test.go

This file was deleted.

34 changes: 0 additions & 34 deletions pkg/signals/signals.go

This file was deleted.

0 comments on commit b9c7528

Please sign in to comment.