modulorails 0.2.2 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.dockerignore +14 -0
  3. data/.gitignore +3 -0
  4. data/.rubocop.yml +62 -0
  5. data/.travis.yml +23 -4
  6. data/Appraisals +18 -0
  7. data/CHANGELOG.md +42 -1
  8. data/Dockerfile.ruby25 +34 -0
  9. data/Dockerfile.ruby26 +28 -0
  10. data/Dockerfile.ruby27 +25 -0
  11. data/Dockerfile.ruby30 +25 -0
  12. data/Dockerfile.ruby31 +25 -0
  13. data/Gemfile.lock +59 -54
  14. data/README.md +16 -4
  15. data/Rakefile +1 -1
  16. data/app/assets/stylesheets/modulorails.css +21 -0
  17. data/app/helpers/modulorails/application_helper.rb +8 -0
  18. data/config/locales/en.yml +3 -0
  19. data/docker-compose.debug.yml +47 -0
  20. data/docker-compose.yml +37 -0
  21. data/entrypoints/appraisal_test.sh +7 -0
  22. data/gemfiles/rails_52.gemfile +9 -0
  23. data/gemfiles/rails_60.gemfile +9 -0
  24. data/gemfiles/rails_61.gemfile +9 -0
  25. data/gemfiles/rails_70.gemfile +9 -0
  26. data/lib/generators/modulorails/docker/docker_generator.rb +19 -0
  27. data/lib/generators/modulorails/docker/templates/Dockerfile.prod.tt +57 -0
  28. data/lib/generators/modulorails/docker/templates/Dockerfile.tt +26 -0
  29. data/lib/generators/modulorails/docker/templates/config/database.yml.tt +32 -0
  30. data/lib/generators/modulorails/docker/templates/docker-compose.prod.yml.tt +50 -0
  31. data/lib/generators/modulorails/docker/templates/docker-compose.yml.tt +71 -0
  32. data/lib/generators/modulorails/docker/templates/entrypoints/docker-entrypoint.sh.tt +20 -0
  33. data/lib/generators/modulorails/docker/templates/entrypoints/webpack-entrypoint.sh.tt +7 -0
  34. data/lib/generators/modulorails/gitlabci/gitlabci_generator.rb +34 -0
  35. data/lib/generators/modulorails/gitlabci/templates/.gitlab-ci.yml.tt +118 -0
  36. data/lib/generators/modulorails/gitlabci/templates/.modulorails-gitlab-ci +6 -0
  37. data/lib/generators/modulorails/gitlabci/templates/config/database-ci.yml.tt +8 -0
  38. data/lib/generators/modulorails/healthcheck/health_check_generator.rb +41 -0
  39. data/lib/generators/modulorails/healthcheck/templates/.modulorails-health_check +6 -0
  40. data/lib/generators/modulorails/healthcheck/templates/config/initializers/health_check.rb.tt +100 -0
  41. data/lib/generators/modulorails/rubocop/rubocop_generator.rb +24 -0
  42. data/lib/generators/modulorails/rubocop/templates/rubocop.yml.tt +18 -0
  43. data/lib/generators/modulorails/self_update/self_update_generator.rb +32 -0
  44. data/lib/generators/modulorails/service/service_generator.rb +13 -0
  45. data/lib/generators/modulorails/service/templates/service.rb.tt +28 -0
  46. data/lib/modulorails/configuration.rb +8 -2
  47. data/lib/modulorails/data.rb +21 -2
  48. data/lib/modulorails/error_data.rb +21 -0
  49. data/lib/modulorails/errors/base_error.rb +4 -0
  50. data/lib/modulorails/errors/errors.rb +3 -0
  51. data/lib/modulorails/errors/invalid_format_error.rb +14 -0
  52. data/lib/modulorails/errors/invalid_value_error.rb +14 -0
  53. data/lib/modulorails/railtie.rb +35 -3
  54. data/lib/modulorails/services/base_service.rb +203 -0
  55. data/lib/modulorails/services/logs_for_method_service.rb +42 -0
  56. data/lib/modulorails/services/services.rb +2 -0
  57. data/lib/modulorails/success_data.rb +17 -0
  58. data/lib/modulorails/validators/database_configuration.rb +9 -3
  59. data/lib/modulorails/version.rb +4 -1
  60. data/lib/modulorails.rb +46 -21
  61. data/modulorails.gemspec +4 -0
  62. metadata +114 -17
  63. data/lib/generators/gitlabci_generator.rb +0 -134
  64. data/lib/generators/templates/.gitlab-ci.yml +0 -72
  65. data/lib/generators/templates/.modulorails-gitlab-ci +0 -3
  66. data/lib/generators/templates/config/database-ci.yml +0 -7
  67. data/lib/modulorails/updater.rb +0 -46
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ # Exit immediately if a command exits with a non-zero status
4
+ set -e
5
+
6
+ # Launch the tests
7
+ bundle exec appraisal rake spec
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rails", "~> 5.2", ">= 5.2.6"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rails", "~> 6.0", ">= 6.0.4.4"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rails", "~> 6.1", ">= 6.1.4.4"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
7
+ gem "rails", "~> 7.0"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ class Modulorails::DockerGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+ desc 'This generator creates Dockerfiles for an app'
8
+
9
+ def create_config_file
10
+ template 'Dockerfile'
11
+ template 'Dockerfile.prod'
12
+ template 'docker-compose.yml'
13
+ template 'docker-compose.prod.yml'
14
+ template 'entrypoints/docker-entrypoint.sh'
15
+ template 'config/database.yml'
16
+ rescue StandardError => e
17
+ $stderr.puts("[Modulorails] Error: cannot generate Docker configuration: #{e.message}")
18
+ end
19
+ end
@@ -0,0 +1,57 @@
1
+ # BUILD IMAGE
2
+ FROM ruby:<%= Modulorails.data.ruby_version %>-alpine as builder
3
+
4
+ ENV RAILS_ENV=production
5
+ WORKDIR /app
6
+
7
+ RUN apk add --update --no-cache \
8
+ alpine-sdk \
9
+ nodejs \
10
+ tzdata \
11
+ yarn \
12
+ shared-mime-info \
13
+ <%- adapter = Modulorails.data.adapter -%>
14
+ <%- if adapter =~ /mysql/ -%>
15
+ mysql-dev
16
+ <%- else -%>
17
+ postgresql-dev
18
+ <%- end -%>
19
+ RUN gem install bundler -v <%= Modulorails.data.bundler_version %>
20
+
21
+ COPY Gemfile Gemfile.lock ./
22
+ RUN bundle check || bundle install --deployment --jobs=2 \
23
+ && rm -rf vendor/bundle/ruby/*/cache/*
24
+
25
+ COPY package.json yarn.lock ./
26
+ RUN yarn install
27
+
28
+ COPY . .
29
+ RUN bundle exec rake assets:precompile
30
+
31
+ # FINAL IMAGE
32
+ FROM ruby:<%= Modulorails.data.ruby_version %>-alpine
33
+
34
+ WORKDIR /app
35
+
36
+ RUN apk add --update --no-cache \
37
+ curl \
38
+ git \
39
+ nodejs \
40
+ tzdata \
41
+ shared-mime-info \
42
+ <%- adapter = Modulorails.data.adapter -%>
43
+ <%- if adapter =~ /mysql/ -%>
44
+ mysql-dev \
45
+ <%- else -%>
46
+ postgresql-dev \
47
+ <%- end -%>
48
+ && rm -rf .git/
49
+
50
+ COPY --from=builder /app .
51
+
52
+ RUN bundle config --local path vendor/bundle \
53
+ && bundle config --local without development:test:assets
54
+
55
+ EXPOSE 3000
56
+
57
+ ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]
@@ -0,0 +1,26 @@
1
+ FROM ruby:<%= Modulorails.data.ruby_version %>-alpine
2
+
3
+ ENV RAILS_ENV=development
4
+ WORKDIR /app
5
+
6
+ RUN apk add --update --no-cache \
7
+ alpine-sdk \
8
+ nodejs \
9
+ yarn \
10
+ tzdata \
11
+ <%- adapter = Modulorails.data.adapter -%>
12
+ <%- if adapter =~ /mysql/ -%>
13
+ mysql-dev
14
+ <%- else -%>
15
+ postgresql-dev
16
+ <%- end -%>
17
+ RUN gem install bundler -v <%= Modulorails.data.bundler_version %>
18
+
19
+ COPY Gemfile Gemfile.lock ./
20
+ RUN bundle install --jobs=2
21
+
22
+ COPY . .
23
+
24
+ EXPOSE 3000
25
+
26
+ ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]
@@ -0,0 +1,32 @@
1
+ <%- image_name = Modulorails.data.name.parameterize -%>
2
+ <%- upper_image_name = image_name.upcase -%>
3
+ <%- adapter = Modulorails.data.adapter -%>
4
+ <%- if adapter =~ /mysql/ -%>
5
+ development: &default
6
+ adapter: mysql2
7
+ encoding: utf8mb4
8
+ collation: utf8mb4_unicode_ci
9
+ database: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_NAME', '<%= image_name %>') %>
10
+ username: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_USERNAME', 'root') %>
11
+ password: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_PASSWORD', '') %>
12
+ host: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_HOST', 'database') %>
13
+ port: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_PORT', 3306) %>
14
+ <%- else -%>
15
+ development: &default
16
+ adapter: pg
17
+ database: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_NAME', '<%= image_name %>') %>
18
+ username: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_USERNAME', 'postgres') %>
19
+ password: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_PASSWORD', 'postgres') %>
20
+ host: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_HOST', 'database') %>
21
+ port: <%%= ENV.fetch('<%= upper_image_name %>_DATABASE_PORT', 5432) %>
22
+ <%- end -%>
23
+
24
+ test:
25
+ <<: *default
26
+ database: <%%= ENV.fetch('<%= upper_image_name %>_TEST_DATABASE_NAME', '<%= image_name %>_test') %>
27
+
28
+ staging:
29
+ <<: *default
30
+
31
+ production:
32
+ <<: *default
@@ -0,0 +1,50 @@
1
+ version: '3.7'
2
+
3
+ <%- image_name = Modulorails.data.name.parameterize -%>
4
+ services:
5
+ app:
6
+ image: modulotechgroup/<%= image_name %>
7
+ build:
8
+ context: .
9
+ dockerfile: Dockerfile.prod
10
+ depends_on:
11
+ - database
12
+ - redis
13
+ ports:
14
+ - '3000:3000'
15
+ environment:
16
+ RAILS_ENV: production
17
+ URL: http://localhost:3000
18
+ <%= image_name.upcase %>_DATABASE_HOST: database
19
+ <%= image_name.upcase %>_DATABASE_NAME: <%= image_name %>
20
+ RAILS_SERVE_STATIC_FILES: 'true'
21
+
22
+ <%- adapter = Modulorails.data.adapter -%>
23
+ <%- if adapter =~ /mysql/ -%>
24
+ database:
25
+ image: mysql/mysql-server:8.0
26
+ volumes:
27
+ - db_data:/var/lib/mysql
28
+ environment:
29
+ MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
30
+ MYSQL_DATABASE: <%= image_name %>
31
+ MYSQL_ROOT_HOST: '%'
32
+ <%- else -%>
33
+ database:
34
+ image: postgres:13.5
35
+ volumes:
36
+ - db_data:/var/lib/postgresql/data
37
+ environment:
38
+ POSTGRES_USER: postgres
39
+ POSTGRES_PASSWORD: postgres
40
+ POSTGRES_DB: <%= image_name %>
41
+ LC_COLLATE: 'en_US.UTF-8'
42
+ LC_CTYPE: 'en_US.UTF-8'
43
+ <%- end -%>
44
+
45
+ redis:
46
+ image: redis:6.2-alpine
47
+
48
+ # Define the volumes references in the services
49
+ volumes:
50
+ db_data:
@@ -0,0 +1,71 @@
1
+ version: '3.7'
2
+
3
+ <%- image_name = Modulorails.data.name.parameterize -%>
4
+ services:
5
+ app:
6
+ image: modulotechgroup/<%= image_name %>:dev
7
+ build:
8
+ context: .
9
+ dockerfile: Dockerfile
10
+ depends_on:
11
+ - database
12
+ - redis
13
+ ports:
14
+ - '3000:3000'
15
+ volumes:
16
+ - .:/app
17
+ environment:
18
+ RAILS_ENV: development
19
+ URL: http://localhost:3000
20
+ <%= image_name.upcase %>_DATABASE_HOST: database
21
+ <%= image_name.upcase %>_DATABASE_NAME: <%= image_name %>
22
+ entrypoint: ./entrypoints/docker-entrypoint.sh
23
+
24
+ <%- adapter = Modulorails.data.adapter -%>
25
+ <%- if adapter =~ /mysql/ -%>
26
+ database:
27
+ image: mysql/mysql-server:8.0
28
+ volumes:
29
+ - db_data:/var/lib/mysql
30
+ environment:
31
+ MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
32
+ MYSQL_DATABASE: <%= image_name %>
33
+ MYSQL_ROOT_HOST: '%'
34
+ <%- else-%>
35
+ database:
36
+ image: postgres:13.5
37
+ volumes:
38
+ - db_data:/var/lib/postgresql/data
39
+ environment:
40
+ POSTGRES_USER: postgres
41
+ POSTGRES_PASSWORD: postgres
42
+ POSTGRES_DB: <%= image_name %>
43
+ LC_COLLATE: 'en_US.UTF-8'
44
+ LC_CTYPE: 'en_US.UTF-8'
45
+ <%- end-%>
46
+
47
+ redis:
48
+ image: redis:6.2-alpine
49
+
50
+ mailcatcher:
51
+ image: tophfr/mailcatcher
52
+ ports:
53
+ - 1080:80
54
+
55
+ webpack:
56
+ image: modulotechgroup/<%= image_name %>:dev
57
+ build:
58
+ context: .
59
+ dockerfile: Dockerfile
60
+ entrypoint: ./entrypoints/webpack-entrypoint.sh
61
+ volumes:
62
+ - .:/app
63
+ ports:
64
+ - '3035:3035'
65
+ environment:
66
+ NODE_ENV: development
67
+ RAILS_ENV: development
68
+ WEBPACKER_DEV_SERVER_HOST: 0.0.0.0
69
+
70
+ volumes:
71
+ db_data:
@@ -0,0 +1,20 @@
1
+ #!/bin/sh
2
+
3
+ # Exit immediately if a command exits with a non-zero status
4
+ set -e
5
+
6
+ # No `node_modules` directory means `yarn install` was never launched.
7
+ # It is mandatory to install yarn dependencies.
8
+ if [ ! -d node_modules ]
9
+ then
10
+ yarn install
11
+ fi
12
+
13
+ # Remove pidfile if it exists else the server will not launch
14
+ if [ -f tmp/pids/server.pid ]
15
+ then
16
+ rm tmp/pids/server.pid
17
+ fi
18
+
19
+ # Launch the application listening from all origins on port 3000
20
+ ./bin/bundle exec rails s -b 0.0.0.0 -p 3000
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ # Exit immediately if a command exits with a non-zero status
4
+ set -e
5
+
6
+ # Launch webpack
7
+ ./bin/webpack-dev-server
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ class Modulorails::GitlabciGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+ desc 'This generator creates a template for a .gitlab-ci.yml file at root'
8
+
9
+ def create_config_file
10
+ # Update the gitlab-ci template
11
+ template '.gitlab-ci.yml'
12
+
13
+ # Remove the database-ci template if it exists.
14
+ # It used to be referenced by the gitlab-ci template.
15
+ remove_file 'config/database-ci.yml'
16
+
17
+ # Create file to avoid this generator on next modulorails launch
18
+ create_keep_file
19
+ rescue StandardError => e
20
+ $stderr.puts("[Modulorails] Error: cannot generate CI configuration: #{e.message}")
21
+ end
22
+
23
+ private
24
+
25
+ def create_keep_file
26
+ file = '.modulorails-gitlab-ci'
27
+
28
+ # Create file to avoid this generator on next modulorails launch
29
+ copy_file(file, file)
30
+
31
+ say "Add #{file} to git"
32
+ %x(git add #{file})
33
+ end
34
+ end
@@ -0,0 +1,118 @@
1
+ <%- image_name = Modulorails.data.name.parameterize -%>
2
+ include:
3
+ - project: 'modulosource/modulotech/devops/gitlab-ci-templates'
4
+ file:
5
+ - '/templates/helm.gitlab-ci.yml'
6
+ - '/templates/integration.gitlab-ci.yml'
7
+ - '/templates/docker-buildx.gitlab-ci.yml'
8
+
9
+ services:
10
+ <%- adapter = Modulorails.data.adapter -%>
11
+ <%- if adapter =~ /mysql/ -%>
12
+ - mysql:8.0
13
+ <%- else -%>
14
+ - postgres:13.5
15
+ <%- end -%>
16
+ - redis:6.2
17
+
18
+ variables:
19
+ IMAGE_NAME: <%= image_name %>
20
+ MYSQL_DATABASE: <%= image_name %>_test
21
+ MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
22
+ <%= image_name.upcase %>_DATABASE_HOST: mysql
23
+
24
+ stages:
25
+ - test
26
+ - build
27
+ - deploy
28
+
29
+ test:
30
+ extends: .test
31
+ script:
32
+ - "bundle exec rake db:create RAILS_ENV=test"
33
+ - "RAILS_ENV=test bundle exec rake db:migrate:reset"
34
+ - RAILS_ENV=test bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml
35
+
36
+ build_integration_image:
37
+ extends: .build_integration_image
38
+
39
+ docker_build:
40
+ extends: .docker_buildx_push
41
+ only:
42
+ - merge_requests
43
+ - staging
44
+
45
+ <%- review_base_url = Modulorails.data.review_base_url -%>
46
+ <%- if review_base_url.present? -%>
47
+ deploy_review:
48
+ extends: .deploy_helm
49
+ variables:
50
+ NAMESPACE: <%= image_name %>-$CI_ENVIRONMENT_SLUG
51
+ NAME: <%= image_name %>
52
+ CHART_NAME: <%= image_name %>
53
+ CONFIG_FILE: config/deploy/kubernetes/review.yaml
54
+ EXTRA_VARS: --set image.tag=$CI_COMMIT_SHORT_SHA --set ingress.hosts[0].host=${CI_ENVIRONMENT_SLUG}.<%= review_base_url %> --set ingress.tls[0].hosts[0]=${CI_ENVIRONMENT_SLUG}.<%= review_base_url %> --set env.url=${CI_ENVIRONMENT_SLUG}.<%= review_base_url %> --set database.password=$DB_PASSWORD --set encryption.key=$ENCRYPTION_KEY
55
+ environment:
56
+ name: review/$CI_COMMIT_REF_SLUG
57
+ url: https://${CI_ENVIRONMENT_SLUG}.<%= review_base_url %>
58
+ on_stop: stop_review
59
+ auto_stop_in: 3 days
60
+ only:
61
+ - merge_requests
62
+
63
+ stop_review:
64
+ extends: .stop_review
65
+ variables:
66
+ NAMESPACE: <%= image_name %>-$CI_ENVIRONMENT_SLUG
67
+ NAME: <%= image_name %>
68
+ only:
69
+ - merge_requests
70
+ <%- end -%>
71
+
72
+ <%- staging_url = Modulorails.data.staging_url -%>
73
+ <%- if staging_url.present? -%>
74
+ deploy_staging:
75
+ extends: .deploy_helm
76
+ variables:
77
+ NAMESPACE: <%= image_name %>
78
+ NAME: <%= image_name %>
79
+ CHART_NAME: <%= image_name %>
80
+ CONFIG_FILE: config/deploy/kubernetes/staging.yaml
81
+ EXTRA_VARS: --set image.tag=$CI_COMMIT_SHORT_SHA --set database.password=$DB_PASSWORD
82
+ environment:
83
+ name: staging
84
+ url: https://<%= staging_url %>
85
+ only:
86
+ - staging
87
+ <%- else -%>
88
+ deploy_staging:
89
+ extends: .deploy_capistrano
90
+ variables:
91
+ STAGE: staging
92
+ only:
93
+ - staging
94
+ <%- end -%>
95
+
96
+ <%- production_url = Modulorails.data.production_url -%>
97
+ <%- if production_url.present? -%>
98
+ deploy_production:
99
+ extends: .deploy_helm
100
+ variables:
101
+ NAMESPACE: <%= image_name %>
102
+ NAME: <%= image_name %>
103
+ CHART_NAME: <%= image_name %>
104
+ CONFIG_FILE: config/deploy/kubernetes/production.yaml
105
+ EXTRA_VARS: --set image.tag=$CI_COMMIT_SHORT_SHA --set database.password=$DB_PASSWORD
106
+ environment:
107
+ name: production
108
+ url: https://<%= production_url %>
109
+ only:
110
+ - master
111
+ <%- else -%>
112
+ deploy_production:
113
+ extends: .deploy_capistrano
114
+ variables:
115
+ STAGE: production
116
+ only:
117
+ - master
118
+ <%- end -%>
@@ -0,0 +1,6 @@
1
+ This file ensures the modulorails gem will not try to override your current .gitlab-ci.yml
2
+ file on Rails start.
3
+
4
+ If you want to reset your CI configuration, you can either:
5
+ - remove this file,
6
+ - or run `rails g modulorails:gitlabci`.
@@ -0,0 +1,8 @@
1
+ <%- adapter = Modulorails.data.adapter -%>
2
+ test:
3
+ host: <%= adapter =~ /mysql/ ? 'mysql' : 'postgres' %>
4
+ adapter: <%= adapter %>
5
+ database: test
6
+ username: root
7
+ password: password
8
+ encoding: utf8
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ class Modulorails::HealthCheckGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+ desc 'This generator creates a configuration for the health_check gem'
8
+
9
+ def create_config_file
10
+ # Update the template
11
+ template 'config/initializers/health_check.rb'
12
+
13
+ # Add the route
14
+ unless File.read(Rails.root.join('config/routes.rb')).match?('health_check_routes')
15
+ inject_into_file 'config/routes.rb', after: "Rails.application.routes.draw do\n" do <<~'RUBY'
16
+ health_check_routes
17
+ RUBY
18
+ end
19
+ end
20
+
21
+ # Update the gem and the Gemfile.lock
22
+ system('bundle install')
23
+
24
+ # Create file to avoid this generator on next modulorails launch
25
+ create_keep_file
26
+ rescue StandardError => e
27
+ $stderr.puts("[Modulorails] Error: cannot generate health_check configuration: #{e.message}")
28
+ end
29
+
30
+ private
31
+
32
+ def create_keep_file
33
+ file = '.modulorails-health_check'
34
+
35
+ # Create file to avoid this generator on next modulorails launch
36
+ copy_file(file, file)
37
+
38
+ say "Add #{file} to git"
39
+ %x(git add #{file})
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ This file ensures the modulorails gem will not try to override your current
2
+ config/initializers/health_check.rb file on Rails start.
3
+
4
+ If you want to reset your heal_check configuration, you can either:
5
+ - remove this file,
6
+ - or run `rails g modulorails:health_check`.
@@ -0,0 +1,100 @@
1
+ HealthCheck.setup do |config|
2
+
3
+ # uri prefix (no leading slash)
4
+ config.uri = 'health'
5
+
6
+ # Text output upon success
7
+ config.success = 'success'
8
+
9
+ # Text output upon failure
10
+ config.failure = 'health_check failed'
11
+
12
+ # Disable the error message to prevent /health_check from leaking
13
+ # sensitive information
14
+ config.include_error_in_response_body = false
15
+
16
+ # Log level (success or failure message with error details is sent to rails log unless this is set to nil)
17
+ config.log_level = 'info'
18
+
19
+ # Timeout in seconds used when checking smtp server
20
+ config.smtp_timeout = 30.0
21
+
22
+ # http status code used when plain text error message is output
23
+ # Set to 200 if you want your want to distinguish between partial (text does not include success) and
24
+ # total failure of rails application (http status of 500 etc)
25
+
26
+ config.http_status_for_error_text = 500
27
+
28
+ # http status code used when an error object is output (json or xml)
29
+ # Set to 200 if you want to distinguish between partial (healthy property == false) and
30
+ # total failure of rails application (http status of 500 etc)
31
+
32
+ config.http_status_for_error_object = 500
33
+
34
+ # bucket names to test connectivity - required only if s3 check used, access permissions can be mixed
35
+ # config.buckets = { 'bucket_name' => %i[R W D] }
36
+
37
+ # You can customize which checks happen on a standard health check, eg to set an explicit list use:
38
+ config.standard_checks = %w[database migrations custom]
39
+
40
+ # Or to exclude one check:
41
+ config.standard_checks -= %w[emailconf]
42
+
43
+ # You can set what tests are run with the 'full' or 'all' parameter
44
+ config.full_checks = %w[database migrations custom email cache redis]
45
+ # config.full_checks = %w[database migrations custom email cache redis sidekiq-redis s3]
46
+
47
+ # Add one or more custom checks that return a blank string if ok, or an error message if there is an error
48
+ # config.add_custom_check do
49
+ # CustomHealthCheck.perform_check # any code that returns blank on success and non blank string upon failure
50
+ # end
51
+
52
+ # Add another custom check with a name, so you can call just specific custom checks. This can also be run using
53
+ # the standard 'custom' check.
54
+ # You can define multiple tests under the same name - they will be run one after the other.
55
+ # config.add_custom_check('sometest') do
56
+ # CustomHealthCheck.perform_another_check # any code that returns blank on success and non blank string upon failure
57
+ # end
58
+
59
+ # max-age of response in seconds
60
+ # cache-control is public when max_age > 1 and basic_auth_username is not set
61
+ # You can force private without authentication for longer max_age by
62
+ # setting basic_auth_username but not basic_auth_password
63
+ config.max_age = 1
64
+
65
+ # Protect health endpoints with basic auth
66
+ # These default to nil and the endpoint is not protected
67
+ # config.basic_auth_username = 'my_username'
68
+ # config.basic_auth_password = 'my_password'
69
+
70
+ # Whitelist requesting IPs by a list of IP and/or CIDR ranges, either IPv4 or IPv6 (uses IPAddr.include? method to check)
71
+ # Defaults to blank which allows any IP
72
+ # config.origin_ip_whitelist = %w(123.123.123.123 10.11.12.0/24 2400:cb00::/32)
73
+
74
+ # Use ActionDispatch::Request's remote_ip method when behind a proxy to pick up the real remote IP for origin_ip_whitelist check
75
+ # Otherwise uses Rack::Request's ip method (the default, and always used by Middleware), which is more susceptible to spoofing
76
+ # See https://stackoverflow.com/questions/10997005/whats-the-difference-between-request-remote-ip-and-request-ip-in-rails
77
+ config.accept_proxied_requests = false
78
+
79
+ # http status code used when the ip is not allowed for the request
80
+ config.http_status_for_ip_whitelist_error = 403
81
+
82
+ # rabbitmq
83
+ # config.rabbitmq_config = {}
84
+
85
+ # When redis url/password is non-standard
86
+ config.redis_url = ENV['REDIS_URL']
87
+ # Only included if set, as url can optionally include passwords as well
88
+ # config.redis_password = 'redis_password' # default ENV['REDIS_PASSWORD']
89
+
90
+ # Failure Hooks to do something more ...
91
+ # checks lists the checks requested
92
+ config.on_failure do |checks, msg|
93
+ # log msg somewhere
94
+ end
95
+
96
+ config.on_success do |checks|
97
+ # flag that everything is well
98
+ end
99
+
100
+ end