dora_web_upgrader 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e791400a872e203332bc43b8774a627e8fa0cba69c80d3e88362eb71e3ba0a94
4
+ data.tar.gz: b389c54da5b3a731f8130c93b0914032e0cf72ef302356ff0b3952cc0febd028
5
+ SHA512:
6
+ metadata.gz: 19e8ee7bc819aec30175b86872dcf0a38382d02893cb866129e2207f2b61fc9b47a37c43c929c1e359296e57b4c7b2f9215b1c9206edad906d85fed6d6f7b1c2
7
+ data.tar.gz: 7c141b2c5401c4cd108fe32c8e8a70734e0e7af5a4bcf67cd30839fe004efc05e39e82e2f23bf881c62621bcc3991ce487c4b78ec529b172fd074f206290d627
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Dora Web Upgrader
2
+
3
+ **Dora Web Upgrader is a [Rails] [engine] that facilitates upgrading a
4
+ [dora]-powered Rails application via webhooks.**
5
+
6
+ [dora], short for *Do*cker container for *Ra*ils applications, is my
7
+ collection of utilities that I use to containerize my Rails applications.
8
+
9
+ I use web hooks to trigger upgrades from within the container whenever I push
10
+ code to the main branch. When this engine receives a `POST` request, it will
11
+ execute dora's `upgrade-app.sh` script if the request contains a pre-configured
12
+ secret value. It sends an e-mail notification when the request has been received,
13
+ and another one when the upgrade process has finished.
14
+
15
+ If the `POST` request does not contain the correct secret, a notification
16
+ e-mail is also send, but no further action is triggered.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'dora_web_upgrader', github: 'bovender/dora_web_upgrader', branch: 'main'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ ```bash
29
+ bundle
30
+ ```
31
+
32
+ Mount the engine onto your Rails application:
33
+
34
+ ```ruby
35
+ # config/routes.rb
36
+ Rails.application.routes.draw do
37
+ mount DoraWebUpgrader::Engine, at: '/dora_web_upgrader'
38
+ # ...
39
+ end
40
+ ```
41
+
42
+ Generate an initializer:
43
+
44
+ ```ruby
45
+ bin/rails generate dora_web_upgrader:install
46
+ ```
47
+
48
+ And customize the resulting `config/initializers/dora_web_upgrader.rb`.
49
+
50
+ To avoid committing the Webhook secret in plain text to source control, the
51
+ default secret is read from the environment variable `$DORA_WEB_UPGRADER_SECRET`.
52
+ Of course you may also choose another means to store and retrieve the secret,
53
+ but make sure not to accidentally publish it.
54
+
55
+ ## Usage
56
+
57
+ To make use of the engine, you must configure a Webhook in your Git repository
58
+ host (e.g., [GitHub] or [Gitea]) to issue a `POST` request to
59
+ `https://my_app.example.com/dora_web_upgrader/upgrade` with JSON data containing
60
+ the secret from the initializer file.
61
+
62
+ (If you mounted the engine at a different path, replace `dora_web_upgrader` in
63
+ the URL with that path.)
64
+
65
+ If everything is configured correctly, the upgrade process will start and finish
66
+ automagically. You should receive two e-mails. If you don't, double-check that
67
+ your mail server allows sending from and to the configured e-mail addresses.
68
+
69
+ ## Development
70
+
71
+ To run tests, install [Docker] and [Docker Compose]. Clone DoraWebUpgrader and
72
+ [dora] into the same parent directory. Start up the container(s), execute `bash`
73
+ in the Rails container and run the tests like so:
74
+
75
+ ```bash
76
+ $ cd dora_web_upgrader/
77
+ $ docker-compose -f ../dora/docker-compose.yml up -d
78
+ Creating network "dora_default" with the default driver
79
+ Creating dora_selenium_1 ... done
80
+ Creating dora_db_1 ... done
81
+ Creating dora_redis_1 ... done
82
+ Creating dora_mailhog_1 ... done
83
+ Creating dora_rails_1 ... done
84
+ Creating dora_adminer_1 ... done
85
+ $ docker-compose -f ../dora/docker-compose.yml exec rails bash
86
+ [dora_web_upgrader development]root@459cb7dc2277:/home/dora/rails$ bin/rails test
87
+ Run options: --seed 41689
88
+
89
+ # Running:
90
+
91
+ ......
92
+
93
+ Finished in 0.180743s, 33.1962 runs/s, 94.0560 assertions/s.
94
+ 6 runs, 17 assertions, 0 failures, 0 errors, 0 skips
95
+ ```
96
+
97
+ (There are several containers included in the `docker-compose.yml` file that
98
+ are not needed to test DoraWebUpgrader, but are part of [dora].)
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
103
+
104
+ [docker]: https://docs.docker.com/engine/install/ubuntu/
105
+ [docker compose]: https://docs.docker.com/compose/install/
106
+ [dora]: https://github.com/bovender/dora
107
+ [engine]: https://guides.rubyonrails.org/engines.html
108
+ [gitea]: https://gitea.io
109
+ [github]: https://github.com
110
+ [Rails]: https://rubyonrails.org
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
9
+
10
+ require "rake/testtask"
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
17
+
18
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/dora_web_upgrader .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module DoraWebUpgrader
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoraWebUpgrader
4
+ class UpgradeController < ApplicationController
5
+ layout false
6
+
7
+ def upgrade
8
+ secret = DoraWebUpgrader.config.secret
9
+ raise 'DoraWebUpgrader.secret is not configured!' if secret.blank?
10
+
11
+ @payload = JSON.parse(request.raw_post)
12
+ if @payload['secret'] == secret
13
+ @message = 'ok'
14
+ UpgradeJob.perform_later
15
+ else
16
+ @message = 'invalid secret'
17
+ end
18
+
19
+ UpgradeMailer.upgrade_started(@message, @payload).deliver_later
20
+
21
+ render json: { message: @message }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module DoraWebUpgrader
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoraWebUpgrader
4
+ class UpgradeJob < ApplicationJob
5
+ queue_as :default
6
+
7
+ def perform(*)
8
+ # Do not upgrade developement environemnts as this might inadvertently
9
+ # overwrite uncommitted changes.
10
+ if %w[production staging].include? Rails.env
11
+ logger.warn '******** PERFORMING APPLICATION UPGRADE ********'
12
+ output = `/usr/local/bin/upgrade-app.sh 2>&1`
13
+ logger.warn output
14
+ result = $CHILD_STATUS.exitstatus
15
+ else
16
+ output = "Rails environment: #{Rails.env} -- not upgrading..."
17
+ logger.warn output
18
+ result = -1
19
+ end
20
+ UpgradeMailer.upgrade_performed(output, result).deliver_now
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoraWebUpgrader
4
+ class ApplicationMailer < ActionMailer::Base
5
+ layout 'mail'
6
+ default from: DoraWebUpgrader.config.email_notifications_from,
7
+ to: DoraWebUpgrader.config.email_notifications_to,
8
+ x_mailer: "DoraWebUpgrader #{VERSION}"
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoraWebUpgrader
4
+ class UpgradeMailer < ApplicationMailer
5
+ def upgrade_started(message, payload)
6
+ @message = message
7
+ @payload = payload
8
+ @payload[:secret] = '(REDACTED)' if @payload[:secret]
9
+ mail subject: "[#{app_identifier} #{Rails.env}] upgrade started"
10
+ end
11
+
12
+ def upgrade_performed(output, result)
13
+ @output = output
14
+ s = case result
15
+ when -1
16
+ 'dry run'
17
+ when 0
18
+ 'succeeded'
19
+ else
20
+ 'failed'
21
+ end
22
+ mail subject: "[#{app_identifier} #{Rails.env}] upgrade #{s}"
23
+ end
24
+
25
+ private
26
+
27
+ def app_identifier
28
+ ::Rails.application.class.module_parent
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # Upgrade performed
2
+
3
+ ```bash
4
+ <%= @output %>
5
+ ```
@@ -0,0 +1,7 @@
1
+ # Upgrade started
2
+
3
+ > <%= @message %>
4
+
5
+ ```json
6
+ <%= JSON.pretty_generate(@payload) %>
7
+ ```
@@ -0,0 +1,5 @@
1
+ <%= yield %>
2
+
3
+ --
4
+ This is an automated e-mail that was sent by DoraWebUpgrader version <%= DoraWebUpgrader::VERSION %>.
5
+ See <https://github.com/bovender/DoraWebUpgrader>.
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Configure DoraWebUpgrader <https://github.com/bovender/DoraWebUpgrader>
4
+ DoraWebUpgrader.configure do |config|
5
+ # Sender address for upgrade notifications.
6
+ # Make sure that this is a valid address that is accepted by the mail server
7
+ # that is configured for the current application.
8
+ config.email_notifications_from = 'webmaster'
9
+
10
+ # Recipient address for upgrade notifications.
11
+ # Make sure that this is a valid address that is accepted by the mail server
12
+ # that is configured for the current application.
13
+ config.email_notifications_to = 'webmaster'
14
+
15
+ # A secret string that must be included in the POST request to the API
16
+ # endpoint. If this is an empty string, the upgrader will raise an error.
17
+ config.secret = ENV['DORA_WEB_UPGRADER_SECRET']
18
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ DoraWebUpgrader::Engine.routes.draw do
4
+ post 'upgrade' => 'upgrade#upgrade'
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dora_web_upgrader/version'
4
+ require 'dora_web_upgrader/engine'
5
+ require 'dora_web_upgrader/configuration'
6
+
7
+ module DoraWebUpgrader
8
+ mattr_accessor :secret
9
+
10
+ class << self
11
+ attr_reader :config
12
+
13
+ def configure
14
+ @config = Configuration.new
15
+ yield config
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module DoraWebUpgrader
2
+ class Configuration
3
+ attr_accessor :email_notifications_from, :email_notifications_to, :secret
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module DoraWebUpgrader
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace DoraWebUpgrader
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoraWebUpgrader
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DoraWebUpgrader
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc 'Install DoraWebUpgrader sample configuration'
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def copy_config
10
+ template 'config/initializers/dora_web_upgrader.rb'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Configure DoraWebUpgrader <https://github.com/bovender/DoraWebUpgrader>
4
+ DoraWebUpgrader.configure do |config|
5
+ # Sender address for upgrade notifications.
6
+ # Make sure that this is a valid address that is accepted by the mail server
7
+ # that is configured for the current application.
8
+ config.email_notifications_from = 'webmaster'
9
+
10
+ # Recipient address for upgrade notifications.
11
+ # Make sure that this is a valid address that is accepted by the mail server
12
+ # that is configured for the current application.
13
+ config.email_notifications_to = 'webmaster'
14
+
15
+ # A secret string that must be included in the POST request to the API
16
+ # endpoint. If this is an empty string, the upgrader will raise an error.
17
+ config.secret = ENV['DORA_WEB_UPGRADER_SECRET']
18
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dora_web_upgrader
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Kraus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: Make use of web hooks that are triggered by pushing codeto a repository
28
+ and have your application taking care ofupgrading itself.
29
+ email: bovender@bovender.de
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/haver_web_upgrader_manifest.js
38
+ - app/assets/stylesheets/haver_web_upgrader/application.css
39
+ - app/controllers/dora_web_upgrader/application_controller.rb
40
+ - app/controllers/dora_web_upgrader/upgrade_controller.rb
41
+ - app/jobs/dora_web_upgrader/application_job.rb
42
+ - app/jobs/dora_web_upgrader/upgrade_job.rb
43
+ - app/mailers/dora_web_upgrader/application_mailer.rb
44
+ - app/mailers/dora_web_upgrader/upgrade_mailer.rb
45
+ - app/views/dora_web_upgrader/upgrade_mailer/upgrade_performed.text.erb
46
+ - app/views/dora_web_upgrader/upgrade_mailer/upgrade_started.text.erb
47
+ - app/views/layouts/mail.text.erb
48
+ - config/initializers/dora_web_upgrader.rb
49
+ - config/routes.rb
50
+ - lib/dora_web_upgrader.rb
51
+ - lib/dora_web_upgrader/configuration.rb
52
+ - lib/dora_web_upgrader/engine.rb
53
+ - lib/dora_web_upgrader/version.rb
54
+ - lib/generators/dora_web_upgrader/install_generator.rb
55
+ - lib/generators/dora_web_upgrader/templates/config/initializers/dora_web_upgrader.rb
56
+ homepage: https://gibhub.com/bovender/dora_web_upgrader
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://gibhub.com/bovender/dora_web_upgrader
62
+ source_code_uri: https://gibhub.com/bovender/dora_web_upgrader
63
+ changelog_uri: https://gibhub.com/bovender/dora_web_upgrader/blob/main/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '2.6'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.1.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Dora Web Upgrader is a Rails engine that facilitates upgrading a dora-powered
83
+ Rails application via webhooks.
84
+ test_files: []