sneaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24e059214d6bade0c5fc4592020d413173bac7ed
4
+ data.tar.gz: 6a4a376ef7e6b41ee25ab3ac284c5f0d3f5307c3
5
+ SHA512:
6
+ metadata.gz: 4aed58f7fd4af2e9be692dfece1b2a3c02b82f6beb657f2f32908d10a016cdda0d1423bc1bb38aa6aa11ac8edc9ff6f21955f08b846c18c2d7f19bcbbfd8a481
7
+ data.tar.gz: 5df67eb52af192d9e9030305b7a6c3f90c59748832c74f29f253623d353e8c686969d38a14ec0b624717b321a9e8773e50a8d2ee066fb8fccba12bba0bfb1934
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sneaker.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Sneaker
2
+
3
+ Sneaker lets you launch a rails command in multiple capistrano stages simultaneously
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sneaker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sneaker
20
+
21
+ ## Usage
22
+
23
+ Use any command like inside a `rails console`
24
+
25
+ ```sh
26
+ sneaker User.count
27
+ ```
28
+
29
+ Escape commands with single quotes
30
+ ```sh
31
+ sneaker 'User.where(updated_at: 1.week.ago..1.day.ago).map(&:login)'
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/i4a/sneaker.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sneaker"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/sneaker ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'sneaker/command'
6
+
7
+ Sneaker::Command.new(ARGV.join(' ')).execute
data/lib/sneaker.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "sneaker/version"
2
+
3
+ module Sneaker
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sneaker/presentation'
4
+ require 'sneaker/stage'
5
+
6
+ module Sneaker
7
+ class Command
8
+ def initialize(command)
9
+ @command = command
10
+ end
11
+
12
+ def execute
13
+ if stage?
14
+ single_execute
15
+ else
16
+ multiple_execute
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def stage
23
+ @stage ||= Stage.find(ENV['stage'])
24
+ end
25
+
26
+ def stage?
27
+ !stage.nil?
28
+ end
29
+
30
+ def single_execute
31
+ puts stage.execute(@command)
32
+ end
33
+
34
+ def multiple_execute
35
+ stages = Stage.all
36
+ presentation = Presentation.new(stages.map(&:name))
37
+ threads = []
38
+ mutex = Mutex.new
39
+
40
+ stages.each do |stage|
41
+ thread = Thread.new do
42
+ result = stage.execute @command
43
+
44
+ mutex.synchronize do
45
+ presentation.set(stage.name, result)
46
+ end
47
+ end
48
+
49
+ threads.push(thread)
50
+ end
51
+
52
+ # Wait for threads to finish
53
+ threads.each(&:join)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tty-table'
4
+ require 'tty-cursor'
5
+ require 'pastel'
6
+
7
+ module Sneaker
8
+ class Presentation
9
+ def initialize(stages)
10
+ @table = []
11
+ @stages = stages.sort
12
+ @cursor = TTY::Cursor
13
+ @pastel = Pastel.new
14
+
15
+ @stages.each do |stage|
16
+ @table << [formatted_stage(stage), @pastel.inverse('loading...')]
17
+ end
18
+
19
+ draw
20
+ end
21
+
22
+ def set(stage, result)
23
+ set_in_table(stage, result)
24
+
25
+ redraw
26
+ end
27
+
28
+ private
29
+
30
+ def formatted_stage(stage)
31
+ @pastel.bold(stage)
32
+ end
33
+
34
+ def formatted_result(result)
35
+ return @pastel.on_red('!error') if result == :error
36
+ return @pastel.red(result) if result == 'false'
37
+ return @pastel.green(result) if result == 'true'
38
+ result
39
+ end
40
+
41
+ def set_in_table(stage, result)
42
+ @table.each do |row|
43
+ row[1] = formatted_result(result) if row.first == formatted_stage(stage)
44
+ end
45
+ end
46
+
47
+ def redraw
48
+ clear
49
+ draw
50
+ end
51
+
52
+ def clear
53
+ print @cursor.up(@tty_table.rows_size + 2)
54
+ print @cursor.clear_line_after
55
+ end
56
+
57
+ def draw
58
+ @tty_table = TTY::Table.new(@table)
59
+ puts @tty_table.render(:unicode)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
4
+
5
+ module Sneaker
6
+ class Stage
7
+ LIST_DIR = File.join(File.dirname(__FILE__), '..', '..', 'config', 'deploy')
8
+
9
+ RAILS_RUNNER_ENV = 'RUBY_GC_TUNE_VERBOSE=0'
10
+
11
+ def self.files
12
+ @files ||=
13
+ Dir[File.join(LIST_DIR, '**rb')]
14
+ end
15
+
16
+ def self.list
17
+ @list ||= build_list
18
+ end
19
+
20
+ def self.find(name)
21
+ list.find { |stage| stage.name == name }
22
+ end
23
+
24
+ def self.build_list
25
+ list =
26
+ files.map do |file|
27
+ new file
28
+ end
29
+
30
+ list.reject { |stage| BAD_STAGES.include?(stage.name) }
31
+ end
32
+
33
+ private_class_method :build_list
34
+
35
+ class << self
36
+ alias all list
37
+ end
38
+
39
+ attr_reader :file
40
+
41
+ def initialize(file)
42
+ @file = file
43
+ end
44
+
45
+ def name
46
+ @name ||=
47
+ File.basename(file)[0...-3]
48
+ end
49
+
50
+ def server
51
+ @server ||=
52
+ server_line[/server '([\.\d\w]+)'/, 1]
53
+ end
54
+
55
+ def user
56
+ @user ||=
57
+ server_line[/user: '([\w]+)'/, 1]
58
+ end
59
+
60
+ def name_application
61
+ @name_application ||=
62
+ name_application_line[/:name_application, '([\w-]+)'/, 1]
63
+ end
64
+
65
+ def execute(rails_command)
66
+ rails_command = "Account.current = Account.where(name: '#{name_application}').first; print proc { #{rails_command} }.call"
67
+ escaped_rails_command = escape_single_quotes(rails_command)
68
+
69
+ server_command = "cd app/current; #{RAILS_RUNNER_ENV} bundle exec rails r '#{escaped_rails_command}'"
70
+ escaped_server_command = escape_single_quotes(server_command)
71
+
72
+ result = `ssh #{force_keys} #{user}@#{server} '#{escaped_server_command}' #{debug}`
73
+
74
+ $CHILD_STATUS.exitstatus.positive? ? :error : result
75
+ end
76
+
77
+ private
78
+
79
+ def server_line
80
+ file_grep(/^server/).first
81
+ end
82
+
83
+ def name_application_line
84
+ file_grep(/^set :name_application/).first
85
+ end
86
+
87
+ def file_grep(pattern)
88
+ File.open(file).grep(pattern)
89
+ end
90
+
91
+ # In shell, a ' character within a 'example' argument must be escaped as '"'"
92
+ #
93
+ # :reek:UtilityFunction really need to move to String class?
94
+ def escape_single_quotes(string)
95
+ string.gsub("'", %q('"'"'))
96
+ end
97
+
98
+ def force_keys
99
+ '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' if ENV['force_keys']
100
+ end
101
+
102
+ def debug
103
+ '2>/dev/null' if !ENV['debug']
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module Sneaker
2
+ VERSION = "0.1.0"
3
+ end
data/sneaker.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sneaker/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sneaker"
8
+ spec.version = Sneaker::VERSION
9
+ spec.authors = ["Ideas4allInnovation"]
10
+ spec.email = ["admin@ideas4allinnovation.com"]
11
+
12
+ spec.summary = %q{Sneak rails commands in multiple capistrano stages}
13
+ spec.description = %q{Sneaker lets you launch a rails command in multiple capistrano stages simultaneously}
14
+ spec.homepage = "https://github.com/i4a/sneaker"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+
27
+ spec.add_dependency "tty-table", "~> 0.10.0"
28
+ spec.add_dependency "tty-cursor", "~> 0.5.0"
29
+ spec.add_dependency "pastel", "~> 0.7.2"
30
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sneaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ideas4allInnovation
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-table
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: tty-cursor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.5.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.5.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pastel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.7.2
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.7.2
97
+ description: Sneaker lets you launch a rails command in multiple capistrano stages
98
+ simultaneously
99
+ email:
100
+ - admin@ideas4allinnovation.com
101
+ executables:
102
+ - sneaker
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/sneaker
115
+ - lib/sneaker.rb
116
+ - lib/sneaker/command.rb
117
+ - lib/sneaker/presentation.rb
118
+ - lib/sneaker/stage.rb
119
+ - lib/sneaker/version.rb
120
+ - sneaker.gemspec
121
+ homepage: https://github.com/i4a/sneaker
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.14
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Sneak rails commands in multiple capistrano stages
144
+ test_files: []
145
+ has_rdoc: