Created
November 28, 2013 18:43
-
-
Save vlado/7696548 to your computer and use it in GitHub Desktop.
Script to easily add/remove custom environment variables (env.custom) on all instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Usage: | |
# Add env variable: /path/to/this/script {environment} add VAR_NAME varvalue | |
# Remove env variable: /path/to/this/script {environment} remove VAR_NAME | |
require 'active_support' | |
app_name = "betterdoc" | |
ENV_NAME = ARGV[0].to_s | |
action = ARGV[1].to_s | |
var_key = ARGV[2].to_s | |
var_value = ARGV[3].to_s | |
def msg(text) | |
puts "----> #{text}" | |
end | |
def run(cmd) | |
puts "* #{cmd}\n\n" | |
`#{cmd}` | |
end | |
def run_ssh(cmd) | |
full_cmd = "ey ssh '#{cmd}' -e #{ENV_NAME}" | |
run(full_cmd).gsub("Loading application data from Engine Yard Cloud...", "") | |
end | |
msg "Getting current env variables" | |
env_custom_string = run_ssh("cat /data/#{app_name}/shared/config/env.custom") | |
env_vars = env_custom_string.split("\n").select { |l| l.match(/^export/) }.inject({}) do |hash,line| | |
key, value = line.gsub("export", "").strip.split("=") | |
hash.merge(key => value) | |
end | |
if action == "add" && !var_key.empty? && !var_value.empty? | |
env_vars[var_key] = var_value | |
elsif action == "remove" && !var_key.empty? | |
env_vars.delete(var_key) | |
else | |
raise "Unknown action '#{action}' or missing key '#{var_key}' or value '#{var_value}'" | |
end | |
File.open("env.custom", "w") do |f| | |
env_vars.keys.sort.each do |key| | |
f.write "export #{key}=#{env_vars[key]}\n" | |
end | |
end | |
msg "Getting dna from engineyard" | |
chef_dna = run_ssh("sudo cat /etc/chef/dna.json") | |
dna = ActiveSupport::JSON.decode(chef_dna) | |
instances = dna['engineyard']['environment']['instances'] | |
instances.each do |instance| | |
msg "Updating #{instance['name'] || 'application'} instance" | |
run "scp env.custom deploy@#{instance['public_hostname']}:/data/#{app_name}/shared/config/env.custom" | |
end | |
msg "Removing tmp env.custom file" | |
run "rm env.custom" | |
msg "[IMPORTANT NOTE] To restart web (unicorn) server run: ey ssh '/engineyard/bin/app_#{app_name} reload' -e #{ENV_NAME}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment