-
Notifications
You must be signed in to change notification settings - Fork 9
/
cloud.go
104 lines (86 loc) · 3.01 KB
/
cloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package install
import (
"errors"
"os"
"github.com/microsoft/tyger/cli/internal/install"
"github.com/microsoft/tyger/cli/internal/install/cloudinstall"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
func NewCloudCommand(parentCommand *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "cloud",
Short: "Manage cloud infrastructure",
Long: "Manage cloud infrastructure",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
return errors.New("a command is required")
},
}
cmd.AddCommand(newCloudInstallCommand())
cmd.AddCommand(newCloudUninstallCommand())
return cmd
}
func newCloudInstallCommand() *cobra.Command {
flags := commonFlags{}
cmd := cobra.Command{
Use: "install -f CONFIG.yml",
Short: "Install cloud infrastructure",
Long: "Install cloud infrastructure",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx, installer := commonPrerun(cmd.Context(), &flags)
cloudInstaller := CheckCloudInstaller(installer)
log.Info().Msg("Starting cloud install")
if err := cloudInstaller.InstallCloud(ctx); err != nil {
if err != install.ErrAlreadyLoggedError {
log.Fatal().Err(err).Send()
}
os.Exit(1)
}
log.Info().Msg("Install complete")
},
}
addCommonFlags(&cmd, &flags)
return &cmd
}
func newCloudUninstallCommand() *cobra.Command {
flags := commonFlags{}
cmd := cobra.Command{
Use: "uninstall -f CONFIG.yml",
Short: "Uninstall cloud infrastructure",
Long: "Uninstall cloud infrastructure",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx, installer := commonPrerun(cmd.Context(), &flags)
cloudInstaller := CheckCloudInstaller(installer)
log.Info().Msg("Starting cloud uninstall")
if err := cloudInstaller.UninstallCloud(ctx); err != nil {
if err != install.ErrAlreadyLoggedError {
log.Fatal().Err(err).Send()
}
os.Exit(1)
}
log.Info().Msg("Uninstall complete")
},
}
addCommonFlags(&cmd, &flags)
return &cmd
}
func addCommonFlags(cmd *cobra.Command, flags *commonFlags) {
cmd.Flags().StringVarP(&flags.configPath, "file", "f", "", "path to the installation configuration YAML file")
cmd.MarkFlagRequired("file")
cmd.Flags().StringToStringVar(&flags.setOverrides, "set", nil, "override config values (e.g. --set cloud.subscriptionID=1234 --set cloud.resourceGroup=mygroup)")
}
func CheckCloudInstaller(installer install.Installer) *cloudinstall.Installer {
cloudInstaller, ok := installer.(*cloudinstall.Installer)
if !ok {
log.Fatal().Msgf("This command is only supported on configurations where the `kind` field is `%s`.", cloudinstall.EnvironmentKindCloud)
}
return cloudInstaller
}