-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
128 lines (108 loc) · 2.77 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2022-present unTill Pro, Ltd.
* @author Maxim Geraskin
*/
package main
import (
_ "embed"
"os"
"os/exec"
"path/filepath"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/voedger/voedger/pkg/goutils/cobrau"
"github.com/voedger/voedger/pkg/goutils/logger"
)
//go:embed version
var version string
// path to SSH key (flag --ssh-key)
var sshKey string
// SSH port
var sshPort string
// skip checking nodes for the presence of the minimum allowable amount of RAM
var skipNodeMemoryCheck bool
var acmeDomains string
var devMode bool
var red func(a ...interface{}) string
var green func(a ...interface{}) string
// nolint
func main() {
cursorOff()
defer cursorOn()
ex, err := os.Executable()
if err != nil {
panic(err)
}
dir := filepath.Dir(ex)
os.Chdir(dir)
red = color.New(color.FgRed).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
logger.PrintLine = printLogLine
prepareScripts()
defer func() {
err := deleteScriptsTempDir()
if err != nil {
loggerError(err.Error())
}
}()
err = execRootCmd(os.Args, version)
if err != nil {
loggerError(err.Error())
cursorOn()
os.Exit(1)
}
}
// adds to the сommand flag --ssh-key
// If the environment variable VOEDGER_SSH_KEY is not established, then the flag is marked as a required
func addSshKeyFlag(cmds ...*cobra.Command) bool {
for _, cmd := range cmds {
cmd.PersistentFlags().StringVar(&sshKey, "ssh-key", "", "Path to SSH key")
value, exists := os.LookupEnv(envVoedgerSshKey)
if !exists || value == "" {
if err := cmd.MarkPersistentFlagRequired("ssh-key"); err != nil {
loggerError(err.Error())
return false
}
}
}
return true
}
// nolint
func cursorOff() {
cmd := exec.Command("setterm", "--cursor", "off")
cmd.Stdout = os.Stdout
cmd.Run()
}
// nolint
func cursorOn() {
cmd := exec.Command("setterm", "--cursor", "on")
cmd.Stdout = os.Stdout
cmd.Run()
}
var rootCmd *cobra.Command
// nolint
func execRootCmd(args []string, ver string) error {
version = ver
rootCmd = cobrau.PrepareRootCmd(
"ctool",
"Cluster managment utility",
args,
version,
newInitCmd(),
newValidateCmd(),
newUpgradeCmd(),
newReplaceCmd(),
newRepeatCmd(),
newBackupCmd(),
newAcmeCmd(),
newRestoreCmd(),
newMonCmd(),
newAlertCmd(),
)
rootCmd.SilenceErrors = true
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Perform a dry run of the command without making any actual changes")
rootCmd.PersistentFlags().BoolVar(&skipNodeMemoryCheck, "skip-node-memory-check", false, "Skip checking nodes for the presence of the minimum allowable amount of RAM")
rootCmd.PersistentFlags().BoolVar(&devMode, "dev-mode", false, "Use development mode for DB stack")
logger.SetLogLevel(getLoggerLevel())
return cobrau.ExecCommandAndCatchInterrupt(rootCmd)
}