Skip to content

Commit

Permalink
feat: Add support for ssh_port config for host
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Jan 27, 2024
1 parent 7b4074e commit 11667e4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ConfigMeta struct {

type ConfigHost struct {
SSHTarget string `yaml:"ssh_target"`
SSHPort int `yaml:"ssh_port"`
SSHPassword string `yaml:"ssh_password"`
SSHOptions []string `yaml:"ssh_options"`
SudoPassword string `yaml:"sudo_password"`
Expand Down
1 change: 1 addition & 0 deletions pkg/libtask/libtask.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libtask

type TaskInput struct {
SSHTarget string
SSHPort int
SSHPassword string
SSHOptions []string
NoStrictHostKeyChecking bool
Expand Down
7 changes: 6 additions & 1 deletion pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,18 @@ func Run(
if !quietOutput {
fmt.Printf("+ play: %s (%d/%d)\n", play.Name, playI, lenPlays)
fmt.Printf(" task: %s (%d/%d)\n", t.Name, taskI, lenTasks)
fmt.Printf(" host: %s (%d/%d)\n", host.SSHTarget, hostI, lenHosts)
if host.SSHPort == 0 {
fmt.Printf(" host: %s (%d/%d)\n", host.SSHTarget, hostI, lenHosts)
} else {
fmt.Printf(" host: %s (port %d) (%d/%d)\n", host.SSHTarget, host.SSHPort, hostI, lenHosts)
}
if play.Sudo {
fmt.Printf(" sudo: %t\n", play.Sudo)
}
}
taskInput := libtask.TaskInput{
SSHTarget: host.SSHTarget,
SSHPort: host.SSHPort,
SSHPassword: host.SSHPassword,
SSHOptions: host.SSHOptions,
SudoPassword: host.SudoPassword,
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/exec_utils/exec_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func SSH(taskInput libtask.TaskInput, cmdArray ...string) error {
if taskInput.NoStrictHostKeyChecking {
args = append([]string{"-o", "StrictHostKeyChecking=no"}, args...)
}
if taskInput.SSHPort != 0 {
args = append([]string{"-p", fmt.Sprintf("%d", taskInput.SSHPort)}, args...)
}
for _, option := range taskInput.SSHOptions {
args = append([]string{"-o", option}, args...)
}
Expand All @@ -74,6 +77,9 @@ func rawSCP(taskInput libtask.TaskInput, localPath string, remotePath string) er
if taskInput.NoStrictHostKeyChecking {
args = append([]string{"-o", "StrictHostKeyChecking=no"}, args...)
}
if taskInput.SSHPort != 0 {
args = append([]string{"-P", fmt.Sprintf("%d", taskInput.SSHPort)}, args...)
}
if taskInput.SSHPassword != "" {
return Exec(taskInput, "sshpass", append([]string{"-p", taskInput.SSHPassword, "scp"}, args...)...)
} else {
Expand All @@ -86,6 +92,9 @@ func rawSCPRemoteToLocal(taskInput libtask.TaskInput, remotePath string, localPa
if taskInput.NoStrictHostKeyChecking {
args = append([]string{"-o", "StrictHostKeyChecking=no"}, args...)
}
if taskInput.SSHPort != 0 {
args = append([]string{"-P", fmt.Sprintf("%d", taskInput.SSHPort)}, args...)
}
if taskInput.SSHPassword != "" {
return Exec(taskInput, "sshpass", append([]string{"-p", taskInput.SSHPassword, "scp"}, args...)...)
} else {
Expand Down

0 comments on commit 11667e4

Please sign in to comment.