From 185a7375886e8c3deb22aa7e2316c9448b2030d2 Mon Sep 17 00:00:00 2001 From: Justin Britten Date: Mon, 8 Jun 2020 17:23:22 -0500 Subject: [PATCH] Configurable Hostname Communication Type (Public/Private DNS/IP) Adding elbas_hostname_type variable to specify which of the following hostname formats is to be used when communicating with instances: public_ip_address, public_dns_name, private_ip_address, private_dns_name --- README.md | 6 ++++++ lib/elbas/aws/base.rb | 7 +++++++ lib/elbas/aws/instance.rb | 6 +++--- lib/elbas/aws/instance_collection.rb | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8360418..ea64077 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,12 @@ set :aws_secret_key, ENV['AWS_SECRET_ACCESS_KEY'] set :aws_region, ENV['AWS_REGION'] ``` +Set the means through which to communicate with instances. Select from `public_dns_name` *(the default)*, `public_ip_address`, `private_dns_name` and `private_ip_address`. For example, if you are using a bastion host and SSHing into your instances via a private subnet you would want to add the following: + +```ruby +set :elbas_hostname_type, 'private_ip_address' +``` + ## Usage Instead of using Capistrano's `server` method, use `autoscale` instead in diff --git a/lib/elbas/aws/base.rb b/lib/elbas/aws/base.rb index 5ddade8..8573add 100644 --- a/lib/elbas/aws/base.rb +++ b/lib/elbas/aws/base.rb @@ -3,6 +3,9 @@ module AWS class Base include Capistrano::DSL + HOSTNAME_TYPES = %w(public_ip_address public_dns_name private_ip_address private_dns_name).freeze + DEFAULT_HOSTNAME_TYPE = 'public_dns_name'.freeze + attr_reader :aws_counterpart def aws_client(namespace = aws_namespace) @@ -31,6 +34,10 @@ def aws_region fetch :aws_region end + def elbas_hostname_type + HOSTNAME_TYPES.include?(fetch(:elbas_hostname_type)) ? fetch(:elbas_hostname_type) : DEFAULT_HOSTNAME_TYPE + end + def self.aws_client(namespace = aws_namespace) Base.new.aws_client namespace end diff --git a/lib/elbas/aws/instance.rb b/lib/elbas/aws/instance.rb index 0530215..3826336 100644 --- a/lib/elbas/aws/instance.rb +++ b/lib/elbas/aws/instance.rb @@ -5,15 +5,15 @@ class Instance < Base attr_reader :aws_counterpart, :id, :state - def initialize(id, public_dns, state) + def initialize(id, hostname, state) @id = id - @public_dns = public_dns + @hostname = hostname @state = state @aws_counterpart = aws_namespace::Instance.new id, client: aws_client end def hostname - @public_dns + @hostname end def running? diff --git a/lib/elbas/aws/instance_collection.rb b/lib/elbas/aws/instance_collection.rb index c0ac6cf..c7b6471 100644 --- a/lib/elbas/aws/instance_collection.rb +++ b/lib/elbas/aws/instance_collection.rb @@ -8,7 +8,7 @@ class InstanceCollection < Base def initialize(ids) @ids = ids @instances = query_instances_by_ids(ids).map do |i| - Instance.new(i.instance_id, i.public_dns_name, i.state.code) + Instance.new(i.instance_id, i.send(elbas_hostname_type), i.state.code) end end