diff options
author | Nick Thomas <nick@gitlab.com> | 2017-12-06 12:39:25 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2017-12-13 18:53:32 +0300 |
commit | ab4fa64308176cc069e6a731d35a53c886af805e (patch) | |
tree | 09d0c5d8a1a50abe3aa1aef418407b3379d3d2ec /lib/gitlab/tcp_checker.rb | |
parent | 8ff63039f1ee5f6e31a8b910e323977e7de3c634 (diff) |
Add a gitlab:tcp_check rake task
This allows us to avoid relying on telnet / netcat being installed
Diffstat (limited to 'lib/gitlab/tcp_checker.rb')
-rw-r--r-- | lib/gitlab/tcp_checker.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/gitlab/tcp_checker.rb b/lib/gitlab/tcp_checker.rb new file mode 100644 index 00000000000..6e24e46d0ea --- /dev/null +++ b/lib/gitlab/tcp_checker.rb @@ -0,0 +1,45 @@ +module Gitlab + class TcpChecker + attr_reader :remote_host, :remote_port, :local_host, :local_port, :error + + def initialize(remote_host, remote_port, local_host = nil, local_port = nil) + @remote_host = remote_host + @remote_port = remote_port + @local_host = local_host + @local_port = local_port + end + + def local + join_host_port(local_host, local_port) + end + + def remote + join_host_port(remote_host, remote_port) + end + + def check(timeout: 10) + Socket.tcp( + remote_host, remote_port, + local_host, local_port, + connect_timeout: timeout + ) do |sock| + @local_port, @local_host = Socket.unpack_sockaddr_in(sock.local_address) + @remote_port, @remote_host = Socket.unpack_sockaddr_in(sock.remote_address) + end + + true + rescue => err + @error = err + + false + end + + private + + def join_host_port(host, port) + host = "[#{host}]" if host.include?(':') + + "#{host}:#{port}" + end + end +end |