summaryrefslogtreecommitdiff
path: root/qa/qa/service/runner.rb
blob: 6fc5984b12af070a3dfb89486453c6e5d82a1883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

require 'securerandom'

module QA
  module Service
    class Runner
      include Service::Shellout

      attr_accessor :token, :address, :tags, :image, :run_untagged
      attr_writer :config

      def initialize(name)
        @image = 'gitlab/gitlab-runner:alpine'
        @name = name || "qa-runner-#{SecureRandom.hex(4)}"
        @network = Runtime::Scenario.attributes[:network] || 'test'
        @tags = %w[qa test]
        @run_untagged = false
      end

      def config
        @config ||= <<~END
          concurrent = 1
          check_interval = 0

          [session_server]
            session_timeout = 1800
        END
      end

      def network
        shell "docker network inspect #{@network}"
      rescue CommandError
        'bridge'
      else
        @network
      end

      def pull
        shell "docker pull #{@image}"
      end

      def register!
        shell <<~CMD.tr("\n", ' ')
          docker run -d --rm --entrypoint=/bin/sh
          --network #{network} --name #{@name}
          -p 8093:8093
          -e CI_SERVER_URL=#{@address}
          -e REGISTER_NON_INTERACTIVE=true
          -e REGISTRATION_TOKEN=#{@token}
          -e RUNNER_EXECUTOR=shell
          -e RUNNER_TAG_LIST=#{@tags.join(',')}
          -e RUNNER_NAME=#{@name}
          #{@image} -c "#{register_command}"
        CMD
      end

      def remove!
        shell "docker rm -f #{@name}"
      end

      private

      def register_command
        <<~CMD
          printf '#{config.chomp.gsub(/\n/, "\\n").gsub('"', '\"')}' > /etc/gitlab-runner/config.toml &&
          gitlab-runner register --run-untagged=#{@run_untagged} &&
          gitlab-runner run
        CMD
      end
    end
  end
end