Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/packages/debian')
-rw-r--r--app/services/packages/debian/create_distribution_service.rb75
-rw-r--r--app/services/packages/debian/destroy_distribution_service.rb33
-rw-r--r--app/services/packages/debian/update_distribution_service.rb80
3 files changed, 188 insertions, 0 deletions
diff --git a/app/services/packages/debian/create_distribution_service.rb b/app/services/packages/debian/create_distribution_service.rb
new file mode 100644
index 00000000000..c6df033e3c1
--- /dev/null
+++ b/app/services/packages/debian/create_distribution_service.rb
@@ -0,0 +1,75 @@
+# frozen_string_literal: true
+
+module Packages
+ module Debian
+ class CreateDistributionService
+ def initialize(container, user, params)
+ @container, @params = container, params
+ @params[:creator] = user
+
+ @components = params.delete(:components) || ['main']
+
+ @architectures = params.delete(:architectures) || ['amd64']
+ @architectures += ['all']
+
+ @distribution = nil
+ @errors = []
+ end
+
+ def execute
+ create_distribution
+ end
+
+ private
+
+ attr_reader :container, :params, :components, :architectures, :distribution, :errors
+
+ def append_errors(record, prefix = '')
+ return if record.valid?
+
+ prefix = "#{prefix} " unless prefix.empty?
+ @errors += record.errors.full_messages.map { |message| "#{prefix}#{message}" }
+ end
+
+ def create_distribution
+ @distribution = container.debian_distributions.new(params)
+
+ append_errors(distribution)
+ return error unless errors.empty?
+
+ distribution.transaction do
+ if distribution.save
+ create_components
+ create_architectures
+
+ success
+ end
+ end || error
+ end
+
+ def create_components
+ create_objects(distribution.components, components, error_label: 'Component')
+ end
+
+ def create_architectures
+ create_objects(distribution.architectures, architectures, error_label: 'Architecture')
+ end
+
+ def create_objects(objects, object_names_from_params, error_label: )
+ object_names_from_params.each do |name|
+ new_object = objects.create(name: name)
+ append_errors(new_object, error_label)
+ raise ActiveRecord::Rollback unless new_object.persisted?
+ end
+ end
+
+ def success
+ ServiceResponse.success(payload: { distribution: distribution }, http_status: :created)
+ end
+
+ def error
+ ServiceResponse.error(message: errors.to_sentence, payload: { distribution: distribution })
+ end
+ end
+ end
+end
diff --git a/app/services/packages/debian/destroy_distribution_service.rb b/app/services/packages/debian/destroy_distribution_service.rb
new file mode 100644
index 00000000000..bef1127fece
--- /dev/null
+++ b/app/services/packages/debian/destroy_distribution_service.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Packages
+ module Debian
+ class DestroyDistributionService
+ def initialize(distribution)
+ @distribution = distribution
+ end
+
+ def execute
+ destroy_distribution
+ end
+
+ private
+
+ def destroy_distribution
+ if @distribution.destroy
+ success
+ else
+ error("Unable to destroy Debian #{@distribution.model_name.human.downcase}")
+ end
+ end
+
+ def success
+ ServiceResponse.success
+ end
+
+ def error(message)
+ ServiceResponse.error(message: message, payload: { distribution: @distribution })
+ end
+ end
+ end
+end
diff --git a/app/services/packages/debian/update_distribution_service.rb b/app/services/packages/debian/update_distribution_service.rb
new file mode 100644
index 00000000000..5bb59b854e9
--- /dev/null
+++ b/app/services/packages/debian/update_distribution_service.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: true
+
+module Packages
+ module Debian
+ class UpdateDistributionService
+ def initialize(distribution, params)
+ @distribution, @params = distribution, params
+
+ @components = params.delete(:components)
+
+ @architectures = params.delete(:architectures)
+ @architectures += ['all'] unless @architectures.nil?
+
+ @errors = []
+ end
+
+ def execute
+ update_distribution
+ end
+
+ private
+
+ attr_reader :distribution, :params, :components, :architectures, :errors
+
+ def append_errors(record, prefix = '')
+ return if record.valid?
+
+ prefix = "#{prefix} " unless prefix.empty?
+ @errors += record.errors.full_messages.map { |message| "#{prefix}#{message}" }
+ end
+
+ def update_distribution
+ distribution.transaction do
+ if distribution.update(params)
+ update_components if components
+ update_architectures if architectures
+
+ success
+ else
+ append_errors(distribution)
+ error
+ end
+ end || error
+ end
+
+ def update_components
+ update_objects(distribution.components, components, error_label: 'Component')
+ end
+
+ def update_architectures
+ update_objects(distribution.architectures, architectures, error_label: 'Architecture')
+ end
+
+ def update_objects(objects, object_names_from_params, error_label: )
+ current_object_names = objects.map(&:name)
+ missing_object_names = object_names_from_params - current_object_names
+ extra_object_names = current_object_names - object_names_from_params
+
+ missing_object_names.each do |name|
+ new_object = objects.create(name: name)
+ append_errors(new_object, error_label)
+ raise ActiveRecord::Rollback unless new_object.persisted?
+ end
+
+ extra_object_names.each do |name|
+ object = objects.with_name(name).first
+ raise ActiveRecord::Rollback unless object.destroy
+ end
+ end
+
+ def success
+ ServiceResponse.success(payload: { distribution: distribution })
+ end
+
+ def error
+ ServiceResponse.error(message: errors.to_sentence, payload: { distribution: distribution })
+ end
+ end
+ end
+end