Skip to content

Instantly share code, notes, and snippets.

@joshgarnett
Created July 7, 2016 18:57
Show Gist options
  • Save joshgarnett/607f8f231a20815a80514eabee16927e to your computer and use it in GitHub Desktop.
Save joshgarnett/607f8f231a20815a80514eabee16927e to your computer and use it in GitHub Desktop.
Ruby helper class for working with terraform
require 'json'
require 'logger'
# Helper class for interacting with Terraform
class TerraformHelper
def initialize(s3_bucket)
@logger = Logger.new(STDOUT)
@logger.level = Logger::DEBUG
@logger.formatter = proc do |severity, datetime, _progname, msg|
format("%-5s [%s] - %s\n", severity, datetime.strftime('%F %T,%L'), msg)
end
@s3_bucket = s3_bucket
end
# This assumes environments are ../terraform/environments from this script
def terraform_directory(environment)
terraform_dir = File.join(File.dirname(__FILE__), '..', 'terraform', 'environments', environment)
fail "#{terraform_dir} does not exist" unless File.directory?(terraform_dir)
terraform_dir
end
# Configures the s3 backend and runs terraform get
def init_environment(environment)
terraform_dir = terraform_directory(environment)
unless system("rm -rf #{terraform_dir}/.terraform")
@logger.error("rm -rf #{terraform_dir}/.terraform failed")
return false
end
set_config = "terraform remote config -backend=s3 -backend-config='bucket=#{@s3_bucket}' " \
"-backend-config='key=#{environment}/terraform.tfstate' " \
"-backend-config='region=us-west-2'"
unless system("cd #{terraform_dir} && #{set_config}")
@logger.error('Failed to set the terraform config')
return false
end
unless system("cd #{terraform_dir} && terraform get > /dev/null 2>&1")
@logger.error('terraform get failed')
return false
end
return true
rescue RuntimeError => ex
@logger.error("failed to initialize the environment: #{ex}")
return false
end
def taint_resource(environment, module_name, resource_name)
terraform_dir = terraform_directory(environment)
unless system("cd #{terraform_dir} && terraform taint --module=#{module_name} #{resource_name}")
@logger.error('terraform taint failed')
return false
end
return true
rescue RuntimeError => ex
@logger.error("failed to taint the resource: #{ex}")
return false
end
def apply(environment)
terraform_dir = terraform_directory(environment)
plan_file = "#{environment}.plan"
unless system("cd #{terraform_dir} && terraform plan --module-depth=1 -out #{plan_file}")
@logger.error('terraform plan failed')
return false
end
unless system("cd #{terraform_dir} && terraform apply #{plan_file}")
@logger.error('terraform apply failed')
return false
end
return true
rescue RuntimeError => ex
@logger.error("failed to apply the plan: #{ex}")
return false
end
def destroy(environment, force = false)
terraform_dir = terraform_directory(environment)
force_flag = force ? '-force' : ''
unless system("cd #{terraform_dir} && terraform destroy #{force_flag}")
@logger.error('terraform destroy failed')
return false
end
return true
rescue RuntimeError => ex
@logger.error("failed to destroy the resource: #{ex}")
return false
end
def write_vars(environment, terraform_vars)
terraform_dir = terraform_directory(environment)
File.open("#{terraform_dir}/terraform.tfvars", 'w') do |f|
f.write(JSON.pretty_generate(terraform_vars) + "\n")
end
rescue RuntimeError => ex
@logger.error("failed to save the terraform vars: #{ex}")
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment