-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
287 lines (243 loc) · 6.74 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# frozen_string_literal: true
require 'confidante'
require 'git'
require 'os'
require 'rake_circle_ci'
require 'rake_git'
require 'rake_git_crypt'
require 'rake_github'
require 'rake_gpg'
require 'rake_ssh'
require 'rake_terraform'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'securerandom'
require 'semantic'
require 'yaml'
require_relative 'lib/paths'
require_relative 'lib/version'
configuration = Confidante.configuration
def repo
Git.open(Pathname.new('.'))
end
def latest_tag
repo.tags.map do |tag|
Semantic::Version.new(tag.name)
end.max
end
def tmpdir
base = (ENV.fetch('TMPDIR', nil) || '/tmp')
OS.osx? ? "/private#{base}" : base
end
task default: %i[
test:code:fix
test:unit
test:integration
]
RakeTerraform.define_installation_tasks(
path: File.join(Dir.pwd, 'vendor', 'terraform'),
version: '1.3.6'
)
RakeGitCrypt.define_standard_tasks(
namespace: :git_crypt,
provision_secrets_task_name: :'secrets:provision',
destroy_secrets_task_name: :'secrets:destroy',
install_commit_task_name: :'git:commit',
uninstall_commit_task_name: :'git:commit',
gpg_user_key_paths: %w[
config/gpg
config/secrets/ci/gpg.public
]
)
namespace :git do
RakeGit.define_commit_task(
argument_names: [:message]
) do |t, args|
t.message = args.message
end
end
namespace :encryption do
namespace :directory do
desc 'Ensure CI secrets directory exists.'
task :ensure do
FileUtils.mkdir_p('config/secrets/ci')
end
end
namespace :passphrase do
desc 'Generate encryption passphrase for CI GPG key'
task generate: ['directory:ensure'] do
File.write(
'config/secrets/ci/encryption.passphrase',
SecureRandom.base64(36)
)
end
end
end
namespace :keys do
namespace :deploy do
RakeSSH.define_key_tasks(
path: 'config/secrets/ci/',
comment: '[email protected]'
)
end
namespace :secrets do
namespace :gpg do
RakeGPG.define_generate_key_task(
output_directory: 'config/secrets/ci',
name_prefix: 'gpg',
owner_name: 'InfraBlocks Maintainers',
owner_email: '[email protected]',
owner_comment: 'terraform-google-project-hierarchy CI Key'
)
end
task generate: ['gpg:generate']
end
end
namespace :secrets do
namespace :directory do
desc 'Ensure secrets directory exists and is set up correctly'
task :ensure do
FileUtils.mkdir_p('config/secrets')
unless File.exist?('config/secrets/.unlocked')
File.write('config/secrets/.unlocked', 'true')
end
end
end
desc 'Generate all generatable secrets.'
task generate: %w[
directory:ensure
encryption:passphrase:generate
keys:deploy:generate
keys:secrets:generate
]
desc 'Provision all secrets.'
task provision: [:generate]
desc 'Delete all secrets.'
task :destroy do
rm_rf 'config/secrets'
end
desc 'Rotate all secrets.'
task rotate: [:'git_crypt:reinstall']
end
RakeCircleCI.define_project_tasks(
namespace: :circle_ci,
project_slug: 'github/infrablocks/terraform-google-project-hierarchy'
) do |t|
circle_ci_config =
YAML.load_file('config/secrets/circle_ci/config.yaml')
t.api_token = circle_ci_config['circle_ci_api_token']
t.environment_variables = {
ENCRYPTION_PASSPHRASE:
File.read('config/secrets/ci/encryption.passphrase')
.chomp,
CIRCLECI_API_KEY:
YAML.load_file(
'config/secrets/circle_ci/config.yaml'
)['circle_ci_api_token']
}
t.checkout_keys = []
t.ssh_keys = [
{
hostname: 'github.com',
private_key: File.read('config/secrets/ci/ssh.private')
}
]
end
RakeGithub.define_repository_tasks(
namespace: :github,
repository: 'infrablocks/terraform-google-project-hierarchy'
) do |t|
github_config =
YAML.load_file('config/secrets/github/config.yaml')
t.access_token = github_config['github_personal_access_token']
t.deploy_keys = [
{
title: 'CircleCI',
public_key: File.read('config/secrets/ci/ssh.public')
}
]
end
namespace :pipeline do
desc 'Prepare CircleCI Pipeline'
task prepare: %i[
circle_ci:env_vars:ensure
circle_ci:checkout_keys:ensure
circle_ci:ssh_keys:ensure
github:deploy_keys:ensure
]
end
RuboCop::RakeTask.new
namespace :test do
namespace :code do
desc 'Run all checks on the test code'
task check: [:rubocop]
desc 'Attempt to automatically fix issues with the test code'
task fix: [:'rubocop:autocorrect_all']
end
desc 'Run module unit tests'
RSpec::Core::RakeTask.new(unit: ['terraform:ensure']) do |t|
t.pattern = 'spec/unit/**{,/*/**}/*_spec.rb'
t.rspec_opts = '-I spec/unit'
ENV['GOOGLE_CLOUD_PROJECT'] = configuration.gcp_project_id
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = configuration.gcp_credentials
end
desc 'Run module integration tests'
RSpec::Core::RakeTask.new(integration: ['terraform:ensure']) do |t|
t.pattern = 'spec/integration/**{,/*/**}/*_spec.rb'
t.rspec_opts = '-I spec/integration'
ENV['GOOGLE_CLOUD_PROJECT'] = configuration.gcp_project_id
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = configuration.gcp_credentials
end
end
namespace :deployment do
namespace :prerequisites do
RakeTerraform.define_command_tasks(
configuration_name: 'prerequisites',
argument_names: [:seed]
) do |t, args|
deployment_configuration =
configuration
.for_scope(role: :prerequisites)
.for_overrides(args.to_h)
t.source_directory = 'spec/unit/infra/prerequisites'
t.work_directory = 'build/infra'
t.state_file = deployment_configuration.state_file
t.vars = deployment_configuration.vars
end
end
namespace :root do
RakeTerraform.define_command_tasks(
configuration_name: 'root',
argument_names: [:seed]
) do |t, args|
deployment_configuration =
configuration
.for_scope(role: :root)
.for_overrides(args.to_h)
t.source_directory = 'spec/unit/infra/root'
t.work_directory = 'build/infra'
t.environment = {
'GOOGLE_APPLICATION_CREDENTIALS' =>
deployment_configuration.gcp_credentials
}
t.state_file = deployment_configuration.state_file
t.vars = deployment_configuration.vars
end
end
end
namespace :version do
desc 'Bump version for specified type (pre, major, minor, patch)'
task :bump, [:type] do |_, args|
next_tag = latest_tag.send("#{args.type}!")
repo.add_tag(next_tag.to_s)
repo.push('origin', 'main', tags: true)
puts "Bumped version to #{next_tag}."
end
desc 'Release module'
task :release do
next_tag = latest_tag.release!
repo.add_tag(next_tag.to_s)
repo.push('origin', 'main', tags: true)
puts "Released version #{next_tag}."
end
end