This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
100 lines (81 loc) · 2.64 KB
/
init.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
package main
import (
"flag"
"fmt"
"log"
"os"
)
const (
banner = " _ _ \n" +
"| | | | \n" +
"| |__ ___| |_ __ ___ ___ _ __ ___ __ _ _ __\n" +
"| '_ \\ / _ \\ | '_ ` _ \\/ __| '_ ` _ \\ / _` | '_ \\ \n" +
"| | | | __/ | | | | | \\__ \\ | | | | | (_| | | | | \n" +
"|_| |_|\\___|_|_| |_| |_|___/_| |_| |_|\\__,_|_| |_|"
slogan = "A Helm-Charts-as-Code tool.\n\n"
)
// init is executed after all package vars are initialized [before the main() func in this case].
// It checks if Helm and Kubectl exist and configures: the connection to the k8s cluster, helm repos, namespaces, etc.
func init() {
//parsing command line flags
flag.StringVar(&file, "f", "example.toml", "desired state file name")
flag.BoolVar(&apply, "apply", false, "apply the plan directly")
flag.BoolVar(&debug, "debug", false, "show the execution logs")
flag.BoolVar(&help, "help", false, "show Helmsman help")
flag.BoolVar(&v, "v", false, "show the version")
flag.BoolVar(&verbose, "verbose", false, "show verbose execution logs")
flag.StringVar(&nsOverride, "ns-override", "", "override defined namespaces with this one")
flag.BoolVar(&skipValidation, "skip-validation", false, "skip desired state validation")
flag.BoolVar(&applyLabels, "apply-labels", false, "apply Helmsman labels to Helm state for all defined apps.")
flag.Parse()
fmt.Println(banner + "version: " + version + "\n" + slogan)
if v {
fmt.Println("Helmsman version: " + version)
os.Exit(0)
}
if help {
printHelp()
os.Exit(0)
}
//fmt.Println("Helmsman version: " + version)
if !toolExists("helm") {
log.Fatal("ERROR: helm is not installed/configured correctly. Aborting!")
}
if !toolExists("kubectl") {
log.Fatal("ERROR: kubectl is not installed/configured correctly. Aborting!")
}
// read the TOML/YAML desired state file
result, msg := fromFile(file, &s)
if result {
log.Printf(msg)
} else {
log.Fatal(msg)
}
if !skipValidation {
// validate the desired state content
if result, msg := s.validate(); !result { // syntax validation
log.Fatal(msg)
}
} else {
log.Println("INFO: desired state validation is skipped.")
}
if applyLabels {
for _, r := range s.Apps {
labelResource(r)
}
}
}
// toolExists returns true if the tool is present in the environment and false otherwise.
// It takes as input the tool's command to check if it is recognizable or not. e.g. helm or kubectl
func toolExists(tool string) bool {
cmd := command{
Cmd: "bash",
Args: []string{"-c", tool},
Description: "validating that " + tool + " is installed.",
}
exitCode, _ := cmd.exec(debug, false)
if exitCode != 0 {
return false
}
return true
}