Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 77e027b

Browse files
authored
Merge pull request #4350 from zmthy/amazonec2-ssh-port
Add ssh-port flag to the amazonec2 driver
2 parents 6ab5418 + a639dc8 commit 77e027b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

‎drivers/amazonec2/amazonec2.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
defaultVolumeType = "gp2"
4040
defaultZone = "a"
4141
defaultSecurityGroup = machineSecurityGroupName
42+
defaultSSHPort = 22
4243
defaultSSHUser = "ubuntu"
4344
defaultSpotPrice = "0.50"
4445
defaultBlockDurationMinutes = 0
@@ -211,9 +212,15 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
211212
Usage: "AWS IAM Instance Profile",
212213
EnvVar: "AWS_INSTANCE_PROFILE",
213214
},
215+
mcnflag.IntFlag{
216+
Name: "amazonec2-ssh-port",
217+
Usage: "SSH port",
218+
Value: defaultSSHPort,
219+
EnvVar: "AWS_SSH_PORT",
220+
},
214221
mcnflag.StringFlag{
215222
Name: "amazonec2-ssh-user",
216-
Usage: "Set the name of the ssh user",
223+
Usage: "SSH username",
217224
Value: defaultSSHUser,
218225
EnvVar: "AWS_SSH_USER",
219226
},
@@ -294,6 +301,7 @@ func NewDriver(hostName, storePath string) *Driver {
294301
SpotPrice: defaultSpotPrice,
295302
BlockDurationMinutes: defaultBlockDurationMinutes,
296303
BaseDriver: &drivers.BaseDriver{
304+
SSHPort: defaultSSHPort,
297305
SSHUser: defaultSSHUser,
298306
MachineName: hostName,
299307
StorePath: storePath,
@@ -363,7 +371,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
363371
d.VolumeType = flags.String("amazonec2-volume-type")
364372
d.IamInstanceProfile = flags.String("amazonec2-iam-instance-profile")
365373
d.SSHUser = flags.String("amazonec2-ssh-user")
366-
d.SSHPort = 22
374+
d.SSHPort = flags.Int("amazonec2-ssh-port")
367375
d.PrivateIPOnly = flags.Bool("amazonec2-private-address-only")
368376
d.UsePrivateIP = flags.Bool("amazonec2-use-private-address")
369377
d.Monitoring = flags.Bool("amazonec2-monitoring")
@@ -837,6 +845,14 @@ func (d *Driver) GetSSHHostname() (string, error) {
837845
return d.GetIP()
838846
}
839847

848+
func (d *Driver) GetSSHPort() (int, error) {
849+
if d.SSHPort == 0 {
850+
d.SSHPort = defaultSSHPort
851+
}
852+
853+
return d.SSHPort, nil
854+
}
855+
840856
func (d *Driver) GetSSHUsername() string {
841857
if d.SSHUser == "" {
842858
d.SSHUser = defaultSSHUser
@@ -1161,11 +1177,11 @@ func (d *Driver) configureSecurityGroupPermissions(group *ec2.SecurityGroup) ([]
11611177

11621178
perms := []*ec2.IpPermission{}
11631179

1164-
if !hasPorts["22/tcp"] {
1180+
if !hasPorts[fmt.Sprintf("%d/tcp", d.BaseDriver.SSHPort)] {
11651181
perms = append(perms, &ec2.IpPermission{
11661182
IpProtocol: aws.String("tcp"),
1167-
FromPort: aws.Int64(22),
1168-
ToPort: aws.Int64(22),
1183+
FromPort: aws.Int64(int64(d.BaseDriver.SSHPort)),
1184+
ToPort: aws.Int64(int64(d.BaseDriver.SSHPort)),
11691185
IpRanges: []*ec2.IpRange{{CidrIp: aws.String(ipRange)}},
11701186
})
11711187
}

0 commit comments

Comments
 (0)