2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
/*
|
|
|
|
Package cmd is the entrypoint of the Constellation CLI.
|
|
|
|
|
|
|
|
Business logic of the CLI shall be implemented in the internal/cmd package.
|
|
|
|
*/
|
2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cmd"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Execute starts the CLI.
|
|
|
|
func Execute() error {
|
2022-06-07 05:13:34 -04:00
|
|
|
cobra.EnableCommandSorting = false
|
2022-05-11 03:20:37 -04:00
|
|
|
rootCmd := NewRootCmd()
|
2022-03-22 11:03:15 -04:00
|
|
|
ctx, cancel := signalContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
return rootCmd.ExecuteContext(ctx)
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:20:37 -04:00
|
|
|
// NewRootCmd creates the root command.
|
|
|
|
func NewRootCmd() *cobra.Command {
|
|
|
|
rootCmd := &cobra.Command{
|
2023-08-04 07:53:51 -04:00
|
|
|
Use: "constellation",
|
|
|
|
Short: "Manage your Constellation cluster",
|
|
|
|
Long: "Manage your Constellation cluster.",
|
|
|
|
PersistentPreRunE: preRunRoot,
|
2022-05-11 03:20:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set output of cmd.Print to stdout. (By default, it's stderr.)
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
rootCmd.PersistentFlags().StringP("workspace", "C", "", "path to the Constellation workspace")
|
2022-11-21 11:02:33 -05:00
|
|
|
rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging")
|
2023-02-12 16:55:39 -05:00
|
|
|
rootCmd.PersistentFlags().Bool("force", false, "disable version compatibility checks - might result in corrupted clusters")
|
2023-07-05 10:44:57 -04:00
|
|
|
rootCmd.PersistentFlags().String("tf-log", "NONE", "Terraform log level")
|
2022-05-11 03:20:37 -04:00
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
must(rootCmd.MarkPersistentFlagDirname("workspace"))
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewConfigCmd())
|
|
|
|
rootCmd.AddCommand(cmd.NewCreateCmd())
|
2023-10-26 09:59:13 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewApplyCmd())
|
2022-10-07 03:38:43 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewMiniCmd())
|
2023-07-05 10:44:57 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewStatusCmd())
|
2022-06-08 02:14:28 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewVerifyCmd())
|
2022-09-11 10:24:15 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewUpgradeCmd())
|
2022-06-08 02:14:28 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewRecoverCmd())
|
|
|
|
rootCmd.AddCommand(cmd.NewTerminateCmd())
|
2022-12-07 05:48:54 -05:00
|
|
|
rootCmd.AddCommand(cmd.NewIAMCmd())
|
2023-07-05 10:44:57 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewVersionCmd())
|
2023-10-26 09:59:13 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewInitCmd())
|
2023-11-13 12:46:20 -05:00
|
|
|
rootCmd.AddCommand(cmd.NewMaaPatchCmd())
|
2022-05-11 03:20:37 -04:00
|
|
|
|
|
|
|
return rootCmd
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// signalContext returns a context that is canceled on the handed signal.
|
|
|
|
// The signal isn't watched after its first occurrence. Call the cancel
|
|
|
|
// function to ensure the internal goroutine is stopped and the signal isn't
|
|
|
|
// watched any longer.
|
|
|
|
func signalContext(ctx context.Context, sig os.Signal) (context.Context, context.CancelFunc) {
|
|
|
|
sigCtx, stop := signal.NotifyContext(ctx, sig)
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
stopDone := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() { stopDone <- struct{}{} }()
|
|
|
|
defer stop()
|
|
|
|
select {
|
|
|
|
case <-sigCtx.Done():
|
|
|
|
fmt.Println(" Signal caught. Press ctrl+c again to terminate the program immediately.")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cancelFunc := func() {
|
|
|
|
done <- struct{}{}
|
|
|
|
<-stopDone
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigCtx, cancelFunc
|
|
|
|
}
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
func preRunRoot(cmd *cobra.Command, _ []string) error {
|
2022-05-04 12:42:13 -04:00
|
|
|
cmd.SilenceUsage = true
|
2023-08-04 07:53:51 -04:00
|
|
|
|
|
|
|
workspace, err := cmd.Flags().GetString("workspace")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting workspace flag: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to workspace directory if set.
|
|
|
|
if workspace != "" {
|
|
|
|
if err := os.Chdir(workspace); err != nil {
|
|
|
|
return fmt.Errorf("changing from current directory to workspace %q: %w", workspace, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-05-04 12:42:13 -04:00
|
|
|
}
|
|
|
|
|
2022-05-04 02:50:50 -04:00
|
|
|
func must(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|