2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
module Clusters
|
|
|
|
module Applications
|
2019-05-18 00:54:41 +05:30
|
|
|
class Runner < ApplicationRecord
|
|
|
|
VERSION = '0.3.0'.freeze
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
self.table_name = 'clusters_applications_runners'
|
|
|
|
|
|
|
|
include ::Clusters::Concerns::ApplicationCore
|
|
|
|
include ::Clusters::Concerns::ApplicationStatus
|
2018-11-18 11:00:15 +05:30
|
|
|
include ::Clusters::Concerns::ApplicationVersion
|
2018-03-27 19:54:05 +05:30
|
|
|
include ::Clusters::Concerns::ApplicationData
|
|
|
|
|
|
|
|
belongs_to :runner, class_name: 'Ci::Runner', foreign_key: :runner_id
|
2019-05-18 00:54:41 +05:30
|
|
|
delegate :project, :group, to: :cluster
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
default_value_for :version, VERSION
|
|
|
|
|
|
|
|
def chart
|
|
|
|
"#{name}/gitlab-runner"
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
'https://charts.gitlab.io'
|
|
|
|
end
|
|
|
|
|
|
|
|
def values
|
|
|
|
content_values.to_yaml
|
|
|
|
end
|
|
|
|
|
|
|
|
def install_command
|
|
|
|
Gitlab::Kubernetes::Helm::InstallCommand.new(
|
2018-11-18 11:00:15 +05:30
|
|
|
name: name,
|
|
|
|
version: VERSION,
|
2018-11-20 20:47:30 +05:30
|
|
|
rbac: cluster.platform_kubernetes_rbac?,
|
2018-03-27 19:54:05 +05:30
|
|
|
chart: chart,
|
2018-11-18 11:00:15 +05:30
|
|
|
files: files,
|
2018-03-27 19:54:05 +05:30
|
|
|
repository: repository
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_runner
|
|
|
|
runner || create_and_assign_runner
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_and_assign_runner
|
|
|
|
transaction do
|
2018-11-08 19:23:39 +05:30
|
|
|
Ci::Runner.create!(runner_create_params).tap do |runner|
|
2018-03-27 19:54:05 +05:30
|
|
|
update!(runner_id: runner.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def runner_create_params
|
2019-05-18 00:54:41 +05:30
|
|
|
attributes = {
|
2018-10-15 14:42:47 +05:30
|
|
|
name: 'kubernetes-cluster',
|
2019-05-18 00:54:41 +05:30
|
|
|
runner_type: cluster.cluster_type,
|
|
|
|
tag_list: %w[kubernetes cluster]
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
2019-05-18 00:54:41 +05:30
|
|
|
|
|
|
|
if cluster.group_type?
|
|
|
|
attributes.merge(groups: [group])
|
|
|
|
elsif cluster.project_type?
|
|
|
|
attributes.merge(projects: [project])
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def gitlab_url
|
|
|
|
Gitlab::Routing.url_helpers.root_url(only_path: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def specification
|
|
|
|
{
|
|
|
|
"gitlabUrl" => gitlab_url,
|
|
|
|
"runnerToken" => ensure_runner.token,
|
|
|
|
"runners" => { "privileged" => privileged }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_values
|
|
|
|
YAML.load_file(chart_values_file).deep_merge!(specification)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|