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-07-07 11:18:12 +05:30
|
|
|
class Runner < ApplicationRecord
|
2020-01-01 13:55:28 +05:30
|
|
|
VERSION = '0.11.0'
|
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-07-07 11:18:12 +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
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def prepare_uninstall
|
|
|
|
runner&.update!(active: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_uninstall
|
|
|
|
runner.destroy!
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
attributes = {
|
2018-10-15 14:42:47 +05:30
|
|
|
name: 'kubernetes-cluster',
|
2019-07-07 11:18:12 +05:30
|
|
|
runner_type: cluster.cluster_type,
|
|
|
|
tag_list: %w[kubernetes cluster]
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
if cluster.group_type?
|
2019-07-31 22:56:46 +05:30
|
|
|
attributes[:groups] = [group]
|
2019-07-07 11:18:12 +05:30
|
|
|
elsif cluster.project_type?
|
2019-07-31 22:56:46 +05:30
|
|
|
attributes[:projects] = [project]
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
attributes
|
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
|