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 (
|
2022-07-29 23:07:19 -05:00
|
|
|
"sync"
|
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-07-30 14:26:23 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-07-29 19:42:46 -05:00
|
|
|
)
|
|
|
|
|
2022-07-31 00:57:18 -05:00
|
|
|
func execForEach(tgts []container.Container, getSet func(*container.Container) command.CommandSet) {
|
2022-07-29 23:07:19 -05:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := range tgts {
|
2022-08-12 21:16:55 -05:00
|
|
|
async := !tgts[i].StartOrder.Valid
|
|
|
|
runSet := func(cont *container.Container) {
|
|
|
|
if async {
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
2022-07-30 14:26:23 -05:00
|
|
|
set := getSet(cont)
|
|
|
|
for _, cmd := range set.Commands {
|
|
|
|
if err := cmd.Execute(output, fake, set.ID); err != nil {
|
|
|
|
cont.LogEntry().WithFields(logrus.Fields{
|
|
|
|
"error": err,
|
|
|
|
"action": set.ID,
|
|
|
|
}).Errorln("Failed")
|
2022-07-29 23:07:19 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 21:16:55 -05:00
|
|
|
}
|
|
|
|
if async {
|
|
|
|
wg.Add(1)
|
|
|
|
go runSet(&tgts[i])
|
|
|
|
} else {
|
|
|
|
runSet(&tgts[i])
|
|
|
|
}
|
2022-07-29 19:42:46 -05:00
|
|
|
}
|
2022-07-29 23:07:19 -05:00
|
|
|
wg.Wait()
|
2022-07-29 19:42:46 -05:00
|
|
|
}
|
2022-07-29 23:07:19 -05:00
|
|
|
|