-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
214 lines (166 loc) · 6.9 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
# frozen_string_literal: true
require 'rake_leiningen'
require 'rake_terraform'
require 'rake_vault'
require 'vault'
require 'yaml'
require 'rake_fly'
require 'uri'
require_relative 'lib/leiningen_task_set'
task default: %i[library:check library:test:unit]
RakeFly.define_installation_tasks(version: '7.9.0')
RakeLeiningen.define_installation_tasks(
version: '2.10.0'
)
RakeVault.define_installation_tasks(
path: File.join(Dir.pwd, 'vendor', 'vault'),
version: '1.11.2'
)
namespace :vault do
RakeVault.define_login_task(
argument_names: [:role, :address],
) do |t, args|
t.address = args[:address]
t.role = args[:role] || 'read-only'
end
end
namespace :library do
define_check_tasks(fix: true)
namespace :test do
RakeLeiningen.define_test_task(
name: :unit,
type: 'unit',
profile: 'test')
end
namespace :publish do
RakeLeiningen.define_release_task(
name: :release,
profile: 'release') do |t|
t.environment = {
'VERSION' => ENV['VERSION'],
'CLOJARS_DEPLOY_USERNAME' => ENV['CLOJARS_DEPLOY_USERNAME'],
'CLOJARS_DEPLOY_TOKEN' => ENV['CLOJARS_DEPLOY_TOKEN']
}
end
end
desc 'Lint all src files'
task :lint do
puts "Running clj-kondo from ./scripts/lint for " + RUBY_PLATFORM
platform_prefix = /darwin/ =~ RUBY_PLATFORM ? "mac" : "linux"
sh("./scripts/lint/clj-kondo-2021-06-18-#{platform_prefix}",
"--lint", "src/")
puts "Finished running clj-kondo"
end
desc 'Reformat all src files'
task :format do
puts "Running cljstyle from ./scripts/lint for " + RUBY_PLATFORM
platform_prefix = /darwin/ =~ RUBY_PLATFORM ? "mac" : "linux"
sh("./scripts/lint/cljstyle-0-15-0-#{platform_prefix}", "fix")
puts "Finished running cljstyle"
end
task :optimise do
puts 'skipping optimise...'
end
task :idiomise do
puts 'skipping idiomise...'
end
end
namespace :template do
desc 'Populate this template based on the provided parameters.'
task :populate, [:vault_address, :library_name] do |t, args|
library_name = args.library_name.downcase
vault_address = args.vault_address
Rake::Task[:'template:vault'].invoke(vault_address, library_name)
directory_location = "../#{library_name}"
snakecased_name = library_name.underscore
if File.exists?(directory_location)
puts "ERROR: Target directory already exists. Exiting."
exit 1
end
puts
puts "Copying to target directory."
mkdir_p(directory_location)
cp_r("./", directory_location)
puts
puts "Removing old .git directory"
remove_dir(directory_location + "/.git", force = false)
puts
puts "Applying replacements to file contents."
Dir["#{directory_location}/**/*"].each do |path|
next if File.directory?(path)
contents = File.read(path)
contents = contents.gsub("library-template", library_name)
File.open(path, 'w') { |f| f.write(contents) }
end
puts
puts "Renaming directories."
Dir["#{directory_location}/**/*"].each do |path|
if File.directory?(path) && path.match?(/library_template$/)
mv(path, path.gsub(/library_template$/, snakecased_name))
end
end
puts
puts "Setting up git."
Rake::Task[:'template:git'].invoke
puts
puts "Done."
puts "Now manually:"
puts " - Update the README.md"
puts " - Update the project.clj"
puts " - Push to git"
puts " - Push a new pipeline to Concourse"
end
desc 'Copy vault secrets from the library template'
task :vault, [:vault_address, :library_name] do |t, args|
Rake::Task[:'vault:login'].invoke('kv-admin', args[:vault_address])
token = File.read(File.expand_path('~/.vault-token'))
puts args.vault_address
vault_client = Vault::Client.new(address: args.vault_address, token: token)
template_base_path = 'library-template'
library_name = args.library_name.downcase
puts 'copying value for clojars_deploy_username from library-template'
clojars_deploy_username = vault_client.kv('kv').read("#{template_base_path}/clojars_deploy_username").data[:value]
vault_client.kv('kv').write("#{library_name}/clojars_deploy_username", value: clojars_deploy_username)
puts 'copying value for clojars_deploy_token from library-template'
clojars_deploy_token = vault_client.kv('kv').read("#{template_base_path}/clojars_deploy_token").data[:value]
vault_client.kv('kv').write("#{library_name}/clojars_deploy_token", value: clojars_deploy_token)
puts 'copying value for application_builder_image_repository_url from library-template'
application_builder_image_repository_url = vault_client.kv('kv').read("#{template_base_path}/application_builder_image_repository_url").data[:value]
vault_client.kv('kv').write("#{library_name}/application_builder_image_repository_url", value: application_builder_image_repository_url)
puts 'copying value for application_builder_image_tag from library-template'
application_builder_image_tag = vault_client.kv('kv').read("#{template_base_path}/application_builder_image_tag").data[:value]
vault_client.kv('kv').write("#{library_name}/application_builder_image_tag", value: application_builder_image_tag)
puts 'copying value for slack_failure_channel from library-template'
slack_failure_channel = vault_client.kv('kv').read("#{template_base_path}/slack_failure_channel").data[:value]
vault_client.kv('kv').write("#{library_name}/slack_failure_channel", value: slack_failure_channel)
puts 'copying value for slack_failure_message from library-template'
slack_failure_message = vault_client.kv('kv').read("#{template_base_path}/slack_failure_message").data[:value]
vault_client.kv('kv').write("#{library_name}/slack_failure_message", value: slack_failure_message)
puts 'copying value for slack_success_channel from library-template'
slack_success_channel = vault_client.kv('kv').read("#{template_base_path}/slack_success_channel").data[:value]
vault_client.kv('kv').write("#{library_name}/slack_success_channel", value: slack_success_channel)
puts 'copying value for slack_success_message from library-template'
slack_success_message = vault_client.kv('kv').read("#{template_base_path}/slack_success_message").data[:value]
vault_client.kv('kv').write("#{library_name}/slack_success_message", value: slack_success_message)
puts 'copied vault secrets'
end
desc 'Initialises git'
task :git do
puts "Initialising Git..."
`./scripts/init/git.sh`
puts "Done."
puts
end
end
namespace :ci do
RakeFly.define_project_tasks(
pipeline: 'flagsmith-clj',
argument_names: [:concourse_url],
backend: RakeFly::Tasks::Authentication::Login::FlyBackend
) do |t, args|
t.concourse_url = args[:concourse_url]
t.config = "pipelines/pipeline.yaml"
t.non_interactive = true
t.home_directory = 'build/fly'
end
end