-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
184 lines (166 loc) · 4.39 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"flag"
"os"
"sort"
"github.com/hortonworks/cloud-haunter/utils"
_ "github.com/hortonworks/cloud-haunter/action"
_ "github.com/hortonworks/cloud-haunter/aws"
_ "github.com/hortonworks/cloud-haunter/azure"
ctx "github.com/hortonworks/cloud-haunter/context"
_ "github.com/hortonworks/cloud-haunter/filter"
_ "github.com/hortonworks/cloud-haunter/gcp"
_ "github.com/hortonworks/cloud-haunter/hipchat"
_ "github.com/hortonworks/cloud-haunter/operation"
_ "github.com/hortonworks/cloud-haunter/slack"
"github.com/hortonworks/cloud-haunter/types"
log "github.com/sirupsen/logrus"
)
func main() {
defer func() {
if r := recover(); r != nil {
log.Error(r)
os.Exit(1)
}
}()
help := flag.Bool("h", false, "print help")
opType := flag.String("o", "", "type of operation")
filterTypes := flag.String("f", "", "type of filters")
actionType := flag.String("a", "log", "type of action")
cloudTypes := flag.String("c", "", "type of clouds")
filterConfigLoc := flag.String("fc", "", "filterConfig YAML")
dryRun := flag.Bool("d", false, "dry run")
verbose := flag.Bool("v", false, "verbose")
ignoreLabelDisabled := flag.Bool("i", false, "disable ignore label")
exactMatchOwner := flag.Bool("e", false, "exact match owner")
flag.Parse()
if *help {
printHelp()
os.Exit(0)
}
ctx.DryRun = *dryRun
ctx.Verbose = *verbose
if ctx.Verbose {
log.SetLevel(log.DebugLevel)
}
ctx.IgnoreLabelDisabled = *ignoreLabelDisabled
ctx.ExactMatchOwner = *exactMatchOwner
if filterConfigLoc != nil && len(*filterConfigLoc) != 0 {
var err error
ctx.FilterConfig, err = utils.LoadFilterConfig(*filterConfigLoc)
if err != nil {
log.Warnf("[UTIL] Failed to load %s as V1 filter config, trying as V2. Error: %s", *filterConfigLoc, err.Error())
ctx.FilterConfig, err = utils.LoadFilterConfigV2(*filterConfigLoc)
if err != nil {
panic("Unable to parse filter configuration: " + err.Error())
}
}
}
op := func() *types.OpType {
for i := range ctx.Operations {
if i.String() == *opType {
return &i
}
}
return nil
}()
if op == nil {
panic("Operation is not found.")
}
var filters []types.Filter
var filterNames []types.FilterType
selectedFilters := utils.SplitListToMap(*filterTypes)
for f := range ctx.Filters {
if _, ok := selectedFilters[f.String()]; ok {
filters = append(filters, ctx.Filters[f])
filterNames = append(filterNames, f)
}
}
action := func() types.Action {
for i := range ctx.Actions {
if i.String() == *actionType {
return ctx.Actions[i]
}
}
return nil
}()
if action == nil {
panic("Action is not found.")
}
var clouds []types.CloudType
selectedClouds := utils.SplitListToMap(*cloudTypes)
for t := range ctx.CloudProviders {
_, ok := selectedClouds[t.String()]
if len(selectedClouds) == 0 || ok {
clouds = append(clouds, t)
} else {
delete(ctx.CloudProviders, t)
}
}
if len(clouds) == 0 {
panic("Cloud provider not found.")
}
items := ctx.Operations[*op].Execute(clouds)
for _, filter := range filters {
items = filter.Execute(items)
}
action.Execute(*op, filterNames, items)
}
// should be kept in sync with README.md
func printHelp() {
println(`NAME:
Cloud Haunter
USAGE:
ch -o=operation -a=action [-f=filter1,filter2] [-c=cloud1,cloud2]
VERSION:`)
println(" " + ctx.Version)
println(`
AUTHOR(S):
Hortonworks
OPERATIONS:`)
for _, o := range getSortedOperations() {
println("\t-o " + o)
}
println("FILTERS:")
for _, f := range getSortedFilters() {
println("\t-f " + f)
}
println("ACTIONS:")
for _, a := range getSortedActions() {
println("\t-a " + a)
}
println("CLOUDS:")
println("\t-c AWS")
println("\t-c AZURE")
println("\t-c GCP")
println("FILTER_CONFIG:\n\t-fc=/location/of/filter/config.yml")
println("DRY RUN:\n\t-d")
println("VERBOSE:\n\t-v")
println("DISABLE_IGNORE_LABEL:\n\t-i")
println("EXACT_MATCH_OWNERS:\n\t-e")
println("HELP:\n\t-h")
}
func getSortedOperations() []string {
operations := []string{}
for ot := range ctx.Operations {
operations = append(operations, string(ot))
}
sort.Strings(operations)
return operations
}
func getSortedFilters() []string {
filters := []string{}
for f := range ctx.Filters {
filters = append(filters, string(f))
}
sort.Strings(filters)
return filters
}
func getSortedActions() []string {
actions := []string{}
for a := range ctx.Actions {
actions = append(actions, string(a))
}
sort.Strings(actions)
return actions
}