helpers.rb 1.48 KB
Newer Older
1 2 3
module Ci
  module API
    module Helpers
4 5
      BUILD_TOKEN_HEADER = "HTTP_BUILD_TOKEN"
      BUILD_TOKEN_PARAM = :token
6 7
      UPDATE_RUNNER_EVERY = 60

8
      def authenticate_runners!
9
        forbidden! unless runner_registration_token_valid?
10 11 12 13 14 15
      end

      def authenticate_runner!
        forbidden! unless current_runner
      end

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.runners_registration_token
23 24
      end

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
29 30 31 32
          current_runner.update_attributes(contacted_at: Time.now)
        end
      end

Valery Sizov's avatar
Valery Sizov committed
33 34 35 36
      def current_runner
        @runner ||= Runner.find_by_token(params[:token].to_s)
      end

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
42 43 44 45

      def max_artifacts_size
        current_application_settings.max_artifacts_size.megabytes.to_i
      end
46 47 48
    end
  end
end