Class: UffizziCore::DeploymentService

Inherits:
Object
  • Object
show all
Extended by:
DependencyInjectionConcern
Includes:
DependencyInjectionConcern
Defined in:
app/services/uffizzi_core/deployment_service.rb

Constant Summary collapse

MIN_TARGET_PORT_RANGE =
37_000
MAX_TARGET_PORT_RANGE =
39_999
DEPLOYMENT_PROCESS_STATUSES =
{
  building: :building,
  deploying: :deploying,
  failed: :failed,
  queued: :queued,
}.freeze

Class Method Summary collapse

Methods included from DependencyInjectionConcern

ci_module, ci_session, controller_settings_service, deployment_memory_module, domain_module, find_build_parser_module, find_ingress_parser_module, find_volume_parser_module, notification_module, password_protection_module, template_memory_module, user_access_module

Class Method Details

.all_containers_have_unique_ports?(containers) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'app/services/uffizzi_core/deployment_service.rb', line 99

def all_containers_have_unique_ports?(containers)
  ports = containers.map(&:port).compact
  containers.empty? || ports.size == ports.uniq.size
end

.create_from_compose(compose_file, project, user, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/uffizzi_core/deployment_service.rb', line 20

def create_from_compose(compose_file, project, user, params)
  deployment_attributes = ActionController::Parameters.new(compose_file.template.payload)
  deployment_form = UffizziCore::Api::Cli::V1::Deployment::CreateForm.new(deployment_attributes)
  deployment_form.assign_dependences!(project, user)
  deployment_form.compose_file = compose_file
  deployment_form.creation_source = params[:creation_source] || UffizziCore::Deployment.creation_source.compose_file_manual
  deployment_form. = params[:metadata] || {}

  run_deployment_creation_tasks(deployment_form) if deployment_form.save

  deployment_form
end

.deploy_containers(deployment, repeated = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/services/uffizzi_core/deployment_service.rb', line 52

def deploy_containers(deployment, repeated = false)
  if !repeated
    create_activity_items(deployment)
    update_controller_container_names(deployment)
  end

  status = deployment_process_status(deployment)

  case status
  when DEPLOYMENT_PROCESS_STATUSES[:building]
    Rails.logger.info("DEPLOYMENT_PROCESS deployment_id=#{deployment.id} repeat deploy_containers")
    UffizziCore::Deployment::DeployContainersJob.perform_in(1.minute, deployment.id, true)
    unless repeated
      deployment.deployment_events.create!(deployment_state: status)
    end
  when DEPLOYMENT_PROCESS_STATUSES[:deploying]
    Rails.logger.info("DEPLOYMENT_PROCESS deployment_id=#{deployment.id} start deploying into controller")

    containers = deployment.active_containers
    containers_with_variables = add_default_deployment_variables!(containers, deployment)

    UffizziCore::ControllerService.deploy_containers(deployment, containers_with_variables)
    deployment.deployment_events.create!(deployment_state: status)
  else
    Rails.logger.info("DEPLOYMENT_PROCESS deployment_id=#{deployment.id} deployment has builds errors, stopping")
  end
end

.disable!(deployment) ⇒ Object



80
81
82
83
84
85
86
# File 'app/services/uffizzi_core/deployment_service.rb', line 80

def disable!(deployment)
  deployment.disable!
  compose_file = deployment.compose_file || deployment.template&.compose_file
  return unless compose_file&.kind&.temporary?

  compose_file.destroy!
end

.fail!(deployment) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'app/services/uffizzi_core/deployment_service.rb', line 88

def fail!(deployment)
  return if deployment.failed?

  deployment.fail!
  deployment.deployment_events.create!(deployment_state: deployment.state)
  compose_file = deployment.compose_file || deployment.template&.compose_file
  return unless compose_file&.kind&.temporary?

  compose_file.destroy!
end

.failed?(deployment) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
# File 'app/services/uffizzi_core/deployment_service.rb', line 147

def failed?(deployment)
  deployment_status = deployment_process_status(deployment)

  deployment_status == DEPLOYMENT_PROCESS_STATUSES[:failed]
end

.find_unused_port(deployment) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/services/uffizzi_core/deployment_service.rb', line 108

def find_unused_port(deployment)
  selected_port = nil

  Timeout.timeout(20) do
    loop do
      selected_port = rand(MIN_TARGET_PORT_RANGE..MAX_TARGET_PORT_RANGE)

      break if !deployment.containers.exists?(target_port: selected_port)
    end
  end

  selected_port
end

.ingress_container?(containers) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/services/uffizzi_core/deployment_service.rb', line 104

def ingress_container?(containers)
  containers.empty? || containers.map(&:receive_incoming_requests).count(true) == 1
end

.pull_request_payload_present?(deployment) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/services/uffizzi_core/deployment_service.rb', line 143

def pull_request_payload_present?(deployment)
  deployment.continuous_preview_payload.present? && deployment.continuous_preview_payload['pull_request'].present?
end

.setup_ingress_container(deployment, ingress_container, port) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/services/uffizzi_core/deployment_service.rb', line 122

def setup_ingress_container(deployment, ingress_container, port)
  old_deployment_subdomain = deployment.subdomain

  containers = deployment.containers.active

  UffizziCore::Container.transaction do
    containers.update_all(receive_incoming_requests: false, port: nil, public: false)
    containers.find(ingress_container.id).update!(port: port, public: true, receive_incoming_requests: true)
  end

  deployment.reload

  new_deployment_subdomain = DomainService.build_subdomain(deployment)

  if new_deployment_subdomain != old_deployment_subdomain
    deployment.update(subdomain: new_deployment_subdomain)
  end

  UffizziCore::Deployment::DeployContainersJob.perform_async(deployment.id)
end

.update_from_compose(compose_file, project, user, deployment, metadata) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/uffizzi_core/deployment_service.rb', line 33

def update_from_compose(compose_file, project, user, deployment, )
  deployment_attributes = ActionController::Parameters.new(compose_file.template.payload)

  deployment_form = UffizziCore::Api::Cli::V1::Deployment::UpdateForm.new(deployment_attributes)
  deployment_form.assign_dependences!(project, user)
  deployment_form.compose_file = compose_file
  deployment_form. =  || {}

  ActiveRecord::Base.transaction do
    deployment.containers.destroy_all
    deployment.compose_file.destroy! if deployment.compose_file&.kind&.temporary?
    deployment.activate unless deployment.active?
    new_params = params_for_update_deployment(deployment_form, compose_file)
    deployment.update!(new_params)
  end

  deployment
end