diff options
author | Rémy Coutable <remy@rymai.me> | 2017-12-13 20:28:04 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-12-13 20:28:04 +0300 |
commit | 4fe92b1de16469e0f8534d7c85d3e8b5c868b558 (patch) | |
tree | 75e42f706b6c5a0a926b08822b19ee7de0b447a1 /lib | |
parent | 013e681625ada14ba977c5a66ab104924a9fdce6 (diff) | |
parent | ab4fa64308176cc069e6a731d35a53c886af805e (diff) |
Merge branch 'add-tcp-check-rake-task' into 'master'
Add a gitlab:tcp_check rake task
See merge request gitlab-org/gitlab-ce!15759
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/tcp_checker.rb | 45 | ||||
-rw-r--r-- | lib/tasks/gitlab/tcp_check.rake | 20 |
2 files changed, 65 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 diff --git a/lib/tasks/gitlab/tcp_check.rake b/lib/tasks/gitlab/tcp_check.rake new file mode 100644 index 00000000000..1400f57d6b9 --- /dev/null +++ b/lib/tasks/gitlab/tcp_check.rake @@ -0,0 +1,20 @@ +namespace :gitlab do + desc "GitLab | Check TCP connectivity to a specific host and port" + task :tcp_check, [:host, :port] => :environment do |_t, args| + unless args.host && args.port + puts "Please specify a host and port: `rake gitlab:tcp_check[example.com,80]`".color(:red) + exit 1 + end + + checker = Gitlab::TcpChecker.new(args.host, args.port) + + if checker.check + puts "TCP connection from #{checker.local} to #{checker.remote} succeeded".color(:green) + else + puts "TCP connection to #{checker.remote} failed: #{checker.error}".color(:red) + puts + puts 'Check that host and port are correct, and that the traffic is permitted through any firewalls.' + exit 1 + end + end +end |