ccl/cmd/execute.go

98 lines
2.8 KiB
Go
Raw Normal View History

2022-07-29 19:42:46 -05:00
/*
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"
2022-07-29 23:07:19 -05:00
"sync"
"time"
2022-07-29 19:42:46 -05:00
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
2022-07-29 23:07:19 -05:00
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
2022-08-13 13:09:48 -05:00
log "github.com/sirupsen/logrus"
2022-08-13 16:53:57 -05:00
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
2022-07-29 19:42:46 -05:00
)
2022-08-13 16:53:57 -05:00
type runner struct{
GetSet func(*container.Container) command.CommandSet
GroupScale int
}
func execForEach(tgts []container.Container, getSet func(*container.Container) command.CommandSet, groupScale int) {
var ser sync.Mutex // serialize non-async containers
runLevel := make(map[int][]container.Container)
for i := range tgts {
rl := int(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]
ser.Lock()
fmt.Fprintln(output, "*** Running a command set for group", r, mapNames(cs))
go func(async bool, conts []container.Container, ser *sync.Mutex) {
var wg sync.WaitGroup
defer func() {
wg.Wait()
ser.Unlock()
}()
for i := range conts {
wg.Add(1)
go runSet(&conts[i], getSet, &wg)
}
if !async {
time.Sleep(2 * time.Second)
}
}(r == 0, cs, &ser)
2022-07-29 19:42:46 -05:00
}
2022-08-13 16:53:57 -05:00
ser.Lock()
2022-07-29 19:42:46 -05:00
}
2022-07-29 23:07:19 -05:00
func runSet(cont *container.Container, getSet func(*container.Container) command.CommandSet, wg *sync.WaitGroup) {
defer wg.Done()
set := getSet(cont)
for _, cmd := range set.Commands {
if err := cmd.Execute(output, fake, set.ID); err != nil {
2022-08-13 13:09:48 -05:00
cont.LogEntry().WithFields(log.Fields{
"error": err,
"action": set.ID,
}).Errorln("Failed")
return
}
}
}
// For debugging
2022-08-13 16:53:57 -05:00
func mapNames(conts []container.Container) string {
names := []string{}
for i := range conts {
2022-08-13 16:53:57 -05:00
names = append(names, conts[i].Name)
}
2022-08-13 16:53:57 -05:00
return fmt.Sprintf("%v", names)
}