Compare commits
6 Commits
remoteReso
...
6e7d912fa2
Author | SHA1 | Date | |
---|---|---|---|
6e7d912fa2 | |||
b90d1958b2 | |||
c187fbb735 | |||
c3de4386ab | |||
e20141043c | |||
5d3c265ce9 |
3
.changes/unreleased/Changed-20250210-165532.yaml
Normal file
3
.changes/unreleased/Changed-20250210-165532.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
kind: Changed
|
||||
body: Hosts passed to `exec host` now checked against default SSH config files
|
||||
time: 2025-02-10T16:55:32.936776556-06:00
|
19
cmd/host.go
19
cmd/host.go
@ -15,14 +15,16 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// Holds command list to run
|
||||
// Holds list of hosts to run commands on
|
||||
var hostsList []string
|
||||
|
||||
// Holds command list to run
|
||||
var cmdList []string
|
||||
|
||||
func init() {
|
||||
|
||||
hostExecCommand.Flags().StringArrayVarP(&hostsList, "hosts", "m", nil, "Accepts space-separated names of hosts.")
|
||||
hostExecCommand.Flags().StringArrayVarP(&cmdList, "command", "c", nil, "Accepts space-separated names of commands.")
|
||||
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.")
|
||||
parseS3Config()
|
||||
|
||||
}
|
||||
@ -42,14 +44,19 @@ func Host(cmd *cobra.Command, args []string) {
|
||||
if hostsList == nil {
|
||||
logging.ExitWithMSG("error: hosts must be specified", 1, &backyConfOpts.Logger)
|
||||
}
|
||||
// host is only checked when we read the SSH File
|
||||
// so a check may not be needed here
|
||||
// but we can check if the host is in the config file
|
||||
|
||||
for _, h := range hostsList {
|
||||
// check if h exists in the config file
|
||||
_, hostFound := backyConfOpts.Hosts[h]
|
||||
if !hostFound {
|
||||
// check if h exists in the SSH config file
|
||||
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}
|
||||
}
|
||||
}
|
||||
if cmdList == nil {
|
||||
logging.ExitWithMSG("error: commands must be specified", 1, &backyConfOpts.Logger)
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const versionStr = "0.6.1"
|
||||
const versionStr = "0.7.0"
|
||||
|
||||
var (
|
||||
versionCmd = &cobra.Command{
|
||||
|
@ -34,15 +34,17 @@ var Sprintf = fmt.Sprintf
|
||||
func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([]string, error) {
|
||||
|
||||
var (
|
||||
outputArr []string
|
||||
ArgsStr string
|
||||
ArgsStr string // concatenating the arguments
|
||||
cmdOutBuf bytes.Buffer
|
||||
cmdOutWriters io.Writer
|
||||
errSSH error
|
||||
|
||||
envVars = environmentVars{
|
||||
file: command.Env,
|
||||
env: command.Environment,
|
||||
}
|
||||
|
||||
outputArr []string // holds the output strings returned by processes
|
||||
)
|
||||
|
||||
// Get the command type
|
||||
@ -53,13 +55,13 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
||||
ArgsStr += fmt.Sprintf(" %s", v)
|
||||
}
|
||||
|
||||
// print the user's password if it is updated
|
||||
if command.Type == "user" {
|
||||
if command.UserOperation == "password" {
|
||||
cmdCtxLogger.Info().Str("password", command.UserPassword).Msg("user password to be updated")
|
||||
}
|
||||
}
|
||||
|
||||
var errSSH error
|
||||
// is host defined
|
||||
if command.Host != nil {
|
||||
outputArr, errSSH = command.RunCmdSSH(cmdCtxLogger, opts)
|
||||
|
@ -757,3 +757,12 @@ func (h *Host) DetectOS(opts *ConfigOpts) (string, error) {
|
||||
osName := string(output)
|
||||
return osName, nil
|
||||
}
|
||||
|
||||
func CheckIfHostHasHostName(host string) (bool, string) {
|
||||
HostName, err := ssh_config.DefaultUserSettings.GetStrict(host, "HostName")
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
println(HostName)
|
||||
return HostName != "", HostName
|
||||
}
|
||||
|
Reference in New Issue
Block a user