From c46417c5064867d72debb15c4e280db24c6ab73c Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Thu, 2 Nov 2017 18:55:02 +0100 Subject: [PATCH] Rename App to Applications --- app/models/clusters/concerns.rb | 4 --- .../applications/base_helm_service.rb | 25 ++++++++++++++++ .../check_installation_progress_service.rb | 30 +++++++++++++++++++ .../fetch_installation_status_service.rb | 15 ++++++++++ .../finalize_installation_service.rb | 17 +++++++++++ .../clusters/applications/install_service.rb | 24 +++++++++++++++ app/services/clusters/base_helm_service.rb | 23 -------------- ...check_app_installation_progress_service.rb | 28 ----------------- .../fetch_app_installation_status_service.rb | 13 -------- .../finalize_app_installation_service.rb | 15 ---------- app/services/clusters/install_app_service.rb | 22 -------------- app/workers/cluster_install_app_worker.rb | 6 ++-- ...luster_wait_for_app_installation_worker.rb | 6 ++-- app/workers/concerns/cluster_app.rb | 11 ------- app/workers/concerns/cluster_applications.rb | 9 ++++++ 15 files changed, 126 insertions(+), 122 deletions(-) delete mode 100644 app/models/clusters/concerns.rb create mode 100644 app/services/clusters/applications/base_helm_service.rb create mode 100644 app/services/clusters/applications/check_installation_progress_service.rb create mode 100644 app/services/clusters/applications/fetch_installation_status_service.rb create mode 100644 app/services/clusters/applications/finalize_installation_service.rb create mode 100644 app/services/clusters/applications/install_service.rb delete mode 100644 app/services/clusters/base_helm_service.rb delete mode 100644 app/services/clusters/check_app_installation_progress_service.rb delete mode 100644 app/services/clusters/fetch_app_installation_status_service.rb delete mode 100644 app/services/clusters/finalize_app_installation_service.rb delete mode 100644 app/services/clusters/install_app_service.rb delete mode 100644 app/workers/concerns/cluster_app.rb create mode 100644 app/workers/concerns/cluster_applications.rb diff --git a/app/models/clusters/concerns.rb b/app/models/clusters/concerns.rb deleted file mode 100644 index cd09863bcfc..00000000000 --- a/app/models/clusters/concerns.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Clusters - module Concerns - end -end diff --git a/app/services/clusters/applications/base_helm_service.rb b/app/services/clusters/applications/base_helm_service.rb new file mode 100644 index 00000000000..68320a3b267 --- /dev/null +++ b/app/services/clusters/applications/base_helm_service.rb @@ -0,0 +1,25 @@ +module Clusters + module Applications + class BaseHelmService + attr_accessor :app + + def initialize(app) + @app = app + end + + protected + + def cluster + app.cluster + end + + def kubeclient + cluster.kubeclient + end + + def helm_api + @helm_api ||= Gitlab::Kubernetes::Helm.new(kubeclient) + end + end + end +end diff --git a/app/services/clusters/applications/check_installation_progress_service.rb b/app/services/clusters/applications/check_installation_progress_service.rb new file mode 100644 index 00000000000..4e8fd9baaf4 --- /dev/null +++ b/app/services/clusters/applications/check_installation_progress_service.rb @@ -0,0 +1,30 @@ +module Clusters + module Applications + class CheckInstallationProgressService < BaseHelmService + def execute + return unless app.installing? + + FetchInstallationStatusService.new(app).execute do |phase, log| + case phase + when 'Succeeded' + if app.make_installed + FinalizeInstallationService.new(app).execute + else + app.make_errored!("Failed to update app record; #{app.errors}") + end + when 'Failed' + app.make_errored!(log || 'Installation silently failed') + FinalizeInstallationService.new(app).execute + else + if Time.now.utc - app.updated_at.to_time.utc > ClusterWaitForAppInstallationWorker::TIMEOUT + app.make_errored!('App installation timeouted') + else + ClusterWaitForAppInstallationWorker.perform_in( + ClusterWaitForAppInstallationWorker::EAGER_INTERVAL, app.name, app.id) + end + end + end + end + end + end +end diff --git a/app/services/clusters/applications/fetch_installation_status_service.rb b/app/services/clusters/applications/fetch_installation_status_service.rb new file mode 100644 index 00000000000..3d082485532 --- /dev/null +++ b/app/services/clusters/applications/fetch_installation_status_service.rb @@ -0,0 +1,15 @@ +module Clusters + module Applications + class FetchInstallationStatusService < BaseHelmService + def execute + return unless app.installing? + + phase = helm_api.installation_status(app) + log = helm_api.installation_log(app) if phase == 'Failed' + yield(phase, log) if block_given? + rescue KubeException => ke + app.make_errored!("Kubernetes error: #{ke.message}") unless app.errored? + end + end + end +end diff --git a/app/services/clusters/applications/finalize_installation_service.rb b/app/services/clusters/applications/finalize_installation_service.rb new file mode 100644 index 00000000000..339d671c091 --- /dev/null +++ b/app/services/clusters/applications/finalize_installation_service.rb @@ -0,0 +1,17 @@ +module Clusters + module Applications + class FinalizeInstallationService < BaseHelmService + def execute + helm_api.delete_installation_pod!(app) + + app.make_errored!('Installation aborted') if aborted? + end + + private + + def aborted? + app.installing? || app.scheduled? + end + end + end +end diff --git a/app/services/clusters/applications/install_service.rb b/app/services/clusters/applications/install_service.rb new file mode 100644 index 00000000000..7fcccb5e78c --- /dev/null +++ b/app/services/clusters/applications/install_service.rb @@ -0,0 +1,24 @@ +module Clusters + module Applications + class InstallService < BaseHelmService + def execute + return unless app.scheduled? + + begin + helm_api.install(app) + + if app.make_installing + ClusterWaitForAppInstallationWorker.perform_in( + ClusterWaitForAppInstallationWorker::INITIAL_INTERVAL, app.name, app.id) + else + app.make_errored!("Failed to update app record; #{app.errors}") + end + rescue KubeException => ke + app.make_errored!("Kubernetes error: #{ke.message}") + rescue StandardError + app.make_errored!("Can't start installation process") + end + end + end + end +end diff --git a/app/services/clusters/base_helm_service.rb b/app/services/clusters/base_helm_service.rb deleted file mode 100644 index 0a95955a204..00000000000 --- a/app/services/clusters/base_helm_service.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Clusters - class BaseHelmService - attr_accessor :app - - def initialize(app) - @app = app - end - - protected - - def cluster - app.cluster - end - - def kubeclient - cluster.kubeclient - end - - def helm_api - @helm_api ||= Gitlab::Kubernetes::Helm.new(kubeclient) - end - end -end diff --git a/app/services/clusters/check_app_installation_progress_service.rb b/app/services/clusters/check_app_installation_progress_service.rb deleted file mode 100644 index ff3398bbd63..00000000000 --- a/app/services/clusters/check_app_installation_progress_service.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Clusters - class CheckAppInstallationProgressService < BaseHelmService - def execute - return unless app.installing? - - FetchAppInstallationStatusService.new(app).execute do |phase, log| - case phase - when 'Succeeded' - if app.make_installed - FinalizeAppInstallationService.new(app).execute - else - app.make_errored!("Failed to update app record; #{app.errors}") - end - when 'Failed' - app.make_errored!(log || 'Installation silently failed') - FinalizeAppInstallationService.new(app).execute - else - if Time.now.utc - app.updated_at.to_time.utc > ClusterWaitForAppInstallationWorker::TIMEOUT - app.make_errored!('App installation timeouted') - else - ClusterWaitForAppInstallationWorker.perform_in( - ClusterWaitForAppInstallationWorker::EAGER_INTERVAL, app.name, app.id) - end - end - end - end - end -end diff --git a/app/services/clusters/fetch_app_installation_status_service.rb b/app/services/clusters/fetch_app_installation_status_service.rb deleted file mode 100644 index 9b281c77c49..00000000000 --- a/app/services/clusters/fetch_app_installation_status_service.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Clusters - class FetchAppInstallationStatusService < BaseHelmService - def execute - return unless app.installing? - - phase = helm_api.installation_status(app) - log = helm_api.installation_log(app) if phase == 'Failed' - yield(phase, log) if block_given? - rescue KubeException => ke - app.make_errored!("Kubernetes error: #{ke.message}") unless app.errored? - end - end -end diff --git a/app/services/clusters/finalize_app_installation_service.rb b/app/services/clusters/finalize_app_installation_service.rb deleted file mode 100644 index b9d5da063eb..00000000000 --- a/app/services/clusters/finalize_app_installation_service.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Clusters - class FinalizeAppInstallationService < BaseHelmService - def execute - helm_api.delete_installation_pod!(app) - - app.make_errored!('Installation aborted') if aborted? - end - - private - - def aborted? - app.installing? || app.scheduled? - end - end -end diff --git a/app/services/clusters/install_app_service.rb b/app/services/clusters/install_app_service.rb deleted file mode 100644 index 63f15abaa6d..00000000000 --- a/app/services/clusters/install_app_service.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Clusters - class InstallAppService < BaseHelmService - def execute - return unless app.scheduled? - - begin - helm_api.install(app) - - if app.make_installing - ClusterWaitForAppInstallationWorker.perform_in( - ClusterWaitForAppInstallationWorker::INITIAL_INTERVAL, app.name, app.id) - else - app.make_errored!("Failed to update app record; #{app.errors}") - end - rescue KubeException => ke - app.make_errored!("Kubernetes error: #{ke.message}") - rescue StandardError - app.make_errored!("Can't start installation process") - end - end - end -end diff --git a/app/workers/cluster_install_app_worker.rb b/app/workers/cluster_install_app_worker.rb index 4993b2b7349..899aed904e4 100644 --- a/app/workers/cluster_install_app_worker.rb +++ b/app/workers/cluster_install_app_worker.rb @@ -1,11 +1,11 @@ class ClusterInstallAppWorker include Sidekiq::Worker include ClusterQueue - include ClusterApp + include ClusterApplications def perform(app_name, app_id) - find_app(app_name, app_id) do |app| - Clusters::InstallAppService.new(app).execute + find_application(app_name, app_id) do |app| + Clusters::Applications::InstallService.new(app).execute end end end diff --git a/app/workers/cluster_wait_for_app_installation_worker.rb b/app/workers/cluster_wait_for_app_installation_worker.rb index 7e480c42fd4..d5974c467c4 100644 --- a/app/workers/cluster_wait_for_app_installation_worker.rb +++ b/app/workers/cluster_wait_for_app_installation_worker.rb @@ -1,15 +1,15 @@ class ClusterWaitForAppInstallationWorker include Sidekiq::Worker include ClusterQueue - include ClusterApp + include ClusterApplications INITIAL_INTERVAL = 30.seconds EAGER_INTERVAL = 10.seconds TIMEOUT = 20.minutes def perform(app_name, app_id) - find_app(app_name, app_id) do |app| - Clusters::CheckAppInstallationProgressService.new(app).execute + find_application(app_name, app_id) do |app| + Clusters::Applications::CheckInstallationProgressService.new(app).execute end end end diff --git a/app/workers/concerns/cluster_app.rb b/app/workers/concerns/cluster_app.rb deleted file mode 100644 index 947cdaefcb7..00000000000 --- a/app/workers/concerns/cluster_app.rb +++ /dev/null @@ -1,11 +0,0 @@ -module ClusterApp - extend ActiveSupport::Concern - - included do - def find_app(app_name, id) - Clusters::Cluster::APPLICATIONS[app_name].find(id).try do |app| - yield(app) if block_given? - end - end - end -end diff --git a/app/workers/concerns/cluster_applications.rb b/app/workers/concerns/cluster_applications.rb new file mode 100644 index 00000000000..24ecaa0b52f --- /dev/null +++ b/app/workers/concerns/cluster_applications.rb @@ -0,0 +1,9 @@ +module ClusterApplications + extend ActiveSupport::Concern + + included do + def find_application(app_name, id, &blk) + Clusters::Cluster::APPLICATIONS[app_name].find(id).try(&blk) + end + end +end