-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
295 lines (264 loc) · 7.82 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
"math/rand"
"runtime"
"strings"
"sync"
"text/template"
"time"
)
var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
ccxApiServer *ccxServer
axlServer *AxlServer
roundTime statData
GoVersion = runtime.Version() // build GO version
infoTmpl = `
{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
platform: {{.platform}}
`
src = rand.NewSource(time.Now().UnixNano()) // randomize base string
maxRandomSize = 10 // required size of random string
)
func VersionDetail() string {
m := map[string]string{
"program": ApplicationName,
"version": Version,
"revision": Revision,
"branch": Branch,
"buildUser": BuildUser,
"buildDate": BuildDate,
"goVersion": GoVersion,
"platform": runtime.GOOS + "/" + runtime.GOARCH,
}
t := template.Must(template.New("version").Parse(infoTmpl))
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "version", m); err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
func readActualData() {
var wg sync.WaitGroup
wg.Add(1)
go asyncCcxResourceList(ccxApiServer, &wg)
wg.Add(1)
go asyncAxlReadUsers(axlServer, &wg)
wg.Add(1)
go asyncCcxTeamList(ccxApiServer, &wg)
wg.Wait()
log.Infof("Finish initial data read from all sources")
log.Infof("finish read %d/%d CCX teams", len(ccxTeamActiveList.Team), len(ccxTeamActiveList.getGeneratedTeams()))
log.Infof("finish read %d/%d CCX users", len(ccxUserActiveList.Resource), len(ccxUserActiveList.getGeneratedUsers()))
log.Infof("finish read %d/%d CUCM users %d enabled for CCX", len(axlEndUsersList.Rows), len(axlEndUsersList.getGeneratedUsers()), len(axlEndUsersList.getGeneratedCcxUsers()))
}
type chanData struct {
Id int
SetLine bool
}
func setAgents() {
data := axlEndUsersList.getGeneratedUsers()
nowAxlEnabled := len(axlEndUsersList.getGeneratedCcxUsers())
nowCcxEnabled := len(ccxUserActiveList.Resource)
if nowAxlEnabled > nowCcxEnabled {
log.Error("problem CCX not synchronized with AXL")
programExit(1)
}
operation := *Config.finalNumber > nowCcxEnabled
var needUpdate int
if *Config.finalNumber == 0 {
needUpdate = nowAxlEnabled
} else {
if operation { // add
needUpdate = *Config.finalNumber - nowCcxEnabled
} else { //remove
needUpdate = nowCcxEnabled - *Config.finalNumber
if needUpdate > nowAxlEnabled { // not generated more than need remove
needUpdate = nowAxlEnabled
}
}
}
roundTime.Direction = operation
roundTime.ExpectUpdates = needUpdate
setCcx := 0
removeCcx := 0
updates := 0
roundUpdates := 0
lastUpdate := 0
channelData := make(chan chanData, CcxForceMaxUsers+5)
roundTime.StartTime = time.Now()
timeStart := time.Now()
for i := 0; i < len(data); i++ {
if data[i].isEnableForCcx() == operation {
continue
}
if !data[i].isEnableForCcx() && operation {
setCcx++
channelData <- chanData{
Id: i,
SetLine: true,
}
updates++
roundUpdates++
}
if data[i].isEnableForCcx() && !operation {
removeCcx++
channelData <- chanData{
Id: i,
SetLine: false,
}
updates++
roundUpdates++
}
if needUpdate-removeCcx-setCcx < 1 {
break
}
if roundUpdates >= CcxForceMaxUsers {
if updates == lastUpdate {
log.Errorf("same updates numbers")
break
}
log.Infof("start run GO routine for manipulate with AXL %d for %d updates", updates/CcxForceMaxUsers, CcxForceMaxUsers)
runUpdateBatch(&channelData, updates/CcxForceMaxUsers, &timeStart)
lastUpdate = updates
roundUpdates = 0
if (updates/CcxForceMaxUsers)%5 == 0 {
fmt.Print(roundTime.StringFinal())
}
}
}
if len(channelData) > 0 {
log.Infof("start run GO routine for manipulate with AXL last update for %d updates", updates%CcxForceMaxUsers)
runUpdateBatch(&channelData, updates/CcxForceMaxUsers+1, &timeStart)
}
log.Infof("system set %d and remove %d agents", setCcx, removeCcx)
}
func runUpdateBatch(channelData *chan chanData, round int, startTime *time.Time) {
var wg sync.WaitGroup
updates := len(*channelData)
for i := 0; i < 4; i++ {
wg.Add(1)
go processAxlStream(channelData, &wg)
}
wg.Wait()
axlEnd := time.Now()
var force *ccxForceResponse
var err error
for i := 0; i < CxxForceDownRepeat; i++ {
force, err = CcxResourceForceSync()
if err == nil {
break
}
if i < 2 && err.Error() == "CCX service is down" {
log.Infof("CCX service is down - wait additional %d minutes", CcxForceDownTime)
time.Sleep(CcxForceDownTime * time.Minute)
continue
}
break
}
ccxEnd := time.Now()
roundTime.addUpdateRound(round, updates, *startTime, axlEnd, ccxEnd)
if err != nil {
log.Errorf("round %d has problem force update CCX users, %s", round, err)
programExit(1)
}
log.Infof("round %d after force update get %d/%d agents", round, len(force.AgentInfo), len(force.getGenerated()))
time.Sleep(CcxForceWaitTime * time.Second)
*startTime = time.Now()
}
func processAxlStream(updateChannel *chan chanData, wd *sync.WaitGroup) {
id := fmt.Sprintf("ASYNC-%s%s", AxlIdPrefix, RandomString())
log.WithField("id", id).Tracef("start async procedure %s", id)
data := axlEndUsersList.getGeneratedUsers()
var wg sync.WaitGroup
for {
select {
case d := <-*updateChannel:
wg.Add(1)
go data[d.Id].switchCcxLine(axlServer, d.SetLine, &wg)
wg.Wait()
default:
wd.Done()
log.WithField("id", id).Tracef("exit async procedure %s", id)
return
}
}
}
func correctTeams() {
log.Infof("correct teams from [%d] to [%d]", len(ccxTeamActiveList.getGeneratedTeams()), ccxUserActiveList.getNecessaryTeams())
needTeams := ccxUserActiveList.getNecessaryTeams()
hasTeams := len(ccxTeamActiveList.getGeneratedTeams())
if needTeams < 1 {
for _, team := range ccxTeamActiveList.getGeneratedTeams() {
_ = team.deleteTeam(ccxApiServer)
}
return
}
if needTeams < hasTeams {
teams := ccxTeamActiveList.getTeamToDelete(hasTeams - needTeams)
for _, team := range teams {
_ = team.deleteTeam(ccxApiServer)
ccxTeamActiveList.removeTeamName(team.TeamName)
}
}
for i := 0; i < ccxUserActiveList.getNecessaryTeams(); i++ {
_ = ccxTeamActiveList.createUpdateTeam(ccxApiServer, i+1, ccxUserActiveList.getGeneratedUsersForTeam(i+1))
}
}
func processCommands() {
ccxApiServer = newCcxServer()
axlServer = newAxlServer()
readActualData()
if *showOnly {
return
}
if *Config.finalNumber == 0 {
if len(axlEndUsersList.getGeneratedCcxUsers()) == 0 {
log.Infof("program does nothing all generated users on server [%s] are removed", *Config.ccServer)
}
log.Infof("program delete all generated users on server [%s]", *Config.ccServer)
setAgents()
} else {
if len(ccxUserActiveList.Resource) == *Config.finalNumber {
log.Infof("program does nothing enabled users on server [%s] is [%d]", *Config.ccServer, len(ccxUserActiveList.Resource))
} else {
log.Infof("program set number of users from [%d] to [%d] on server [%s]", len(ccxUserActiveList.Resource), *Config.finalNumber, *Config.ccServer)
setAgents()
}
}
// update user list
var wg sync.WaitGroup
wg.Add(1)
go asyncCcxResourceList(ccxApiServer, &wg)
wg.Wait()
log.Infof("finish read %d/%d CCX users", len(ccxUserActiveList.Resource), len(ccxUserActiveList.getGeneratedUsers()))
correctTeams()
}
func main() {
timeStart := time.Now()
exitCode := 0
kingpin.Version(VersionDetail())
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.SetLevel(getLevel())
roundTime.StartTime = time.Now()
// start process data
if Config.validate() {
fmt.Println(Config.toString())
processCommands()
}
timeDuration := time.Since(timeStart)
log.Infof("program run [%s]", timeDuration.String())
programExit(exitCode)
}