This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
deploy.go
128 lines (116 loc) · 3.06 KB
/
deploy.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
package cmd
import (
"os"
log "github.com/flanksource/commons/logger"
"github.com/flanksource/karina/pkg/phases/order"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var deployExclude []string
var Deploy = &cobra.Command{
Use: "deploy",
}
func init() {
var PhasesCmd = &cobra.Command{
Use: "phases",
Run: func(cmd *cobra.Command, args []string) {
phases := order.GetAllPhases()
p := getPlatform(cmd)
if _, err := p.GetClientset(); err != nil {
log.Fatalf("Failed to connect to platform, aborting deployment: %s", err)
os.Exit(1)
}
// we track the failure status, and continue on failure to allow degraded operations
failed := false
// first deploy strictly ordered phases, these phases are often dependencies for other phases
for _, name := range order.PhaseOrder {
flag, _ := cmd.Flags().GetBool(name)
if !flag {
continue
}
if err := phases[name](p); err != nil {
log.Errorf("Failed to deploy %s: %v", name, errors.WithStack(err))
failed = true
}
// remove the phase from the map so it isn't run again
delete(phases, name)
}
for name, fn := range phases {
flag, _ := cmd.Flags().GetBool(name)
if !flag {
continue
}
if err := fn(p); err != nil {
log.Errorf("Failed to deploy %s: %v", name, errors.WithStack(err))
failed = true
}
}
if failed {
os.Exit(1)
}
},
}
Deploy.AddCommand(PhasesCmd)
for name, fn := range order.GetAllPhases() {
_name := name
_fn := fn
PhasesCmd.Flags().Bool(name, false, "Deploy "+name)
Deploy.AddCommand(&cobra.Command{
Use: name,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
p := getPlatform(cmd)
if err := _fn(p); err != nil {
log.Fatalf("Failed to deploy %s: %v", _name, err)
}
},
})
}
var all = &cobra.Command{
Use: "all",
Short: "Build everything",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
p := getPlatform(cmd)
phases := order.GetPhases()
all := order.GetAllPhases()
// we track the failure status, and continue on failure to allow degraded operations
failed := false
for _, phase := range order.BootstrapPhases {
if sliceContains(deployExclude, phase) {
p.Tracef("Skipping excluded phase %s", phase)
continue
}
p.Tracef("Deploying %s", phase)
if err := all[phase](p); err != nil {
log.Errorf("Failed to deploy %s: %v", phase, err)
failed = true
}
}
for name, fn := range phases {
if sliceContains(deployExclude, name) {
p.Tracef("Skipping excluded phase %s", name)
continue
}
p.Tracef("Deploying %s", name)
if err := fn(p); err != nil {
log.Errorf("Failed to deploy %s: %v", name, err)
failed = true
}
}
if failed {
os.Exit(1)
}
},
}
all.Flags().StringSliceVar(&deployExclude, "exclude", []string{}, "A list of phases to exclude from deployment")
Deploy.AddCommand(all)
}
func sliceContains(slice []string, s string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}