Compare commits
3 Commits
11d5121954
...
3eced64453
Author | SHA1 | Date | |
---|---|---|---|
3eced64453 | |||
c284d928fd | |||
dd9da9452b |
3
.changes/unreleased/Added-20250715-202303.yaml
Normal file
3
.changes/unreleased/Added-20250715-202303.yaml
Normal 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
|
@ -21,7 +21,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
execCmd.AddCommand(hostExecCommand)
|
||||
execCmd.AddCommand(hostExecCommand, hostsExecCommand)
|
||||
|
||||
}
|
||||
|
||||
|
55
cmd/hosts.go
Normal file
55
cmd/hosts.go
Normal 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)
|
||||
}
|
@ -22,7 +22,6 @@ commands:
|
||||
outputToLog: true
|
||||
Args:
|
||||
- "-v"
|
||||
host: email-svr
|
||||
|
||||
cmdLists:
|
||||
TestHooks:
|
||||
|
@ -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
|
@ -2,5 +2,5 @@ hosts:
|
||||
docker:
|
||||
port: 2222
|
||||
Hostname: localhost
|
||||
user: backy
|
||||
user: root
|
||||
IdentityFile: ./docker/backytest
|
@ -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
|
||||
|
Reference in New Issue
Block a user