1#!/usr/bin/env ruby
2
3require 'tempfile'
4require 'socket'
5
6ADDR = 'socket'.freeze
7
8def main(gitaly_dir)
9  gitaly_dir = File.realpath(gitaly_dir)
10  bin_dir = File.join(gitaly_dir, '_build', 'bin')
11
12  version = IO.popen("#{File.join(bin_dir, 'gitaly')} -version").read.delete_prefix('Gitaly, version ').strip
13  version_from_file = IO.read(File.join(gitaly_dir, 'VERSION')).strip
14
15  # Use start_with? instead of == because the version output could use git describe, if it is a source install
16  # eg: Gitaly, version 1.75.0-14-gd1ecb43f
17  abort "\nversion check failed: VERSION file contained '#{version_from_file}' but 'gitaly -version' reported '#{version}'."\
18  " If you are working from a fork, please fetch the latest tags." unless version.start_with?(version_from_file)
19
20  Dir.mktmpdir do |dir|
21    Dir.chdir(dir)
22    File.write(File.join("#{gitaly_dir}/ruby/git-hooks", '.gitlab_shell_secret'), 'test_gitlab_shell_token')
23
24    File.write('config.toml', <<~CONFIG
25      socket_path = "#{ADDR}"
26      bin_dir = "#{bin_dir}"
27
28      [[storage]]
29      name = "default"
30      path = "#{dir}"
31
32      [gitaly-ruby]
33      dir = "#{gitaly_dir}/ruby"
34
35      [gitlab-shell]
36      dir = "#{gitaly_dir}/ruby/git-hooks"
37
38      [gitlab]
39      url = 'http://gitlab_url'
40
41      CONFIG
42              )
43
44    pid = nil
45
46    begin
47      start = Time.now
48      pid = spawn(File.join(bin_dir, 'gitaly'), 'config.toml')
49      wait_connect
50      puts
51      puts "\n\nconnection established after #{Time.now - start} seconds\n\n"
52    ensure
53      if pid
54        Process.kill("KILL", pid)
55        Process.wait(pid)
56      end
57    end
58  end
59end
60
61def wait_connect
62  repeats = 100
63  sleep_time = 0.1
64
65  repeats.times do
66    begin
67      Socket.unix(ADDR)
68      return
69    rescue # rubocop:disable Lint/RescueWithoutErrorClass
70      print '.'
71      sleep(sleep_time)
72    end
73  end
74
75  puts "failed to connect to gitaly after #{repeats * sleep_time}s"
76
77  abort
78end
79
80unless ARGV.count == 1
81  abort "Usage: #{$PROGRAM_NAME} GITALY_DIR"
82end
83
84main(ARGV.first)
85