3 Commits

Author SHA1 Message Date
3eced64453 CLI: Exec subcommand host 2025-07-15 20:24:30 -05:00
c284d928fd CLI: Exec subcommand host 2025-07-15 20:24:06 -05:00
dd9da9452b CLI: Exec subcommand host 2025-07-15 20:23:58 -05:00
7 changed files with 69 additions and 3 deletions

View File

@ -0,0 +1,3 @@
kind: Added
body: 'CLI: Exec subcommand `hosts`. See documentation for more details.'
time: 2025-07-15T20:23:03.647128713-05:00

View File

@ -21,7 +21,7 @@ var (
)
func init() {
execCmd.AddCommand(hostExecCommand)
execCmd.AddCommand(hostExecCommand, hostsExecCommand)
}

55
cmd/hosts.go Normal file
View File

@ -0,0 +1,55 @@
package cmd
import (
"git.andrewnw.xyz/CyberShell/backy/pkg/backy"
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
"github.com/spf13/cobra"
)
var (
hostsExecCommand = &cobra.Command{
Use: "hosts [--command=command1 --command=command2 ... | -c command1 -c command2 ...]",
Short: "Runs command defined in config file on the hosts in order specified.",
Long: "Hosts executes specified commands on all the hosts defined in config file.\nUse the --commands or -c flag to choose the commands.",
Run: Hosts,
}
)
func init() {
hostsExecCommand.Flags().StringArrayVarP(&cmdList, "command", "c", nil, "Accepts space-separated names of commands. Specify multiple times for multiple commands.")
parseS3Config()
}
// cli input should be hosts and commands. Hosts are defined in config files.
// commands can be passed by the following mutually exclusive options:
// 1. as a list of commands defined in the config file
// 2. stdin (on command line) (TODO)
func Hosts(cmd *cobra.Command, args []string) {
backyConfOpts := backy.NewConfigOptions(configFile,
backy.SetLogFile(logFile),
backy.EnableCommandStdOut(cmdStdOut),
backy.SetHostsConfigFile(hostsConfigFile))
backyConfOpts.InitConfig()
backyConfOpts.ParseConfigurationFile()
for _, h := range backyConfOpts.Hosts {
hostsList = append(hostsList, h.Host)
}
if cmdList == nil {
logging.ExitWithMSG("error: commands must be specified", 1, &backyConfOpts.Logger)
}
for _, c := range cmdList {
_, cmdFound := backyConfOpts.Cmds[c]
if !cmdFound {
logging.ExitWithMSG("cmd "+c+" not found", 1, &backyConfOpts.Logger)
}
}
backyConfOpts.ExecCmdsOnHosts(cmdList, hostsList)
}

View File

@ -22,7 +22,6 @@ commands:
outputToLog: true
Args:
- "-v"
host: email-svr
cmdLists:
TestHooks:

View File

@ -1,3 +1,4 @@
cd ~/Projects/backy/tests/docker
docker container rm -f ssh_server_container
docker build -t ssh_server_image .
docker run -d -p 2222:22 --name ssh_server_container ssh_server_image

View File

@ -2,5 +2,5 @@ hosts:
docker:
port: 2222
Hostname: localhost
user: backy
user: root
IdentityFile: ./docker/backytest

View File

@ -38,6 +38,14 @@ commands:
packageManager: apt
packageOperation: checkVersion
installJq:
type: package
shell: bash
packages:
- name: "jq"
packageManager: apt
packageOperation: install
checkDockerVersionWithInvalidRegex:
type: package
shell: zsh