diff options
Diffstat (limited to 'scripts/review_apps/automated_cleanup.rb')
-rwxr-xr-x | scripts/review_apps/automated_cleanup.rb | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/scripts/review_apps/automated_cleanup.rb b/scripts/review_apps/automated_cleanup.rb index 4166070f7cd..bc0e76b9bc5 100755 --- a/scripts/review_apps/automated_cleanup.rb +++ b/scripts/review_apps/automated_cleanup.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'gitlab' -require_relative File.expand_path('../../lib/quality/helm_client.rb', __dir__) -require_relative File.expand_path('../../lib/quality/kubernetes_client.rb', __dir__) +require "gitlab" +require_relative File.expand_path("../../lib/quality/helm_client.rb", __dir__) +require_relative File.expand_path("../../lib/quality/kubernetes_client.rb", __dir__) class AutomatedCleanup attr_reader :project_path, :gitlab_token @@ -10,27 +10,27 @@ class AutomatedCleanup DEPLOYMENTS_PER_PAGE = 100 HELM_RELEASES_BATCH_SIZE = 5 IGNORED_HELM_ERRORS = [ - 'transport is closing', - 'error upgrading connection' + "transport is closing", + "error upgrading connection", ].freeze IGNORED_KUBERNETES_ERRORS = [ - 'NotFound' + "NotFound", ].freeze def self.ee? - ENV['CI_PROJECT_NAME'] == 'gitlab-ee' || File.exist?('CHANGELOG-EE.md') + ENV["CI_PROJECT_NAME"] == "gitlab-ee" || File.exist?("CHANGELOG-EE.md") end - def initialize(project_path: ENV['CI_PROJECT_PATH'], gitlab_token: ENV['GITLAB_BOT_REVIEW_APPS_CLEANUP_TOKEN']) + def initialize(project_path: ENV["CI_PROJECT_PATH"], gitlab_token: ENV["GITLAB_BOT_REVIEW_APPS_CLEANUP_TOKEN"]) @project_path = project_path @gitlab_token = gitlab_token - ENV['TILLER_NAMESPACE'] ||= review_apps_namespace + ENV["TILLER_NAMESPACE"] ||= review_apps_namespace end def gitlab @gitlab ||= begin Gitlab.configure do |config| - config.endpoint = 'https://gitlab.com/api/v4' + config.endpoint = "https://gitlab.com/api/v4" # gitlab-bot's token "GitLab review apps cleanup" config.private_token = gitlab_token end @@ -40,7 +40,7 @@ class AutomatedCleanup end def review_apps_namespace - self.class.ee? ? 'review-apps-ee' : 'review-apps-ce' + self.class.ee? ? "review-apps-ee" : "review-apps-ce" end def helm @@ -61,7 +61,7 @@ class AutomatedCleanup gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE).auto_paginate do |deployment| environment = deployment.environment - next unless environment.name.start_with?('review/') + next unless environment.name.start_with?("review/") next if checked_environments.include?(environment.slug) last_deploy = deployment.created_at @@ -74,7 +74,7 @@ class AutomatedCleanup elsif deployed_at < stop_threshold stop_environment(environment, deployment) else - print_release_state(subject: 'Review app', release_name: environment.slug, release_date: last_deploy, action: 'leaving') + print_release_state(subject: "Review app", release_name: environment.slug, release_date: last_deploy, action: "leaving") end checked_environments << environment.slug @@ -87,10 +87,10 @@ class AutomatedCleanup threshold_day = threshold_time(days: days) helm_releases.each do |release| - if release.status == 'FAILED' || release.last_update < threshold_day + if release.status == "FAILED" || release.last_update < threshold_day delete_helm_release(release) else - print_release_state(subject: 'Release', release_name: release.name, release_date: release.last_update, action: 'leaving') + print_release_state(subject: "Release", release_name: release.name, release_date: release.last_update, action: "leaving") end end end @@ -98,23 +98,23 @@ class AutomatedCleanup private def delete_environment(environment, deployment) - print_release_state(subject: 'Review app', release_name: environment.slug, release_date: deployment.created_at, action: 'deleting') + print_release_state(subject: "Review app", release_name: environment.slug, release_date: deployment.created_at, action: "deleting") gitlab.delete_environment(project_path, environment.id) end def stop_environment(environment, deployment) - print_release_state(subject: 'Review app', release_name: environment.slug, release_date: deployment.created_at, action: 'stopping') + print_release_state(subject: "Review app", release_name: environment.slug, release_date: deployment.created_at, action: "stopping") gitlab.stop_environment(project_path, environment.id) end def helm_releases - args = ['--all', '--date', "--max #{HELM_RELEASES_BATCH_SIZE}"] + args = ["--all", "--date", "--max #{HELM_RELEASES_BATCH_SIZE}"] helm.releases(args: args) end def delete_helm_release(release) - print_release_state(subject: 'Release', release_name: release.name, release_status: release.status, release_date: release.last_update, action: 'cleaning') + print_release_state(subject: "Release", release_name: release.name, release_status: release.status, release_date: release.last_update, action: "cleaning") helm.delete(release_name: release.name) kubernetes.cleanup(release_name: release.name) rescue Quality::HelmClient::CommandFailedError => ex @@ -148,13 +148,13 @@ end automated_cleanup = AutomatedCleanup.new -timed('Review apps cleanup') do +timed("Review apps cleanup") do automated_cleanup.perform_gitlab_environment_cleanup!(days_for_stop: 2, days_for_delete: 3) end puts -timed('Helm releases cleanup') do +timed("Helm releases cleanup") do automated_cleanup.perform_helm_releases_cleanup!(days: 3) end |