-
Notifications
You must be signed in to change notification settings - Fork 9
dev #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
dev #204
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
473e9ca
feat: add playbook support with run, stop, list, and inspect commands
recepgunes1 611d098
docs: update README with playbook/inspect commands and fix Go version…
recepgunes1 65dd082
Merge pull request #200 from HappyHackingSpace/feat/add-playbook-temp…
recepgunes1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| tmpl "github.com/happyhackingspace/vt/pkg/template" | ||
| "github.com/rs/zerolog/log" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // newPlaybookCommand creates the playbook command with its subcommands. | ||
| func (c *CLI) newPlaybookCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "playbook", | ||
| Short: "Manage and run playbooks (collections of templates)", | ||
| } | ||
|
|
||
| cmd.AddCommand(c.newPlaybookRunCommand()) | ||
| cmd.AddCommand(c.newPlaybookStopCommand()) | ||
| cmd.AddCommand(c.newPlaybookListCommand()) | ||
| return cmd | ||
| } | ||
|
|
||
| // newPlaybookRunCommand creates the playbook run subcommand. | ||
| func (c *CLI) newPlaybookRunCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "run", | ||
| Short: "Run all templates defined in a playbook file", | ||
| Run: func(cmd *cobra.Command, _ []string) { | ||
| playbookID, err := cmd.Flags().GetString("id") | ||
| if err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| providerName, err := cmd.Flags().GetString("provider") | ||
| if err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| provider, ok := c.app.GetProvider(providerName) | ||
| if !ok { | ||
| log.Fatal().Msgf("provider %s not found", providerName) | ||
| } | ||
|
|
||
| pb, err := tmpl.GetPlaybookByID(c.app.Playbooks, playbookID) | ||
| if err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| log.Info().Msgf("running playbook '%s' [%s] (%d templates)", pb.Info.Name, pb.ID, len(pb.Templates)) | ||
|
|
||
| var failed []string | ||
| for _, templateID := range pb.Templates { | ||
| template, err := tmpl.GetByID(c.app.Templates, templateID) | ||
| if err != nil { | ||
| log.Error().Msgf("skipping '%s': %v", templateID, err) | ||
| failed = append(failed, templateID) | ||
| continue | ||
| } | ||
|
|
||
| log.Info().Msgf("starting template '%s'", templateID) | ||
| if err := provider.Start(template); err != nil { | ||
| log.Error().Msgf("failed to start '%s': %v", templateID, err) | ||
| failed = append(failed, templateID) | ||
| continue | ||
| } | ||
|
|
||
| if len(template.PostInstall) > 0 { | ||
| log.Info().Msgf("post-install instructions for '%s':", templateID) | ||
| for _, instruction := range template.PostInstall { | ||
| fmt.Printf(" %s\n", instruction) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(failed) > 0 { | ||
| log.Warn().Msgf("playbook finished with errors — failed templates: %s", strings.Join(failed, ", ")) | ||
| } else { | ||
| log.Info().Msgf("playbook '%s' completed successfully", pb.Info.Name) | ||
| } | ||
|
Comment on lines
+77
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return non-zero exit when any playbook item fails. Both Proposed fix if len(failed) > 0 {
- log.Warn().Msgf("playbook finished with errors — failed templates: %s", strings.Join(failed, ", "))
+ log.Fatal().Msgf("playbook finished with errors — failed templates: %s", strings.Join(failed, ", "))
} else {
log.Info().Msgf("playbook '%s' completed successfully", pb.Info.Name)
} if len(failed) > 0 {
- log.Warn().Msgf("playbook stop finished with errors — failed templates: %s", strings.Join(failed, ", "))
+ log.Fatal().Msgf("playbook stop finished with errors — failed templates: %s", strings.Join(failed, ", "))
} else {
log.Info().Msgf("playbook '%s' stopped successfully", pb.Info.Name)
}Also applies to: 140-144 🤖 Prompt for AI Agents |
||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().String("id", "", "Specify a playbook ID to run") | ||
| cmd.Flags().StringP("provider", "p", "docker-compose", | ||
| fmt.Sprintf("Specify the provider (%s)", strings.Join(c.providerNames(), ", "))) | ||
|
|
||
| if err := cmd.MarkFlagRequired("id"); err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // newPlaybookStopCommand creates the playbook stop subcommand. | ||
| func (c *CLI) newPlaybookStopCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "stop", | ||
| Short: "Stop all templates defined in a playbook", | ||
| Run: func(cmd *cobra.Command, _ []string) { | ||
| playbookID, err := cmd.Flags().GetString("id") | ||
| if err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| providerName, err := cmd.Flags().GetString("provider") | ||
| if err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| provider, ok := c.app.GetProvider(providerName) | ||
| if !ok { | ||
| log.Fatal().Msgf("provider %s not found", providerName) | ||
| } | ||
|
|
||
| pb, err := tmpl.GetPlaybookByID(c.app.Playbooks, playbookID) | ||
| if err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| log.Info().Msgf("stopping playbook '%s' [%s] (%d templates)", pb.Info.Name, pb.ID, len(pb.Templates)) | ||
|
|
||
| var failed []string | ||
| for _, templateID := range pb.Templates { | ||
| template, err := tmpl.GetByID(c.app.Templates, templateID) | ||
| if err != nil { | ||
| log.Error().Msgf("skipping '%s': %v", templateID, err) | ||
| failed = append(failed, templateID) | ||
| continue | ||
| } | ||
|
|
||
| log.Info().Msgf("stopping template '%s'", templateID) | ||
| if err := provider.Stop(template); err != nil { | ||
| log.Error().Msgf("failed to stop '%s': %v", templateID, err) | ||
| failed = append(failed, templateID) | ||
| } | ||
| } | ||
|
|
||
| if len(failed) > 0 { | ||
| log.Warn().Msgf("playbook stop finished with errors — failed templates: %s", strings.Join(failed, ", ")) | ||
| } else { | ||
| log.Info().Msgf("playbook '%s' stopped successfully", pb.Info.Name) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().String("id", "", "Specify a playbook ID to stop") | ||
| cmd.Flags().StringP("provider", "p", "docker-compose", | ||
| fmt.Sprintf("Specify the provider (%s)", strings.Join(c.providerNames(), ", "))) | ||
|
|
||
| if err := cmd.MarkFlagRequired("id"); err != nil { | ||
| log.Fatal().Msgf("%v", err) | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // newPlaybookListCommand creates the playbook list subcommand. | ||
| func (c *CLI) newPlaybookListCommand() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List all available playbooks", | ||
| Run: func(_ *cobra.Command, _ []string) { | ||
| tmpl.ListPlaybooks(c.app.Playbooks) | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update
--idhelp text to match new playbook support.The flag description still says template-only, but the command now accepts both template and playbook IDs.
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents