harbour 0.0.1
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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +1 -0
- data/TODO +3 -0
- data/harbour.gemspec +22 -0
- data/lib/harbour.rb +45 -0
- data/lib/harbour/version.rb +3 -0
- data/spec/port_spec.rb +35 -0
- data/spec/server/config.ru +7 -0
- data/spec/spec_helper.rb +36 -0
- metadata +93 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kris Leech
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Harbour
|
2
|
+
|
3
|
+
With harbour you can:
|
4
|
+
|
5
|
+
* See if a port is open
|
6
|
+
* Find the pid of the process listening on a port
|
7
|
+
* Kill the process listening on a port
|
8
|
+
|
9
|
+
Note: Only works on MacOS, GNU/Linux coming soon...
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'harbour'
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
port = Harbour::Port.new(8080)
|
20
|
+
port.open? # => true
|
21
|
+
port.pid # => 1282
|
22
|
+
port.term!
|
23
|
+
port.kill!
|
24
|
+
|
25
|
+
### Terminating a process
|
26
|
+
|
27
|
+
port = Harbour::Port.new(8080)
|
28
|
+
port.term!(:timeout => 10, :try_kill => true)
|
29
|
+
|
30
|
+
* `timeout` - the number of seconds to wait for the process to terminate before
|
31
|
+
raising an exception
|
32
|
+
* `try_kill` - if a TERM signal fails before timeout then send a KILL signal
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
41
|
+
|
42
|
+
## Author
|
43
|
+
|
44
|
+
Kris Leech / teamcoding.com
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/TODO
ADDED
@@ -0,0 +1,3 @@
|
|
1
|
+
Add timeout to pid, since the port may be open but no pid from lsof. Doing this will also allow the sleep(2) to be removed from start_server! in specs.
|
2
|
+
add a self.all_ports which lists all open ports, maybe returning Port objects.
|
3
|
+
raise exceptions instead of returning nil
|
data/harbour.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'harbour/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "harbour"
|
8
|
+
gem.version = Harbour::VERSION
|
9
|
+
gem.authors = ["Kris Leech"]
|
10
|
+
gem.email = ["kris.leech@interkonect.com"]
|
11
|
+
gem.description = %q{Query and terminate processes listening on ports}
|
12
|
+
gem.summary = %q{Query and terminate processes listening on ports}
|
13
|
+
gem.homepage = "https://github.com/krisleech/harbour"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
gem.add_development_dependency 'rack'
|
22
|
+
end
|
data/lib/harbour.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "harbour/version"
|
2
|
+
|
3
|
+
module Harbour
|
4
|
+
class Port
|
5
|
+
attr_reader :port
|
6
|
+
|
7
|
+
def initialize(port)
|
8
|
+
@port = port
|
9
|
+
end
|
10
|
+
|
11
|
+
def open?
|
12
|
+
command "nc -z localhost #{port}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def pid
|
16
|
+
return nil unless open?
|
17
|
+
`lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`
|
18
|
+
end
|
19
|
+
|
20
|
+
def term!(options = {})
|
21
|
+
return nil unless open?
|
22
|
+
command "kill #{pid}"
|
23
|
+
ensure_open(options[:timeout] || 10)
|
24
|
+
end
|
25
|
+
|
26
|
+
def kill!(options = {})
|
27
|
+
return nil unless open?
|
28
|
+
command "kill -9 #{pid}"
|
29
|
+
ensure_open(options[:timeout] || 10)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def ensure_open(within = 10)
|
35
|
+
within.times do
|
36
|
+
break if open?
|
37
|
+
sleep(1)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def command(_command)
|
42
|
+
system "#{_command} > /dev/null"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/port_spec.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Harbour::Port do
|
4
|
+
let(:port) { 3000 }
|
5
|
+
|
6
|
+
it '#open? returns false if port is not open' do
|
7
|
+
Harbour::Port.new(8080).open?.should be_false
|
8
|
+
end
|
9
|
+
|
10
|
+
it '#open? returns true if port is open' do
|
11
|
+
with_server(port) do
|
12
|
+
Harbour::Port.new(3000).open?.should be_true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it '#pid returns process pid' do
|
17
|
+
with_server(port) do
|
18
|
+
Harbour::Port.new(port).pid.should_not be_nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it '#term! terminates process listening on port' do
|
23
|
+
with_server(port) do
|
24
|
+
Harbour::Port.new(3000).term!
|
25
|
+
port_open?(port).should be_false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it '#kill! terminates process listening on port' do
|
30
|
+
with_server(port) do
|
31
|
+
Harbour::Port.new(3000).kill!
|
32
|
+
port_open?(port).should be_false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
require 'harbour'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
7
|
+
config.run_all_when_everything_filtered = true
|
8
|
+
config.filter_run :focus
|
9
|
+
config.order = 'random'
|
10
|
+
end
|
11
|
+
|
12
|
+
def with_server(port = '8000', &block)
|
13
|
+
start_server!(port)
|
14
|
+
yield
|
15
|
+
stop_server!(port)
|
16
|
+
end
|
17
|
+
|
18
|
+
def start_server!(port)
|
19
|
+
return if system "nc -z localhost #{port}"
|
20
|
+
server_root = File.join(File.dirname(__FILE__), 'server')
|
21
|
+
system "cd #{server_root} && rackup -d -p #{port} &"
|
22
|
+
sleep(2)
|
23
|
+
end
|
24
|
+
|
25
|
+
def stop_server!(port)
|
26
|
+
pid = `lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`
|
27
|
+
system "kill -9 #{pid}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def port_open?(port)
|
31
|
+
uri = URI.parse("http://localhost:#{port}")
|
32
|
+
response = Net::HTTP.get_print(uri)
|
33
|
+
response.include? 'Hello'
|
34
|
+
rescue
|
35
|
+
false
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: harbour
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kris Leech
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rack
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Query and terminate processes listening on ports
|
47
|
+
email:
|
48
|
+
- kris.leech@interkonect.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- TODO
|
60
|
+
- harbour.gemspec
|
61
|
+
- lib/harbour.rb
|
62
|
+
- lib/harbour/version.rb
|
63
|
+
- spec/port_spec.rb
|
64
|
+
- spec/server/config.ru
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: https://github.com/krisleech/harbour
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.23
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Query and terminate processes listening on ports
|
90
|
+
test_files:
|
91
|
+
- spec/port_spec.rb
|
92
|
+
- spec/server/config.ru
|
93
|
+
- spec/spec_helper.rb
|