Skip to content

Commit ab0c72b

Browse files
zamaudiossadedin
authored andcommitted
aws: Add ability to resolve private IPs for non-public ec2s
1 parent fbd2426 commit ab0c72b

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main/groovy/bpipe/executor/AWSEC2CommandExecutor.groovy

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ class AWSEC2CommandExecutor extends CloudExecutor {
401401
throw new CapacityTemporarilyUnavailableException("Unable to acquire instance for job $id (image=$image, instanceType=$instanceType) due to $ex.errorCode", ex)
402402
}
403403

404+
boolean isPublicIp = config.getOrDefault('publicIp', true)
404405
this.instanceId = result.reservation.instances[0].instanceId
405-
this.hostname = queryHostName()
406+
this.hostname = queryHostName(isPublicIp)
406407

407408
log.info "Instance $instanceId started with host name $hostname"
408409
}
@@ -446,6 +447,7 @@ class AWSEC2CommandExecutor extends CloudExecutor {
446447
void connectInstance(Map config) {
447448

448449
this.autoShutdown = config.getOrDefault('autoShutdown', this.autoShutdown)
450+
boolean isPublicIp = config.getOrDefault('publicIp', true)
449451

450452
createClient(config)
451453

@@ -468,7 +470,7 @@ class AWSEC2CommandExecutor extends CloudExecutor {
468470
}
469471

470472
assert this.instanceId
471-
this.hostname = queryHostName()
473+
this.hostname = queryHostName(isPublicIp)
472474
log.info "Resolved hostname $hostname for instance $instanceId"
473475
}
474476

@@ -495,22 +497,30 @@ class AWSEC2CommandExecutor extends CloudExecutor {
495497
}
496498

497499
@CompileStatic
498-
protected String queryHostName() {
500+
protected String queryHostName(boolean isPublicIp) {
499501
String hostname = Utils.withRetries(8) {
500502
DescribeInstancesResult result = describeInstance();
501503
for(Reservation reservations : result.getReservations()) {
502504
Instance instance = reservations.instances.find { it.instanceId == instanceId }
503-
if(instance.publicDnsName) {
504-
return instance.publicDnsName
505+
if(isPublicIp) {
506+
if(instance.publicDnsName) {
507+
return instance.publicDnsName
508+
}
509+
}
510+
else {
511+
if(instance.privateDnsName) {
512+
// Client may not be able to resolve hostname for private subnet, so return the IP instead
513+
return instance.privateIpAddress
514+
}
505515
}
506516
}
507517

508518
log.info "Still waiting for $instanceId to have a hostname"
509-
throw new IllegalStateException('Instance does not have public host name yet')
519+
throw new IllegalStateException('Instance does not have a host name yet')
510520
}
511521

512522
if(!hostname)
513-
throw new IllegalStateException('Could not resolve public DNS hostname for instance ' + instanceId)
523+
throw new IllegalStateException('Could not resolve DNS hostname/IP for instance ' + instanceId)
514524

515525
return hostname
516526
}

0 commit comments

Comments
 (0)