2016-02-09 17:06:55 +00:00
|
|
|
module Projects
|
|
|
|
class UpdatePagesConfigurationService < BaseService
|
|
|
|
attr_reader :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-02-15 13:44:54 +00:00
|
|
|
update_file(pages_config_file, pages_config.to_json)
|
2016-02-09 17:06:55 +00:00
|
|
|
reload_daemon
|
|
|
|
success
|
|
|
|
rescue => e
|
|
|
|
error(e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
def pages_config
|
|
|
|
{
|
|
|
|
domains: pages_domains_config
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def pages_domains_config
|
|
|
|
project.pages_domains.map do |domain|
|
|
|
|
{
|
|
|
|
domain: domain.domain,
|
|
|
|
certificate: domain.certificate,
|
|
|
|
key: domain.key,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-09 17:06:55 +00: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 15:05:17 +00:00
|
|
|
update_file(pages_update_file, SecureRandom.hex(64))
|
2016-02-09 17:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pages_path
|
|
|
|
@pages_path ||= project.pages_path
|
|
|
|
end
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
def pages_config_file
|
2016-02-10 15:21:39 +00:00
|
|
|
File.join(pages_path, 'config.json')
|
2016-02-09 17:06:55 +00:00
|
|
|
end
|
|
|
|
|
2016-02-12 15:05:17 +00:00
|
|
|
def pages_update_file
|
2017-01-27 01:58:42 +00:00
|
|
|
File.join(::Settings.pages.path, '.update')
|
2016-02-12 15:05:17 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 17:06:55 +00:00
|
|
|
def update_file(file, data)
|
2016-02-12 15:05:17 +00:00
|
|
|
unless data
|
2016-02-15 13:44:54 +00:00
|
|
|
FileUtils.remove(file, force: true)
|
2016-02-12 15:05:17 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
temp_file = "#{file}.#{SecureRandom.hex(16)}"
|
2016-02-15 13:44:54 +00:00
|
|
|
File.open(temp_file, 'w') do |f|
|
|
|
|
f.write(data)
|
2016-02-09 17:06:55 +00:00
|
|
|
end
|
2016-02-15 13:44:54 +00:00
|
|
|
FileUtils.move(temp_file, file, force: true)
|
2016-02-12 15:05:17 +00:00
|
|
|
ensure
|
|
|
|
# In case if the updating fails
|
2016-02-15 13:44:54 +00:00
|
|
|
FileUtils.remove(temp_file, force: true)
|
2016-02-09 17:06:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|