Class: Environment
Constant Summary
ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE
ApplicationRecord::MAX_PLUCK
Class Method Summary
collapse
Instance Method Summary
collapse
#present
#perform_fast_destroy
#run_after_commit, #run_after_commit_or_now
#clear_memoization, #strong_memoize, #strong_memoized?
cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order
#serializable_hash
Class Method Details
.count_by_state ⇒ Object
182
183
184
185
186
187
188
|
# File 'app/models/environment.rb', line 182
def count_by_state
environments_count_by_state = group(:state).count
valid_states.each_with_object({}) do |state, count_hash|
count_hash[state] = environments_count_by_state[state.to_s] || 0
end
end
|
.find_or_create_by_name(name) ⇒ Object
169
170
171
|
# File 'app/models/environment.rb', line 169
def self.find_or_create_by_name(name)
find_or_create_by(name: name)
end
|
.for_id_and_slug(id, slug) ⇒ Object
150
151
152
|
# File 'app/models/environment.rb', line 150
def self.for_id_and_slug(id, slug)
find_by(id: id, slug: slug)
end
|
.max_deployment_id_query ⇒ Object
154
155
156
157
158
159
|
# File 'app/models/environment.rb', line 154
def self.max_deployment_id_query
Arel.sql(
Deployment.select(Deployment.arel_table[:id].maximum)
.where(Deployment.arel_table[:environment_id].eq(arel_table[:id])).to_sql
)
end
|
.pluck_names ⇒ Object
161
162
163
|
# File 'app/models/environment.rb', line 161
def self.pluck_names
pluck(:name)
end
|
.pluck_unique_names ⇒ Object
165
166
167
|
# File 'app/models/environment.rb', line 165
def self.pluck_unique_names
pluck('DISTINCT(environments.name)')
end
|
.schedule_to_delete(at_time = 1.week.from_now) ⇒ Object
177
178
179
|
# File 'app/models/environment.rb', line 177
def self.schedule_to_delete(at_time = 1.week.from_now)
update_all(auto_delete_at: at_time)
end
|
.valid_states ⇒ Object
173
174
175
|
# File 'app/models/environment.rb', line 173
def self.valid_states
self.state_machine.states.map(&:name)
end
|
Instance Method Details
#actions_for(environment) ⇒ Object
327
328
329
330
331
332
333
|
# File 'app/models/environment.rb', line 327
def actions_for(environment)
return [] unless manual_actions
manual_actions.select do |action|
action.expanded_environment_name == environment
end
end
|
#additional_metrics(*args) ⇒ Object
377
378
379
380
381
|
# File 'app/models/environment.rb', line 377
def additional_metrics(*args)
return unless has_metrics_and_can_query?
prometheus_adapter.query(:additional_metrics_environment, self, *args.map(&:to_f))
end
|
#auto_stop_in ⇒ Object
432
433
434
|
# File 'app/models/environment.rb', line 432
def auto_stop_in
auto_stop_at - Time.current if auto_stop_at
end
|
#auto_stop_in=(value) ⇒ Object
436
437
438
439
440
441
|
# File 'app/models/environment.rb', line 436
def auto_stop_in=(value)
return unless value
return unless parsed_result = ChronicDuration.parse(value)
self.auto_stop_at = parsed_result.seconds.from_now
end
|
#calculate_reactive_cache ⇒ Object
345
346
347
348
349
|
# File 'app/models/environment.rb', line 345
def calculate_reactive_cache
return unless has_terminals? && !project.pending_delete?
deployment_platform.calculate_reactive_cache_for(self)
end
|
#cancel_deployment_jobs! ⇒ Object
286
287
288
289
290
291
292
293
294
|
# File 'app/models/environment.rb', line 286
def cancel_deployment_jobs!
active_deployments.builds.each do |build|
Gitlab::OptimisticLocking.retry_lock(build, name: 'environment_cancel_deployment_jobs') do |build|
build.cancel! if build&.cancelable?
end
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e, environment_id: id, deployment_id: deployment.id)
end
end
|
#clear_all_caches ⇒ Object
467
468
469
470
|
# File 'app/models/environment.rb', line 467
def clear_all_caches
expire_etag_cache
clear_reactive_cache!
end
|
#clear_prometheus_reactive_cache!(query_name) ⇒ Object
237
238
239
|
# File 'app/models/environment.rb', line 237
def clear_prometheus_reactive_cache!(query_name)
cluster_prometheus_adapter&.clear_prometheus_reactive_cache!(query_name, self)
end
|
#cluster_prometheus_adapter ⇒ Object
#deployment_namespace ⇒ Object
351
352
353
354
355
|
# File 'app/models/environment.rb', line 351
def deployment_namespace
strong_memoize(:kubernetes_namespace) do
deployment_platform.cluster.kubernetes_namespace_for(self) if deployment_platform
end
end
|
420
421
422
423
424
|
# File 'app/models/environment.rb', line 420
def deployment_platform
strong_memoize(:deployment_platform) do
project.deployment_platform(environment: self.name)
end
end
|
#elastic_stack_available? ⇒ Boolean
443
444
445
|
# File 'app/models/environment.rb', line 443
def elastic_stack_available?
!!deployment_platform&.cluster&.elastic_stack_available?
end
|
#etag_cache_key ⇒ Object
406
407
408
409
410
|
# File 'app/models/environment.rb', line 406
def etag_cache_key
Gitlab::Routing.url_helpers.project_environments_path(
project,
format: :json)
end
|
#expire_etag_cache ⇒ Object
400
401
402
403
404
|
# File 'app/models/environment.rb', line 400
def expire_etag_cache
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(etag_cache_key)
end
end
|
#external_url_for(path, commit_sha) ⇒ Object
391
392
393
394
395
396
397
398
|
# File 'app/models/environment.rb', line 391
def external_url_for(path, commit_sha)
return unless self.external_url
public_path = project.public_path_for_source_path(path, commit_sha)
return unless public_path
[external_url.delete_suffix('/'), public_path.delete_prefix('/')].join('/')
end
|
#folder_name ⇒ Object
412
413
414
|
# File 'app/models/environment.rb', line 412
def folder_name
self.environment_type || self.name
end
|
#for_name_like ⇒ Object
Search environments which have names like the given query. Do not set a large limit unless you've confirmed that it works on gitlab.com scale.
86
87
88
|
# File 'app/models/environment.rb', line 86
scope :for_name_like, -> (query, limit: 5) do
where(arel_table[:name].matches("#{sanitize_sql_like query}%")).limit(limit)
end
|
276
277
278
279
280
|
# File 'app/models/environment.rb', line 276
def formatted_external_url
return unless external_url
external_url.gsub(%r{\A.*?://}, '')
end
|
#has_metrics? ⇒ Boolean
357
358
359
|
# File 'app/models/environment.rb', line 357
def has_metrics?
available? && (prometheus_adapter&.configured? || has_sample_metrics?)
end
|
#has_opened_alert? ⇒ Boolean
365
366
367
|
# File 'app/models/environment.rb', line 365
def has_opened_alert?
latest_opened_most_severe_alert.present?
end
|
#has_running_deployments? ⇒ Boolean
369
370
371
|
# File 'app/models/environment.rb', line 369
def has_running_deployments?
all_deployments.running.exists?
end
|
#has_sample_metrics? ⇒ Boolean
361
362
363
|
# File 'app/models/environment.rb', line 361
def has_sample_metrics?
!!ENV['USE_SAMPLE_METRICS']
end
|
#has_terminals? ⇒ Boolean
335
336
337
|
# File 'app/models/environment.rb', line 335
def has_terminals?
available? && deployment_platform.present? && last_deployment.present?
end
|
#includes_commit?(sha) ⇒ Boolean
262
263
264
265
266
|
# File 'app/models/environment.rb', line 262
def includes_commit?(sha)
return false unless last_deployment
last_deployment.includes_commit?(sha)
end
|
#ingresses ⇒ Object
455
456
457
458
459
|
# File 'app/models/environment.rb', line 455
def ingresses
return unless rollout_status_available?
deployment_platform.ingresses(deployment_namespace)
end
|
#knative_services_finder ⇒ Object
426
427
428
429
430
|
# File 'app/models/environment.rb', line 426
def knative_services_finder
if last_deployment&.cluster
Clusters::KnativeServicesFinder.new(last_deployment.cluster, self)
end
end
|
#last_deployable ⇒ Object
191
192
193
|
# File 'app/models/environment.rb', line 191
def last_deployable
last_deployment&.deployable
end
|
#last_deployed_at ⇒ Object
268
269
270
|
# File 'app/models/environment.rb', line 268
def last_deployed_at
last_deployment.try(:created_at)
end
|
#last_deployment_group ⇒ Object
This method returns the deployment records of the last deployment pipeline, that successfully executed to this environment. e.g. A pipeline contains
- deploy job A => production environment
- deploy job B => production environment
In this case, `last_deployment_group` returns both deployments, whereas `last_deployable` returns only B.
205
206
207
208
209
210
|
# File 'app/models/environment.rb', line 205
def last_deployment_group
return Deployment.none unless last_deployment_pipeline
successful_deployments.where(
deployable_id: last_deployment_pipeline.latest_builds.pluck(:id))
end
|
#last_deployment_pipeline ⇒ Object
195
196
197
|
# File 'app/models/environment.rb', line 195
def last_deployment_pipeline
last_deployable&.pipeline
end
|
#last_visible_deployable ⇒ Object
224
225
226
227
228
|
# File 'app/models/environment.rb', line 224
def last_visible_deployable
return super if association_cached?(:last_visible_deployable)
last_visible_deployment&.deployable
end
|
#last_visible_pipeline ⇒ Object
231
232
233
234
235
|
# File 'app/models/environment.rb', line 231
def last_visible_pipeline
return super if association_cached?(:last_visible_pipeline)
last_visible_deployable&.pipeline
end
|
#metrics ⇒ Object
373
374
375
|
# File 'app/models/environment.rb', line 373
def metrics
prometheus_adapter.query(:environment, self) if has_metrics_and_can_query?
end
|
#name_without_type ⇒ Object
416
417
418
|
# File 'app/models/environment.rb', line 416
def name_without_type
@name_without_type ||= name.delete_prefix("#{environment_type}/")
end
|
#patch_ingress(ingress, data) ⇒ Object
461
462
463
464
465
|
# File 'app/models/environment.rb', line 461
def patch_ingress(ingress, data)
return unless rollout_status_available?
deployment_platform.patch_ingress(deployment_namespace, ingress, data)
end
|
#predefined_variables ⇒ Object
245
246
247
248
249
250
|
# File 'app/models/environment.rb', line 245
def predefined_variables
Gitlab::Ci::Variables::Collection.new
.append(key: 'CI_ENVIRONMENT_NAME', value: name)
.append(key: 'CI_ENVIRONMENT_SLUG', value: slug)
.append(key: 'CI_ENVIRONMENT_TIER', value: tier)
end
|
#prometheus_adapter ⇒ Object
#recently_updated_on_branch?(ref) ⇒ Boolean
252
253
254
|
# File 'app/models/environment.rb', line 252
def recently_updated_on_branch?(ref)
ref.to_s == last_deployment.try(:ref)
end
|
#reset_auto_stop ⇒ Object
323
324
325
|
# File 'app/models/environment.rb', line 323
def reset_auto_stop
update_column(:auto_stop_at, nil)
end
|
#rollout_status ⇒ Object
447
448
449
450
451
452
453
|
# File 'app/models/environment.rb', line 447
def rollout_status
return unless rollout_status_available?
result = rollout_status_with_reactive_cache
result || ::Gitlab::Kubernetes::RolloutStatus.loading
end
|
#set_environment_type ⇒ Object
256
257
258
259
260
|
# File 'app/models/environment.rb', line 256
def set_environment_type
names = name.split('/')
self.environment_type = names.many? ? names.first : nil
end
|
#should_link_to_merge_requests? ⇒ Boolean
472
473
474
|
# File 'app/models/environment.rb', line 472
def should_link_to_merge_requests?
unfoldered? || production? || staging?
end
|
#slug ⇒ Object
387
388
389
|
# File 'app/models/environment.rb', line 387
def slug
super.presence || generate_slug
end
|
#stop_actions ⇒ Object
315
316
317
318
319
320
321
|
# File 'app/models/environment.rb', line 315
def stop_actions
strong_memoize(:stop_actions) do
last_deployment_group.map(&:stop_action).compact
end
end
|
#stop_actions_available? ⇒ Boolean
282
283
284
|
# File 'app/models/environment.rb', line 282
def stop_actions_available?
available? && stop_actions.present?
end
|
#stop_with_actions!(current_user) ⇒ Object
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'app/models/environment.rb', line 296
def stop_with_actions!(current_user)
return unless available?
stop!
actions = []
stop_actions.each do |stop_action|
Gitlab::OptimisticLocking.retry_lock(
stop_action,
name: 'environment_stop_with_actions'
) do |build|
actions << build.play(current_user)
end
end
actions
end
|
#terminals ⇒ Object
339
340
341
342
343
|
# File 'app/models/environment.rb', line 339
def terminals
with_reactive_cache do |data|
deployment_platform.terminals(self, data)
end
end
|
#unfoldered? ⇒ Boolean
476
477
478
|
# File 'app/models/environment.rb', line 476
def unfoldered?
environment_type.nil?
end
|