helpers.rb 1.5 KB
Newer Older
D
Douwe Maan 已提交
1 2 3
module Ci
  module API
    module Helpers
K
Kamil Trzcinski 已提交
4 5
      BUILD_TOKEN_HEADER = "HTTP_BUILD_TOKEN"
      BUILD_TOKEN_PARAM = :token
K
Kamil Trzcinski 已提交
6 7
      UPDATE_RUNNER_EVERY = 60

D
Douwe Maan 已提交
8
      def authenticate_runners!
9
        forbidden! unless runner_registration_token_valid?
D
Douwe Maan 已提交
10 11 12 13 14 15
      end

      def authenticate_runner!
        forbidden! unless current_runner
      end

K
Kamil Trzcinski 已提交
16 17 18 19 20
      def authenticate_build_token!(build)
        token = (params[BUILD_TOKEN_PARAM] || env[BUILD_TOKEN_HEADER]).to_s
        forbidden! unless token && build.valid_token?(token)
      end

21
      def runner_registration_token_valid?
22
        params[:token] == current_application_settings.ensure_runners_registration_token
23 24
      end

D
Douwe Maan 已提交
25
      def update_runner_last_contact
26 27 28
        # Use a random threshold to prevent beating DB updates
        contacted_at_max_age = UPDATE_RUNNER_EVERY + Random.rand(UPDATE_RUNNER_EVERY)
        if current_runner.contacted_at.nil? || Time.now - current_runner.contacted_at >= contacted_at_max_age
D
Douwe Maan 已提交
29 30 31 32
          current_runner.update_attributes(contacted_at: Time.now)
        end
      end

V
Valery Sizov 已提交
33 34 35 36
      def current_runner
        @runner ||= Runner.find_by_token(params[:token].to_s)
      end

D
Douwe Maan 已提交
37 38 39 40 41
      def update_runner_info
        return unless params["info"].present?
        info = attributes_for_keys(["name", "version", "revision", "platform", "architecture"], params["info"])
        current_runner.update(info)
      end
K
Kamil Trzcinski 已提交
42 43 44 45

      def max_artifacts_size
        current_application_settings.max_artifacts_size.megabytes.to_i
      end
D
Douwe Maan 已提交
46 47 48
    end
  end
end