Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

update_pages_configuration_service.rb 1.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
module Projects
  class UpdatePagesConfigurationService < BaseService
    attr_reader :project

    def initialize(project)
      @project = project
    end

    def execute
Kamil Trzcinski's avatar
Kamil Trzcinski committed
10
      update_file(pages_config_file, pages_config.to_json)
11
12
13
14
15
16
17
18
      reload_daemon
      success
    rescue => e
      error(e.message)
    end

    private

19
20
    def pages_config
      {
Rob Watson's avatar
Rob Watson committed
21
22
        domains: pages_domains_config,
        https_only: project.pages_https_only?
23
24
25
26
      }
    end

    def pages_domains_config
27
      enabled_pages_domains.map do |domain|
28
29
30
        {
          domain: domain.domain,
          certificate: domain.certificate,
Rob Watson's avatar
Rob Watson committed
31
32
          key: domain.key,
          https_only: project.pages_https_only? && domain.https?
33
34
35
36
        }
      end
    end

37
38
39
40
41
42
43
44
    def enabled_pages_domains
      if Gitlab::CurrentSettings.pages_domain_verification_enabled?
        project.pages_domains.enabled
      else
        project.pages_domains
      end
    end

45
46
47
    def reload_daemon
      # GitLab Pages daemon constantly watches for modification time of `pages.path`
      # It reloads configuration when `pages.path` is modified
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48
      update_file(pages_update_file, SecureRandom.hex(64))
49
50
51
52
53
54
    end

    def pages_path
      @pages_path ||= project.pages_path
    end

55
    def pages_config_file
Kamil Trzcinski's avatar
Kamil Trzcinski committed
56
      File.join(pages_path, 'config.json')
57
58
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
59
    def pages_update_file
60
      File.join(::Settings.pages.path, '.update')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
61
62
    end

63
    def update_file(file, data)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
64
      unless data
Kamil Trzcinski's avatar
Kamil Trzcinski committed
65
        FileUtils.remove(file, force: true)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
66
67
68
69
        return
      end

      temp_file = "#{file}.#{SecureRandom.hex(16)}"
Kamil Trzcinski's avatar
Kamil Trzcinski committed
70
71
      File.open(temp_file, 'w') do |f|
        f.write(data)
72
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
73
      FileUtils.move(temp_file, file, force: true)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74
75
    ensure
      # In case if the updating fails
Kamil Trzcinski's avatar
Kamil Trzcinski committed
76
      FileUtils.remove(temp_file, force: true)
77
78
79
    end
  end
end