debian-mirror-gitlab/app/services/projects/update_pages_configuration_service.rb

110 lines
2.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Projects
class UpdatePagesConfigurationService < BaseService
2019-03-02 22:35:43 +05:30
include Gitlab::Utils::StrongMemoize
2017-08-17 22:00:37 +05:30
attr_reader :project
def initialize(project)
@project = project
end
def execute
2021-04-29 21:17:54 +05:30
return success unless ::Settings.pages.local_store.enabled
2021-04-17 20:07:23 +05:30
2020-10-24 23:57:45 +05:30
# If the pages were never deployed, we can't write out the config, as the
# directory would not exist.
# https://gitlab.com/gitlab-org/gitlab/-/issues/235139
return success unless project.pages_deployed?
unless file_equals?(pages_config_file, pages_config_json)
update_file(pages_config_file, pages_config_json)
reload_daemon
2019-03-02 22:35:43 +05:30
end
2020-10-24 23:57:45 +05:30
success
2017-08-17 22:00:37 +05:30
end
private
2019-03-02 22:35:43 +05:30
def pages_config_json
strong_memoize(:pages_config_json) do
pages_config.to_json
end
end
2017-08-17 22:00:37 +05:30
def pages_config
{
2018-05-09 12:01:36 +05:30
domains: pages_domains_config,
2018-12-05 23:21:45 +05:30
https_only: project.pages_https_only?,
id: project.project_id,
access_control: !project.public_pages?
2017-08-17 22:00:37 +05:30
}
end
def pages_domains_config
2018-03-17 18:26:18 +05:30
enabled_pages_domains.map do |domain|
2017-08-17 22:00:37 +05:30
{
domain: domain.domain,
certificate: domain.certificate,
2018-05-09 12:01:36 +05:30
key: domain.key,
2018-12-05 23:21:45 +05:30
https_only: project.pages_https_only? && domain.https?,
id: project.project_id,
access_control: !project.public_pages?
2017-08-17 22:00:37 +05:30
}
end
end
2018-03-17 18:26:18 +05:30
def enabled_pages_domains
if Gitlab::CurrentSettings.pages_domain_verification_enabled?
project.pages_domains.enabled
else
project.pages_domains
end
end
2017-08-17 22:00:37 +05:30
def reload_daemon
# GitLab Pages daemon constantly watches for modification time of `pages.path`
# It reloads configuration when `pages.path` is modified
update_file(pages_update_file, SecureRandom.hex(64))
end
def pages_path
@pages_path ||= project.pages_path
end
def pages_config_file
File.join(pages_path, 'config.json')
end
def pages_update_file
File.join(::Settings.pages.path, '.update')
end
def update_file(file, data)
temp_file = "#{file}.#{SecureRandom.hex(16)}"
File.open(temp_file, 'w') do |f|
f.write(data)
end
FileUtils.move(temp_file, file, force: true)
ensure
# In case if the updating fails
FileUtils.remove(temp_file, force: true)
end
2019-03-02 22:35:43 +05:30
def file_equals?(file, data)
existing_data = read_file(file)
data == existing_data.to_s
end
def read_file(file)
File.open(file, 'r') do |f|
f.read
end
rescue
nil
end
2017-08-17 22:00:37 +05:30
end
end