1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/karrick/godirwalk"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-pages/metrics"
)
type domains map[string]*domain
type domainsUpdater func(domains domains)
func (d domains) addDomain(rootDomain, group, projectName string, config *domainConfig) {
newDomain := &domain{
Group: group,
ProjectName: projectName,
Config: config,
}
var domainName string
domainName = strings.ToLower(config.Domain)
d[domainName] = newDomain
}
func (d domains) updateGroupDomain(rootDomain, group, projectName string, httpsOnly bool) {
domainName := strings.ToLower(group + "." + rootDomain)
groupDomain := d[domainName]
if groupDomain == nil {
groupDomain = &domain{
Group: group,
Projects: make(projects),
}
}
groupDomain.Projects[projectName] = &project{
HTTPSOnly: httpsOnly,
}
d[domainName] = groupDomain
}
func (d domains) readProjectConfig(rootDomain string, group, projectName string, config *domainsConfig) {
if config == nil {
// This is necessary to preserve the previous behaviour where a
// group domain is created even if no config.json files are
// loaded successfully. Is it safe to remove this?
d.updateGroupDomain(rootDomain, group, projectName, false)
return
}
d.updateGroupDomain(rootDomain, group, projectName, config.HTTPSOnly)
for _, domainConfig := range config.Domains {
config := domainConfig // domainConfig is reused for each loop iteration
if domainConfig.Valid(rootDomain) {
d.addDomain(rootDomain, group, projectName, &config)
}
}
}
func readProject(group, projectName string, fanIn chan<- jobResult) {
if strings.HasPrefix(projectName, ".") {
return
}
// Ignore projects that have .deleted in name
if strings.HasSuffix(projectName, ".deleted") {
return
}
if _, err := os.Lstat(filepath.Join(group, projectName, "public")); err != nil {
return
}
// We read the config.json file _before_ fanning in, because it does disk
// IO and it does not need access to the domains map.
config := &domainsConfig{}
if err := config.Read(group, projectName); err != nil {
config = nil
}
fanIn <- jobResult{group: group, project: projectName, config: config}
}
func readProjects(group string, buf []byte, fanIn chan<- jobResult) {
fis, err := godirwalk.ReadDirents(group, buf)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"group": group,
}).Print("readdir failed")
return
}
for _, project := range fis {
// Ignore non directories
if !project.IsDir() {
continue
}
readProject(group, project.Name(), fanIn)
}
}
type jobResult struct {
group string
project string
config *domainsConfig
}
func (d domains) ReadGroups(rootDomain string) error {
fis, err := godirwalk.ReadDirents(".", nil)
if err != nil {
return err
}
fanOutGroups := make(chan string)
fanIn := make(chan jobResult)
wg := &sync.WaitGroup{}
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
buf := make([]byte, 2*os.Getpagesize())
for group := range fanOutGroups {
readProjects(group, buf, fanIn)
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(fanIn)
}()
done := make(chan struct{})
go func() {
for result := range fanIn {
d.readProjectConfig(rootDomain, result.group, result.project, result.config)
}
close(done)
}()
for _, group := range fis {
if !group.IsDir() {
continue
}
if strings.HasPrefix(group.Name(), ".") {
continue
}
fanOutGroups <- group.Name()
}
close(fanOutGroups)
<-done
return nil
}
const (
updateFile = ".update"
)
func watchDomains(rootDomain string, updater domainsUpdater, interval time.Duration) {
lastUpdate := []byte("no-update")
for {
// Read the update file
update, err := ioutil.ReadFile(updateFile)
if err != nil && !os.IsNotExist(err) {
log.WithError(err).Print("failed to read update timestamp")
time.Sleep(interval)
continue
}
// If it's the same ignore
if bytes.Equal(lastUpdate, update) {
time.Sleep(interval)
continue
}
lastUpdate = update
started := time.Now()
domains := make(domains)
if err := domains.ReadGroups(rootDomain); err != nil {
log.WithError(err).Warn("domain scan failed")
}
duration := time.Since(started).Seconds()
log.WithFields(log.Fields{
"domains": len(domains),
"duration": duration,
"hash": update,
}).Print("updated domains")
if updater != nil {
updater(domains)
}
// Update prometheus metrics
metrics.DomainLastUpdateTime.Set(float64(time.Now().UTC().Unix()))
metrics.DomainsServed.Set(float64(len(domains)))
metrics.DomainUpdates.Inc()
time.Sleep(interval)
}
}
|