From b1aa3f317ddb7a3417646699aaef561460c4ce27 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 16:07:12 +1000 Subject: [PATCH 01/27] Update wheneverize-eb AWS EB changed /opt/elasticbeanstalk/support to /opt/elasticbeanstalk/containerfiles --- bin/wheneverize-eb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/wheneverize-eb b/bin/wheneverize-eb index 46216b5..b485315 100755 --- a/bin/wheneverize-eb +++ b/bin/wheneverize-eb @@ -78,18 +78,18 @@ files: group: root content: | #!/usr/bin/env bash - . /opt/elasticbeanstalk/support/envvars + . /opt/elasticbeanstalk/containerfiles/envvars cd $EB_CONFIG_APP_CURRENT bundle exec setup_cron 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 cron_02_write_cron_setup_complete_file: - cwd: /opt/elasticbeanstalk/support + cwd: /opt/elasticbeanstalk/containerfiles command: touch .cron-setup-complete FILE @@ -129,4 +129,4 @@ else File.open(file, "w") { |f| f.write(aws_credentials_content) } end -puts "[done] wheneverized for Elastic Beanstalk!" \ No newline at end of file +puts "[done] wheneverized for Elastic Beanstalk!" From 20b5add16fa0339f0d40f679fb6bfe5cfb472606 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 16:17:18 +1000 Subject: [PATCH 02/27] Updated /support -> /containerfiles due to changes with AWS ElasticBeanstalk --- README.md | 2 +- bin/create_cron_leader | 8 ++++---- bin/ensure_one_cron_leader | 8 ++++---- bin/remove_cron_leader | 8 ++++---- bin/setup_cron | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f63cbee..ad845ad 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,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 ``` diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 447de28..4e47137 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -26,14 +26,14 @@ end optparse.parse! -ENVIRONMENT_NAME_FILE = "/var/app/support/env_name" +ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[ENV["RAILS_ENV"]] -instance_id = if File.exists?("/var/app/support/instance_id") - File.read("/var/app/support/instance_id") +instance_id = if File.exists?("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/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) } + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } id end end diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 7deca15..50684b7 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -4,14 +4,14 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' -ENVIRONMENT_NAME_FILE = "/var/app/support/env_name" +ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[ENV["RAILS_ENV"]] -instance_id = if File.exists?("/var/app/support/instance_id") - File.read("/var/app/support/instance_id") +instance_id = if File.exists?("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/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) } + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } id end end diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 0d74b9f..fb88103 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -4,14 +4,14 @@ gem 'aws-sdk' require 'aws-sdk' -ENVIRONMENT_NAME_FILE = "/var/app/support/env_name" +ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[ENV["RAILS_ENV"]] -instance_id = if File.exists?("/var/app/support/instance_id") - File.read("/var/app/support/instance_id") +instance_id = if File.exists?("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/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) } + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } id end end diff --git a/bin/setup_cron b/bin/setup_cron index 62a02c1..f2453e4 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -7,11 +7,11 @@ require 'aws-sdk' environment = ENV["RAILS_ENV"] AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[environment] -instance_id = if File.exists?("/var/app/support/instance_id") - File.read("/var/app/support/instance_id") +instance_id = if File.exists?("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/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) } + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } id end end From f1a778d9d1842495db4858f1dc7381efac7806ca Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 16:56:49 +1000 Subject: [PATCH 03/27] Parse configuration file --- bin/create_cron_leader | 3 ++- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 4e47137..a21a868 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -27,7 +27,8 @@ end optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] + instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 50684b7..1bca6b8 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -5,7 +5,7 @@ gem 'aws-sdk' require 'aws-sdk' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index fb88103..eecafc7 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -5,7 +5,7 @@ gem 'aws-sdk' require 'aws-sdk' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/setup_cron b/bin/setup_cron index f2453e4..0c22ad5 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -5,7 +5,7 @@ gem 'aws-sdk' require 'aws-sdk' environment = ENV["RAILS_ENV"] -AWS_CREDENTIALS = YAML.load_file("config/whenever-elasticbeanstalk.yml")[environment] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") From 7a58ae4e69e0d076b3ebfde308aa5c3520293e01 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 17:15:27 +1000 Subject: [PATCH 04/27] Update to use /usr/local/bin/bundle --- bin/create_cron_leader | 3 ++- bin/ensure_one_cron_leader | 4 ++-- bin/remove_cron_leader | 3 ++- bin/setup_cron | 5 +++-- bin/wheneverize-eb | 7 ++++--- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index a21a868..9373c10 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -4,6 +4,7 @@ require 'optparse' require 'rubygems' gem 'aws-sdk' require 'aws-sdk' +require 'erb' options = {} @@ -63,5 +64,5 @@ if leader_instances.count < 1 end unless options[:no_update] - `bundle exec setup_cron` + `su -c "/usr/local/bin/bundle exec setup_cron" $EB_CONFIG_APP_USER` end \ No newline at end of file diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 1bca6b8..c7f1005 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -35,7 +35,7 @@ leader_instances = ec2.instances.inject([]) do |m, i| end if leader_instances.count < 1 - `bundle exec create_cron_leader` + `su -c "/usr/local/bin/bundle exec create_cron_leader" $EB_CONFIG_APP_USER` elsif leader_instances.count > 1 && leader_instances.include?(instance_id) - `bundle exec remove_cron_leader` + `su -c "/usr/local/bin/bundle exec remove_cron_leader" $EB_CONFIG_APP_USER` end \ No newline at end of file diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index eecafc7..b59ca05 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -3,6 +3,7 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' +require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] @@ -38,4 +39,4 @@ if leader_instances.count > 1 && leader_instances.include?(instance_id) ec2.instances[instance_id].tags["leader"] = "false" end -`bundle exec setup_cron` \ No newline at end of file +`su -c "/usr/local/bin/bundle exec setup_cron" $EB_CONFIG_APP_USER` diff --git a/bin/setup_cron b/bin/setup_cron index 0c22ad5..07e1e1a 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -3,6 +3,7 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' +require 'erb' environment = ENV["RAILS_ENV"] AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] @@ -20,7 +21,7 @@ AWS.config(AWS_CREDENTIALS) ec2 = AWS::EC2.new if ec2.instances[instance_id].tags["leader"] == "true" - `bundle exec whenever --roles leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab` + `su -c "/usr/local/bin/bundle exec whenever --roles leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab" $EB_CONFIG_APP_USER` else - `bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab` + `su -c "/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab" $EB_CONFIG_APP_USER` end diff --git a/bin/wheneverize-eb b/bin/wheneverize-eb index b485315..0a97c31 100755 --- a/bin/wheneverize-eb +++ b/bin/wheneverize-eb @@ -4,6 +4,7 @@ require 'optparse' require 'fileutils' +require 'erb' OptionParser.new do |opts| opts.banner = "Usage: #{File.basename($0)} [path]" @@ -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 && su -c \"/usr/local/bin/bundle exec ensure_one_cron_leader\" $EB_CONFIG_APP_USER" end FILE @@ -80,14 +81,14 @@ files: #!/usr/bin/env bash . /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 container_commands: cron_01_set_leader: 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/containerfiles command: touch .cron-setup-complete From a095d5989e32c319798be4d6b87463cc96f9567f Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 17:30:11 +1000 Subject: [PATCH 05/27] erb updates correct --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 4 ++-- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 9373c10..60ee2f8 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -28,7 +28,7 @@ end optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index c7f1005..be8c941 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -5,7 +5,7 @@ gem 'aws-sdk' require 'aws-sdk' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") @@ -37,5 +37,5 @@ end if leader_instances.count < 1 `su -c "/usr/local/bin/bundle exec create_cron_leader" $EB_CONFIG_APP_USER` elsif leader_instances.count > 1 && leader_instances.include?(instance_id) - `su -c "/usr/local/bin/bundle exec remove_cron_leader" $EB_CONFIG_APP_USER` + `su -c "/usr/local/bin/bundle exec create_cron_leader" $EB_CONFIG_APP_USER` end \ No newline at end of file diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index b59ca05..fbda558 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/setup_cron b/bin/setup_cron index 07e1e1a..ce03681 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' environment = ENV["RAILS_ENV"] -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read))[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") From 04cb7830c09d2fdddb891aff46f03082776abec7 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 17:44:50 +1000 Subject: [PATCH 06/27] erb updates correct --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 60ee2f8..3cd0260 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -28,7 +28,7 @@ end optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index be8c941..91a9174 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -5,7 +5,7 @@ gem 'aws-sdk' require 'aws-sdk' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index fbda558..0f557f7 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/setup_cron b/bin/setup_cron index ce03681..9220268 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' environment = ENV["RAILS_ENV"] -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).run)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") From 211a9416841b80a91d86d5e62ccee65288554dc9 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 14 May 2014 17:48:42 +1000 Subject: [PATCH 07/27] erb updates correct --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 3cd0260..bc9feff 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -28,7 +28,7 @@ end optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 91a9174..cd466a9 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -5,7 +5,7 @@ gem 'aws-sdk' require 'aws-sdk' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 0f557f7..755e29c 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/setup_cron b/bin/setup_cron index 9220268..bfd98cf 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' environment = ENV["RAILS_ENV"] -AWS_CREDENTIALS = YAML.load_file(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") From 1041f04354a24902f5f7353a1cda839ff1a83b53 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 11:02:07 +1000 Subject: [PATCH 08/27] Updated scripts to not use in ruby tasks but maintain its use when used by root user. Goal is to always be run as user:webapp --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 4 ++-- bin/remove_cron_leader | 2 +- bin/setup_cron | 4 ++-- bin/wheneverize-eb | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index bc9feff..0136dff 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -64,5 +64,5 @@ if leader_instances.count < 1 end unless options[:no_update] - `su -c "/usr/local/bin/bundle exec setup_cron" $EB_CONFIG_APP_USER` + `/usr/local/bin/bundle exec setup_cron` end \ No newline at end of file diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index cd466a9..c823d95 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -35,7 +35,7 @@ leader_instances = ec2.instances.inject([]) do |m, i| end if leader_instances.count < 1 - `su -c "/usr/local/bin/bundle exec create_cron_leader" $EB_CONFIG_APP_USER` + `/usr/local/bin/bundle exec create_cron_leader` elsif leader_instances.count > 1 && leader_instances.include?(instance_id) - `su -c "/usr/local/bin/bundle exec create_cron_leader" $EB_CONFIG_APP_USER` + `/usr/local/bin/bundle exec create_cron_leader` end \ No newline at end of file diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 755e29c..be888d1 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -39,4 +39,4 @@ if leader_instances.count > 1 && leader_instances.include?(instance_id) ec2.instances[instance_id].tags["leader"] = "false" end -`su -c "/usr/local/bin/bundle exec setup_cron" $EB_CONFIG_APP_USER` +`/usr/local/bin/bundle exec setup_cron` diff --git a/bin/setup_cron b/bin/setup_cron index bfd98cf..088ad82 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -21,7 +21,7 @@ AWS.config(AWS_CREDENTIALS) ec2 = AWS::EC2.new if ec2.instances[instance_id].tags["leader"] == "true" - `su -c "/usr/local/bin/bundle exec whenever --roles leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab" $EB_CONFIG_APP_USER` + `/usr/local/bin/bundle exec whenever --roles leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab` else - `su -c "/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab" $EB_CONFIG_APP_USER` + `/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab` end diff --git a/bin/wheneverize-eb b/bin/wheneverize-eb index 0a97c31..f45c368 100755 --- a/bin/wheneverize-eb +++ b/bin/wheneverize-eb @@ -53,7 +53,7 @@ schedule_content = <<-FILE # Learn more: http://github.com/javan/whenever every 1.minute do - command "cd /var/app/current && su -c \"/usr/local/bin/bundle exec ensure_one_cron_leader\" $EB_CONFIG_APP_USER" + command "cd /var/app/current && /usr/local/bin/bundle exec ensure_one_cron_leader" end FILE From d11b6410333af42c9feda1656b581953a6919598 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 12:38:50 +1000 Subject: [PATCH 09/27] debug test added --- bin/setup_cron | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/setup_cron b/bin/setup_cron index 088ad82..fc22bdc 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -25,3 +25,5 @@ if ec2.instances[instance_id].tags["leader"] == "true" else `/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab` end + +`crontab -l > /var/app/current/log/crontab.status` \ No newline at end of file From 8aff2ac79a4d675c83750d0d825661c8b39a84df Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 12:57:30 +1000 Subject: [PATCH 10/27] debug test added with joel --- bin/setup_cron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/setup_cron b/bin/setup_cron index fc22bdc..e2562b9 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -26,4 +26,4 @@ else `/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{environment}&path=/var/app/current' --update-crontab` end -`crontab -l > /var/app/current/log/crontab.status` \ No newline at end of file +`crontab -l > /var/app/current/log/crontab.$USER.status` From 9550770e0a4bbbea3ae8205fa939262307fde861 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 13:31:52 +1000 Subject: [PATCH 11/27] setup_cron now checks path --- bin/setup_cron | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/setup_cron b/bin/setup_cron index e2562b9..c98d181 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -20,10 +20,12 @@ end AWS.config(AWS_CREDENTIALS) 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" `/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 - -`crontab -l > /var/app/current/log/crontab.$USER.status` From b59f21c7234b1c442cda94823ecf88cb43afe1e0 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 14:20:31 +1000 Subject: [PATCH 12/27] require 'erb' added for YAML parsing support of environmental variables --- bin/create_cron_leader | 1 + bin/ensure_one_cron_leader | 1 + bin/remove_cron_leader | 1 + bin/setup_cron | 1 + 4 files changed, 4 insertions(+) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 0136dff..77eab8b 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -5,6 +5,7 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' require 'erb' +require 'erb' options = {} diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index c823d95..ab8bf92 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -3,6 +3,7 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' +require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index be888d1..b0f4f4d 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -4,6 +4,7 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' +require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] diff --git a/bin/setup_cron b/bin/setup_cron index c98d181..36929a9 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -4,6 +4,7 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' +require 'erb' environment = ENV["RAILS_ENV"] AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] From 7cc8c5c30a9be46d0c6380261fad2d66265ca0e6 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 14:21:35 +1000 Subject: [PATCH 13/27] Added file creation of /etc/profile.d/bundle.sh to wheneverize-eb --- bin/wheneverize-eb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/wheneverize-eb b/bin/wheneverize-eb index f45c368..cb15f77 100755 --- a/bin/wheneverize-eb +++ b/bin/wheneverize-eb @@ -73,6 +73,7 @@ end eb_config_content = <<-FILE files: + # Reload the on deployment /opt/elasticbeanstalk/hooks/appdeploy/post/10_reload_cron.sh: mode: "00700" owner: root @@ -82,7 +83,15 @@ files: . /opt/elasticbeanstalk/containerfiles/envvars cd $EB_CONFIG_APP_CURRENT 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/containerfiles/.cron-setup-complete From cfa8f110f4bbac7f6176c37487ec840b8bdfa57a Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Thu, 15 May 2014 16:12:08 +1000 Subject: [PATCH 14/27] Fixed collection iteration by using .to_a --- bin/create_cron_leader | 3 +-- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 1 - 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 77eab8b..5acaf11 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -5,7 +5,6 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' require 'erb' -require 'erb' options = {} @@ -52,7 +51,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" diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index ab8bf92..e76ef3a 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -28,7 +28,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" diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index b0f4f4d..a63872f 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -29,7 +29,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" diff --git a/bin/setup_cron b/bin/setup_cron index 36929a9..c98d181 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -4,7 +4,6 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' -require 'erb' environment = ENV["RAILS_ENV"] AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] From 3c765459f8770abd5819031087dbf7f64fe1d8b9 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Mon, 23 Jun 2014 22:30:19 +1000 Subject: [PATCH 15/27] Shift to using AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) --- bin/create_cron_leader | 4 ++-- bin/ensure_one_cron_leader | 3 ++- bin/remove_cron_leader | 3 ++- bin/setup_cron | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 5acaf11..7388439 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -30,7 +30,6 @@ optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] - instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") else @@ -40,7 +39,8 @@ else end end -AWS.config(AWS_CREDENTIALS) +# AWS.config(AWS_CREDENTIALS) +AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index e76ef3a..532ce15 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -17,7 +17,8 @@ else end end -AWS.config(AWS_CREDENTIALS) +# AWS.config(AWS_CREDENTIALS) +AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index a63872f..e31a9bf 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -18,7 +18,8 @@ else end end -AWS.config(AWS_CREDENTIALS) +# AWS.config(AWS_CREDENTIALS) +AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/setup_cron b/bin/setup_cron index c98d181..9157c21 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -17,7 +17,8 @@ else end end -AWS.config(AWS_CREDENTIALS) +# AWS.config(AWS_CREDENTIALS) +AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) ec2 = AWS::EC2.new unless (`echo $PATH`).match("/usr/local/bin") From 51e77e7d57ea0b979bfed499630fc0e925a45ba1 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Mon, 23 Jun 2014 22:33:25 +1000 Subject: [PATCH 16/27] Update to use RACK_ENV --- README.md | 4 ++-- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ad845ad..31f230f 100644 --- a/README.md +++ b/README.md @@ -76,9 +76,9 @@ staging: # 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: +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 -RAILS_ENV=staging +RACK_ENV=staging ``` ## Usage diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 7388439..c778fe5 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -28,7 +28,7 @@ end optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 532ce15..effbe84 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -6,7 +6,7 @@ require 'aws-sdk' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index e31a9bf..0dd3f2f 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -7,7 +7,7 @@ require 'erb' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") diff --git a/bin/setup_cron b/bin/setup_cron index 9157c21..131cc5f 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -5,8 +5,8 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' -environment = ENV["RAILS_ENV"] -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RAILS_ENV"]] +environment = ENV["RACK_ENV"] +AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") From 64177bac407e631ef0d354480dc00f404066c1a1 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Mon, 23 Jun 2014 23:24:53 +1000 Subject: [PATCH 17/27] Updates to refer to configuration file for region --- bin/create_cron_leader | 1 + bin/ensure_one_cron_leader | 1 + bin/remove_cron_leader | 1 + bin/setup_cron | 1 + 4 files changed, 4 insertions(+) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index c778fe5..6549700 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -41,6 +41,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) +AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index effbe84..362cf4e 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -19,6 +19,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) +AWS.config(:region => AWS_CREDENTIALS[:region]) unless AWS_CREDENTIALS[:region].nil? ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 0dd3f2f..b8f08aa 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -20,6 +20,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) +AWS.config(:region => AWS_CREDENTIALS[:region]) unless AWS_CREDENTIALS[:region].nil? ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/setup_cron b/bin/setup_cron index 131cc5f..4b544fc 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -19,6 +19,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) +AWS.config(:region => AWS_CREDENTIALS[:region]) unless AWS_CREDENTIALS[:region].nil? ec2 = AWS::EC2.new unless (`echo $PATH`).match("/usr/local/bin") From 8a104e557cc731ba0262372e2e9f5262dd94b34c Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Tue, 24 Jun 2014 18:26:53 +1000 Subject: [PATCH 18/27] Updates to point to appropriate location for regions. Needs to be smarter in the future - ie: get region from current instance? --- bin/ensure_one_cron_leader | 4 ++-- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 362cf4e..6d7b7e1 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -19,7 +19,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS[:region]) unless AWS_CREDENTIALS[:region].nil? +AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) @@ -41,4 +41,4 @@ if leader_instances.count < 1 `/usr/local/bin/bundle exec create_cron_leader` elsif leader_instances.count > 1 && leader_instances.include?(instance_id) `/usr/local/bin/bundle exec create_cron_leader` -end \ No newline at end of file +end diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index b8f08aa..6ec760b 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -20,7 +20,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS[:region]) unless AWS_CREDENTIALS[:region].nil? +AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/setup_cron b/bin/setup_cron index 4b544fc..be910cc 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -19,7 +19,7 @@ end # AWS.config(AWS_CREDENTIALS) AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS[:region]) unless AWS_CREDENTIALS[:region].nil? +AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? ec2 = AWS::EC2.new unless (`echo $PATH`).match("/usr/local/bin") From 82d0a92485e948996d067f8e01a8e4d80f885710 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 25 Jun 2014 10:59:13 +1000 Subject: [PATCH 19/27] Updates to IAM access --- README.md | 14 ++------------ bin/create_cron_leader | 32 +++++++++++--------------------- bin/ec2_metadata | 28 ++++++++++++++++++++++++++++ bin/ensure_one_cron_leader | 8 ++++---- bin/remove_cron_leader | 9 ++++----- bin/setup_cron | 8 ++++---- 6 files changed, 53 insertions(+), 46 deletions(-) create mode 100755 bin/ec2_metadata diff --git a/README.md b/README.md index 31f230f..9c467fb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -66,16 +66,6 @@ Example policy: } ``` -Then add the credentials to your `config/whenever-elasticbeanstalk.yml` 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 `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 RACK_ENV=staging diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 6549700..954ce06 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -1,13 +1,14 @@ #!/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' +require_relative 'ec2_metadata' + +# Options Parsing options = {} - optparse = OptionParser.new do |opts| opts.banner = "Usage: #{File.basename($0)} [options]" @@ -24,24 +25,14 @@ optparse = OptionParser.new do |opts| exit end end - optparse.parse! ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] -instance_id = if File.exists?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_id") -else - if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip - File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } - id - end -end +instance_id = get_instance_id() +region = get_region() -# AWS.config(AWS_CREDENTIALS) -AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? +AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) @@ -60,7 +51,6 @@ leader_instances = ec2.instances.to_a.inject([]) do |m, i| 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 diff --git a/bin/ec2_metadata b/bin/ec2_metadata new file mode 100755 index 0000000..9e5a126 --- /dev/null +++ b/bin/ec2_metadata @@ -0,0 +1,28 @@ +#!/usr/bin/ruby + +require 'rubygems' +gem 'aws-sdk' +require 'aws-sdk' +require 'erb' + +def get_instance_id + instance_id = if File.exists?("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/instance_id") + else + if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + id + end + end + instance_id +end + +def get_availability_zone + availability_zone = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip +end + +def get_region + availability_zone = get_availability_zone + region = availability_zone[0..availability_zone.length-1] + region +end \ No newline at end of file diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 6d7b7e1..016f0ef 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -1,9 +1,9 @@ #!/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/containerfiles/env_name" AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 6ec760b..75cd83c 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -1,10 +1,9 @@ #!/usr/bin/ruby -require 'rubygems' -gem 'aws-sdk' -require 'aws-sdk' -require 'erb' -require 'erb' +require 'rubygems' +gem 'aws-sdk' +require 'aws-sdk' +require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] diff --git a/bin/setup_cron b/bin/setup_cron index be910cc..1973660 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -1,9 +1,9 @@ #!/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["RACK_ENV"] AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] From a2658a235c566b0fd9a0886c27597da3be1abfb0 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 25 Jun 2014 11:18:47 +1000 Subject: [PATCH 20/27] Removed ec2_metadata common file and placed back in each bin file --- bin/create_cron_leader | 16 ++++++++++++---- bin/ec2_metadata | 28 ---------------------------- bin/ensure_one_cron_leader | 8 +++----- bin/remove_cron_leader | 7 +++---- bin/setup_cron | 18 ++++++++---------- 5 files changed, 26 insertions(+), 51 deletions(-) delete mode 100755 bin/ec2_metadata diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 954ce06..3c0d61d 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -7,6 +7,8 @@ require 'aws-sdk' require 'erb' require_relative 'ec2_metadata' +ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" + # Options Parsing options = {} optparse = OptionParser.new do |opts| @@ -27,10 +29,16 @@ optparse = OptionParser.new do |opts| end optparse.parse! -ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" - -instance_id = get_instance_id() -region = get_region() +instance_id = if File.exists?("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/instance_id") +else + if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + id + end +end +availability_zone = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip +region = availability_zone[0..availability_zone.length-1] AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/ec2_metadata b/bin/ec2_metadata deleted file mode 100755 index 9e5a126..0000000 --- a/bin/ec2_metadata +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/ruby - -require 'rubygems' -gem 'aws-sdk' -require 'aws-sdk' -require 'erb' - -def get_instance_id - instance_id = if File.exists?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_id") - else - if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip - File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } - id - end - end - instance_id -end - -def get_availability_zone - availability_zone = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip -end - -def get_region - availability_zone = get_availability_zone - region = availability_zone[0..availability_zone.length-1] - region -end \ No newline at end of file diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 016f0ef..20b7180 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -6,8 +6,6 @@ require 'aws-sdk' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] - instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") else @@ -16,10 +14,10 @@ else id end end +availability_zone = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip +region = availability_zone[0..availability_zone.length-1] -# AWS.config(AWS_CREDENTIALS) -AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? +AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 75cd83c..23171c7 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -6,7 +6,6 @@ require 'aws-sdk' require 'erb' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] instance_id = if File.exists?("/var/app/containerfiles/instance_id") File.read("/var/app/containerfiles/instance_id") @@ -16,10 +15,10 @@ else id end end +availability_zone = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip +region = availability_zone[0..availability_zone.length-1] -# AWS.config(AWS_CREDENTIALS) -AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? +AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new environment_name = if File.exists?(ENVIRONMENT_NAME_FILE) diff --git a/bin/setup_cron b/bin/setup_cron index 1973660..8514928 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -6,20 +6,18 @@ require 'aws-sdk' require 'erb' environment = ENV["RACK_ENV"] -AWS_CREDENTIALS = YAML.load(ERB.new(File.new("config/whenever-elasticbeanstalk.yml").read).result)[ENV["RACK_ENV"]] - instance_id = if File.exists?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_id") + File.read("/var/app/containerfiles/instance_id") else - if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip - File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } - id - end + if id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip + File.open("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + id + end end +availability_zone = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip +region = availability_zone[0..availability_zone.length-1] -# AWS.config(AWS_CREDENTIALS) -AWS.config(:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new) -AWS.config(:region => AWS_CREDENTIALS['region']) unless AWS_CREDENTIALS['region'].nil? +AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new unless (`echo $PATH`).match("/usr/local/bin") From dc9049d8b4b3b2f329b17fea1c2c1a342ba8e042 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 25 Jun 2014 11:33:19 +1000 Subject: [PATCH 21/27] Removed ec2_metadata common file and placed back in each bin file --- bin/create_cron_leader | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 3c0d61d..3d5cc79 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -5,7 +5,6 @@ require 'rubygems' gem 'aws-sdk' require 'aws-sdk' require 'erb' -require_relative 'ec2_metadata' ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" From 481adf81b37bd5c4972f5c2ffe2da9b4fbd91dc9 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 25 Jun 2014 11:51:13 +1000 Subject: [PATCH 22/27] Update to get the availability zone --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 3d5cc79..60d4a5d 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -36,7 +36,7 @@ else id end end -availability_zone = `/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[0..availability_zone.length-1] AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 20b7180..677b1d6 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -14,7 +14,7 @@ else id end end -availability_zone = `/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[0..availability_zone.length-1] AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 23171c7..28eef8f 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -15,7 +15,7 @@ else id end end -availability_zone = `/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[0..availability_zone.length-1] AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) diff --git a/bin/setup_cron b/bin/setup_cron index 8514928..2b23a96 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -14,7 +14,7 @@ else id end end -availability_zone = `/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[0..availability_zone.length-1] AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) From 47e47a5767abbfbe52781c0f66dadbf5ceea9b22 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 25 Jun 2014 12:02:21 +1000 Subject: [PATCH 23/27] Update to get the availability zone --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 60d4a5d..73d2f08 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -37,7 +37,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone[0..availability_zone.length-1] +region = availability_zone.slice(0..availability_zone.length-1) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 677b1d6..9d2a434 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -15,7 +15,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone[0..availability_zone.length-1] +region = availability_zone.slice(0..availability_zone.length-1) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 28eef8f..82cd65b 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -16,7 +16,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone[0..availability_zone.length-1] +region = availability_zone.slice(0..availability_zone.length-1) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/setup_cron b/bin/setup_cron index 2b23a96..7883378 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -15,7 +15,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone[0..availability_zone.length-1] +region = availability_zone.slice(0..availability_zone.length-1) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new From 832bb392fabde7f1ffcc20d16d641adb8a324ebe Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Wed, 25 Jun 2014 12:08:30 +1000 Subject: [PATCH 24/27] Update to get the availability zone --- bin/create_cron_leader | 2 +- bin/ensure_one_cron_leader | 2 +- bin/remove_cron_leader | 2 +- bin/setup_cron | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 73d2f08..99f4ddd 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -37,7 +37,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone.slice(0..availability_zone.length-1) +region = availability_zone.slice(0..availability_zone.length-2) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 9d2a434..57f8893 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -15,7 +15,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone.slice(0..availability_zone.length-1) +region = availability_zone.slice(0..availability_zone.length-2) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 82cd65b..5d84f32 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -16,7 +16,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone.slice(0..availability_zone.length-1) +region = availability_zone.slice(0..availability_zone.length-2) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new diff --git a/bin/setup_cron b/bin/setup_cron index 7883378..9d98839 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -15,7 +15,7 @@ else end end availability_zone = `/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`.strip -region = availability_zone.slice(0..availability_zone.length-1) +region = availability_zone.slice(0..availability_zone.length-2) AWS.config({:credential_provider => AWS::Core::CredentialProviders::EC2Provider.new,:region => region}) ec2 = AWS::EC2.new From 26bc269d5b9e0323e94cac2a40686216f1e74958 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Mon, 4 Aug 2014 11:17:59 +1000 Subject: [PATCH 25/27] Updates for v1.1.4 --- bin/create_cron_leader | 9 +++++---- bin/ensure_one_cron_leader | 9 +++++---- bin/remove_cron_leader | 9 +++++---- bin/setup_cron | 13 +++++++------ lib/whenever-elasticbeanstalk/version.rb | 2 +- whenever-elasticbeanstalk.gemspec | 4 ++-- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index 99f4ddd..d71cdab 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -6,7 +6,8 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' -ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" +EB_CONFIG_APP_SUPPORT = ENV['EB_CONFIG_APP_SUPPORT'] +ENVIRONMENT_NAME_FILE = File.join(EB_CONFIG_APP_SUPPORT,'env_name') # Options Parsing options = {} @@ -28,11 +29,11 @@ optparse = OptionParser.new do |opts| end optparse.parse! -instance_id = if File.exists?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_id") +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("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + File.open(File.join(EB_CONFIG_APP_SUPPORT,instance_id), 'w') {|f| f.write(id) } id end end diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 57f8893..2bf28ff 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -5,12 +5,13 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' -ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" -instance_id = if File.exists?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_id") +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("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + File.open(File.join(EB_CONFIG_APP_SUPPORT,instance_id), 'w') {|f| f.write(id) } id end end diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index 5d84f32..df4c4fb 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -5,13 +5,14 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' -ENVIRONMENT_NAME_FILE = "/var/app/containerfiles/env_name" +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?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_id") +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("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + File.open("instance_id", 'w') {|f| f.write(id) } id end end diff --git a/bin/setup_cron b/bin/setup_cron index 9d98839..9d6a957 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -5,12 +5,13 @@ gem 'aws-sdk' require 'aws-sdk' require 'erb' -environment = ENV["RACK_ENV"] -instance_id = if File.exists?("/var/app/containerfiles/instance_id") - File.read("/var/app/containerfiles/instance_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("/var/app/containerfiles/instance_id", 'w') {|f| f.write(id) } + File.open(File.join(EB_CONFIG_APP_SUPPORT,instance_id), 'w') {|f| f.write(id) } id end end @@ -25,7 +26,7 @@ unless (`echo $PATH`).match("/usr/local/bin") 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` + `/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` + `/usr/local/bin/bundle exec whenever --roles non-leader --set 'environment=#{ENVIRONMENT}&path=/var/app/current' --update-crontab` end diff --git a/lib/whenever-elasticbeanstalk/version.rb b/lib/whenever-elasticbeanstalk/version.rb index 97da9b3..c78d6b3 100644 --- a/lib/whenever-elasticbeanstalk/version.rb +++ b/lib/whenever-elasticbeanstalk/version.rb @@ -1,5 +1,5 @@ module Whenever module Elasticbeanstalk - VERSION = "1.1.3" + VERSION = "1.1.4" end end diff --git a/whenever-elasticbeanstalk.gemspec b/whenever-elasticbeanstalk.gemspec index 828e104..8fadd8b 100644 --- a/whenever-elasticbeanstalk.gemspec +++ b/whenever-elasticbeanstalk.gemspec @@ -7,8 +7,8 @@ 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 = ["chad.mcgimpsey@gmail.com"] + gem.authors = ["Chad McGimpsey","Joel Courtney"] + gem.email = ["chad.mcgimpsey@gmail.com","euphemize@gmail.com"] 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" From 3d74ab4a0b48cbc2bea8ac08d85d5ecb27d8e9b2 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Mon, 4 Aug 2014 11:29:46 +1000 Subject: [PATCH 26/27] Updates to instance_id file path --- bin/create_cron_leader | 16 ++++++++-------- bin/ensure_one_cron_leader | 16 ++++++++-------- bin/remove_cron_leader | 16 ++++++++-------- bin/setup_cron | 16 ++++++++-------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/bin/create_cron_leader b/bin/create_cron_leader index d71cdab..b27c4ba 100755 --- a/bin/create_cron_leader +++ b/bin/create_cron_leader @@ -29,14 +29,14 @@ 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 = 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) diff --git a/bin/ensure_one_cron_leader b/bin/ensure_one_cron_leader index 2bf28ff..b4a9d91 100755 --- a/bin/ensure_one_cron_leader +++ b/bin/ensure_one_cron_leader @@ -7,14 +7,14 @@ 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 +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) diff --git a/bin/remove_cron_leader b/bin/remove_cron_leader index df4c4fb..1975946 100755 --- a/bin/remove_cron_leader +++ b/bin/remove_cron_leader @@ -8,14 +8,14 @@ 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("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) diff --git a/bin/setup_cron b/bin/setup_cron index 9d6a957..a5989f9 100755 --- a/bin/setup_cron +++ b/bin/setup_cron @@ -7,14 +7,14 @@ require 'erb' 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 +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) From 966a857c7b810eaf03572206a3ffa4129ba34d62 Mon Sep 17 00:00:00 2001 From: Joel Courtney Date: Mon, 4 Aug 2014 12:53:27 +1000 Subject: [PATCH 27/27] Updated gemspec --- whenever-elasticbeanstalk.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/whenever-elasticbeanstalk.gemspec b/whenever-elasticbeanstalk.gemspec index 8fadd8b..582cecd 100644 --- a/whenever-elasticbeanstalk.gemspec +++ b/whenever-elasticbeanstalk.gemspec @@ -14,8 +14,8 @@ Gem::Specification.new do |gem| 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) }