Skip to content

Commit

Permalink
chore: playbook run improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Sep 9, 2024
1 parent 6c8269c commit 1009041
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 75 deletions.
157 changes: 84 additions & 73 deletions cmd/playbook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -31,6 +32,8 @@ var Playbook = &cobra.Command{
}

var playbookNamespace string
var outfile string
var outFormat string
var paramFile string
var debugPort int

Expand All @@ -48,6 +51,48 @@ func GetOrCreateAgent(ctx context.Context, name string) (*models.Agent, error) {
return &t, tx.Error
}

func parsePlaybookArgs(ctx context.Context, args []string) (*models.Playbook, *playbook.RunParams, error) {
p, err := playbook.CreateOrSaveFromFile(ctx, args[0])
if err != nil {
return nil, nil, err
}

hostname, _ := os.Hostname()
agent, err := GetOrCreateAgent(ctx, hostname)
if err != nil {
return nil, nil, err
}

var params = playbook.RunParams{
Params: make(map[string]string),
AgentID: &agent.ID,
}

if f, err := os.Open(paramFile); err == nil {
if err := yamlutil.NewYAMLOrJSONDecoder(f, 1024).Decode(&params); err != nil {
return nil, nil, err
}
}

for _, arg := range args[1:] {
parts := strings.Split(arg, "=")
if len(parts) != 2 {
logger.Warnf("Invalid param: %s", arg)
continue
}
if parts[0] == "config" || parts[0] == "config_id" {
params.ConfigID = lo.ToPtr(uuid.MustParse(parts[1]))
} else if parts[0] == "component" || parts[0] == "component_id" {
params.ComponentID = lo.ToPtr(uuid.MustParse(parts[1]))
} else if parts[0] == "check" || parts[0] == "check_id" {
params.CheckID = lo.ToPtr(uuid.MustParse(parts[1]))
} else {
params.Params[parts[0]] = parts[1]
}
}
return p, &params, nil
}

var Run = &cobra.Command{
Use: "run playbook playbook.yaml params.yaml",
Args: cobra.MinimumNArgs(1),
Expand All @@ -65,76 +110,29 @@ var Run = &cobra.Command{

shutdown.AddHook(stop)

e := echo.New(ctx)

shutdown.AddHook(func() {
echo.Shutdown(e)
})
if debugPort >= 0 {
e := echo.New(ctx)

shutdown.AddHook(func() {
for k, v := range logger.GetNamedLoggingLevels() {
logger.Infof("logger: %s=%v", k, v)
}
shutdown.AddHook(func() {
echo.Shutdown(e)
})

for k, v := range properties.Global.GetAll() {
logger.Infof("property: %s=%v", k, v)
}
})
shutdown.WaitForSignal()

if debugPort >= 0 {
if debugPort == 0 {
debugPort = duty.FreePort()
}
go echo.Start(e, debugPort)
}
shutdown.WaitForSignal()

p, err := playbook.CreateOrSaveFromFile(ctx, args[0])
if err != nil {
logger.Fatalf(err.Error())
return
}

hostname, _ := os.Hostname()
agent, err := GetOrCreateAgent(ctx, hostname)
p, params, err := parsePlaybookArgs(ctx, args)
if err != nil {
logger.Fatalf(err.Error())
return
}

var params = playbook.RunParams{
Params: make(map[string]string),
AgentID: &agent.ID,
}

if f, err := os.Open(paramFile); err == nil {
if err := yamlutil.NewYAMLOrJSONDecoder(f, 1024).Decode(&params); err != nil {
logger.Fatalf(err.Error())
return
}
}

for _, arg := range args[1:] {
parts := strings.Split(arg, "=")
if len(parts) != 2 {
logger.Warnf("Invalid param: %s", arg)
continue
}
if parts[0] == "config" || parts[0] == "config_id" {
params.ConfigID = lo.ToPtr(uuid.MustParse(parts[1]))
} else if parts[0] == "component" || parts[0] == "component_id" {
params.ComponentID = lo.ToPtr(uuid.MustParse(parts[1]))
} else if parts[0] == "check" || parts[0] == "check_id" {
params.CheckID = lo.ToPtr(uuid.MustParse(parts[1]))
} else {
params.Params[parts[0]] = parts[1]
}
shutdown.ShutdownAndExit(1, err.Error())
}

ctx = ctx.WithUser(auth.GetSystemUser(&ctx))
run, err := playbook.Run(ctx, p, params)
run, err := playbook.Run(ctx, p, *params)
if err != nil {
logger.Fatalf(err.Error())
logger.Errorf("%+v", err)
return
}

Expand All @@ -155,7 +153,6 @@ var Run = &cobra.Command{
}

for action != nil {

runAction, err := run.StartAction(ctx.DB(), action.Name)

if err != nil {
Expand All @@ -182,22 +179,39 @@ var Run = &cobra.Command{

summary, err := playbook.GetPlaybookStatus(ctx, run.ID)

b, _ := yaml.Marshal(summary)
fmt.Println(string(b))
if err != nil {
shutdown.ShutdownAndExit(1, err.Error())
}

saveOutput(summary, outfile, outFormat)

if summary.Run.Status != models.PlaybookRunStatusCompleted {
shutdown.ShutdownAndExit(1, fmt.Sprintf("Playbook run status: %s ", summary.Run.Status))
}

},
}

func saveOutput(object any, file string, format string) {
var out string
if outFormat == "yaml" {
b, _ := yaml.Marshal(object)
out = string(b)
} else {
b, _ := json.MarshalIndent(object, "", " ")
out = string(b)
}

if outfile != "" {
_ = os.WriteFile(outfile, []byte(out), 0600)
} else {
fmt.Println(out)
}
}

var Submit = &cobra.Command{
Use: "submit playbook playbook.yaml params.yaml",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
PersistentPreRun: PreRun,
RunE: func(cmd *cobra.Command, args []string) error {
logger.UseSlog()
Expand All @@ -213,20 +227,14 @@ var Submit = &cobra.Command{

shutdown.WaitForSignal()

p, err := playbook.CreateOrSaveFromFile(ctx, args[0])
p, params, err := parsePlaybookArgs(ctx, args)
params.AgentID = nil
if err != nil {
return err
}

var params playbook.RunParams

if f, err := os.Open(paramFile); err == nil {
if err := yamlutil.NewYAMLOrJSONDecoder(f, 1024).Decode(&params); err != nil {
return err
}
shutdown.ShutdownAndExit(1, err.Error())
}

run, err := playbook.Run(ctx, p, params)
ctx = ctx.WithUser(auth.GetSystemUser(&ctx))
run, err := playbook.Run(ctx, p, *params)
if err != nil {
return err
}
Expand All @@ -240,7 +248,10 @@ var Submit = &cobra.Command{
func init() {
Playbook.PersistentFlags().StringVarP(&playbookNamespace, "namespace", "n", "default", "Namespace for playbook to run under")
Playbook.PersistentFlags().StringVarP(&paramFile, "params", "p", "", "YAML/JSON file containing parameters")
Run.Flags().IntVar(&debugPort, "debug-port", 0, "Start an HTTP server to use the /debug routes, Use -1 to disable and 0 to pick a free port")
Run.Flags().IntVar(&debugPort, "debug-port", -1, "Start an HTTP server to use the /debug routes, Use -1 to disable and 0 to pick a free port")
Run.Flags().StringVarP(&outfile, "out-file", "o", "", "Write playbook summary to file instead of stdout")
Run.Flags().StringVarP(&outFormat, "out-format", "f", "yaml", "Format of output file or stdout (yaml or json)")

Playbook.AddCommand(Run, Submit)
Root.AddCommand(Playbook)
}
3 changes: 2 additions & 1 deletion playbook/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (r *RunParams) setDefaults(ctx context.Context, spec v1.PlaybookSpec, templ
}

func (r *RunParams) validateParams(params []v1.PlaybookParameter) error {
all := lo.Map(params, func(v v1.PlaybookParameter, _ int) string { return v.Name })
required := lo.Map(lo.Filter(params, func(v v1.PlaybookParameter, _ int) bool {
return v.Required
}), func(v v1.PlaybookParameter, _ int) string { return v.Name })
Expand All @@ -112,7 +113,7 @@ func (r *RunParams) validateParams(params []v1.PlaybookParameter) error {
)

if len(unknownParams) != 0 {
return oops.Errorf("unknown parameter(s): %s", strings.Join(unknownParams, ", "))
return oops.Errorf("unknown parameter(s): %s, valid parameters are: %s", strings.Join(unknownParams, ", "), strings.Join(all, ", "))
}

return nil
Expand Down
1 change: 1 addition & 0 deletions playbook/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func Run(ctx context.Context, playbook *models.Playbook, req RunParams) (*models
PlaybookID: playbook.ID,
Status: models.PlaybookRunStatusPending,
Parameters: req.Params,
AgentID: req.AgentID,
}

if ctx.User() != nil {
Expand Down
2 changes: 1 addition & 1 deletion playbook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func ScheduleRun(ctx context.Context, run models.PlaybookRun) error {
}, action.Name))
}

if run.AgentID == nil && len(action.RunsOn) == 0 || lo.Contains(action.RunsOn, Main) {
if run.AgentID == nil && (len(action.RunsOn) == 0 || lo.Contains(action.RunsOn, Main)) {
if runAction, err := run.StartAction(ctx.DB(), action.Name); err != nil {
return ctx.Oops("db").Wrap(err)
} else {
Expand Down

0 comments on commit 1009041

Please sign in to comment.