Skip to content

Commit

Permalink
Merge pull request #182 from c4710n/target-port
Browse files Browse the repository at this point in the history
Add support for targetPort
  • Loading branch information
srhb authored Mar 7, 2022
2 parents 940865c + 00ad27b commit 5b85237
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/eval-machines.nix
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ in rec {

machines =
flip mapAttrs nodes (n: v': let v = scrubOptionValue v'; in
{ inherit (v.config.deployment) targetHost targetUser secrets healthChecks buildOnly substituteOnDestination tags;
{ inherit (v.config.deployment) targetHost targetPort targetUser secrets healthChecks buildOnly substituteOnDestination tags;
name = n;
nixosRelease = v.config.system.nixos.release or (removeSuffix v.config.system.nixos.version.suffix v.config.system.nixos.version);
nixConfig = mapAttrs
Expand Down
8 changes: 8 additions & 0 deletions data/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ in
'';
};

targetPort = mkOption {
type = nullOr int;
default = null;
description = ''
The port number of remote host used for deployment.
'';
};

targetUser = mkOption {
type = str;
default = "";
Expand Down
1 change: 1 addition & 0 deletions healthchecks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type Host interface {
GetName() string
GetTargetHost() string
GetTargetPort() int
GetTargetUser() string
GetHealthChecks() HealthChecks
}
Expand Down
8 changes: 8 additions & 0 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Host struct {
Name string
NixosRelease string
TargetHost string
TargetPort int
TargetUser string
Secrets map[string]secrets.Secret
BuildOnly bool
Expand Down Expand Up @@ -66,6 +67,10 @@ func (host *Host) GetTargetHost() string {
return host.TargetHost
}

func (host *Host) GetTargetPort() int {
return host.TargetPort
}

func (host *Host) GetTargetUser() string {
return host.TargetUser
}
Expand Down Expand Up @@ -385,6 +390,9 @@ func Push(ctx *ssh.SSHContext, host Host, paths ...string) (err error) {
if ctx.SkipHostKeyCheck {
sshOpts = append(sshOpts, fmt.Sprintf("%s", "-o StrictHostkeyChecking=No -o UserKnownHostsFile=/dev/null"))
}
if host.TargetPort != 0 {
sshOpts = append(sshOpts, fmt.Sprintf("-p %d", host.TargetPort))
}
if ctx.ConfigFile != "" {
sshOpts = append(sshOpts, fmt.Sprintf("-F %s", ctx.ConfigFile))
}
Expand Down
12 changes: 12 additions & 0 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Context interface {
type Host interface {
GetName() string
GetTargetHost() string
GetTargetPort() int
GetTargetUser() string
}

Expand Down Expand Up @@ -94,6 +95,17 @@ func (ctx *SSHContext) sshArgs(host Host, transfer *FileTransfer) (cmd string, a
args = append(args, "-F", ctx.ConfigFile)
}
var hostAndDestination = host.GetTargetHost()
if host.GetTargetPort() != 0 {
var optionName string
if transfer != nil {
optionName = "-P"
} else {
optionName = "-p"
}

args = append(args, optionName, fmt.Sprintf("%d", host.GetTargetPort()))
}

if transfer != nil {
args = append(args, transfer.Source)
hostAndDestination += ":" + transfer.Destination
Expand Down

0 comments on commit 5b85237

Please sign in to comment.