Class: RailsTemplate18f::Generators::TerraformGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Base, CloudGovOptions
Defined in:
lib/generators/rails_template18f/terraform/terraform_generator.rb

Instance Method Summary collapse

Instance Method Details

#ignore_filesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/rails_template18f/terraform/terraform_generator.rb', line 58

def ignore_files
  unless skip_git?
    append_to_file ".gitignore", "\n      # Terraform\n      .terraform.lock.hcl\n      **/.terraform/*\n      secrets.*.tfvars\n      env_vars.auto.tfvars\n      terraform.tfstate\n      terraform.tfstate.backup\n      terraform/dist\n    EOM\n  end\nend\n"

#installObject



18
19
20
21
# File 'lib/generators/rails_template18f/terraform/terraform_generator.rb', line 18

def install
  directory "terraform", mode: :preserve
  chmod "terraform/terraform.sh", 0o755
end

#install_bootstrapObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/rails_template18f/terraform/terraform_generator.rb', line 23

def install_bootstrap
  if use_gitlab_backend?
    directory "gitlab_bootstrap", "terraform/bootstrap", mode: :preserve
  elsif use_s3_backend?
    directory "s3_bootstrap/common", "terraform/bootstrap", mode: :preserve
    if terraform_manage_spaces?
      template "s3_bootstrap/full/main.tf", "terraform/bootstrap/main.tf"
      copy_file "s3_bootstrap/full/imports.tf.tftpl", "terraform/bootstrap/templates/imports.tf.tftpl"
    else
      template "s3_bootstrap/sandbox/main.tf", "terraform/bootstrap/main.tf"
      copy_file "s3_bootstrap/sandbox/imports.tf.tftpl", "terraform/bootstrap/templates/imports.tf.tftpl"
    end
  else
    remove_dir "terraform/.shadowenv.d"
  end
  unless terraform_manage_spaces?
    remove_file "terraform/bootstrap/users.auto.tfvars"
    remove_file "terraform/production.env.tfvars"
  end
end

#install_githookObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/generators/rails_template18f/terraform/terraform_generator.rb', line 79

def install_githook
  githook_file = ".githooks/pre-commit"
  if File.exist?(File.expand_path(githook_file, destination_root))
    append_to_file githook_file, "\n#{githook_content}"
  else
    create_file githook_file, "      #! /usr/bin/env bash\n      #\n      # This hook runs on `git commit` and will prevent you from committing without\n      # approval from the linter and tests.\n      #\n      # To run, this file must be symlinked to:\n      # .git/hooks/pre-commit\n      #\n      # To bypass this hook, run:\n      # $ git commit --no-verify\n      # $ git commit -n\n\n      \#{githook_content}\n    EOM\n    chmod githook_file, 0o755\n  end\nend\n"

#install_shadowenvObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/rails_template18f/terraform/terraform_generator.rb', line 44

def install_shadowenv
  unless use_local_backend?
    append_to_file "Brewfile", "\n      # shadowenv for loading terraform backend secrets\n      brew \"shadowenv\"\n    EOB\n    insert_into_file \"README.md\", indent(<<~EOR), after: /\\* Install homebrew dependencies: `brew bundle`\\n/\n      * [shadowenv](https://shopify.github.io/shadowenv/)\n        * See the [quick start](https://shopify.github.io/shadowenv/getting-started/#add-to-your-shell-profile) for instructions on loading shadowenv in your shell\n    EOR\n  end\nend\n"

#update_readmeObject



74
75
76
77
# File 'lib/generators/rails_template18f/terraform/terraform_generator.rb', line 74

def update_readme
  gsub_file "README.md", /^(### Automatic linting)\s*$/, '\1 and terraform formatting'
  gsub_file "README.md", /(ruby linting) (on every)/, '\1 and terraform formatting \2'
end