capistrano-forwarding 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: 9bec3e813b57388649728bfcbdcba6ee4d83863a
4
+ data.tar.gz: 4f910606fcb18716fe1be7b7fc84b877bba72adc
5
+ SHA512:
6
+ metadata.gz: 681986122dcc7007eeaeb905c2e56564e5c757fd301dfd23d2227be0f230885292fba24ccb7d50eacb77bdce2b7aa6eb2596aff00e7c1bbd0377f2cc71fed846
7
+ data.tar.gz: a4f9e44c950851150c5acd34e03585f77654a6f284fe271f1e01a6c6b06a8228bf3eadaefe2ec91858b76a4f9adfc54c16f136ae0c902bc73a34afb957338543
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Capistrano SSH Port Forwarding Extension
2
+
3
+ Provide SSH port forwarding while deploying so that we can call
4
+ local service like [Spitball](https://github.com/freels/spitball) server from remote hosts.
5
+
6
+ Currently it supports only remote forwarding.
7
+
8
+ ## Installation
9
+
10
+ Add this line in your Gemfile.
11
+
12
+ gem 'capistrano-forwarding'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install capistrano-forwarding
21
+
22
+ ## Usage
23
+
24
+ Currently it provides only `remote_forwarding` configuration
25
+ which is forwarding remote ports from remote hosts to local.
26
+
27
+ Here is an example in your `Capfile` or `config/deploy.rb`.
28
+
29
+ require "capistrano/fowarding"
30
+
31
+ desc "test task"
32
+ task "test" do
33
+ remote_forwarding [
34
+ [3000, "127.0.0.1", 3000]
35
+ ] do
36
+ run "curl http://127.0.0.1:3000/"
37
+ end
38
+ end
39
+
40
+ Before trying this, run something like HTTP server on local port 3000.
41
+
42
+ $ ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port => 3000).start'
@@ -0,0 +1,2 @@
1
+ require "capistrano/forwarding/version"
2
+ require "capistrano/forwarding/remote"
@@ -0,0 +1,147 @@
1
+ module Capistrano
2
+ # Support SSH remote port forwarding
3
+ module Forwarding
4
+ class Remote
5
+ # See lib/net/ssh/service/forward.rb
6
+ class Forwarding
7
+ # See lib/net/ssh/loggable.rb
8
+ include Net::SSH::Loggable
9
+
10
+ attr_reader :forwarding, :session
11
+
12
+ def initialize(forwarding, session)
13
+ @forwarding = forwarding
14
+ @session = session
15
+ @logger = session.logger
16
+ raise ArgumentError, "Invalid forwarding" unless valid?
17
+ end
18
+
19
+ def to_s
20
+ "#{session.host}:#{forwarding.join(":")}"
21
+ end
22
+
23
+ def forward!
24
+ info { "forwarding remote #{self}" }
25
+ session.forward.remote(*forwarding)
26
+ end
27
+
28
+ def cancel!
29
+ info { "cancelling forwarding remote #{self}" }
30
+ session.forward.cancel_remote(*remote_port_and_host)
31
+ end
32
+
33
+ def ready?
34
+ session.forward.active_remotes.include?(remote_port_and_host)
35
+ end
36
+
37
+ def gone?
38
+ !ready?
39
+ end
40
+
41
+ private
42
+
43
+ # Default remote host is "127.0.0.1".
44
+ # See /lib/net/ssh/service/forward.rb.
45
+ def remote_port_and_host
46
+ [forwarding[2], forwarding[3] || "127.0.0.1"]
47
+ end
48
+
49
+ def valid?
50
+ forwarding.size == 3 || forwarding.size == 4
51
+ end
52
+ end
53
+
54
+ include Processable
55
+
56
+ attr_reader :sessions, :forwardings, :options
57
+
58
+ def self.forward(forwardings, sessions, options = {}, &block)
59
+ new(forwardings, sessions, options).forward!(&block)
60
+ end
61
+
62
+ def initialize(forwardings, sessions, options = {})
63
+ sessions.each{|s| s.logger.level = Logger::INFO}
64
+
65
+ @forwardings = sessions.map do |session|
66
+ forwardings.map do |forwarding|
67
+ Forwarding.new(forwarding, session)
68
+ end
69
+ end.flatten
70
+ @sessions = sessions
71
+ @options = options
72
+ end
73
+
74
+ def forward!
75
+ start(options[:timeout])
76
+
77
+ # If we use run or similar command in the block for the connections,
78
+ # the forwardings are also processed by the loop of these commands.
79
+ # See lib/capistrano/command.rb and lib/capistrano/processable.rb.
80
+ yield(sessions) if block_given?
81
+
82
+ # Since we may still have channels which are not completely processed by the
83
+ # loop of these commands, run the loop for taking care of them until
84
+ # all channels has been gone.
85
+ loop unless options[:no_wait]
86
+
87
+ stop(options[:timeout])
88
+ end
89
+
90
+ private
91
+
92
+ def start(timeout = nil)
93
+ forwardings.each{|f| f.forward!}
94
+ # Process until all forward requests are ready.
95
+ loop(timeout){ !ready? }
96
+ end
97
+
98
+ def stop(timeout = nil)
99
+ forwardings.each{|f| f.cancel!}
100
+ # Process until all forward cancel requests are accepted.
101
+ loop(timeout){ !gone? }
102
+ end
103
+
104
+ # Preserve a reference to Kernel#loop
105
+ alias :loop_forever :loop
106
+
107
+ # Process until all channels in sessions are gone or while the block is true.
108
+ # See /lib/net/ssh/connection/session.rb
109
+ def loop(timeout = nil, &block)
110
+ running = block || Proc.new { busy? }
111
+ loop_forever{ break unless process_iteration(timeout, &running) }
112
+ end
113
+
114
+ def busy?
115
+ sessions.any?(&:busy?)
116
+ end
117
+
118
+ def ready?
119
+ forwardings.all?(&:ready?)
120
+ end
121
+
122
+ def gone?
123
+ forwardings.all?(&:gone?)
124
+ end
125
+ end
126
+ end
127
+
128
+ class Configuration
129
+ # Capistrano configuration example.
130
+ # Forwarding remote port 3000 to local 3000 and ask remote hosts to HTTP GET
131
+ # from local HTTP server running on port 3000.
132
+ # remote_forwarding [
133
+ # [3000, "127.0.0.1", 3000]
134
+ # ] do
135
+ # run "curl 'http://127.0.0.1:3000/'"
136
+ # end
137
+ def remote_forwarding(fowardings, options = {})
138
+ options = add_default_command_options(options)
139
+ execute_on_servers(options) do |servers|
140
+ targets = servers.map{|s| sessions[s]}
141
+ Forwarding::Remote.forward(fowardings, targets) do
142
+ yield
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Forwarding
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-forwarding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshimasa Niwa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-29 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Provide SSH port forwarding while deploying
56
+ email:
57
+ - niw@niw.at
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files:
61
+ - README.md
62
+ files:
63
+ - lib/capistrano/forwarding.rb
64
+ - lib/capistrano/forwarding/remote.rb
65
+ - lib/capistrano/forwarding/version.rb
66
+ - README.md
67
+ homepage: https://github.com/niw/capistrano-forwarding
68
+ licenses: []
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.0.0
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Provide SSH port forwarding while deploying
90
+ test_files: []