deploy_pin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4914431f26a50b1860ca43f67f44cacce2f60657e0b15a509a4d374395f42631
4
+ data.tar.gz: 638c64f5c4e57a422c287b5aef988cc9bc962213a38a1a7efc58c238cf395dac
5
+ SHA512:
6
+ metadata.gz: a8e26e27ed3f6cd8303637e595bec231b03a834c889253817aa6eb8fdc811a73f0360388bbd5567df912b8772a09a41bcf1814943f7afde179aa5aa1d971bb5a
7
+ data.tar.gz: '00168ce157b6cb8c3d8bc67edbefa37fa251cd152953425407fbeb544ee3946834c2f1c368c6fe524a81c5ef56f645e9749ca8705494b5bce9eaf7ded8d3586d'
@@ -0,0 +1,20 @@
1
+ Copyright 2019 rafael
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.
@@ -0,0 +1,86 @@
1
+ [![Build Status](https://travis-ci.org/skcc321/deploy_pin.svg?branch=master)](https://travis-ci.org/skcc321/deploy_pin)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/c0a9ca97c1f9c0478ffc/maintainability)](https://codeclimate.com/github/skcc321/deploy_pin/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/c0a9ca97c1f9c0478ffc/test_coverage)](https://codeclimate.com/github/skcc321/deploy_pin/test_coverage)
4
+
5
+ # DeployPin
6
+
7
+ ![DeployPin](http://hereisfree.com/content1//pic/zip/2009109935062477801.jpg)
8
+
9
+ Sometimes we need to execute set of commands (tasks) after/before deployment.
10
+ Most likely you use migrations for such things, but that is not what is related to migration at all.
11
+ Also sometimes you need to execute some code before migration or later, after migration without blocking of main thread.
12
+
13
+ deploy_pins is exactly what you need.
14
+
15
+ ## Usage
16
+
17
+ To generate new task template file
18
+ ```bash
19
+ rails g deploy_pin:task
20
+ # or
21
+ rails g deploy_pin:task --parallel
22
+ ```
23
+
24
+ To list all pending tasks
25
+ ```bash
26
+ rake deploy_pin:list
27
+ ```
28
+
29
+ To run all pending tasks
30
+ ```bash
31
+ rake deploy_pin:run
32
+ ```
33
+
34
+ ### Groupped tasks
35
+ ~~~ please define allowed groups in config/initializers/deploy_pin.rb ~~~
36
+
37
+ if you want to group tasks around "allowed_group"
38
+
39
+ ```bash
40
+ rails g deploy_pin:task allowed_group
41
+ # or
42
+ rails g deploy_pin:task allowed_group --parallel
43
+ ```
44
+
45
+ To list all pending tasks
46
+ ```bash
47
+ rake deploy_pin:list[allowed_group]
48
+ ```
49
+
50
+ To run all pending tasks
51
+ ```bash
52
+ rake deploy_pin:run[allowed_group]
53
+ ```
54
+
55
+ ## Installation
56
+ Add this line to your application's Gemfile:
57
+
58
+ ```ruby
59
+ gem 'deploy_pin'
60
+ ```
61
+
62
+ And then execute:
63
+ ```bash
64
+ $ bundle
65
+ ```
66
+
67
+ Or install it yourself as:
68
+ ```bash
69
+ $ gem install deploy_pin
70
+ ```
71
+
72
+ ### then generate configuration file
73
+ ```bash
74
+ rails g deploy_pin:install
75
+ ```
76
+
77
+ and run migration
78
+ ```bash
79
+ rake db:migrate
80
+ ```
81
+
82
+ ## Contributing
83
+ Contribution directions go here.
84
+
85
+ ## License
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DeployPin'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,3 @@
1
+ class DeployPin::ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeployPin::Record < DeployPin::ApplicationRecord
4
+ self.table_name = 'deploy_pins'
5
+ end
6
+
@@ -0,0 +1,27 @@
1
+ require "deploy_pin/runner"
2
+ require "deploy_pin/task"
3
+ require "deploy_pin/engine"
4
+ require "parallel"
5
+ require "ruby-progressbar"
6
+
7
+ module DeployPin
8
+ OPTIONS = %i(
9
+ tasks_path
10
+ fallback_group
11
+ groups
12
+ )
13
+
14
+ OPTIONS.each do |option|
15
+ instance_eval %{
16
+ def #{option}(val = nil)
17
+ return @@#{option} unless val.present?
18
+
19
+ @@#{option} = val
20
+ end
21
+ }
22
+ end
23
+
24
+ def self.setup(&block)
25
+ instance_eval(&block)
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ class DeployPin::Engine < ::Rails::Engine
2
+ isolate_namespace DeployPin
3
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # executes tasks
4
+ module DeployPin::Runner
5
+ def self.run(group:)
6
+ tasks = pending(group: group)
7
+ tasks.each_with_index do |task, index|
8
+ puts "[#{index + 1}/#{tasks.count}] Task UUID #{task.uuid}"
9
+ task.run
10
+ puts ""
11
+ end
12
+ end
13
+
14
+ def self.list(group:)
15
+ pending(group: group).each_with_index do |task, index|
16
+ puts "======= Task ##{index} ========"
17
+ puts task.script
18
+ puts ""
19
+ end
20
+
21
+ puts "======= summary ========"
22
+ puts "tasks number: #{pending(group: group).count}"
23
+ end
24
+
25
+ def self.pending(group:)
26
+ files = Dir["#{DeployPin.tasks_path}/*.rb"]
27
+
28
+ records = DeployPin::Record.pluck(:uuid)
29
+
30
+ files.map do |file|
31
+ task = DeployPin::Task.new(file)
32
+ task.parse_file
33
+ task unless records.include?(task.uuid) || task.group != group
34
+ end.compact
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Task wrapper
4
+ class DeployPin::Task
5
+ attr_reader :file,
6
+ :group,
7
+ :uuid,
8
+ :script
9
+
10
+ def initialize(file)
11
+ @file = file
12
+ @uuid = nil
13
+ @group = nil
14
+ @script = ""
15
+ end
16
+
17
+ def run
18
+ eval(@script)
19
+ end
20
+
21
+ def parse_file
22
+ @script = File.read(file)
23
+ @script.lines[0] =~ /\A# (\d+):(\w+)/
24
+ @uuid = $1
25
+ @group = $2
26
+ end
27
+
28
+ def details
29
+ puts @uuid, @group, @script
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module DeployPin
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates base migration to store tasks state in the DB
3
+
4
+ Example:
5
+ rails generate deploy_pin:install
6
+
7
+ This will create:
8
+ db/migrate/create_deploy_pins.rb
@@ -0,0 +1,19 @@
1
+ class DeployPin::InstallGenerator < Rails::Generators::Base
2
+ include Rails::Generators::Migration
3
+
4
+ source_root File.expand_path('templates', __dir__)
5
+ desc "Add the migration & initializer for DeployPin"
6
+
7
+ def self.next_migration_number(path)
8
+ next_migration_number = current_migration_number(path) + 1
9
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
10
+ end
11
+
12
+ def copy_migrations
13
+ migration_template "create_deploy_pins.rb", "db/migrate/create_deploy_pins.rb"
14
+ end
15
+
16
+ def copy_initializer
17
+ template "deploy_pin.rb", "config/initializers/deploy_pin.rb"
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ class CreateDeployPins < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :deploy_pins do |t|
4
+ t.string :uuid
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ DeployPin.setup do
4
+ tasks_path "lib/deploy_pin"
5
+ groups ["post_script"]
6
+ fallback_group "post_script"
7
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates tasks for execution on deployment
3
+
4
+ Example:
5
+ rails generate deploy_pin:task
6
+
7
+ This will create:
8
+ lib/deploy_pin/task.rb
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeployPin::TaskGenerator < Rails::Generators::Base
4
+ class_option :parallel, type: :boolean
5
+ argument :group, default: "post_script"
6
+
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ desc 'This generator creates deploy_pin task at lib/deploy_pin/'
10
+ def create_task_file
11
+ raise "Not allowed 'group' argument! possible values are #{DeployPin.groups}" \
12
+ unless DeployPin.groups.include?(group)
13
+
14
+ template_file = if options[:parallel]
15
+ 'parallel_task.rb.erb'
16
+ else
17
+ 'task.rb.erb'
18
+ end
19
+
20
+ @uuid = Time.now.strftime('%Y%m%d%H%M%S')
21
+ template template_file, "#{DeployPin.tasks_path}/#{@uuid}_task.rb"
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # <%= @uuid %>:<%= group %>
2
+
3
+ # === parallel task code goes down here ===
4
+ 10.times { DeployPin::Record.create(uuid: "hello") }
5
+
6
+ Parallel.each(DeployPin::Record.where(uuid: "hello"), progress: "Doing stuff") do |pin|
7
+ ActiveRecord::Base.connection_pool.with_connection do
8
+ pin.update_attribute(:uuid, "new uuid")
9
+ end
10
+ sleep(0.2)
11
+ end
12
+
13
+
14
+
15
+ # Create record in the DB to avoid multiple execution
16
+ DeployPin::Record.create(uuid: "<%= @uuid %>")
@@ -0,0 +1,13 @@
1
+ # <%= @uuid %>:<%= group %>
2
+
3
+ # === task code goes down here ===
4
+ progressbar = ProgressBar.create(title: "Doing stuff", total: 20, format: '%t |%E | %B | %a')
5
+
6
+ 20.times { progressbar.increment; sleep(0.2) }
7
+
8
+
9
+
10
+
11
+
12
+ # Create record in the DB to avoid multiple execution
13
+ DeployPin::Record.create(uuid: "<%= @uuid %>")
@@ -0,0 +1,14 @@
1
+ namespace :deploy_pin do
2
+ desc "run pending tasks"
3
+ task :run, [:group] => :environment do |t, args|
4
+ args.with_defaults(group: DeployPin.fallback_group)
5
+
6
+ DeployPin::Runner.run(args)
7
+ end
8
+
9
+ task :list, [:group] => :environment do |t, args|
10
+ args.with_defaults(group: DeployPin.fallback_group)
11
+
12
+ DeployPin::Runner.list(args)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deploy_pin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rafael
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-26 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: 5.2.2
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.2.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 5.2.2
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.2.2.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: parallel
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: ruby-progressbar
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.10'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.10'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: bundler
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.17'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.17'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.12'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.12'
103
+ - !ruby/object:Gem::Dependency
104
+ name: minitest
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '5.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '5.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: mysql2
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 0.5.2
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 0.5.2
131
+ - !ruby/object:Gem::Dependency
132
+ name: pg
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '0.18'
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 0.18.4
141
+ type: :development
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '0.18'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 0.18.4
151
+ description: pin some task around deployment to execute them during deployment circle
152
+ email:
153
+ - skcc321@gmail.com
154
+ executables: []
155
+ extensions: []
156
+ extra_rdoc_files: []
157
+ files:
158
+ - MIT-LICENSE
159
+ - README.md
160
+ - Rakefile
161
+ - app/models/deploy_pin/application_record.rb
162
+ - app/models/deploy_pin/record.rb
163
+ - lib/deploy_pin.rb
164
+ - lib/deploy_pin/engine.rb
165
+ - lib/deploy_pin/runner.rb
166
+ - lib/deploy_pin/task.rb
167
+ - lib/deploy_pin/version.rb
168
+ - lib/generators/deploy_pin/install/USAGE
169
+ - lib/generators/deploy_pin/install/install_generator.rb
170
+ - lib/generators/deploy_pin/install/templates/create_deploy_pins.rb
171
+ - lib/generators/deploy_pin/install/templates/deploy_pin.rb
172
+ - lib/generators/deploy_pin/task/USAGE
173
+ - lib/generators/deploy_pin/task/task_generator.rb
174
+ - lib/generators/deploy_pin/task/templates/parallel_task.rb.erb
175
+ - lib/generators/deploy_pin/task/templates/task.rb.erb
176
+ - lib/tasks/deploy_pin_tasks.rake
177
+ homepage: https://github.com/skcc321/deploy_pin
178
+ licenses:
179
+ - MIT
180
+ metadata:
181
+ allowed_push_host: https://rubygems.org
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.7.6
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: pin some task around deployment
202
+ test_files: []