debian-mirror-gitlab/app/models/ci/build_runner_session.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
module Ci
# The purpose of this class is to store Build related runner session.
# Data will be removed after transitioning from running to any state.
2021-10-27 15:23:28 +05:30
class BuildRunnerSession < Ci::ApplicationRecord
2019-12-04 20:38:33 +05:30
TERMINAL_SUBPROTOCOL = 'terminal.gitlab.com'
2021-03-08 18:12:59 +05:30
DEFAULT_SERVICE_NAME = 'build'
DEFAULT_PORT_NAME = 'default_port'
2019-07-07 11:18:12 +05:30
2018-11-08 19:23:39 +05:30
self.table_name = 'ci_builds_runner_session'
belongs_to :build, class_name: 'Ci::Build', inverse_of: :runner_session
validates :build, presence: true
2023-01-10 11:22:00 +05:30
validates :url, public_url: { schemes: %w(https) }
2018-11-08 19:23:39 +05:30
def terminal_specification
2023-01-10 11:22:00 +05:30
wss_url = Gitlab::UrlHelpers.as_wss(Addressable::URI.escape(self.url))
2019-07-07 11:18:12 +05:30
return {} unless wss_url.present?
2023-01-10 11:22:00 +05:30
parsed_wss_url = URI.parse(wss_url)
parsed_wss_url.path += '/exec'
channel_specification(parsed_wss_url, TERMINAL_SUBPROTOCOL)
2019-07-07 11:18:12 +05:30
end
2020-06-23 00:09:42 +05:30
def service_specification(service: nil, path: nil, port: nil, subprotocols: nil)
return {} unless url.present?
port = port.presence || DEFAULT_PORT_NAME
service = service.presence || DEFAULT_SERVICE_NAME
2023-01-10 11:22:00 +05:30
parsed_url = URI.parse(Addressable::URI.escape(self.url))
parsed_url.path += "/proxy/#{service}/#{port}/#{path}"
2020-06-23 00:09:42 +05:30
subprotocols = subprotocols.presence || ::Ci::BuildRunnerSession::TERMINAL_SUBPROTOCOL
2023-01-10 11:22:00 +05:30
channel_specification(parsed_url, subprotocols)
2020-06-23 00:09:42 +05:30
end
2019-07-07 11:18:12 +05:30
private
2023-01-10 11:22:00 +05:30
def channel_specification(parsed_url, subprotocol)
return {} if subprotocol.blank? || parsed_url.blank?
2018-11-08 19:23:39 +05:30
{
2019-07-07 11:18:12 +05:30
subprotocols: Array(subprotocol),
2023-01-10 11:22:00 +05:30
url: Addressable::URI.unescape(parsed_url.to_s),
2018-11-18 11:00:15 +05:30
headers: { Authorization: [authorization.presence] }.compact,
2018-11-08 19:23:39 +05:30
ca_pem: certificate.presence
}
end
end
end