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
/
Copy pathplan.go
124 lines (106 loc) · 3.35 KB
/
plan.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
package main
import (
"fmt"
"log"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
// orderedDecision type representing a Decision and it's priority weight
type orderedDecision struct {
Description string
Priority int
}
// orderedCommand type representing a Command and it's priority weight and the targeted release from the desired state
type orderedCommand struct {
Command command
Priority int
targetRelease *release
}
// plan type representing the plan of actions to make the desired state come true.
type plan struct {
Commands []orderedCommand
Decisions []orderedDecision
Created time.Time
}
// createPlan initializes an empty plan
func createPlan() plan {
p := plan{
Commands: []orderedCommand{},
Decisions: []orderedDecision{},
Created: time.Now().UTC(),
}
return p
}
// addCommand adds a command type to the plan
func (p *plan) addCommand(cmd command, priority int, r *release) {
oc := orderedCommand{
Command: cmd,
Priority: priority,
targetRelease: r,
}
p.Commands = append(p.Commands, oc)
}
// addDecision adds a decision type to the plan
func (p *plan) addDecision(decision string, priority int) {
od := orderedDecision{
Description: decision,
Priority: priority,
}
p.Decisions = append(p.Decisions, od)
}
// execPlan executes the commands (actions) which were added to the plan.
func (p plan) execPlan() {
p.sortPlan()
log.Println("INFO: Executing the plan ... ")
for _, cmd := range p.Commands {
if exitCode, msg := cmd.Command.exec(debug, verbose); exitCode != 0 {
logError("Command returned with exit code: " + string(exitCode) + ". And error message: " + msg)
} else {
if cmd.targetRelease != nil {
labelResource(cmd.targetRelease)
}
if _, err := url.ParseRequestURI(s.Settings["slackWebhook"]); err == nil {
notifySlack(cmd.Command.Description+" ... SUCCESS!", s.Settings["slackWebhook"], false, true)
}
}
}
}
// printPlanCmds prints the actual commands that will be executed as part of a plan.
func (p plan) printPlanCmds() {
fmt.Println("Printing the commands of the current plan ...")
for _, cmd := range p.Commands {
fmt.Println(cmd.Command.Args[1])
}
}
// printPlan prints the decisions made in a plan.
func (p plan) printPlan() {
fmt.Println("----------------------")
log.Printf("INFO: Plan generated at: %s \n", p.Created.Format("Mon Jan _2 2006 15:04:05"))
for _, decision := range p.Decisions {
fmt.Println(decision.Description + " -- priority: " + strconv.Itoa(decision.Priority))
}
}
// sendPlanToSlack sends the description of plan commands to slack if a webhook is provided.
func (p plan) sendPlanToSlack() {
if _, err := url.ParseRequestURI(s.Settings["slackWebhook"]); err == nil {
str := ""
for _, c := range p.Commands {
str = str + c.Command.Description + "\n"
}
notifySlack(strings.TrimRight(str, "\n"), s.Settings["slackWebhook"], false, false)
}
}
// sortPlan sorts the slices of commands and decisions based on priorities
// the lower the priority value the earlier a command should be attempted
func (p plan) sortPlan() {
log.Println("INFO: sorting the commands in the plan based on priorities (order flags) ... ")
sort.SliceStable(p.Commands, func(i, j int) bool {
return p.Commands[i].Priority < p.Commands[j].Priority
})
sort.SliceStable(p.Decisions, func(i, j int) bool {
return p.Decisions[i].Priority < p.Decisions[j].Priority
})
}