Skip to content

Commit

Permalink
Enable command substitution for ssh config
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta committed Dec 25, 2024
1 parent 2292e0d commit 9afd9b6
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions internal/digraph/executor/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mitchellh/mapstructure"
"golang.org/x/crypto/ssh"

"github.com/dagu-org/dagu/internal/cmdutil"
"github.com/dagu-org/dagu/internal/digraph"
)

Expand Down Expand Up @@ -83,34 +84,44 @@ func newSSHExec(_ context.Context, step digraph.Step) (Executor, error) {
)

if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create decoder: %w", err)
}

if err := md.Decode(step.ExecutorConfig.Config); err != nil {
return nil, err
return nil, fmt.Errorf("failed to decode ssh config: %w", err)
}

var port string
switch v := def.Port.(type) {
case int:
port = fmt.Sprintf("%d", v)
case string:
port = v
default:
port = fmt.Sprintf("%v", def.Port)
}
if port == "" {
port = "22"
}

cfg := &sshExecConfig{
cfg, err := cmdutil.SubstituteStringFields(sshExecConfig{
User: def.User,
IP: def.IP,
Key: def.Key,
Password: def.Password,
Port: port,
})
if err != nil {
return nil, fmt.Errorf("failed to substitute string fields for ssh config: %w", err)
}

// Handle Port as either string or int
port := os.ExpandEnv(fmt.Sprintf("%v", def.Port))
if port == "" {
port = "22"
}
cfg.Port = port

// StrictHostKeyChecking is not supported yet.
if def.StrictHostKeyChecking {
return nil, errStrictHostKey
}

// Select the authentication method.
authMethod, err := selectSSHAuthMethod(cfg)
authMethod, err := selectSSHAuthMethod(&cfg)
if err != nil {
return nil, err
}
Expand All @@ -126,7 +137,7 @@ func newSSHExec(_ context.Context, step digraph.Step) (Executor, error) {

return &sshExec{
step: step,
config: cfg,
config: &cfg,
sshConfig: sshConfig,
stdout: os.Stdout,
}, nil
Expand Down

0 comments on commit 9afd9b6

Please sign in to comment.