Created
April 21, 2015 13:48
-
-
Save benmmurphy/724e52aff8229d221d50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app.rb | |
require 'sinatra' | |
require 'securerandom' | |
require 'aws-sdk' | |
set :protection, :except => :frame_options | |
HOSTED_ZONE_ID = "HOLLAID" | |
DOMAIN = "holla.net" | |
SUFFIX = "-bad.#{DOMAIN}" | |
get '/' do | |
if !request.host.end_with?(SUFFIX) | |
subdomain = SecureRandom.hex(16) | |
@server = "#{subdomain}#{SUFFIX}" | |
erb :index | |
else | |
if request.host.end_with?(SUFFIX) | |
domain_sufix = ".#{DOMAIN}" | |
update_domain(request.host) | |
end | |
erb :subdomain | |
end | |
end | |
def update_domain(subdomain) | |
puts "updating domain..#{subdomain}" | |
route53 = Aws::Route53::Client.new( | |
region: 'eu-west-1' | |
) | |
resp = route53.change_resource_record_sets( | |
# required | |
hosted_zone_id: HOSTED_ZONE_ID, | |
# required | |
change_batch: { | |
# required | |
changes: [ | |
{ | |
# required | |
action: "UPSERT", | |
# required | |
resource_record_set: { | |
# required | |
name: "#{subdomain}", | |
# required | |
type: "A", | |
ttl: 60, | |
resource_records: [ | |
{ | |
# required | |
value: "127.0.0.1", | |
}, | |
] | |
}, | |
}, | |
], | |
}, | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment