Class: Environment

Inherits:
ApplicationRecord show all
Includes:
CacheMarkdownField, FastDestroyAll::Helpers, FromUnion, Gitlab::Utils::StrongMemoize, NullifyIfBlank, Presentable, ReactiveCaching
Defined in:
app/models/environment.rb

Constant Summary collapse

LONG_STOP =
1.week

Constants included from CacheMarkdownField

CacheMarkdownField::INVALIDATED_BY

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from CacheMarkdownField

#skip_markdown_cache_validation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

Methods included from FastDestroyAll::Helpers

#perform_fast_destroy

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from CacheMarkdownField

#attribute_invalidated?, #banzai_render_context, #cached_html_for, #cached_html_up_to_date?, #can_cache_field?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #mentionable_attributes_changed?, #mentioned_filtered_user_ids_for, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #store_mentions!, #store_mentions_after_commit?, #updated_cached_html_for

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, 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

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.count_by_stateObject



273
274
275
276
277
278
279
# File 'app/models/environment.rb', line 273

def count_by_state
  environments_count_by_state = group(:state).count

  valid_states.index_with do |state|
    environments_count_by_state[state.to_s] || 0
  end
end

.find_or_create_by_name(name) ⇒ Object



254
255
256
# File 'app/models/environment.rb', line 254

def self.find_or_create_by_name(name)
  find_or_create_by(name: name)
end

.for_id_and_slug(id, slug) ⇒ Object



235
236
237
# File 'app/models/environment.rb', line 235

def self.for_id_and_slug(id, slug)
  find_by(id: id, slug: slug)
end

.max_deployment_id_queryObject



239
240
241
242
243
244
# File 'app/models/environment.rb', line 239

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

.nestedObject



266
267
268
269
270
# File 'app/models/environment.rb', line 266

def self.nested
  group('COALESCE(environment_type, id::text)', 'COALESCE(environment_type, name)')
    .select('COALESCE(environment_type, id::text), COALESCE(environment_type, name) AS name', 'COUNT(*) AS size', 'MAX(id) AS last_id')
    .order('name ASC')
end

.pluck_namesObject



246
247
248
# File 'app/models/environment.rb', line 246

def self.pluck_names
  pluck(:name)
end

.pluck_unique_namesObject



250
251
252
# File 'app/models/environment.rb', line 250

def self.pluck_unique_names
  pluck('DISTINCT(environments.name)')
end

.schedule_to_delete(at_time = 1.week.from_now) ⇒ Object



262
263
264
# File 'app/models/environment.rb', line 262

def self.schedule_to_delete(at_time = 1.week.from_now)
  update_all(auto_delete_at: at_time)
end

.valid_statesObject



258
259
260
# File 'app/models/environment.rb', line 258

def self.valid_states
  self.state_machine.states.map(&:name)
end

Instance Method Details

#actions_for(environment) ⇒ Object



407
408
409
410
411
412
413
# File 'app/models/environment.rb', line 407

def actions_for(environment)
  return [] unless manual_actions

  manual_actions.select do |action|
    action.expanded_environment_name == environment
  end
end

#additional_metrics(*args) ⇒ Object



457
458
459
460
461
# File 'app/models/environment.rb', line 457

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_inObject



512
513
514
# File 'app/models/environment.rb', line 512

def auto_stop_in
  auto_stop_at - Time.current if auto_stop_at
end

#auto_stop_in=(value) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'app/models/environment.rb', line 516

def auto_stop_in=(value)
  if value.nil?
    # Handles edge case when auto_stop_at is already set and the new value is nil.
    # Possible by setting `auto_stop_in: null` in the CI configuration yml.
    self.auto_stop_at = nil

    return
  end

  parser = ::Gitlab::Ci::Build::DurationParser.new(value)

  self.auto_stop_at = parser.seconds_from_now
rescue ChronicDuration::DurationParseError => ex
  Gitlab::ErrorTracking.track_exception(ex, project_id: self.project_id, environment_id: self.id)
  raise ex
end

#calculate_reactive_cacheObject



425
426
427
428
429
# File 'app/models/environment.rb', line 425

def calculate_reactive_cache
  return unless has_terminals? && !project.pending_delete?

  deployment_platform.calculate_reactive_cache_for(self)
end

#cancel_deployment_jobs!Object



358
359
360
361
362
363
364
365
366
# File 'app/models/environment.rb', line 358

def cancel_deployment_jobs!
  active_deployments.jobs.each do |job|
    Gitlab::OptimisticLocking.retry_lock(job, name: 'environment_cancel_deployment_jobs') do |job|
      job.cancel! if job&.cancelable?
    end
  rescue StandardError => e
    Gitlab::ErrorTracking.track_exception(e, environment_id: id, deployment_id: deployment.id)
  end
end

#clear_all_cachesObject



553
554
555
556
# File 'app/models/environment.rb', line 553

def clear_all_caches
  expire_etag_cache
  clear_reactive_cache!
end

#clear_prometheus_reactive_cache!(query_name) ⇒ Object



306
307
308
# File 'app/models/environment.rb', line 306

def clear_prometheus_reactive_cache!(query_name)
  cluster_prometheus_adapter&.clear_prometheus_reactive_cache!(query_name, self)
end

#cluster_prometheus_adapterObject



310
311
312
# File 'app/models/environment.rb', line 310

def cluster_prometheus_adapter
  @cluster_prometheus_adapter ||= ::Gitlab::Prometheus::Adapter.new(project, deployment_platform&.cluster).cluster_prometheus_adapter
end

#deploy_freezesObject



566
567
568
569
570
# File 'app/models/environment.rb', line 566

def deploy_freezes
  Gitlab::SafeRequestStore.fetch("project:#{project_id}:freeze_periods_for_environments") do
    project.freeze_periods
  end
end

#deployment_namespaceObject



431
432
433
434
435
# File 'app/models/environment.rb', line 431

def deployment_namespace
  strong_memoize(:kubernetes_namespace) do
    deployment_platform.cluster.kubernetes_namespace_for(self) if deployment_platform
  end
end

#deployment_platformObject



500
501
502
503
504
# File 'app/models/environment.rb', line 500

def deployment_platform
  strong_memoize(:deployment_platform) do
    project.deployment_platform(environment: self.name)
  end
end

#etag_cache_keyObject



486
487
488
489
490
# File 'app/models/environment.rb', line 486

def etag_cache_key
  Gitlab::Routing.url_helpers.project_environments_path(
    project,
    format: :json)
end

#expire_etag_cacheObject



480
481
482
483
484
# File 'app/models/environment.rb', line 480

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



471
472
473
474
475
476
477
478
# File 'app/models/environment.rb', line 471

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_nameObject



492
493
494
# File 'app/models/environment.rb', line 492

def folder_name
  self.environment_type || self.name
end

#for_name_likeObject

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.



153
154
155
156
157
# File 'app/models/environment.rb', line 153

scope :for_name_like, ->(query, limit: 5) do
  top_level = 'LOWER(environments.name) LIKE LOWER(?) || \'%\''

  where(top_level, sanitize_sql_like(query)).limit(limit)
end

#formatted_external_urlObject



348
349
350
351
352
# File 'app/models/environment.rb', line 348

def formatted_external_url
  return unless external_url

  external_url.gsub(%r{\A.*?://}, '')
end

#has_metrics?Boolean

Returns:

  • (Boolean)


437
438
439
# File 'app/models/environment.rb', line 437

def has_metrics?
  available? && (prometheus_adapter&.configured? || has_sample_metrics?)
end

#has_opened_alert?Boolean

Returns:

  • (Boolean)


445
446
447
# File 'app/models/environment.rb', line 445

def has_opened_alert?
  latest_opened_most_severe_alert.present?
end

#has_running_deployments?Boolean

Returns:

  • (Boolean)


449
450
451
# File 'app/models/environment.rb', line 449

def has_running_deployments?
  all_deployments.running.exists?
end

#has_sample_metrics?Boolean

Returns:

  • (Boolean)


441
442
443
# File 'app/models/environment.rb', line 441

def has_sample_metrics?
  !!ENV['USE_SAMPLE_METRICS']
end

#has_terminals?Boolean

Returns:

  • (Boolean)


415
416
417
# File 'app/models/environment.rb', line 415

def has_terminals?
  available? && deployment_platform.present? && last_deployment.present?
end

#includes_commit?(sha) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
333
334
# File 'app/models/environment.rb', line 330

def includes_commit?(sha)
  return false unless last_deployment

  last_deployment.includes_commit?(sha)
end

#ingressesObject



541
542
543
544
545
# File 'app/models/environment.rb', line 541

def ingresses
  return unless rollout_status_available?

  deployment_platform.ingresses(deployment_namespace)
end

#knative_services_finderObject



506
507
508
509
510
# File 'app/models/environment.rb', line 506

def knative_services_finder
  if last_deployment&.cluster
    Clusters::KnativeServicesFinder.new(last_deployment.cluster, self)
  end
end

#last_deployableObject



282
283
284
# File 'app/models/environment.rb', line 282

def last_deployable
  last_deployment&.deployable
end

#last_deployed_atObject



336
337
338
# File 'app/models/environment.rb', line 336

def last_deployed_at
  last_deployment.try(:created_at)
end

#last_finished_deployableObject



286
287
288
# File 'app/models/environment.rb', line 286

def last_finished_deployable
  last_finished_deployment&.deployable
end

#last_finished_deployment_groupObject



399
400
401
# File 'app/models/environment.rb', line 399

def last_finished_deployment_group
  Deployment.last_finished_deployment_group_for_environment(self)
end

#last_finished_pipelineObject



290
291
292
# File 'app/models/environment.rb', line 290

def last_finished_pipeline
  last_finished_deployable&.pipeline
end

#last_visible_deployableObject



298
299
300
# File 'app/models/environment.rb', line 298

def last_visible_deployable
  last_visible_deployment&.deployable
end

#last_visible_pipelineObject



302
303
304
# File 'app/models/environment.rb', line 302

def last_visible_pipeline
  last_visible_deployable&.pipeline
end

#latest_finished_jobsObject



294
295
296
# File 'app/models/environment.rb', line 294

def latest_finished_jobs
  last_finished_pipeline&.latest_finished_jobs
end

#long_stopping?Boolean

Returns:

  • (Boolean)


340
341
342
# File 'app/models/environment.rb', line 340

def long_stopping?
  stopping? && self.updated_at < LONG_STOP.ago
end

#metricsObject



453
454
455
# File 'app/models/environment.rb', line 453

def metrics
  prometheus_adapter.query(:environment, self) if has_metrics_and_can_query?
end

#name_without_typeObject



496
497
498
# File 'app/models/environment.rb', line 496

def name_without_type
  @name_without_type ||= name.delete_prefix("#{environment_type}/")
end

#patch_ingress(ingress, data) ⇒ Object



547
548
549
550
551
# File 'app/models/environment.rb', line 547

def patch_ingress(ingress, data)
  return unless rollout_status_available?

  deployment_platform.patch_ingress(deployment_namespace, ingress, data)
end

#predefined_variablesObject



314
315
316
317
318
# File 'app/models/environment.rb', line 314

def predefined_variables
  Gitlab::Ci::Variables::Collection.new
    .append(key: 'CI_ENVIRONMENT_NAME', value: name)
    .append(key: 'CI_ENVIRONMENT_SLUG', value: slug)
end

#prometheus_adapterObject



463
464
465
# File 'app/models/environment.rb', line 463

def prometheus_adapter
  @prometheus_adapter ||= Gitlab::Prometheus::Adapter.new(project, deployment_platform&.cluster).prometheus_adapter
end

#recently_updated_on_branch?(ref) ⇒ Boolean

Returns:

  • (Boolean)


320
321
322
# File 'app/models/environment.rb', line 320

def recently_updated_on_branch?(ref)
  ref.to_s == last_deployment.try(:ref)
end

#ref_pathObject



344
345
346
# File 'app/models/environment.rb', line 344

def ref_path
  "refs/#{Repository::REF_ENVIRONMENTS}/#{slug}"
end

#reset_auto_stopObject



403
404
405
# File 'app/models/environment.rb', line 403

def reset_auto_stop
  update_column(:auto_stop_at, nil)
end

#rollout_statusObject



533
534
535
536
537
538
539
# File 'app/models/environment.rb', line 533

def rollout_status
  return unless rollout_status_available?

  result = rollout_status_with_reactive_cache

  result || ::Gitlab::Kubernetes::RolloutStatus.loading
end

#set_environment_typeObject



324
325
326
327
328
# File 'app/models/environment.rb', line 324

def set_environment_type
  names = name.split('/')

  self.environment_type = names.many? ? names.first : nil
end

Returns:

  • (Boolean)


558
559
560
# File 'app/models/environment.rb', line 558

def should_link_to_merge_requests?
  unfoldered? || production? || staging?
end

#slugObject



467
468
469
# File 'app/models/environment.rb', line 467

def slug
  super.presence || generate_slug
end

#stop_actionsObject



394
395
396
# File 'app/models/environment.rb', line 394

def stop_actions
  last_finished_deployment_group.map(&:stop_action).compact
end

#stop_actions_available?Boolean

Returns:

  • (Boolean)


354
355
356
# File 'app/models/environment.rb', line 354

def stop_actions_available?
  available? && stop_actions.present?
end

#stop_with_actions!Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'app/models/environment.rb', line 372

def stop_with_actions!
  return unless available?

  stop!

  actions = []

  stop_actions.each do |stop_action|
    play_job = ->(job) do
      actions << job.play(job.user)
    rescue StateMachines::InvalidTransition
      # Ci::PlayBuildService rescues an error of StateMachines::InvalidTransition and fall back to retry.
      # However, Ci::PlayBridgeService doesn't rescue it, so we're ignoring the error if it's not playable.
      # We should fix this inconsistency in https://gitlab.com/gitlab-org/gitlab/-/issues/420855.
    end

    play_job.call(stop_action)
  end

  actions
end

#terminalsObject



419
420
421
422
423
# File 'app/models/environment.rb', line 419

def terminals
  with_reactive_cache do |data|
    deployment_platform.terminals(self, data)
  end
end

#unfoldered?Boolean

Returns:

  • (Boolean)


562
563
564
# File 'app/models/environment.rb', line 562

def unfoldered?
  environment_type.nil?
end

#wait_for_stop?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'app/models/environment.rb', line 368

def wait_for_stop?
  stop_actions.present?
end