-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlifecycles.go
More file actions
142 lines (121 loc) · 3.96 KB
/
lifecycles.go
File metadata and controls
142 lines (121 loc) · 3.96 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package commands
import (
"fmt"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/controller-sdk-go/config"
"github.com/drycc/workflow-cli/internal/loader"
)
func getLifecycleHandlerString(ptype, handler, signal string, lifecycleHandler *api.LifecycleHandler) string {
if lifecycleHandler.Exec != nil {
return fmt.Sprintf("%s %s exec %v %s", handler, ptype, lifecycleHandler.Exec.Command, signal)
} else if lifecycleHandler.Sleep != nil {
return fmt.Sprintf("%s %s sleep %v %s", handler, ptype, lifecycleHandler.Sleep, signal)
} else if lifecycleHandler.TCPSocket != nil {
return fmt.Sprintf("%s %s tcp-socket port=%v %s", handler, ptype, lifecycleHandler.TCPSocket.Port, signal)
} else if lifecycleHandler.HTTPGet != nil {
return fmt.Sprintf(
"%s %s http-get headers=%v path=%s port=%d %s",
handler,
ptype,
lifecycleHandler.HTTPGet.HTTPHeaders,
lifecycleHandler.HTTPGet.Path,
lifecycleHandler.HTTPGet.Port,
signal,
)
}
return ""
}
// LifecyclesList lists an app's lifecycles.
func (d *DryccCmd) LifecyclesList(appID, ptype string, version int) error {
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
config, err := config.List(s.Client, appID, version)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
if len(config.Lifecycle) == 0 {
d.Println(fmt.Sprintf("No lifecycle found in %s app.", appID))
return nil
}
ptypes := []string{}
if ptype != "" {
ptypes = append(ptypes, ptype)
} else {
for ptype := range config.Lifecycle {
ptypes = append(ptypes, ptype)
}
}
table := d.getDefaultFormatTable([]string{})
table.Append([]string{"App:", config.App})
table.Append([]string{"UUID:", config.UUID})
table.Append([]string{"Created:", d.formatTime(config.Created)})
table.Append([]string{"Updated:", d.formatTime(config.Updated)})
table.Append([]string{"Lifecycle:"})
for _, ptype := range sortPtypes(ptypes) {
if lifecycle, ok := config.Lifecycle[ptype]; ok {
if lifecycle.StopSignal != "" {
table.Append([]string{"", fmt.Sprintf("stopSignal=%s", lifecycle.StopSignal)})
}
if lifecycle.PostStart != nil {
table.Append([]string{"", getLifecycleHandlerString(ptype, "postStart", lifecycle.StopSignal, *lifecycle.PostStart)})
}
if lifecycle.PreStop != nil {
table.Append([]string{"", getLifecycleHandlerString(ptype, "preStop", lifecycle.StopSignal, *lifecycle.PreStop)})
}
}
}
table.Render()
return nil
}
// LifecyclesSet sets an app's lifecycle.
func (d *DryccCmd) LifecyclesSet(appID, ptype string, lifecycle *api.Lifecycle) error {
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
d.Print("Applying lifecycle... ")
quit := progress(d.WOut)
configObj := api.Config{Lifecycle: make(map[string]*api.Lifecycle)}
configObj.Lifecycle[ptype] = lifecycle
_, err = config.Set(s.Client, appID, configObj, true)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return d.LifecyclesList(appID, ptype, -1)
}
// LifecyclesUnset removes an app's lifecycle.
func (d *DryccCmd) LifecyclesUnset(appID, ptype string, handlers []string) error {
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
if err != nil {
return err
}
d.Print("Applying lifecycle... ")
quit := progress(d.WOut)
configObj := api.Config{Lifecycle: make(map[string]*api.Lifecycle)}
lifecycle := &api.Lifecycle{}
var nullLifecycleHandler *api.LifecycleHandler
for _, handler := range handlers {
switch handler {
case "postStart":
lifecycle.PostStart = &nullLifecycleHandler
case "preStop":
lifecycle.PreStop = &nullLifecycleHandler
default:
return fmt.Errorf("unknown lifecycle handler: %s", handler)
}
}
configObj.Lifecycle[ptype] = lifecycle
_, err = config.Set(s.Client, appID, configObj, true)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return d.LifecyclesList(appID, ptype, -1)
}