class Pfm::Command::Base

def deploy_setupv2

def deploy_setupv2
  Terraform::Binary.configure do |config|
    config.version = SETTINGS['TERRAFORM_VERSION']
    config.download_path = "/tmp/#{SecureRandom.uuid}"
  end
  # Create dynamic variables file for terraform based on config
  keys = {}
  vars_file = ''
  env_metadata = JSON.parse(open(@config[:config_file]).read)
  ['account', 'environment', 'ec2', 'application'].each do |section|
    env_metadata[section].each do |key, value|
      # skip dups
      next unless keys[key].nil?
      # replace null with empty string
      value = '' if value.nil?
      # skip lists and maps
      next unless (value.instance_of? String)
      # add to vars file and record key for dups
      keys[key] = 'parsed'
      unless upgraded_terraform?
        vars_file += <<~EOH
          variable "#{key}" {}
        EOH
      end
      # load value into envrionment
      Idlc::Deploy::Config.add_deployment_var(key, value)
    end
  end
  # write vars file
  unless upgraded_terraform?
    File.open("#{config[:working_dir]}/#{env_metadata['environment_key']}-tfvars.tf", 'w') { |file| file.write(vars_file) }
  end
  # Pass some extra vars for Terraform
  Idlc::Deploy::Config.add_deployment_var('aws_region', SETTINGS['AWS_REGION'])
  Idlc::Deploy::Config.add_deployment_var('environment_key', env_metadata['environment_key'])
  Idlc::Deploy::Config.add_deployment_var('version', env_metadata['environment']['inf_version'])
  Idlc::Deploy::Config.add_deployment_var('app_release', @config[:app_release])
  Idlc::Deploy::Config.add_deployment_var('encrypted_svc_pass', env_metadata['environment']['encrypted']['svc_pass'])
  ENV['APP_RELEASE'] = @config[:app_release]
  FileUtils.mkdir_p "inf/env/kp"
  download_private_key(env_metadata['account']['tfstate_bucket'], env_metadata['environment_key'], "inf/env/kp")
  Idlc::Deploy::Keypair.generate("inf/env/kp")
  if upgraded_terraform?
    FileUtils.ln_sf("../inf", "package-infra/inf")
    FileUtils.ln_sf("../terraform.auto.tfvars", "package-infra/svc_pass.auto.tfvars")
  end
  config = Idlc::Deploy::Config.new(SETTINGS['AWS_REGION'])
  config.configure_state(
    env_metadata['account']['tfstate_bucket'],
    env_metadata['environment_key'],
    @config[:working_dir]
  )
end