Skip to content

Instantly share code, notes, and snippets.

@tobalsgithub
Created November 7, 2015 16:50
Show Gist options
  • Save tobalsgithub/c0ba5d6a3178732cd3ab to your computer and use it in GitHub Desktop.
Save tobalsgithub/c0ba5d6a3178732cd3ab to your computer and use it in GitHub Desktop.
VHOST script
require 'aws-sdk-v1'
require 'aptible/auth'
require 'aptible/api'
require 'io/console'
# Fill in these required params
###############################
if ENV["APTIBLE_EMAIL"] then
EMAIL = ENV["APTIBLE_EMAIL"]
else
puts "Enter aptible email: "
EMAIL = gets.strip
end
if ENV['APTIBLE_PASSWORD'] then
PASSWORD = ENV['APTIBLE_PASSWORD']
else
puts "Enter aptible password: "
PASSWORD = STDIN.noecho(&:gets).strip
end
ROLE_ARN = 'arn:aws:iam::916150859591:role/redox-cross-account'
if ENV['IAM_ACCESS_KEY_ID'] then
IAM_ACCESS_KEY_ID = ENV['IAM_ACCESS_KEY_ID']
else
puts "Enter IAM_ACCESS_KEY_ID: "
IAM_ACCESS_KEY_ID = gets.strip
end
if ENV['IAM_SECRET_ACCESS_KEY'] then
IAM_SECRET_ACCESS_KEY = ENV['IAM_SECRET_ACCESS_KEY']
else
puts "Enter IAM_SECRET_ACCESS_KEY: "
IAM_SECRET_ACCESS_KEY = STDIN.noecho(&:gets).strip
end
HANDLE = 'redoxengine-prod'
ELB_NAME = 'billings-listener-manual'
PROCESS_TYPES = %w(exposed-port-9960 exposed-port-9961 exposed-port-9962 exposed-port-9963 exposed-port-9964 exposed-port-9965 exposed-port-9967 exposed-port-9968 exposed-port-9969)
EXTERNAL_PORTS = [9960, 9961, 9962, 9963, 9964, 9965, 9967, 9968, 9969]
###############################
# Find current port, via Aptible API
token = Aptible::Auth::Token.create(email: EMAIL, password: PASSWORD)
app = Aptible::Api::App.all(token: token).find { |a| a.handle == HANDLE }
services = PROCESS_TYPES.map do |type|
app.services.find { |s| s.process_type == type }
end
containers = services.map do |service|
service.current_release.containers.find { |c| c.layer == 'app' }
end
# Update ELB's listener(s) and health check to point at current port
sts = AWS::STS::Client.new(
access_key_id: IAM_ACCESS_KEY_ID,
secret_access_key: IAM_SECRET_ACCESS_KEY
)
session = sts.assume_role(role_arn: ROLE_ARN, role_session_name: 'testing')
elb = AWS::ELB.new(session.credentials.to_hash).load_balancers[ELB_NAME]
elb.listeners.each(&:delete)
containers.each_with_index do |container, i|
elb.listeners.create(
port: EXTERNAL_PORTS[i],
protocol: :tcp,
instance_port: container.port + 1,
instance_protocol: :tcp
)
end
elb.configure_health_check(
healthy_threshold: 2,
unhealthy_threshold: 10,
timeout: 15,
interval: 30,
target: "TCP:#{containers.first.port + 1}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment