-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcivicops.rb
440 lines (344 loc) · 10.9 KB
/
civicops.rb
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
require 'open-uri'
require 'securerandom'
@app_name = app_name.gsub('_', '-')
def main
welcome_message
set_environment_variables
configure_postgres
configure_timezone
configure_github
web_stack if yes?('> Web stack?', :green)
devops_stack if yes?('> DevOps stack?', :green)
code_analysis_stack if yes?('> Code analysis stack?', :green)
tests_stack if yes?('> Tests stack?', :green)
setup_sidekiq if yes?('> Configure Sidekiq + Redis?', :green)
file_upload_to_aws if yes?('> Carrierwave + AWS S3?', :green)
setup_aws_ses if yes?('> Send mail with AWS SES?', :green)
setup_recaptcha if yes?('> Setup reCAPTCHA?', :green)
clean_gemfile
finish_message
end
def welcome_message
message = <<~MESSAGE
===========================================
____ _ _ ___
/ ___(_)_ _(_) ___ / _ \\ _ __ ___
| | | \\ \\ / / |/ __| | | | '_ \\/ __|
| |___| |\\ V /| | (__| |_| | |_) \\__ \\
\\____|_| \\_/ |_|\\___|\\___/| .__/|___/
|_|
===========================================
® Cívica Digital 2017
===========================================
MESSAGE
say message, :blue
end
def set_environment_variables
say 'Setting default environment variables...', :yellow
environment_variables = <<~CONFIG
AWS_ACCESS_KEY=changeme
AWS_REGION=us-east-1
AWS_SECRET_KEY=changeme
RAILS_LOG_TO_STDOUT=true
RAILS_SERVE_STATIC_FILES=true
SECRET_KEY_BASE=#{SecureRandom.hex(64)}
CONFIG
create_file 'deploy/staging/provisions/environment', environment_variables
end
def clean_gemfile
say 'Cleaning the Gemfile...', :yellow
gsub_file('Gemfile', /^\s*#.*\n/, '') # Remove commented lines
gsub_file('Gemfile', /^\n\n/, '') # Remove double newlines
gsub_file('Gemfile', /gem 'tzinfo.*/, '') # Remove tzinfo-data gem
end
def configure_postgres
say 'Configuring PostgreSQL...', :yellow
gsub_file('Gemfile', /sqlite3/, 'pg') # Use PostgreSQL instead of SQLite
gem 'pg'
environment_variables = <<~CONFIG
DATABASE_URL=postgresql://postgres@db/#{@app_name}_production
CONFIG
remove_file 'config/database.yml.example'
download 'config/database.yml'
append_to_file 'deploy/staging/provisions/environment', environment_variables
end
def configure_timezone
say 'Configuring Timezone...', :yellow
environment 'config.time_zone = "Mexico City"'
end
def configure_github
say 'Configuring GitHub...', :yellow
download '.github/settings.yml'
end
def web_stack
gem 'bourbon'
gem 'font-awesome-rails'
gem 'haml'
gem 'haml-rails'
gem 'jquery-rails'
gem 'neat'
gem 'sass-rails'
gem_group :development do
gem 'better_errors'
gem 'binding_of_caller'
end
download 'config/initializers/better_errors.rb'
end
def devops_stack
gem 'commit_hash'
gem 'wait_pg'
gem 'health_check'
gem 'newrelic_rpm'
gem 'rollbar'
gem 'timber'
download 'config/newrelic.yml'
download 'config/initializers/health_check.rb'
download 'config/initializers/rollbar.rb'
download 'config/initializers/timber.rb'
download '.gitignore'
download 'Makefile'
timber_config_development = <<~CONFIG
# Install the Timber.io logger, but do not send logs.
logger = Timber::Logger.new(nil)
logger.level = config.log_level
config.logger = ActiveSupport::TaggedLogging.new(logger)
CONFIG
timber_config_production = <<~CONFIG
# Install the Timber.io logger, send logs over HTTP or STDOUT
if ENV['TIMBER_API_KEY'].present?
log_device = Timber::LogDevices::HTTP.new(ENV['TIMBER_API_KEY'])
else
log_device = STDOUT
end
logger = Timber::Logger.new(log_device)
logger.level = config.log_level
config.logger = ActiveSupport::TaggedLogging.new(logger)
CONFIG
full_errors_optional = <<~CONFIG
config.consider_all_requests_local = ENV.fetch('FULL_ERROR_REPORTS') { false }
CONFIG
environment_variables = <<~CONFIG
NEW_RELIC_ENV=staging
NEW_RELIC_LICENSE_KEY=changeme
ROLLBAR_ACCESS_TOKEN=changeme
ROLLBAR_ENV=staging
TIMBER_API_KEY=changeme
FULL_ERROR_REPORTS=true
CONFIG
environment timber_config_development, env: 'development'
environment timber_config_production, env: 'production'
environment full_errors_optional, env: 'production'
append_to_file 'deploy/staging/provisions/environment', environment_variables
docker
jenkins
terraform
configure_git_crypt
end
def code_analysis_stack
gem_group :development do
gem 'brakeman', require: false
gem 'bundler-audit', require: false
gem 'rails_best_practices', require: false
gem 'reek', require: false
gem 'rubocop', require: false
gem 'rubocop-rspec', require: false
end
gem_group :test do
gem 'bullet'
end
download 'config/initializers/bullet.rb'
download 'config/rails_best_practices.yml'
download '.reek'
download '.rubocop.yml'
end
def tests_stack
say 'Setting up RSpec and Factory Bot...', :yellow
gem_group :development, :test do
gem 'dotenv-rails'
gem 'factory_bot_rails'
gem 'pry-rails'
gem 'rspec-rails'
end
say 'Use `json_body` to access the response body in tests.', :yellow
download 'spec/support/api_helper.rb'
say 'Use the FactoryBot methods without the namespace.', :yellow
download 'spec/support/factory_bot.rb'
end
def docker
say 'Configuring Docker...', :yellow
download '.dockerignore'
download 'docker-compose.yml'
download 'Dockerfile'
download 'Dockerfile.dev'
end
def jenkins
say 'Configuring Jenkins...', :yellow
download 'Jenkinsfile'
end
def terraform
say 'Adding scripts to setup the server...', :yellow
download 'deploy/staging/scripts/setup-server.sh'
download 'deploy/staging/scripts/update-container.sh'
say 'Configuring Terraform to deploy a staging environment...', :yellow
download 'deploy/staging/provisions/docker-compose.yml'
download 'deploy/staging/main.tf'
download 'deploy/staging/provisions/traefik.toml'
end
def file_upload_to_aws
gem 'carrierwave'
gem 'fog-aws'
aws_bucket_config = <<~CONFIG
# ----------------------------------------------------------------------
# File storage (S3)
# ----------------------------------------------------------------------
resource "aws_s3_bucket" "file-storage" {
bucket = "${var.project_name}-staging"
acl = "private"
tags {
Environment = "staging"
}
lifecycle {
prevent_destroy = true
}
}
resource "aws_iam_user_policy" "s3" {
name = "${var.project_name}-S3"
user = "${aws_iam_user.project.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"${aws_s3_bucket.file-storage.arn}",
"${aws_s3_bucket.file-storage.arn}/*"
]
}
]
}
EOF
}
CONFIG
bucket_output = <<~CONFIG
output "bucket" {
value = "${aws_s3_bucket.file-storage.bucket}"
}
CONFIG
environment_variables = <<~CONFIG
AWS_S3_BUCKET=changeme
CONFIG
download 'config/initializers/carrierwave.rb'
insert_into_file 'deploy/staging/main.tf', aws_bucket_config, before: '# Output'
append_to_file 'deploy/staging/main.tf', bucket_output
append_to_file 'deploy/staging/provisions/environment', environment_variables
end
def setup_aws_ses
gem 'aws-ses', require: 'aws/ses'
ses_policy = <<~CONFIG
# ----------------------------------------------------------------------
# Email provider (SES)
# ----------------------------------------------------------------------
resource "aws_iam_user_policy" "ses" {
name = "${var.project_name}-SES"
user = "${aws_iam_user.project.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ses:*",
"Resource": "*"
}
]
}
EOF
}
CONFIG
environment_variables = <<~CONFIG
EMAIL_FROM=noreply+#{@app_name}@civica.digital
CONFIG
mail = "ENV.fetch('EMAIL_FROM') { '[email protected]' }"
application_mailer = " default from: #{mail}"
mailer_config = " config.mailer_sender = #{mail}"
download 'config/initializers/mailer.rb'
environment 'config.action_mailer.delivery_method = :ses', env: 'production'
insert_into_file 'deploy/staging/main.tf', ses_policy, before: '# Output'
insert_into_file 'config/initializers/devise.rb', mailer_config, before: ' # ==> ORM configuration'
append_to_file 'deploy/staging/provisions/environment', environment_variables
append_to_file 'app/mailers/application_mailer.rb', application_mailer, after: 'layout "mailer"'
end
def setup_recaptcha
gem 'recaptcha', require: 'recaptcha/rails'
download 'config/initializers/recaptcha.rb'
end
def setup_sidekiq
gem 'redis'
gem 'sidekiq'
db_volume = ' db_data: {}'
# Indentation is important, please, respect it
redis_volume = "\n redis: {}"
redis_service = <<-YML
redis:
command: redis-server --appendonly no --save ""
image: redis:3.2-alpine
volumes:
- redis:/var/lib/redis/data
YML
sidekiq_service = <<-YML
sidekiq:
<<: *web
command: bundle exec sidekiq
depends_on:
- redis
YML
download 'config/initializers/sidekiq.rb'
insert_into_file 'docker-compose.yml', redis_volume, after: db_volume
insert_into_file 'docker-compose.yml', redis_service, after: 'services:'
append_to_file 'docker-compose.yml', sidekiq_service
insert_into_file 'deploy/staging/provisions/docker-compose.yml', redis_volume, after: db_volume
insert_into_file 'deploy/staging/provisions/docker-compose.yml', redis_service, after: 'services:'
append_to_file 'deploy/staging/provisions/docker-compose.yml', sidekiq_service
environment_variables = <<~CONFIG
REDIS_URL=redis://redis:6379
CONFIG
append_to_file 'deploy/staging/provisions/environment', environment_variables
end
def download(file, &block)
repository = 'https://raw.githubusercontent.com/civica-digital/civic-generator'
source = "#{repository}/master/share/#{file}"
render = open(source) { |input| input.binmode.read }
render.gsub!('{{project_name}}', @app_name)
create_file file, render
end
def configure_git_crypt
say 'Configuring git-crypt...', :yellow
team_members = %w(
abisosa
fercreek
mikesaurio
mroutis
rafaelcr
ricalanis
)
`git-crypt init`
team_members.each { |user| `curl https://keybase.io/#{user}/pgp_keys.asc | gpg --import` }
team_members.each { |user| `git-crypt add-gpg-user --trusted #{user[0..2]}` }
gitattributes = <<~CONF
**/provisions/** filter=git-crypt diff=git-crypt
CONF
create_file '.gitattributes', gitattributes
end
def finish_message
message = <<~MESSAGE
===========================================
Awesome!
Setup your local development environment
with Docker using the command: `make dev`
===========================================
MESSAGE
say message, :blue
end
main