Skip to content

Commit

Permalink
Merge branch 'jufemaiz-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Courtney committed Aug 4, 2014
2 parents fbb40b5 + b116473 commit 6a3b58a
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 107 deletions.
20 changes: 5 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ every 1.minute do
end
```

### Create AWS IAM user
### EC2 Instance IAM Role Permissions

In order for the scripts to work, you need to supply AWS credentials for a user with access to EC2 instances and tags. It is recommended to create a new user with limited access.
In order for the scripts to work, you need to ensure that the EC2 Instance Role has access to EC2 instances and tags (further reading at [AWS Documentation](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/AWSHowTo.iam.roles.aeb.html) ). Ensure that your EC2 instance has at a minimum the following permissions:

Example policy:
```json
Expand All @@ -66,19 +66,9 @@ Example policy:
}
```

Then add the credentials to your `config/whenever-elasticbeanstalk.yml` file.
Make sure to add the `RACK_ENV` environment variable to your environment if you haven't already done so. This variable is not created automatically by AWS. You can add the following line to your `.elasticbeanstalk/optionsettings.appname-env` file:
```yaml
staging:
access_key_id: 'your access key'
secret_access_key: 'your secret access key'
# If you are not using the default us-east-1 region, specify it here
# For available regions see: http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region
# region: 'eu-west-1'
```

Make sure to add the `RAILS_ENV` environment variable to your environment if you haven't already done so. This variable is not created automatically by AWS. You can add the following line to your `.elasticbeanstalk/optionsettings.appname-env` file:
```yaml
RAILS_ENV=staging
RACK_ENV=staging
```

## Usage
Expand All @@ -99,7 +89,7 @@ end
To run a task on all instance, omit the `roles` option.
```ruby
every 1.minute do
command "touch /opt/elasticbeanstalk/support/.cron_check"
command "touch /opt/elasticbeanstalk/containerfiles/.cron_check"
end
```

Expand Down
44 changes: 22 additions & 22 deletions bin/create_cron_leader
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/ruby

require 'optparse'
require 'rubygems'
gem 'aws-sdk'
require 'aws-sdk'
require 'erb'
require 'optparse'
require 'rubygems'
gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

options = {}
EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_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]"

Expand All @@ -24,22 +27,20 @@ optparse = OptionParser.new do |opts|
exit
end
end

optparse.parse!

ENVIRONMENT_NAME_FILE = "/var/app/support/env_name"
AWS_CREDENTIALS = YAML.load(ERB.new(File.read("config/whenever-elasticbeanstalk.yml")).result)[ENV["RAILS_ENV"]]

instance_id = if File.exists?("/var/app/support/instance_id")
File.read("/var/app/support/instance_id")
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open("/var/app/support/instance_id", 'w') {|f| f.write(id) }
id
end
end
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
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)

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

environment_name = if File.exists?(ENVIRONMENT_NAME_FILE)
Expand All @@ -50,18 +51,17 @@ else
env_name
end

leader_instances = ec2.instances.inject([]) do |m, i|
leader_instances = ec2.instances.to_a.inject([]) do |m, i|
m << i.id if i.tags["elasticbeanstalk:environment-name"] == environment_name &&
i.status == :running &&
i.tags["leader"] == "true"
m
end

if leader_instances.count < 1
instance_id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
ec2.instances[instance_id].tags["leader"] = "true"
end

unless options[:no_update]
`bundle exec setup_cron`
`/usr/local/bin/bundle exec setup_cron`
end
41 changes: 21 additions & 20 deletions bin/ensure_one_cron_leader
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#!/usr/bin/ruby

require 'rubygems'
gem 'aws-sdk'
require 'aws-sdk'
require 'erb'
require 'rubygems'
gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

ENVIRONMENT_NAME_FILE = "/var/app/support/env_name"
AWS_CREDENTIALS = YAML.load(ERB.new(File.read("config/whenever-elasticbeanstalk.yml")).result)[ENV["RAILS_ENV"]]
EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_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
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)

instance_id = if File.exists?("/var/app/support/instance_id")
File.read("/var/app/support/instance_id")
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open("/var/app/support/instance_id", 'w') {|f| f.write(id) }
id
end
end

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

environment_name = if File.exists?(ENVIRONMENT_NAME_FILE)
Expand All @@ -28,15 +29,15 @@ else
env_name
end

leader_instances = ec2.instances.inject([]) do |m, i|
leader_instances = ec2.instances.to_a.inject([]) do |m, i|
m << i.id if i.tags["elasticbeanstalk:environment-name"] == environment_name &&
i.status == :running &&
i.tags["leader"] == "true"
m
end

if leader_instances.count < 1
`bundle exec create_cron_leader`
`/usr/local/bin/bundle exec create_cron_leader`
elsif leader_instances.count > 1 && leader_instances.include?(instance_id)
`bundle exec remove_cron_leader`
end
`/usr/local/bin/bundle exec create_cron_leader`
end
42 changes: 22 additions & 20 deletions bin/remove_cron_leader
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#!/usr/bin/ruby
require 'rubygems'
gem 'aws-sdk'

require 'aws-sdk'
require 'erb'

ENVIRONMENT_NAME_FILE = "/var/app/support/env_name"
AWS_CREDENTIALS = YAML.load(ERB.new(File.read("config/whenever-elasticbeanstalk.yml")).result)[ENV["RAILS_ENV"]]

instance_id = if File.exists?("/var/app/support/instance_id")
File.read("/var/app/support/instance_id")
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open("/var/app/support/instance_id", 'w') {|f| f.write(id) }
id
end
end

AWS.config(AWS_CREDENTIALS)
require 'rubygems'
gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_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
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)
Expand All @@ -28,7 +30,7 @@ else
env_name
end

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

`bundle exec setup_cron`
`/usr/local/bin/bundle exec setup_cron`
39 changes: 22 additions & 17 deletions bin/setup_cron
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
#!/usr/bin/ruby
require 'rubygems'
gem 'aws-sdk'

require 'aws-sdk'
require 'erb'
require 'rubygems'
gem 'aws-sdk'
require 'aws-sdk'
require 'erb'

environment = ENV["RAILS_ENV"]
AWS_CREDENTIALS = YAML.load(ERB.new(File.read("config/whenever-elasticbeanstalk.yml")).result)[ENV["RAILS_ENV"]]

instance_id = if File.exists?("/var/app/support/instance_id")
File.read("/var/app/support/instance_id")
else
if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
File.open("/var/app/support/instance_id", 'w') {|f| f.write(id) }
id
EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_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
end
availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip
region = availability_zone.slice(0..availability_zone.length-2)

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

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

if ec2.instances[instance_id].tags["leader"] == "true"
`bundle exec whenever --roles leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab`
`/usr/local/bin/bundle exec whenever --roles leader --set 'environment=#{ENVIRONMENT}&path=/var/app/current' --update-crontab`
else
`bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab`
`/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{ENVIRONMENT}&path=/var/app/current' --update-crontab`
end
26 changes: 18 additions & 8 deletions bin/wheneverize-eb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'optparse'
require 'fileutils'
require 'erb'

OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [path]"
Expand Down Expand Up @@ -52,7 +53,7 @@ schedule_content = <<-FILE
# Learn more: http://github.com/javan/whenever
every 1.minute do
command "cd /var/app/current && bundle exec ensure_one_cron_leader"
command "cd /var/app/current && /usr/local/bin/bundle exec ensure_one_cron_leader"
end
FILE

Expand All @@ -72,24 +73,33 @@ end

eb_config_content = <<-FILE
files:
# Reload the on deployment
/opt/elasticbeanstalk/hooks/appdeploy/post/10_reload_cron.sh:
mode: "00700"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
. /opt/elasticbeanstalk/containerfiles/envvars
cd $EB_CONFIG_APP_CURRENT
bundle exec setup_cron
su -c "/usr/local/bin/bundle exec setup_cron" $EB_CONFIG_APP_USER
# Add Bundle to the PATH
"/etc/profile.d/bundle.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
export PATH=$PATH:/usr/local/bin
encoding: plain
container_commands:
cron_01_set_leader:
test: test ! -f /opt/elasticbeanstalk/support/.cron-setup-complete
test: test ! -f /opt/elasticbeanstalk/containerfiles/.cron-setup-complete
leader_only: true
cwd: /var/app/ondeck
command: bundle exec create_cron_leader --no-update
command: su -c "/usr/local/bin/bundle exec create_cron_leader --no-update" $EB_CONFIG_APP_USER
cron_02_write_cron_setup_complete_file:
cwd: /opt/elasticbeanstalk/support
cwd: /opt/elasticbeanstalk/containerfiles
command: touch .cron-setup-complete
FILE

Expand Down Expand Up @@ -129,4 +139,4 @@ else
File.open(file, "w") { |f| f.write(aws_credentials_content) }
end

puts "[done] wheneverized for Elastic Beanstalk!"
puts "[done] wheneverized for Elastic Beanstalk!"
2 changes: 1 addition & 1 deletion lib/whenever-elasticbeanstalk/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Whenever
module Elasticbeanstalk
VERSION = "1.1.3"
VERSION = "1.1.4"
end
end
8 changes: 4 additions & 4 deletions whenever-elasticbeanstalk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Gem::Specification.new do |gem|
gem.name = "whenever-elasticbeanstalk"
gem.version = Whenever::Elasticbeanstalk::VERSION
gem.platform = Gem::Platform::RUBY
gem.authors = ["Chad McGimpsey"]
gem.email = ["[email protected]"]
gem.authors = ["Chad McGimpsey","Joel Courtney"]
gem.email = ["[email protected]","[email protected]"]
gem.description = %q{Use Whenever on AWS Elastic Beanstalk}
gem.summary = %q{Allows you to run cron jobs easily on one or all AWS Elastic Beanstalk instances.}
gem.homepage = "https://github.com/dignoe/whenever-elasticbeanstalk"
gem.license = 'MIT'

gem.add_dependency('whenever')
gem.add_dependency('aws-sdk')
gem.add_dependency('whenever','~> 0.9.2')
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

0 comments on commit 6a3b58a

Please sign in to comment.