1# frozen_string_literal: true 2 3module Ci 4 # The purpose of this class is to store Build related runner session. 5 # Data will be removed after transitioning from running to any state. 6 class BuildRunnerSession < Ci::ApplicationRecord 7 TERMINAL_SUBPROTOCOL = 'terminal.gitlab.com' 8 DEFAULT_SERVICE_NAME = 'build' 9 DEFAULT_PORT_NAME = 'default_port' 10 11 self.table_name = 'ci_builds_runner_session' 12 13 belongs_to :build, class_name: 'Ci::Build', inverse_of: :runner_session 14 15 validates :build, presence: true 16 validates :url, addressable_url: { schemes: %w(https) } 17 18 def terminal_specification 19 wss_url = Gitlab::UrlHelpers.as_wss(self.url) 20 return {} unless wss_url.present? 21 22 wss_url = "#{wss_url}/exec" 23 channel_specification(wss_url, TERMINAL_SUBPROTOCOL) 24 end 25 26 def service_specification(service: nil, path: nil, port: nil, subprotocols: nil) 27 return {} unless url.present? 28 29 port = port.presence || DEFAULT_PORT_NAME 30 service = service.presence || DEFAULT_SERVICE_NAME 31 url = "#{self.url}/proxy/#{service}/#{port}/#{path}" 32 subprotocols = subprotocols.presence || ::Ci::BuildRunnerSession::TERMINAL_SUBPROTOCOL 33 34 channel_specification(url, subprotocols) 35 end 36 37 private 38 39 def channel_specification(url, subprotocol) 40 return {} if subprotocol.blank? || url.blank? 41 42 { 43 subprotocols: Array(subprotocol), 44 url: url, 45 headers: { Authorization: [authorization.presence] }.compact, 46 ca_pem: certificate.presence 47 } 48 end 49 end 50end 51