mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-04 09:18:48 -06:00
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
/*
|
|
Package cmd defines user commands accessible from the cli tool.
|
|
|
|
Copyright © 2022 Joel D. Elkins <joel@elkins.co>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
|
|
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.org/x/exp/maps"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
func execForEach(tgts []*container.Container, getSet func(*container.Container) command.Set, groupScale int) {
|
|
runLevel := make(map[int][]*container.Container)
|
|
|
|
for i := range tgts {
|
|
rl := tgts[i].StartGroup * groupScale
|
|
runLevel[rl] = append(runLevel[rl], tgts[i])
|
|
}
|
|
|
|
rls := maps.Keys(runLevel)
|
|
slices.Sort(rls)
|
|
|
|
for _, r := range rls {
|
|
cs := runLevel[r]
|
|
var dispScale string
|
|
if groupScale == 0 {
|
|
dispScale = "<all>"
|
|
} else {
|
|
dispScale = strconv.Itoa(r / groupScale)
|
|
}
|
|
fmt.Fprintln(output, "*** Running a command set for group", dispScale, mapNames(cs))
|
|
|
|
wg := new(sync.WaitGroup)
|
|
for i := range cs {
|
|
wg.Add(1)
|
|
cont := cs[i]
|
|
go func() {
|
|
defer wg.Done()
|
|
set := getSet(cont)
|
|
for _, cmd := range set.Commands {
|
|
if err := cmd.Execute(output, fake, set.ID); err != nil {
|
|
cont.LogEntry().WithFields(log.Fields{
|
|
"error": err,
|
|
"action": set.ID,
|
|
}).Errorln("Failed")
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if r != 0 {
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|
|
}
|
|
|
|
// For debugging
|
|
func mapNames(conts []*container.Container) string {
|
|
names := []string{}
|
|
for i := range conts {
|
|
names = append(names, conts[i].Name)
|
|
}
|
|
return fmt.Sprintf("%v", names)
|
|
}
|