Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

EBS AMI restructure #1

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions bin/create_cron_leader
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_SUPPORT']
EB_CONFIG_APP_SUPPORT = "/opt/elasticbeanstalk/support"
ENVIRONMENT_NAME_FILE = File.join(EB_CONFIG_APP_SUPPORT,'env_name')

# Options Parsing
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options]"

# Define the options, and what they do
options[:no_update] = false
opts.on( '--no-update', 'Do not update crontab after making leader' ) do
Expand All @@ -29,27 +29,13 @@ optparse = OptionParser.new do |opts|
end
optparse.parse!

instance_id = if File.exists?(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
File.read(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'), 'w') {|f| f.write(id) }
id
end
end
instance_id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)

AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region})
ec2 = AWS::EC2.new

environment_name = if File.exists?(ENVIRONMENT_NAME_FILE)
File.read(ENVIRONMENT_NAME_FILE)
else
env_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]
File.open(ENVIRONMENT_NAME_FILE, 'w') {|f| f.write(env_name) }
env_name
end
environment_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]

leader_instances = ec2.instances.to_a.inject([]) do |m, i|
m << i.id if i.tags["elasticbeanstalk:environment-name"] == environment_name &&
Expand All @@ -63,5 +49,5 @@ if leader_instances.count < 1
end

unless options[:no_update]
`/usr/local/bin/bundle exec setup_cron`
end
`./bin/setup_cron`
end
26 changes: 7 additions & 19 deletions bin/ensure_one_cron_leader
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,17 @@ gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_SUPPORT']
EB_CONFIG_APP_SUPPORT = "/opt/elasticbeanstalk/support"
ENVIRONMENT_NAME_FILE = File.join(EB_CONFIG_APP_SUPPORT,'env_name')
instance_id = if File.exists?(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
File.read(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'), 'w') {|f| f.write(id) }
id
end
end

instance_id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)
region = availability_zone.slice(0..availability_zone.length-2)

AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region})
ec2 = AWS::EC2.new
environment_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]

environment_name = if File.exists?(ENVIRONMENT_NAME_FILE)
File.read(ENVIRONMENT_NAME_FILE)
else
env_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]
File.open(ENVIRONMENT_NAME_FILE, 'w') {|f| f.write(env_name) }
env_name
end

leader_instances = ec2.instances.to_a.inject([]) do |m, i|
m << i.id if i.tags["elasticbeanstalk:environment-name"] == environment_name &&
Expand All @@ -37,7 +25,7 @@ leader_instances = ec2.instances.to_a.inject([]) do |m, i|
end

if leader_instances.count < 1
`/usr/local/bin/bundle exec create_cron_leader`
`./bin/create_cron_leader`
elsif leader_instances.count > 1 && leader_instances.include?(instance_id)
`/usr/local/bin/bundle exec create_cron_leader`
`./bin/create_cron_leader`
end
24 changes: 5 additions & 19 deletions bin/remove_cron_leader
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,16 @@ gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_SUPPORT']
EB_CONFIG_APP_SUPPORT = "/opt/elasticbeanstalk/support"
ENVIRONMENT_NAME_FILE = File.join(EB_CONFIG_APP_SUPPORT,'env_name')

instance_id = if File.exists?(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
File.read(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'), 'w') {|f| f.write(id) }
id
end
end
instance_id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)
region = availability_zone.slice(0..availability_zone.length-2)

AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region})
ec2 = AWS::EC2.new

environment_name = if File.exists?(ENVIRONMENT_NAME_FILE)
File.read(ENVIRONMENT_NAME_FILE)
else
env_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]
File.open(ENVIRONMENT_NAME_FILE, 'w') {|f| f.write(env_name) }
env_name
end
environment_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]

leader_instances = ec2.instances.to_a.inject([]) do |m, i|
m << i.id if i.tags["elasticbeanstalk:environment-name"] == environment_name &&
Expand All @@ -41,4 +27,4 @@ if leader_instances.count > 1 && leader_instances.include?(instance_id)
ec2.instances[instance_id].tags["leader"] = "false"
end

`/usr/local/bin/bundle exec setup_cron`
`./bin/setup_cron`
22 changes: 7 additions & 15 deletions bin/setup_cron
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@ gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_SUPPORT']
EB_CONFIG_APP_SUPPORT = "/opt/elasticbeanstalk/support"
ENVIRONMENT = ENV["RACK_ENV"]
instance_id = if File.exists?(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
File.read(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'))
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open(File.join(EB_CONFIG_APP_SUPPORT,'instance_id'), 'w') {|f| f.write(id) }
id
end
end

instance_id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)
region = availability_zone.slice(0..availability_zone.length-2)

AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region})
ec2 = AWS::EC2.new
environment_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]

unless (`echo $PATH`).match("/usr/local/bin")
`export PATH=/usr/local/bin:$PATH`
end

if ec2.instances[instance_id].tags["leader"] == "true"
`/usr/local/bin/bundle exec whenever --roles leader --set 'environment=#{ENVIRONMENT}&path=/var/app/current' --update-crontab`
else
`/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{ENVIRONMENT}&path=/var/app/current' --update-crontab`
end
roles = ec2.instances[instance_id].tags["leader"].eql?("true") ? ["leader", ENVIRONMENT] : ["non-leader"]
`./bin/whenever --roles #{roles.join(',')} --set 'environment=#{ENVIRONMENT}&path=/var/app/current' --update-crontab --user root`
2 changes: 1 addition & 1 deletion whenever-elasticbeanstalk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
gem.license = 'MIT'

gem.add_dependency('whenever','~> 0.9.2')
gem.add_dependency('aws-sdk', '~> 1.50.0')
gem.add_dependency('aws-sdk', '>= 1.50.0')

gem.files = `git ls-files`.split($/)
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
Expand Down