Skip to content

Instantly share code, notes, and snippets.

@homanchou
Last active May 3, 2018 23:55
Show Gist options
  • Save homanchou/5f24c94f51ee89add8ce84689942ca29 to your computer and use it in GitHub Desktop.
Save homanchou/5f24c94f51ee89add8ce84689942ca29 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# This script will override/interpolate variables in apex project.#{env}.json
# environment with their ec2 system manager parameter store counterparts
# by making a aws cli ssm call and overriding the environment variables by
# writing them to the command line, which takes precedent over the json file
# you symlink this script to your $PATH e.g /usr/local/bin/myapex -> scripts/myapex
# then alias the original apex to use it e.g. alias apex='myapex'
# so you can use apex normally and it will grab the interpolated values for you
# e.g apex deploy --env sb_sandbox
# e.g. apex infra plan --env sb_sandbox
require 'json'
require 'getoptlong'
original_args = ARGV.join(" ")
if ARGV[0] == "deploy"
opts = GetoptLong.new(
[ '--env', '-e', GetoptLong::REQUIRED_ARGUMENT ]
)
_, env = opts.get_option
abort("--env is required for deploy") if env.nil?
file = File.read("project.#{env}.json")
data_hash = JSON.parse(file)
environment = data_hash["environment"]
profile = data_hash["profile"]
abort("profile required in project.#{env}.json") if profile.nil?
#go grab all the vars starting with ${ssm.}
ssm_to_get = environment.map do |k,v|
v.scan(/^\$\{ssm\.(.*?)\}/)
end.flatten
cmd = "aws ssm get-parameters --names #{ssm_to_get.join(" ")} --output json --profile #{profile}"
json_response = JSON.parse(`#{cmd}`)
set_vars = {}
#add those values to our big list
json_response.dig("Parameters").each do |param_store|
set_vars[param_store["Name"]] = param_store["Value"]
end
cmdline_format = set_vars.map {|k,v| "#{k}='#{v.gsub("'","\''")}'" }.join(" -s ")
enhanced_cmd = "apex #{original_args} -s #{cmdline_format}"
exec(enhanced_cmd)
else
exec("apex #{original_args}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment