-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
173 lines (118 loc) · 4.03 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
require 'fileutils'
require 'rspec/core/rake_task'
require 'tmpdir'
BUCKET_REGION = 'eu-west-1'
BUCKET_NAME = "net.dean-wilson-terraform-state-#{BUCKET_REGION}-#{ENV['DEPLOY_ENV']}"
ALLOWED_ENVIRONMENTS = %w(test staging production)
##### You should not need to edit anything under here
PROJECT_DIR = 'projects'.freeze
desc 'Validate the environment and required variables'
task :validate_environment do
unless ENV.include?('DEPLOY_ENV') && ALLOWED_ENVIRONMENTS.include?(ENV['DEPLOY_ENV'])
warn "Please set 'DEPLOY_ENV' environment variable to one of #{ALLOWED_ENVIRONMENTS.join(', ')}"
exit 1
end
unless ENV.include?('PROJECT_NAME')
warn 'Please set the "PROJECT_NAME" environment variable.'
exit 1
end
unless project_name.empty?
unless File.exist? File.join(PROJECT_DIR, project_name)
warn "Unable to find project #{project_name} in #{PROJECT_DIR}"
exit 1
end
end
end
desc 'Check for a local statefile'
task :local_state_check do
state_file = 'terraform.tfstate'
if File.exist? state_file
warn "Local state file (#{state_file}) should not exist. We use remote state files."
exit 1
end
end
desc 'Purge remote state file'
task :purge_remote_state do
state_file = '.terraform/terraform.tfstate'
FileUtils.rm state_file if File.exist? state_file
if File.exist? state_file
warn "state file #{state_file} should not exist."
exit 1
end
end
desc 'Configure the remote state location'
task configure_state: [:validate_environment, :purge_remote_state] do
args = []
args << 'terraform remote config'
args << '-backend=s3'
args << '-backend-config="acl=private"'
args << "-backend-config='bucket=#{BUCKET_NAME}'"
args << '-backend-config="encrypt=true"'
args << "-backend-config='key=terraform-#{project_name}.tfstate'"
args << "-backend-config='region=#{BUCKET_REGION}'"
system(args.join(' ')) or raise 'Error running Terraform to configure state'
end
desc 'Show the terraform plan'
task plan: [:configure_state] do
tmp_dir = _flatten_project
system("terraform plan -module-depth=-1 #{common_args} #{tmp_dir}")
FileUtils.rm_r tmp_dir unless debug
end
desc 'Apply the terraform resources'
task apply: [:configure_state] do
tmp_dir = _flatten_project
command = "terraform apply #{common_args} #{tmp_dir}"
puts command
system(command)
FileUtils.rm_r tmp_dir unless debug
end
desc 'Destroy terraform resources'
task destroy: [:configure_state] do
tmp_dir = _flatten_project
command = "terraform destroy #{common_args} #{tmp_dir}"
puts command
system(command)
FileUtils.rm_r tmp_dir unless debug
end
desc 'create and display the resource graph'
task graph: [:configure_state] do
tmp_dir = _flatten_project
system("terraform graph #{tmp_dir} | dot -Tpng > graph.png")
FileUtils.rm_r tmp_dir unless debug
end
desc 'Run the given projects awsspec tests'
RSpec::Core::RakeTask.new('spec') do |task|
spec_dir = File.join(PROJECT_DIR, project_name, 'spec')
base_specs = Dir["#{spec_dir}/*_spec.rb"]
environment_specs = Dir["#{spec_dir}/#{deploy_env}/*_spec.rb"]
all_specs = base_specs + environment_specs
task.pattern = all_specs.join(',')
end
# Terraform doesn't let you have .tf files in nested subdirectories so
# _flatten_project grabs all the terraform files we want in our run
# and combines them in to a single directory.
def _flatten_project
tmp_dir = Dir.mktmpdir('tf-temp')
base_path = File.join(PROJECT_DIR, project_name, 'resources')
# add an inner loop here if we want to copy other file extensions too
['configs', base_path, "#{base_path}/#{deploy_env}"].each do |dir|
next if Dir["#{dir}/*.tf"].empty?
puts "Working on #{Dir[dir + '/*.tf']}" if debug
system("terraform get #{dir}")
FileUtils.cp(Dir["#{dir}/*.tf"], tmp_dir)
end
tmp_dir
end
########################### Util functions
def debug
ENV['DEBUG']
end
def deploy_env
ENV['DEPLOY_ENV']
end
def project_name
ENV['PROJECT_NAME']
end
def common_args
" -var-file=variables/common.tfvars -var-file=variables/#{deploy_env}.tfvars "
end