2025-01-03 23:30:07 -06:00
package cmd
import (
"git.andrewnw.xyz/CyberShell/backy/pkg/backy"
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
"github.com/spf13/cobra"
)
var (
hostExecCommand = & cobra . Command {
2025-02-08 15:17:34 -06:00
Use : "host [--command=command1 --command=command2 ... | -c command1 -c command2 ...] [--hosts=host1 --hosts=hosts2 ... | -m host1 -m host2 ...] " ,
2025-01-03 23:30:07 -06:00
Short : "Runs command defined in config file on the hosts in order specified." ,
Long : "Host executes specified commands on the hosts defined in config file.\nUse the --commands or -c flag to choose the commands." ,
Run : Host ,
}
)
2025-02-10 16:55:54 -06:00
// Holds list of hosts to run commands on
2025-01-03 23:30:07 -06:00
var hostsList [ ] string
2025-02-10 16:55:54 -06:00
// Holds command list to run
2025-01-03 23:30:07 -06:00
var cmdList [ ] string
func init ( ) {
2025-02-10 17:00:02 -06:00
hostExecCommand . Flags ( ) . StringArrayVarP ( & hostsList , "hosts" , "m" , nil , "Accepts space-separated names of hosts. Specify multiple times for multiple hosts." )
hostExecCommand . Flags ( ) . StringArrayVarP ( & cmdList , "command" , "c" , nil , "Accepts space-separated names of commands. Specify multiple times for multiple commands." )
2025-02-08 15:17:34 -06:00
parseS3Config ( )
2025-01-03 23:30:07 -06:00
}
// 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 Host ( cmd * cobra . Command , args [ ] string ) {
2025-02-20 14:53:44 -06:00
backyConfOpts := backy . NewOpts ( cfgFile , backy . SetLogFile ( logFile ) , backy . SetCmdStdOut ( cmdStdOut ) )
2025-01-03 23:30:07 -06:00
backyConfOpts . InitConfig ( )
2025-01-14 09:42:43 -06:00
backyConfOpts . ReadConfig ( )
2025-01-03 23:30:07 -06:00
// check CLI input
if hostsList == nil {
logging . ExitWithMSG ( "error: hosts must be specified" , 1 , & backyConfOpts . Logger )
}
2025-02-11 12:19:09 -06:00
2025-01-03 23:30:07 -06:00
for _ , h := range hostsList {
2025-02-11 12:19:09 -06:00
// check if h exists in the config file
2025-01-03 23:30:07 -06:00
_ , hostFound := backyConfOpts . Hosts [ h ]
if ! hostFound {
2025-02-11 12:19:09 -06:00
// check if h exists in the SSH config file
2025-02-10 16:55:54 -06:00
hostFoundInConfig , s := backy . CheckIfHostHasHostName ( h )
if ! hostFoundInConfig {
logging . ExitWithMSG ( "host " + h + " not found" , 1 , & backyConfOpts . Logger )
}
// create host with hostname and host
backyConfOpts . Hosts [ h ] = & backy . Host { Host : h , HostName : s }
2025-01-03 23:30:07 -06:00
}
}
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 . ExecCmdsSSH ( cmdList , hostsList )
}