deploy_pin 1.10.0 → 2.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 +4 -4
- data/README.md +77 -9
- data/Rakefile +16 -0
- data/exe/deploy_pin +8 -0
- data/lib/deploy_pin/cli.rb +83 -0
- data/lib/deploy_pin/database.rb +6 -1
- data/lib/deploy_pin/database_engine.rb +2 -2
- data/lib/deploy_pin/engine.rb +2 -1
- data/lib/deploy_pin/generators/base.rb +96 -0
- data/lib/deploy_pin/generators/install.rb +65 -0
- data/lib/deploy_pin/generators/rails_generator.rb +79 -0
- data/lib/deploy_pin/generators/task.rb +150 -0
- data/lib/{generators/deploy_pin/task/templates → deploy_pin/generators/templates/task}/rspec/task_spec.rb.erb +2 -2
- data/lib/deploy_pin/generators/upgrade.rb +23 -0
- data/lib/deploy_pin/generators.rb +6 -0
- data/lib/deploy_pin/runner.rb +15 -1
- data/lib/deploy_pin/storage.rb +11 -3
- data/lib/deploy_pin/version.rb +1 -1
- data/lib/deploy_pin.rb +62 -8
- data/lib/generators/deploy_pin/install/install_generator.rb +10 -2
- data/lib/generators/deploy_pin/task/USAGE +1 -1
- data/lib/generators/deploy_pin/task/task_generator.rb +8 -123
- data/lib/generators/deploy_pin/upgrade/upgrade_generator.rb +8 -1
- data/lib/tasks/deploy_pin_tasks.rake +39 -17
- metadata +117 -19
- /data/lib/{generators/deploy_pin/install/templates → deploy_pin/generators/templates/install}/create_deploy_pins.rb +0 -0
- /data/lib/{generators/deploy_pin/install/templates → deploy_pin/generators/templates/install}/deploy_pin.rb +0 -0
- /data/lib/{generators/deploy_pin/task/templates → deploy_pin/generators/templates/task}/parallel_task.rb.erb +0 -0
- /data/lib/{generators/deploy_pin/task/templates → deploy_pin/generators/templates/task}/task.rb.erb +0 -0
- /data/lib/{generators/deploy_pin/upgrade/templates → deploy_pin/generators/templates/upgrade}/upgrade_deploy_pins.rb +0 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'deploy_pin/generators/base'
|
|
4
|
+
|
|
5
|
+
module DeployPin
|
|
6
|
+
module Generators
|
|
7
|
+
# `deploy_pin task <title> --group <group> [--parallel] [--recurring --identifier <id>]`
|
|
8
|
+
# (`rails g deploy_pin:task` in a Rails app): creates a task file in
|
|
9
|
+
# {DeployPin.tasks_path}, and a test for it with the configured
|
|
10
|
+
# {DeployPin.test_framework} in {DeployPin.tests_path} (`spec/deploy_pin`
|
|
11
|
+
# for RSpec by default).
|
|
12
|
+
#
|
|
13
|
+
# @example Skip the test, or pick the framework for one task
|
|
14
|
+
# deploy_pin task backfill_users -g I --skip-test
|
|
15
|
+
# deploy_pin task backfill_users -g I --test-framework=rspec
|
|
16
|
+
class Task < Base
|
|
17
|
+
# Supported test frameworks: the default directory for the tests (used
|
|
18
|
+
# when {DeployPin.tests_path} is not set) and the test file name, where
|
|
19
|
+
# `%<task>s` is the task file name without `.rb`.
|
|
20
|
+
TEST_FRAMEWORKS = {
|
|
21
|
+
rspec: { path: 'spec/deploy_pin', file: '%<task>s_spec.rb' }
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
# Printed when no test framework is configured and none is detected.
|
|
25
|
+
UNDETECTED_TEST_FRAMEWORK = 'test for the task: no test framework detected. ' \
|
|
26
|
+
'Add rspec (rspec-rails in a Rails app) to the :development group, ' \
|
|
27
|
+
'or set DeployPin.test_framework :rspec'
|
|
28
|
+
|
|
29
|
+
class_option :parallel, type: :boolean, aliases: '-p', desc: 'Use the Parallel task template'
|
|
30
|
+
class_option :recurring, type: :boolean, aliases: '-r', desc: 'Run the task on every deployment'
|
|
31
|
+
class_option :identifier, aliases: '-i', desc: 'Identifier of a recurring task'
|
|
32
|
+
argument :title, required: true, desc: 'Task name, e.g. backfill_user_names'
|
|
33
|
+
class_option :group, aliases: '-g', desc: 'Group of the task (default: DeployPin.fallback_group)'
|
|
34
|
+
class_option :author, aliases: '-a', desc: 'Author shown in the task title (default: $USER)'
|
|
35
|
+
class_option :test_framework, type: :string,
|
|
36
|
+
desc: "Test framework for the task's test (default: DeployPin.test_framework)"
|
|
37
|
+
class_option :skip_test, type: :boolean, default: false, desc: 'Do not generate a test for the task'
|
|
38
|
+
|
|
39
|
+
templates 'task'
|
|
40
|
+
desc 'Creates a DeployPin task in DeployPin.tasks_path, and a test for it'
|
|
41
|
+
|
|
42
|
+
# @param args [Array] Thor arguments, options and configuration
|
|
43
|
+
# @raise [Thor::Error] see {#validate}
|
|
44
|
+
def initialize(*args)
|
|
45
|
+
super
|
|
46
|
+
|
|
47
|
+
validate
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Writes the task file, named after its identifier and title.
|
|
51
|
+
# @return [void]
|
|
52
|
+
def create_task_file
|
|
53
|
+
template_file = if options[:parallel]
|
|
54
|
+
'parallel_task.rb.erb'
|
|
55
|
+
else
|
|
56
|
+
'task.rb.erb'
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
@author = options[:author] || ENV['USER']
|
|
60
|
+
@group = group
|
|
61
|
+
@recurring = options[:recurring]
|
|
62
|
+
@identifier = @recurring ? options[:identifier] : Time.now.strftime('%Y%m%d%H%M%S')
|
|
63
|
+
template template_file, task_path
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Writes the test for the task, unless tests are turned off.
|
|
67
|
+
# @return [void]
|
|
68
|
+
def create_test_file
|
|
69
|
+
framework = test_framework
|
|
70
|
+
return if framework == false # turned off on purpose
|
|
71
|
+
return say_status(:skip, UNDETECTED_TEST_FRAMEWORK, :yellow) unless framework
|
|
72
|
+
|
|
73
|
+
template "#{framework}/task_spec.rb.erb", test_file_path(framework)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @return [void]
|
|
77
|
+
# @raise [Thor::Error] if the group is missing or not in {DeployPin.groups},
|
|
78
|
+
# or the test framework is not one of {TEST_FRAMEWORKS}
|
|
79
|
+
def validate
|
|
80
|
+
raise Thor::Error, set_color('Missing required option: --group', :red) if group.blank?
|
|
81
|
+
|
|
82
|
+
unless Array(DeployPin.groups).include?(group)
|
|
83
|
+
raise Thor::Error, set_color("Group '#{group}' is not defined in DeployPin.groups", :red)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
validate_test_framework
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
# The --group option, else {DeployPin.fallback_group}. Resolved when the
|
|
92
|
+
# generator runs, after the configuration is loaded.
|
|
93
|
+
def group
|
|
94
|
+
options[:group].presence || DeployPin.fallback_group
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Whether the spec is for a Rails app: it then requires `rails_helper` (rspec-rails)
|
|
98
|
+
# and loads the tasks with `Rails.application.load_tasks`, which also loads the
|
|
99
|
+
# app's own rake files (e.g. hooks on deploy_pin:run). Otherwise it requires
|
|
100
|
+
# `spec_helper` and uses DeployPin.load_tasks.
|
|
101
|
+
def rails_app?
|
|
102
|
+
defined?(::Rails::Application) || File.exist?(File.join(destination_root, 'spec/rails_helper.rb'))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The task file name without the extension, e.g. `20190401135040_backfill_users`.
|
|
106
|
+
def task_name
|
|
107
|
+
@recurring ? "r_#{@identifier}_#{title}" : "#{@identifier}_#{title}"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def task_path
|
|
111
|
+
"#{DeployPin.tasks_path}/#{task_name}.rb"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The test file in {DeployPin.tests_path}, or in the framework's default directory.
|
|
115
|
+
def test_file_path(framework)
|
|
116
|
+
settings = TEST_FRAMEWORKS.fetch(framework)
|
|
117
|
+
directory = DeployPin.tests_path.presence || settings[:path]
|
|
118
|
+
|
|
119
|
+
File.join(directory.to_s, format(settings[:file], task: task_name))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# The --test-framework flag or DeployPin.test_framework, else the detected one.
|
|
123
|
+
#
|
|
124
|
+
# @return [Symbol, false, nil] the framework; false when tests are turned
|
|
125
|
+
# off (--skip-test, `test_framework false`); nil when none is detected
|
|
126
|
+
def test_framework
|
|
127
|
+
return false if options[:skip_test]
|
|
128
|
+
|
|
129
|
+
configured = options[:test_framework].presence || DeployPin.test_framework
|
|
130
|
+
return configured if configured == false
|
|
131
|
+
|
|
132
|
+
configured&.to_sym || detected_test_framework
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# RSpec is the only supported framework, detected by its constant.
|
|
136
|
+
def detected_test_framework
|
|
137
|
+
:rspec if defined?(::RSpec)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def validate_test_framework
|
|
141
|
+
framework = test_framework
|
|
142
|
+
return if !framework || TEST_FRAMEWORKS.key?(framework)
|
|
143
|
+
|
|
144
|
+
raise Thor::Error, set_color(
|
|
145
|
+
"Unknown test framework '#{framework}'. Supported: #{TEST_FRAMEWORKS.keys.join(', ')}", :red
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'rails_helper'
|
|
3
|
+
require '<%= rails_app? ? 'rails_helper' : 'spec_helper' %>'
|
|
4
4
|
require 'rake'
|
|
5
5
|
|
|
6
6
|
# Runs the task end to end, the same way a deploy does: `rake 'deploy_pin:run[<%= @identifier %>]'`.
|
|
7
7
|
RSpec.describe 'DeployPin task <%= task_name %>' do
|
|
8
8
|
subject(:run_task) { Rake::Task['deploy_pin:run'].invoke('<%= @identifier %>') }
|
|
9
9
|
|
|
10
|
-
before(:all) { Rails.application
|
|
10
|
+
before(:all) { <%= rails_app? ? 'Rails.application' : 'DeployPin' %>.load_tasks unless Rake::Task.task_defined?('deploy_pin:run') }
|
|
11
11
|
|
|
12
12
|
before do
|
|
13
13
|
Rake::Task['deploy_pin:run'].reenable
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'deploy_pin/generators/base'
|
|
4
|
+
|
|
5
|
+
module DeployPin
|
|
6
|
+
module Generators
|
|
7
|
+
# `deploy_pin upgrade`: creates the migration that upgrades the
|
|
8
|
+
# `deploy_pins` table for resumable tasks (DeployPin 1.7).
|
|
9
|
+
class Upgrade < Base
|
|
10
|
+
templates 'upgrade'
|
|
11
|
+
desc 'Creates the migration that upgrades the deploy_pins table'
|
|
12
|
+
|
|
13
|
+
class_option :migrations_path, type: :string, default: 'db/migrate',
|
|
14
|
+
desc: 'Directory of the ActiveRecord migrations'
|
|
15
|
+
|
|
16
|
+
# Creates the migration that upgrades the `deploy_pins` table.
|
|
17
|
+
# @return [void]
|
|
18
|
+
def copy_migrations
|
|
19
|
+
migration_template 'upgrade_deploy_pins.rb', 'upgrade_deploy_pins'
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/deploy_pin/runner.rb
CHANGED
|
@@ -54,12 +54,26 @@ module DeployPin
|
|
|
54
54
|
self.print("Tasks number: #{DeployPin::Collector.new(identifiers:).tasks_count}")
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
# Variables that name the app environment, by priority, when the app is not a
|
|
58
|
+
# Rails app: Rails, Rack, then Sinatra and other Rack apps.
|
|
59
|
+
ENV_VARIABLES = %w[RAILS_ENV RACK_ENV APP_ENV].freeze
|
|
60
|
+
|
|
57
61
|
# Prints a message, except in the test environment.
|
|
58
62
|
#
|
|
59
63
|
# @param msg [String]
|
|
60
64
|
# @return [void]
|
|
61
65
|
def self.print(msg)
|
|
62
|
-
puts(msg) unless
|
|
66
|
+
puts(msg) unless test_env?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Whether the app runs in its test environment: `Rails.env` in a Rails
|
|
70
|
+
# app, else the first of {ENV_VARIABLES} that is set.
|
|
71
|
+
#
|
|
72
|
+
# @return [Boolean]
|
|
73
|
+
def self.test_env?
|
|
74
|
+
return ::Rails.env.test? if defined?(::Rails.env)
|
|
75
|
+
|
|
76
|
+
ENV.values_at(*ENV_VARIABLES).find(&:present?) == 'test'
|
|
63
77
|
end
|
|
64
78
|
end
|
|
65
79
|
end
|
data/lib/deploy_pin/storage.rb
CHANGED
|
@@ -8,6 +8,11 @@ module DeployPin
|
|
|
8
8
|
# storage :active_record # default, or :mongoid
|
|
9
9
|
# end
|
|
10
10
|
#
|
|
11
|
+
# @example Your own record class, e.g. for Sequel or ROM
|
|
12
|
+
# DeployPin.setup do
|
|
13
|
+
# storage DeployPinRecord
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
11
16
|
# A record class must respond to `.find_by(uuid:)` (returning a record or
|
|
12
17
|
# nil) and `.create(uuid:)`. Its instances must respond to `#progress`,
|
|
13
18
|
# `#completed_at`, `#update(attributes)` and `#increment!(attribute, by)`.
|
|
@@ -19,10 +24,13 @@ module DeployPin
|
|
|
19
24
|
mongoid: -> { DeployPin::MongoidRecord }
|
|
20
25
|
}.freeze
|
|
21
26
|
|
|
22
|
-
# @return [Class] {Record} or {
|
|
23
|
-
# @raise [ArgumentError] if {DeployPin.storage} is
|
|
27
|
+
# @return [Class] {Record}, {MongoidRecord}, or the class set as {DeployPin.storage}
|
|
28
|
+
# @raise [ArgumentError] if {DeployPin.storage} is neither a class nor one of {PROVIDERS}
|
|
24
29
|
def self.record_class
|
|
25
|
-
|
|
30
|
+
storage = DeployPin.storage
|
|
31
|
+
return storage if storage.is_a?(Module)
|
|
32
|
+
|
|
33
|
+
PROVIDERS.fetch(storage.to_sym) do
|
|
26
34
|
raise ArgumentError,
|
|
27
35
|
"Unknown DeployPin storage #{DeployPin.storage.inspect}. Supported: #{PROVIDERS.keys.join(', ')}"
|
|
28
36
|
end.call
|
data/lib/deploy_pin/version.rb
CHANGED
data/lib/deploy_pin.rb
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'active_support'
|
|
4
|
+
require 'active_support/core_ext'
|
|
5
|
+
require 'deploy_pin/version'
|
|
3
6
|
require 'deploy_pin/deployment_state'
|
|
4
7
|
require 'deploy_pin/runner'
|
|
5
8
|
require 'deploy_pin/collector'
|
|
6
9
|
require 'deploy_pin/parallel_wrapper'
|
|
7
10
|
require 'deploy_pin/task'
|
|
8
11
|
require 'deploy_pin/task_criteria'
|
|
9
|
-
require 'deploy_pin/engine'
|
|
10
12
|
require 'deploy_pin/database'
|
|
11
13
|
require 'deploy_pin/database_engine'
|
|
12
14
|
require 'deploy_pin/storage'
|
|
13
15
|
require 'parallel'
|
|
14
16
|
require 'ruby-progressbar'
|
|
15
17
|
require 'colorize'
|
|
18
|
+
require 'deploy_pin/engine' if defined?(Rails::Engine)
|
|
16
19
|
|
|
17
20
|
# DeployPin runs one-off and recurring tasks around a deployment
|
|
18
21
|
# (for example, data migrations before or after the code is released).
|
|
@@ -22,7 +25,9 @@ require 'colorize'
|
|
|
22
25
|
# and each non-recurring task runs only once: its completion is stored in
|
|
23
26
|
# the configured {DeployPin.storage}.
|
|
24
27
|
#
|
|
25
|
-
#
|
|
28
|
+
# DeployPin works in Rails and in any other Ruby app. Configure it in the
|
|
29
|
+
# file generated by `deploy_pin install` (`config/deploy_pin.rb`), or by
|
|
30
|
+
# `rails g deploy_pin:install` in a Rails app (`config/initializers/deploy_pin.rb`).
|
|
26
31
|
#
|
|
27
32
|
# @example config/initializers/deploy_pin.rb
|
|
28
33
|
# DeployPin.setup do
|
|
@@ -52,7 +57,7 @@ require 'colorize'
|
|
|
52
57
|
# @return [Hash, nil]
|
|
53
58
|
#
|
|
54
59
|
# @!method self.fallback_group(val = nil)
|
|
55
|
-
# Default group for `rails g deploy_pin:task` when `--group` is not given.
|
|
60
|
+
# Default group for `deploy_pin task` (`rails g deploy_pin:task`) when `--group` is not given.
|
|
56
61
|
# @param val [String, nil] new value; omit to read the current one
|
|
57
62
|
# @return [String, nil]
|
|
58
63
|
#
|
|
@@ -87,10 +92,11 @@ require 'colorize'
|
|
|
87
92
|
# @return [ActiveSupport::Duration, Numeric, nil]
|
|
88
93
|
#
|
|
89
94
|
# @!method self.storage(val = nil)
|
|
90
|
-
# Where task records are stored: `:active_record` (default)
|
|
95
|
+
# Where task records are stored: `:active_record` (default), `:mongoid`, or
|
|
96
|
+
# a record class of your own.
|
|
91
97
|
# @see Storage
|
|
92
|
-
# @param val [Symbol, nil] new value; omit to read the current one
|
|
93
|
-
# @return [Symbol]
|
|
98
|
+
# @param val [Symbol, Class, nil] new value; omit to read the current one
|
|
99
|
+
# @return [Symbol, Class]
|
|
94
100
|
#
|
|
95
101
|
# @!method self.task_wrapper(val = nil)
|
|
96
102
|
# Wraps the execution of every task, for example to add logging or error
|
|
@@ -101,7 +107,7 @@ require 'colorize'
|
|
|
101
107
|
# @return [Proc]
|
|
102
108
|
#
|
|
103
109
|
# @!method self.test_framework(val = nil)
|
|
104
|
-
# Test framework for the test file that `
|
|
110
|
+
# Test framework for the test file that `deploy_pin task` creates
|
|
105
111
|
# next to each task; `false` turns test generation off. When not set, RSpec
|
|
106
112
|
# is used if the `RSpec` constant is loaded (e.g. rspec-rails in the
|
|
107
113
|
# `:development` group); otherwise no test is generated.
|
|
@@ -110,7 +116,7 @@ require 'colorize'
|
|
|
110
116
|
# @return [Symbol, false, nil]
|
|
111
117
|
#
|
|
112
118
|
# @!method self.tests_path(val = nil)
|
|
113
|
-
# Directory for the tests that `
|
|
119
|
+
# Directory for the tests that `deploy_pin task` creates. When not
|
|
114
120
|
# set, the test framework's default is used (`spec/deploy_pin` for RSpec).
|
|
115
121
|
# @param val [String, nil] new value; omit to read the current one
|
|
116
122
|
# @return [String, nil]
|
|
@@ -183,6 +189,54 @@ module DeployPin
|
|
|
183
189
|
|
|
184
190
|
setup_defaults!
|
|
185
191
|
|
|
192
|
+
# Configuration files loaded by {load_config} when none is given: the one
|
|
193
|
+
# `deploy_pin install` creates, then the Rails initializer.
|
|
194
|
+
CONFIG_FILES = %w[config/deploy_pin.rb config/initializers/deploy_pin.rb].freeze
|
|
195
|
+
|
|
196
|
+
# The rake file with the `deploy_pin:*` tasks.
|
|
197
|
+
TASKS_FILE = File.expand_path('tasks/deploy_pin_tasks.rake', __dir__)
|
|
198
|
+
|
|
199
|
+
# Defines the `deploy_pin:*` rake tasks. Rails apps get them from {Engine};
|
|
200
|
+
# other apps call this from their Rakefile.
|
|
201
|
+
#
|
|
202
|
+
# The tasks first run the app's `environment` task when it is defined (load
|
|
203
|
+
# the app and connect to the database there), else {load_config}.
|
|
204
|
+
#
|
|
205
|
+
# @example Rakefile
|
|
206
|
+
# require 'deploy_pin'
|
|
207
|
+
# DeployPin.load_tasks
|
|
208
|
+
#
|
|
209
|
+
# task :environment do
|
|
210
|
+
# require_relative 'config/environment'
|
|
211
|
+
# end
|
|
212
|
+
# @return [void]
|
|
213
|
+
def self.load_tasks
|
|
214
|
+
require 'rake'
|
|
215
|
+
load TASKS_FILE
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Loads the configuration: the given files, or else the first of
|
|
219
|
+
# {CONFIG_FILES} that exists in `root`.
|
|
220
|
+
#
|
|
221
|
+
# @param files [Array<String>, String, nil] files to load, e.g. `config/environment.rb`
|
|
222
|
+
# @param root [String] directory the files are relative to
|
|
223
|
+
# @return [Array<String>] the loaded files
|
|
224
|
+
# @raise [ArgumentError] if a given file does not exist
|
|
225
|
+
def self.load_config(files = nil, root: Dir.pwd)
|
|
226
|
+
paths = Array(files).map { |file| File.expand_path(file, root) }
|
|
227
|
+
missing = paths.reject { |path| File.exist?(path) }
|
|
228
|
+
raise ArgumentError, "No such file: #{missing.join(', ')}" if missing.any?
|
|
229
|
+
|
|
230
|
+
paths = default_config_file(root) if paths.empty?
|
|
231
|
+
paths.each { |path| require path }
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# @param root [String] the app directory
|
|
235
|
+
# @return [Array<String>] the first of {CONFIG_FILES} that exists in `root`, if any
|
|
236
|
+
def self.default_config_file(root)
|
|
237
|
+
CONFIG_FILES.map { |file| File.expand_path(file, root) }.select { |path| File.exist?(path) }.first(1)
|
|
238
|
+
end
|
|
239
|
+
|
|
186
240
|
# Whether an option has been set, even to `nil`.
|
|
187
241
|
#
|
|
188
242
|
# @param option [Symbol] one of {OPTIONS}
|
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'rails/generators'
|
|
4
|
+
require 'rails/generators/migration'
|
|
5
|
+
require 'deploy_pin/generators/base'
|
|
6
|
+
|
|
3
7
|
module DeployPin
|
|
4
8
|
# `rails g deploy_pin:install [--storage=mongoid]`: creates the initializer
|
|
5
|
-
# and, for ActiveRecord, the `deploy_pins` migration.
|
|
9
|
+
# and, for ActiveRecord, the `deploy_pins` migration. Outside Rails,
|
|
10
|
+
# `deploy_pin install` ({Generators::Install}) does the same.
|
|
6
11
|
class InstallGenerator < Rails::Generators::Base
|
|
7
12
|
include Rails::Generators::Migration
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
# Rails' own migration action, so numbering (timestamped or sequential) and
|
|
15
|
+
# re-runs behave like other Rails generators. The templates are shared with
|
|
16
|
+
# {Generators::Install}, which `deploy_pin install` runs outside Rails.
|
|
17
|
+
source_root File.join(DeployPin::Generators::Base::TEMPLATES_ROOT, 'install')
|
|
10
18
|
desc 'Add the migration & initializer for DeployPin'
|
|
11
19
|
|
|
12
20
|
class_option :storage, type: :string, default: 'active_record', enum: %w[active_record mongoid],
|
|
@@ -1,130 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'rails/generators'
|
|
4
|
+
require 'deploy_pin/generators/rails_generator'
|
|
5
|
+
|
|
3
6
|
module DeployPin
|
|
4
|
-
# `rails g deploy_pin:task <title> --group <group
|
|
5
|
-
#
|
|
6
|
-
# the configured {DeployPin.test_framework} in {DeployPin.tests_path}
|
|
7
|
-
# (`spec/deploy_pin` for RSpec by default).
|
|
8
|
-
#
|
|
9
|
-
# @example Skip the test, or pick the framework for one task
|
|
10
|
-
# rails g deploy_pin:task backfill_users -g I --skip-test
|
|
11
|
-
# rails g deploy_pin:task backfill_users -g I --test-framework=rspec
|
|
7
|
+
# `rails g deploy_pin:task <title> --group <group>`: creates a task and its
|
|
8
|
+
# test. Runs {Generators::Task}, see there for the options.
|
|
12
9
|
class TaskGenerator < Rails::Generators::Base
|
|
13
|
-
|
|
14
|
-
# when {DeployPin.tests_path} is not set) and the test file name, where
|
|
15
|
-
# `%<task>s` is the task file name without `.rb`.
|
|
16
|
-
TEST_FRAMEWORKS = {
|
|
17
|
-
rspec: { path: 'spec/deploy_pin', file: '%<task>s_spec.rb' }
|
|
18
|
-
}.freeze
|
|
19
|
-
|
|
20
|
-
# Printed when no test framework is configured and none is detected.
|
|
21
|
-
UNDETECTED_TEST_FRAMEWORK = 'test for the task: no test framework detected. ' \
|
|
22
|
-
'Add rspec-rails to the :development group, or set DeployPin.test_framework :rspec'
|
|
23
|
-
|
|
24
|
-
class_option :parallel, type: :boolean, aliases: '-p'
|
|
25
|
-
class_option :recurring, type: :boolean, aliases: '-r'
|
|
26
|
-
class_option :identifier, aliases: '-i'
|
|
27
|
-
argument :title, required: true
|
|
28
|
-
class_option :group, aliases: '-g', default: DeployPin.fallback_group
|
|
29
|
-
class_option :author, aliases: '-a'
|
|
30
|
-
class_option :test_framework, type: :string,
|
|
31
|
-
desc: "Test framework for the task's test (default: DeployPin.test_framework)"
|
|
32
|
-
class_option :skip_test, type: :boolean, default: false, desc: 'Do not generate a test for the task'
|
|
33
|
-
|
|
34
|
-
source_root File.expand_path('templates', __dir__)
|
|
35
|
-
|
|
36
|
-
desc 'This generator creates deploy_pin task at lib/deploy_pin/'
|
|
37
|
-
|
|
38
|
-
def initialize(*args)
|
|
39
|
-
super
|
|
40
|
-
|
|
41
|
-
validate
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# Writes the task file, named after its identifier and title.
|
|
45
|
-
# @return [void]
|
|
46
|
-
def create_task_file
|
|
47
|
-
template_file = if options[:parallel]
|
|
48
|
-
'parallel_task.rb.erb'
|
|
49
|
-
else
|
|
50
|
-
'task.rb.erb'
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
@author = options[:author] || ENV['USER']
|
|
54
|
-
@group = options[:group]
|
|
55
|
-
@recurring = options[:recurring]
|
|
56
|
-
@identifier = @recurring ? options[:identifier] : Time.now.strftime('%Y%m%d%H%M%S')
|
|
57
|
-
template template_file, task_path
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# Writes the test for the task, unless tests are turned off.
|
|
61
|
-
# @return [void]
|
|
62
|
-
def create_test_file
|
|
63
|
-
framework = test_framework
|
|
64
|
-
return if framework == false # turned off on purpose
|
|
65
|
-
return say_status(:skip, UNDETECTED_TEST_FRAMEWORK, :yellow) unless framework
|
|
66
|
-
|
|
67
|
-
template "#{framework}/task_spec.rb.erb", test_file_path(framework)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
# @return [void]
|
|
71
|
-
# @raise [Thor::Error] if the group is missing or not in {DeployPin.groups},
|
|
72
|
-
# or the test framework is not one of {TEST_FRAMEWORKS}
|
|
73
|
-
def validate
|
|
74
|
-
raise Thor::Error, set_color('Missing required option: --group', :red) if options[:group].blank?
|
|
75
|
-
|
|
76
|
-
unless DeployPin.groups.include?(options[:group])
|
|
77
|
-
raise Thor::Error, set_color("Group '#{options[:group]}' is not defined in DeployPin.groups", :red)
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
validate_test_framework
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
private
|
|
84
|
-
|
|
85
|
-
# The task file name without the extension, e.g. `20190401135040_backfill_users`.
|
|
86
|
-
def task_name
|
|
87
|
-
@recurring ? "r_#{@identifier}_#{title}" : "#{@identifier}_#{title}"
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def task_path
|
|
91
|
-
"#{DeployPin.tasks_path}/#{task_name}.rb"
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# The test file in {DeployPin.tests_path}, or in the framework's default directory.
|
|
95
|
-
def test_file_path(framework)
|
|
96
|
-
settings = TEST_FRAMEWORKS.fetch(framework)
|
|
97
|
-
directory = DeployPin.tests_path.presence || settings[:path]
|
|
98
|
-
|
|
99
|
-
File.join(directory.to_s, format(settings[:file], task: task_name))
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
# The --test-framework flag or DeployPin.test_framework, else the detected one.
|
|
103
|
-
#
|
|
104
|
-
# @return [Symbol, false, nil] the framework; false when tests are turned
|
|
105
|
-
# off (--skip-test, `test_framework false`); nil when none is detected
|
|
106
|
-
def test_framework
|
|
107
|
-
return false if options[:skip_test]
|
|
108
|
-
|
|
109
|
-
configured = options[:test_framework].presence || DeployPin.test_framework
|
|
110
|
-
return configured if configured == false
|
|
111
|
-
|
|
112
|
-
configured&.to_sym || detected_test_framework
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
# RSpec is the only supported framework, detected by its constant
|
|
116
|
-
# (no dependency on Rails' generator settings).
|
|
117
|
-
def detected_test_framework
|
|
118
|
-
:rspec if defined?(::RSpec)
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
def validate_test_framework
|
|
122
|
-
framework = test_framework
|
|
123
|
-
return if !framework || TEST_FRAMEWORKS.key?(framework)
|
|
10
|
+
include DeployPin::Generators::RailsGenerator
|
|
124
11
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
)
|
|
128
|
-
end
|
|
12
|
+
runs DeployPin::Generators::Task
|
|
13
|
+
desc File.read(File.expand_path('USAGE', __dir__))
|
|
129
14
|
end
|
|
130
15
|
end
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'rails/generators'
|
|
4
|
+
require 'rails/generators/migration'
|
|
5
|
+
require 'deploy_pin/generators/base'
|
|
6
|
+
|
|
3
7
|
module DeployPin
|
|
4
8
|
# This generator is used to generate the migration file for upgrading db schema for 1.7 version support.
|
|
5
9
|
class UpgradeGenerator < Rails::Generators::Base
|
|
6
10
|
include Rails::Generators::Migration
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
# Rails' own migration action, so numbering (timestamped or sequential) and
|
|
13
|
+
# re-runs behave like other Rails generators. The templates are shared with
|
|
14
|
+
# {Generators::Upgrade}, which `deploy_pin upgrade` runs outside Rails.
|
|
15
|
+
source_root File.join(DeployPin::Generators::Base::TEMPLATES_ROOT, 'upgrade')
|
|
9
16
|
desc 'Add the upgrade migration for DeployPin.'
|
|
10
17
|
|
|
11
18
|
# @param path [String] migrations directory
|
|
@@ -1,32 +1,54 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
namespace :deploy_pin do
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
# The groups and task identifiers of the task arguments; all groups only when
|
|
5
|
+
# no argument is given. An explicit empty selection (`invoke([])`) selects
|
|
6
|
+
# nothing, as in 1.10.0. From the command line, Rake splits
|
|
7
|
+
# `deploy_pin:run[I, !II]` on commas: the first value is `args.identifiers`
|
|
8
|
+
# and the others are in `args.extras`. From Ruby, `invoke` may also get a
|
|
9
|
+
# string with commas ('I, !II'), an array (%w[I II]) or identifiers as
|
|
10
|
+
# integers: strings are split, the rest is kept.
|
|
11
|
+
parse_identifiers = lambda do |args|
|
|
12
|
+
next DeployPin.groups if args.identifiers.nil? && args.extras.empty?
|
|
13
|
+
|
|
14
|
+
values = [args.identifiers, *args.extras].flatten.compact
|
|
15
|
+
|
|
16
|
+
values.flat_map { |value| value.is_a?(String) ? value.split(/\s*,\s*/).reject(&:empty?) : [value] }
|
|
17
|
+
end
|
|
8
18
|
|
|
9
|
-
|
|
19
|
+
# Loads the app: its `environment` task when it has one (Rails, Hanami, or
|
|
20
|
+
# your own), else the DeployPin configuration file (see DeployPin.load_config).
|
|
21
|
+
task :environment do
|
|
22
|
+
if Rake::Task.task_defined?(:environment)
|
|
23
|
+
Rake::Task[:environment].invoke
|
|
24
|
+
elsif DeployPin.groups.nil?
|
|
25
|
+
DeployPin.load_config
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
desc 'run pending tasks'
|
|
30
|
+
task :run, [:identifiers] => 'deploy_pin:environment' do |_t, args|
|
|
31
|
+
DeployPin::Runner.run(identifiers: parse_identifiers.call(args))
|
|
10
32
|
end
|
|
11
33
|
|
|
12
|
-
task :list, [:identifiers] => :environment do |_t, args|
|
|
13
|
-
|
|
34
|
+
task :list, [:identifiers] => 'deploy_pin:environment' do |_t, args|
|
|
35
|
+
identifiers = parse_identifiers.call(args)
|
|
14
36
|
|
|
15
|
-
DeployPin::Runner.list(
|
|
16
|
-
DeployPin::Runner.summary(
|
|
37
|
+
DeployPin::Runner.list(identifiers:)
|
|
38
|
+
DeployPin::Runner.summary(identifiers:)
|
|
17
39
|
end
|
|
18
40
|
|
|
19
|
-
task :short_list, [:identifiers] => :environment do |_t, args|
|
|
20
|
-
|
|
41
|
+
task :short_list, [:identifiers] => 'deploy_pin:environment' do |_t, args|
|
|
42
|
+
identifiers = parse_identifiers.call(args)
|
|
21
43
|
|
|
22
|
-
DeployPin::Runner.short_list(
|
|
23
|
-
DeployPin::Runner.summary(
|
|
44
|
+
DeployPin::Runner.short_list(identifiers:)
|
|
45
|
+
DeployPin::Runner.summary(identifiers:)
|
|
24
46
|
end
|
|
25
47
|
|
|
26
|
-
task :mark_done, [:identifiers] => :environment do |_t, args|
|
|
27
|
-
|
|
48
|
+
task :mark_done, [:identifiers] => 'deploy_pin:environment' do |_t, args|
|
|
49
|
+
identifiers = parse_identifiers.call(args)
|
|
28
50
|
|
|
29
|
-
DeployPin::Runner.mark_done(
|
|
30
|
-
DeployPin::Runner.summary(
|
|
51
|
+
DeployPin::Runner.mark_done(identifiers:)
|
|
52
|
+
DeployPin::Runner.summary(identifiers:)
|
|
31
53
|
end
|
|
32
54
|
end
|