-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
129 lines (104 loc) · 3.35 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
package main
import (
"strings"
"gitlab.com/yakshaving.art/hurrdurr/internal"
"gitlab.com/yakshaving.art/hurrdurr/internal/api"
"gitlab.com/yakshaving.art/hurrdurr/internal/state"
"gitlab.com/yakshaving.art/hurrdurr/internal/util"
"github.com/sirupsen/logrus"
)
func main() {
logrus.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
args := parseArgs()
SetupLogger(args.Debug, args.Trace)
conf, err := util.LoadConfig(args.ConfigFile, args.ChecksumCheck)
if err != nil {
logrus.Fatalf("failed to load configuration: %s", err)
}
logrus.Debugf("configuration loaded from file %s", args.ConfigFile)
if args.ManageBots {
if err := util.ValidateBots(conf.Bots, args.BotUsernameRegex); err != nil {
logrus.Fatalf("failed validating bots users: %s", err)
}
}
client := api.NewGitlabAPIClient(
api.GitlabAPIClientArgs{
GitlabToken: args.GitlabToken,
GitlabBaseURL: args.GitlabBaseURL,
GitlabGhostUser: args.GhostUser,
Concurrency: args.Concurrency,
})
var currentState internal.State
if args.AutoDevOpsMode {
logrus.Infof("loading partial state from gitlab")
err := api.CreateLazyQuerier(&client)
if err != nil {
logrus.Fatalf("failed to create lazy querier from gitlab instance: %s", err)
}
currentState, err = api.LoadPartialGitlabState(conf, client)
if err != nil {
logrus.Fatalf("failed to load partial live state from gitlab instance: %s", err)
}
logrus.Infof("done loading partial state from gitlab")
} else {
logrus.Infof("loading full state from gitlab")
err := api.CreatePreloadedQuerier(&client)
if err != nil {
logrus.Fatalf("failed to preload querier from gitlab instance: %s", err)
}
currentState, err = api.LoadFullGitlabState(client)
if err != nil {
logrus.Fatalf("failed to load full live state from gitlab instance: %s", err)
}
logrus.Infof("done loading full state from gitlab")
}
desiredState, err := state.LoadStateFromFile(conf, client.Querier)
logrus.Infof("loading desired state from file %s", args.ConfigFile)
if err != nil {
logrus.Fatalf("failed to load desired state from file %s: %s", args.ConfigFile, err)
}
logrus.Infof("done loading desired state from file %s", args.ConfigFile)
actions, err := state.Diff(currentState, desiredState, state.DiffArgs{
DiffGroups: args.ManageACLs,
DiffProjects: args.ManageACLs,
DiffUsers: args.ManageUsers,
DiffBots: args.ManageBots,
Yolo: args.YoloMode,
})
if err != nil {
logrus.Fatalf("failed to diff current and desired state: %s", err)
}
logrus.Debugf("diff calculated")
var actionClient internal.APIClient
if args.DryRun {
logrus.Println("changes proposed [dryrun]:")
actionClient = api.DryRunAPIClient{
Append: func(change string) {
logrus.Printf(" %s", change)
},
}
} else {
logrus.Print("executing changes:")
actionClient = client
}
if len(actions) == 0 {
logrus.Print(" no changes necessary")
}
for _, action := range actions {
if err := action.Execute(actionClient); err != nil {
logrus.Fatalf("Failed to run action: %s", err)
}
}
logrus.Debugf("all actions executed")
if len(desiredState.UnhandledGroups()) > 0 {
logrus.Print("unhandled groups detected:")
for _, ug := range desiredState.UnhandledGroups() {
if args.SnoopDepth == 0 || strings.Count(ug, "/") <= args.SnoopDepth {
logrus.Infof(" %s", ug)
}
}
}
logrus.Infof("done")
}