2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-09 12:06:55 -05:00
|
|
|
module Projects
|
|
|
|
class UpdatePagesConfigurationService < BaseService
|
2019-01-16 07:00:43 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-02-09 12:06:55 -05:00
|
|
|
attr_reader :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-01-16 07:00:43 -05:00
|
|
|
if file_equals?(pages_config_file, pages_config_json)
|
|
|
|
return success(reload: false)
|
2019-01-16 06:48:50 -05:00
|
|
|
end
|
|
|
|
|
2019-01-16 07:00:43 -05:00
|
|
|
update_file(pages_config_file, pages_config_json)
|
|
|
|
reload_daemon
|
|
|
|
success(reload: true)
|
2016-02-09 12:06:55 -05:00
|
|
|
rescue => e
|
|
|
|
error(e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-16 07:00:43 -05:00
|
|
|
def pages_config_json
|
|
|
|
strong_memoize(:pages_config_json) do
|
|
|
|
pages_config.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-10 09:06:31 -05:00
|
|
|
def pages_config
|
|
|
|
{
|
2018-01-03 03:07:03 -05:00
|
|
|
domains: pages_domains_config,
|
2018-10-05 09:41:11 -04:00
|
|
|
https_only: project.pages_https_only?,
|
|
|
|
id: project.project_id,
|
|
|
|
access_control: !project.public_pages?
|
2016-02-10 09:06:31 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def pages_domains_config
|
2018-02-06 08:25:46 -05:00
|
|
|
enabled_pages_domains.map do |domain|
|
2016-02-10 09:06:31 -05:00
|
|
|
{
|
|
|
|
domain: domain.domain,
|
|
|
|
certificate: domain.certificate,
|
2018-01-03 03:07:03 -05:00
|
|
|
key: domain.key,
|
2018-10-05 09:41:11 -04:00
|
|
|
https_only: project.pages_https_only? && domain.https?,
|
|
|
|
id: project.project_id,
|
|
|
|
access_control: !project.public_pages?
|
2016-02-10 09:06:31 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-06 08:25:46 -05:00
|
|
|
def enabled_pages_domains
|
|
|
|
if Gitlab::CurrentSettings.pages_domain_verification_enabled?
|
|
|
|
project.pages_domains.enabled
|
|
|
|
else
|
|
|
|
project.pages_domains
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-09 12:06:55 -05:00
|
|
|
def reload_daemon
|
|
|
|
# GitLab Pages daemon constantly watches for modification time of `pages.path`
|
|
|
|
# It reloads configuration when `pages.path` is modified
|
2016-02-12 10:05:17 -05:00
|
|
|
update_file(pages_update_file, SecureRandom.hex(64))
|
2016-02-09 12:06:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def pages_path
|
|
|
|
@pages_path ||= project.pages_path
|
|
|
|
end
|
|
|
|
|
2016-02-10 09:06:31 -05:00
|
|
|
def pages_config_file
|
2016-02-10 10:21:39 -05:00
|
|
|
File.join(pages_path, 'config.json')
|
2016-02-09 12:06:55 -05:00
|
|
|
end
|
|
|
|
|
2016-02-12 10:05:17 -05:00
|
|
|
def pages_update_file
|
2017-01-26 20:58:42 -05:00
|
|
|
File.join(::Settings.pages.path, '.update')
|
2016-02-12 10:05:17 -05:00
|
|
|
end
|
|
|
|
|
2016-02-09 12:06:55 -05:00
|
|
|
def update_file(file, data)
|
2016-02-12 10:05:17 -05:00
|
|
|
temp_file = "#{file}.#{SecureRandom.hex(16)}"
|
2016-02-15 08:44:54 -05:00
|
|
|
File.open(temp_file, 'w') do |f|
|
|
|
|
f.write(data)
|
2016-02-09 12:06:55 -05:00
|
|
|
end
|
2016-02-15 08:44:54 -05:00
|
|
|
FileUtils.move(temp_file, file, force: true)
|
2016-02-12 10:05:17 -05:00
|
|
|
ensure
|
|
|
|
# In case if the updating fails
|
2016-02-15 08:44:54 -05:00
|
|
|
FileUtils.remove(temp_file, force: true)
|
2016-02-09 12:06:55 -05:00
|
|
|
end
|
2019-01-16 06:48:50 -05:00
|
|
|
|
2019-01-16 07:00:43 -05:00
|
|
|
def file_equals?(file, data)
|
|
|
|
existing_data = read_file(file)
|
|
|
|
data == existing_data.to_s
|
|
|
|
end
|
|
|
|
|
2019-01-16 06:48:50 -05:00
|
|
|
def read_file(file)
|
|
|
|
File.open(file, 'r') do |f|
|
|
|
|
f.read
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
2016-02-09 12:06:55 -05:00
|
|
|
end
|
|
|
|
end
|