2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-05 09:55:10 -04:00
|
|
|
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-08-03 02:08:50 -04:00
|
|
|
class BuildRunnerSession < Ci::ApplicationRecord
|
2021-04-27 08:10:12 -04:00
|
|
|
include IgnorableColumns
|
|
|
|
|
|
|
|
ignore_columns :build_id_convert_to_bigint, remove_with: '14.1', remove_after: '2021-07-22'
|
2018-07-05 09:55:10 -04:00
|
|
|
|
2019-08-31 15:57:00 -04:00
|
|
|
TERMINAL_SUBPROTOCOL = 'terminal.gitlab.com'
|
2021-01-07 16:10:18 -05:00
|
|
|
DEFAULT_SERVICE_NAME = 'build'
|
|
|
|
DEFAULT_PORT_NAME = 'default_port'
|
2019-04-04 14:32:02 -04:00
|
|
|
|
2018-07-05 09:55:10 -04:00
|
|
|
self.table_name = 'ci_builds_runner_session'
|
|
|
|
|
|
|
|
belongs_to :build, class_name: 'Ci::Build', inverse_of: :runner_session
|
|
|
|
|
|
|
|
validates :build, presence: true
|
2019-04-11 02:29:07 -04:00
|
|
|
validates :url, addressable_url: { schemes: %w(https) }
|
2018-07-05 09:55:10 -04:00
|
|
|
|
|
|
|
def terminal_specification
|
2019-04-04 14:32:02 -04:00
|
|
|
wss_url = Gitlab::UrlHelpers.as_wss(self.url)
|
|
|
|
return {} unless wss_url.present?
|
|
|
|
|
|
|
|
wss_url = "#{wss_url}/exec"
|
|
|
|
channel_specification(wss_url, TERMINAL_SUBPROTOCOL)
|
|
|
|
end
|
|
|
|
|
2020-05-25 11:07:58 -04:00
|
|
|
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
|
|
|
|
url = "#{self.url}/proxy/#{service}/#{port}/#{path}"
|
|
|
|
subprotocols = subprotocols.presence || ::Ci::BuildRunnerSession::TERMINAL_SUBPROTOCOL
|
|
|
|
|
|
|
|
channel_specification(url, subprotocols)
|
|
|
|
end
|
|
|
|
|
2019-04-04 14:32:02 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def channel_specification(url, subprotocol)
|
|
|
|
return {} if subprotocol.blank? || url.blank?
|
2018-07-05 09:55:10 -04:00
|
|
|
|
|
|
|
{
|
2019-04-04 14:32:02 -04:00
|
|
|
subprotocols: Array(subprotocol),
|
|
|
|
url: url,
|
2018-07-24 08:05:48 -04:00
|
|
|
headers: { Authorization: [authorization.presence] }.compact,
|
2018-07-05 09:55:10 -04:00
|
|
|
ca_pem: certificate.presence
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|