-
-
Save jedschneider/2332b34108f171608fcab8eae444292f to your computer and use it in GitHub Desktop.
build script for terraform deploys
This file contains 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 | |
require 'optparse' | |
require 'fileutils' | |
require 'open3' | |
options = { | |
parallelism: 10, | |
init: false | |
} | |
OptionParser.new do |parser| | |
parser.banner = 'Usage: build [options] TARGET' | |
parser.on('-i', '--init', 'Run terraform init (default false)') | |
parser.on('-p', '--parallelism [PARALLELISM]', 'Run terraform apply with PARALLELISM, (default 10)', Integer) | |
parser.on('-v', '--vault', 'wrap with aws-vault (default false)') | |
parser.on('-t', '--tag IMAGE_TAG', 'Run terraform apply with supplied IMAGE_TAG)') | |
end.parse!(into: options) | |
raise OptionParser::MissingArgument, 'TARGET' if ARGV.empty? | |
FileUtils.cd('terraform/docker') | |
vault = options[:vault] ? "aws-vault exec #{ARGV[0]} --" : '' | |
env_vars = { | |
'TF_VAR_github_token' => ENV['BUNDLE_GITHUB__COM'] || `op read "<some 1pass cred>"` | |
} | |
env_vars.merge!('<some project>_image_tag' => options[:tag]) if options[:tag] | |
if options[:init] | |
FileUtils.rm_rf(['.terraform', 'terraform.lock.hcl']) | |
cmd = [vault, 'terraform init'].join(' ') | |
puts "running: '#{cmd}'" | |
Open3.popen3(env_vars, cmd) do |_stdin, stdout, stderr, wait_thread| | |
stdout_reader = Thread.new { stdout.each { |line| puts line } } | |
stderr_reader = Thread.new { stderr.each { |line| puts line } } | |
stdout_reader.join | |
stderr_reader.join | |
wait_thread.join | |
end | |
end | |
cmd = [vault, "terraform apply -auto-approve -parallelism=#{options[:parallelism]}"].join(' ') | |
puts "running: '#{cmd}'" | |
Open3.popen3(env_vars, cmd) do |_stdin, stdout, stderr, wait_thread| | |
stdout_reader = Thread.new { stdout.each { |line| puts line } } | |
stderr_reader = Thread.new { stderr.each { |line| puts line } } | |
stdout_reader.join | |
stderr_reader.join | |
wait_thread.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment