From 6e99226cca41f36d92c4ccb2cd398d2256091adc Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 10 Feb 2016 12:07:46 +0100 Subject: [PATCH] Added PagesDomain --- app/models/pages_domain.rb | 29 ++++++++++++++ app/models/project.rb | 38 +------------------ ...808_add_pages_custom_domain_to_projects.rb | 10 ----- .../20160210105555_create_pages_domain.rb | 14 +++++++ 4 files changed, 45 insertions(+), 46 deletions(-) create mode 100644 app/models/pages_domain.rb delete mode 100644 db/migrate/20160209125808_add_pages_custom_domain_to_projects.rb create mode 100644 db/migrate/20160210105555_create_pages_domain.rb diff --git a/app/models/pages_domain.rb b/app/models/pages_domain.rb new file mode 100644 index 00000000000..eebdf7501de --- /dev/null +++ b/app/models/pages_domain.rb @@ -0,0 +1,29 @@ +class PagesDomain < ActiveRecord::Base + belongs_to :project + + validates :domain, hostname: true + validates_uniqueness_of :domain, allow_nil: true, allow_blank: true + validates :certificate, certificate: true, allow_nil: true, allow_blank: true + validates :key, certificate_key: true, allow_nil: true, allow_blank: true + + attr_encrypted :pages_custom_certificate_key, mode: :per_attribute_iv_and_salt, key: Gitlab::Application.secrets.db_key_base + + after_create :update + after_save :update + after_destroy :update + + def url + return unless domain + return unless Dir.exist?(project.public_pages_path) + + if certificate + return "https://#{domain}" + else + return "http://#{domain}" + end + end + + def update + UpdatePagesConfigurationService.new(project).execute + end +end diff --git a/app/models/project.rb b/app/models/project.rb index f447c2bf293..dac52a0fc5e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -150,6 +150,7 @@ class Project < ActiveRecord::Base has_many :lfs_objects, through: :lfs_objects_projects has_many :project_group_links, dependent: :destroy has_many :invited_groups, through: :project_group_links, source: :group + has_many :pages_domains, dependent: :destroy has_many :todos, dependent: :destroy has_many :notification_settings, dependent: :destroy, as: :source @@ -205,18 +206,11 @@ class Project < ActiveRecord::Base presence: true, inclusion: { in: ->(_object) { Gitlab.config.repositories.storages.keys } } - validates :pages_custom_domain, hostname: true, allow_blank: true, allow_nil: true - validates_uniqueness_of :pages_custom_domain, allow_nil: true, allow_blank: true - validates :pages_custom_certificate, certificate: true, allow_nil: true, allow_blank: true - validates :pages_custom_certificate_key, certificate_key: true, allow_nil: true, allow_blank: true - add_authentication_token_field :runners_token before_save :ensure_runners_token mount_uploader :avatar, AvatarUploader - attr_encrypted :pages_custom_certificate_key, mode: :per_attribute_iv_and_salt, key: Gitlab::Application.secrets.db_key_base - # Scopes default_scope { where(pending_delete: false) } @@ -1184,17 +1178,6 @@ class Project < ActiveRecord::Base "#{url}/#{path}" end - def pages_custom_url - return unless pages_custom_domain - return unless Dir.exist?(public_pages_path) - - if Gitlab.config.pages.https - return "https://#{pages_custom_domain}" - else - return "http://#{pages_custom_domain}" - end - end - def pages_path File.join(Settings.pages.path, path_with_namespace) end @@ -1203,32 +1186,15 @@ class Project < ActiveRecord::Base File.join(pages_path, 'public') end - def remove_pages_certificate - update( - pages_custom_certificate: nil, - pages_custom_certificate_key: nil - ) - - UpdatePagesConfigurationService.new(self).execute - end - def remove_pages # 1. We rename pages to temporary directory # 2. We wait 5 minutes, due to NFS caching # 3. We asynchronously remove pages with force - temp_path = "#{path}.#{SecureRandom.hex}" + temp_path = "#{path}.#{SecureRandom.hex}.deleted" if Gitlab::PagesTransfer.new.rename_project(path, temp_path, namespace.path) PagesWorker.perform_in(5.minutes, :remove, namespace.path, temp_path) end - - update( - pages_custom_certificate: nil, - pages_custom_certificate_key: nil, - pages_custom_domain: nil - ) - - UpdatePagesConfigurationService.new(self).execute end def wiki diff --git a/db/migrate/20160209125808_add_pages_custom_domain_to_projects.rb b/db/migrate/20160209125808_add_pages_custom_domain_to_projects.rb deleted file mode 100644 index 13b42d18a7a..00000000000 --- a/db/migrate/20160209125808_add_pages_custom_domain_to_projects.rb +++ /dev/null @@ -1,10 +0,0 @@ -class AddPagesCustomDomainToProjects < ActiveRecord::Migration - def change - add_column :projects, :pages_custom_certificate, :text - add_column :projects, :encrypted_pages_custom_certificate_key, :text - add_column :projects, :encrypted_pages_custom_certificate_key_iv, :string - add_column :projects, :encrypted_pages_custom_certificate_key_salt, :string - add_column :projects, :pages_custom_domain, :string, unique: true - add_column :projects, :pages_redirect_http, :boolean, default: false, null: false - end -end diff --git a/db/migrate/20160210105555_create_pages_domain.rb b/db/migrate/20160210105555_create_pages_domain.rb new file mode 100644 index 00000000000..9af206143bd --- /dev/null +++ b/db/migrate/20160210105555_create_pages_domain.rb @@ -0,0 +1,14 @@ +class CreatePagesDomain < ActiveRecord::Migration + def change + create_table :pages_domains do |t| + t.integer :project_id + t.text :certificate + t.text :encrypted_key + t.string :encrypted_key_iv + t.string :encrypted_key_salt + t.string :domain + end + + add_index :pages_domains, :domain, unique: true + end +end