capistrano-fault-tolerant 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
+ SHA1:
3
+ metadata.gz: d4fd77f8ab1d2d67fa617a9bfc792bbdd9727eb1
4
+ data.tar.gz: bd8d1db0485e2719d62b9160fb86fefeda7fbd9d
5
+ SHA512:
6
+ metadata.gz: 724eed828872091b45d2db66ac2dc90089ea7ec323ca0557dcf3c4e5b9e7e16a58912899f99ff657305532cc70b23d80334f73d1daf1d5fac7f274a025b03334
7
+ data.tar.gz: 587da335e2534d3a02a2741fb5443f36366023a7c119d48ec722132cca9a623e42e039671c6deed24a8ce265ab6b1e01a84b678f620469d89fa387de81b77130
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-fault-tolerant.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kir Shatrov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Fault Tolerant Capistrano
2
+
3
+ Imagine you have hundreds of hosts in Capistrano, and you'd like to allow specific command to fail on `5%` of your hosts.
4
+ The gem brings fault tolerant commands to Capistrano DSL:
5
+
6
+ ```ruby
7
+ on roles(:web), failure_tolerance: 0.05 do
8
+ within release_path do
9
+ execute "./script-that-may-fail-sometimes"
10
+ end
11
+ end
12
+ ```
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ # it's important to set `require: false`
21
+ gem 'capistrano-fault-tolerant', require: false
22
+ ```
23
+
24
+ You'll also need the edge version of SSHKit which includes [the commit](https://github.com/capistrano/sshkit/commit/8d1ca5202cfd4dfd73b67412c99aafc655640060):
25
+
26
+ ```
27
+ gem 'sshkit', github: 'capistrano/sshkit'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+
35
+ ## Usage
36
+
37
+ Add to `Capfile`:
38
+ ```ruby
39
+ require 'capistrano-fault-tolerant'
40
+ ```
41
+
42
+ Now you can use `failure_tolerance` option with any command:
43
+
44
+ ```ruby
45
+ on roles(:web), failure_tolerance: 0.05 do
46
+ within release_path do
47
+ execute "./script-that-may-fail-sometimes"
48
+ end
49
+ end
50
+ ```
51
+
52
+ **If > 5% of `web` hosts will fail, Capistrano will continue to deploy.**
53
+
54
+ **When < 5% of `web` hosts will fail, Capistrano will stop with an exception as it usually does with failed commands.**
55
+
56
+ The gem can also call a callback on new failed host:
57
+
58
+ ```
59
+ # config/deploy.rb
60
+ CapistranoFaultTolerant.on_failed_host = ->(host) {
61
+ # notify someone about failed host
62
+ puts "HOST FAILED: #{host}"
63
+ }
64
+ ```
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/capistrano-fault-tolerant.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano-fault-tolerant/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "capistrano-fault-tolerant"
8
+ spec.version = CapistranoFaultTolerant::VERSION
9
+ spec.authors = ["Kir Shatrov"]
10
+ spec.email = ["shatrov@me.com"]
11
+
12
+ spec.summary = %q{Brings fault tolerant command to Capistrano}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/kirs/capistrano-fault-tolerant"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "capistrano", "~> 3.4"
23
+ spec.add_dependency "sshkit", "~> 1.8"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest"
28
+ end
@@ -0,0 +1,73 @@
1
+ require 'sshkit'
2
+ require 'thread'
3
+
4
+ module CapistranoFaultTolerant
5
+ class FailedHosts
6
+ TooManyHostsFailedError = Class.new(StandardError)
7
+
8
+ def initialize(total_hosts, tolerance)
9
+ @max_hosts_to_fail = (total_hosts * tolerance).floor
10
+ @hosts = Set.new
11
+ end
12
+
13
+ attr_reader :hosts
14
+
15
+ def push(host)
16
+ if @hosts.size >= @max_hosts_to_fail
17
+ raise TooManyHostsFailedError, "the limit of failed hosts is full. Failed hosts: #{@hosts.to_a.join("\n")}"
18
+ end
19
+
20
+ @hosts << host
21
+ if CapistranoFaultTolerant.on_failed_host
22
+ CapistranoFaultTolerant.on_failed_host.call(host)
23
+ end
24
+ end
25
+
26
+ def include?(host)
27
+ @hosts.include?(host)
28
+ end
29
+ end
30
+
31
+ def self.on_failed_host
32
+ @on_failed_host
33
+ end
34
+
35
+ def self.on_failed_host=(val)
36
+ @on_failed_host = val
37
+ end
38
+
39
+ class Runner < SSHKit::Runner::Abstract
40
+ def execute
41
+ threads = []
42
+ hosts.each do |host|
43
+ threads << Thread.new(host) do |h|
44
+ begin
45
+ backend(h, &block).run
46
+ rescue StandardError, Errno::ETIMEDOUT, Net::SSH::ConnectionTimeout => e
47
+ b = SSHKit.config.backend.new(h)
48
+ b.error "Exception while executing #{host.user ? "as #{host.user}@" : "on host "}#{host}: #{e.message}"
49
+
50
+ options[:failed_hosts].push(h)
51
+ end
52
+ end
53
+ end
54
+ threads.map(&:join)
55
+ end
56
+ end
57
+
58
+ module ExtendedDSL
59
+ def on(hosts, options={}, &block)
60
+ if options[:failure_tolerance]
61
+ options[:failed_hosts] = CapistranoFaultTolerant::FailedHosts.new(hosts.size, options.delete(:failure_tolerance))
62
+ options[:in] = CapistranoFaultTolerant::Runner
63
+ end
64
+ super(hosts, options, &block)
65
+ end
66
+ end
67
+ end
68
+
69
+ if defined?(Capistrano)
70
+ self.extend CapistranoFaultTolerant::ExtendedDSL
71
+ else
72
+ raise "capistrano_fault_tolerant gem should be required only in Capfile"
73
+ end
@@ -0,0 +1,3 @@
1
+ module CapistranoFaultTolerant
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-fault-tolerant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kir Shatrov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sshkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - shatrov@me.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/setup
97
+ - capistrano-fault-tolerant.gemspec
98
+ - lib/capistrano-fault-tolerant.rb
99
+ - lib/capistrano-fault-tolerant/version.rb
100
+ homepage: https://github.com/kirs/capistrano-fault-tolerant
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.5.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Brings fault tolerant command to Capistrano
124
+ test_files: []