-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.go
443 lines (417 loc) · 10.9 KB
/
package.go
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) 2016, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package boltPackages
import (
"crypto/rand"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/boltdb/bolt"
"resenje.org/boltutils"
"gopherpit.com/gopherpit/services/packages"
)
var (
bucketNamePackages = []byte("Packages")
bucketNameIndexPackageIDDomainID = []byte("Index_PackageID_DomainID")
bucketNameIndexDomainIDPathPackageID = []byte("Index_DomainID_Path_PackageID")
bucketNameIndexDisabledPackageIDs = []byte("Index_Disabled_PackageID_1")
hostAndPortRegex = regexp.MustCompile(`^([a-z0-9]+[\-a-z0-9\.]*)(?:\:\d+)?$`)
)
type packageRecord struct {
id string
DomainID string `json:"domain-id,omitempty"`
Path string `json:"path,omitempty"`
VCS packages.VCS `json:"vcs,omitempty"`
RepoRoot string `json:"repo-root,omitempty"`
RefType packages.RefType `json:"ref-type,omitempty"`
RefName string `json:"ref-name,omitempty"`
GoSource string `json:"go-source,omitempty"`
RedirectURL string `json:"redirect-url,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}
func (p packageRecord) export(tx *bolt.Tx) (pkg *packages.Package, err error) {
pkg = &packages.Package{
ID: p.id,
Path: p.Path,
VCS: p.VCS,
RepoRoot: p.RepoRoot,
RefType: p.RefType,
RefName: p.RefName,
GoSource: p.GoSource,
RedirectURL: p.RedirectURL,
Disabled: p.Disabled,
}
if tx != nil && p.DomainID != "" {
d, err := getDomainRecordByID(tx, []byte(p.DomainID))
if err != nil {
return pkg, err
}
pkg.Domain = d.export()
}
return
}
func (p *packageRecord) update(tx *bolt.Tx, o *packages.PackageOptions) (changes packages.Changes, err error) {
if o == nil {
return
}
if o.Domain != nil {
prevDomain := &domainRecord{}
newDomain := &domainRecord{}
if p.DomainID != "" {
prevDomain, err = getDomainRecord(tx, []byte(p.DomainID))
if err != nil {
return changes, err
}
}
if *o.Domain != "" {
newDomain, err = getDomainRecord(tx, []byte(*o.Domain))
if err != nil {
return changes, err
}
p.DomainID = newDomain.id
} else {
p.DomainID = ""
}
if prevDomain.id != newDomain.id {
changes = append(changes, packages.Change{
Field: "domain-id",
From: stringToStringPtr(prevDomain.id),
To: stringToStringPtr(newDomain.id),
})
changes = append(changes, packages.Change{
Field: "domain-fqdn",
From: stringToStringPtr(prevDomain.FQDN),
To: stringToStringPtr(newDomain.FQDN),
})
}
}
if o.Path != nil {
if p.Path != *o.Path {
changes = append(changes, packages.Change{
Field: "path",
From: stringToStringPtr(p.Path),
To: stringToStringPtr(*o.Path),
})
}
p.Path = *o.Path
}
if o.VCS != nil {
if p.VCS != *o.VCS {
changes = append(changes, packages.Change{
Field: "vcs",
From: stringToStringPtr(string(p.VCS)),
To: stringToStringPtr(string(*o.VCS)),
})
}
p.VCS = *o.VCS
}
if o.RepoRoot != nil {
if p.RepoRoot != *o.RepoRoot {
changes = append(changes, packages.Change{
Field: "repo-root",
From: stringToStringPtr(p.RepoRoot),
To: stringToStringPtr(*o.RepoRoot),
})
}
p.RepoRoot = *o.RepoRoot
}
if o.RefType != nil {
if p.RefType != *o.RefType {
changes = append(changes, packages.Change{
Field: "ref-type",
From: stringToStringPtr(string(p.RefType)),
To: stringToStringPtr(string(*o.RefType)),
})
}
p.RefType = *o.RefType
}
if o.RefName != nil {
if p.RefName != *o.RefName {
changes = append(changes, packages.Change{
Field: "ref-name",
From: stringToStringPtr(p.RefName),
To: stringToStringPtr(*o.RefName),
})
}
p.RefName = *o.RefName
}
if o.GoSource != nil {
if p.GoSource != *o.GoSource {
changes = append(changes, packages.Change{
Field: "go-source",
From: stringToStringPtr(p.GoSource),
To: stringToStringPtr(*o.GoSource),
})
}
p.GoSource = *o.GoSource
}
if o.RedirectURL != nil {
if p.RedirectURL != *o.RedirectURL {
changes = append(changes, packages.Change{
Field: "redirect-url",
From: stringToStringPtr(p.RedirectURL),
To: stringToStringPtr(*o.RedirectURL),
})
}
p.RedirectURL = *o.RedirectURL
}
if o.Disabled != nil {
if p.Disabled != *o.Disabled {
changes = append(changes, packages.Change{
Field: "disabled",
From: boolPtrToStringPtr(&p.Disabled),
To: boolPtrToStringPtr(o.Disabled),
})
}
p.Disabled = *o.Disabled
}
return
}
func getPackageRecord(tx *bolt.Tx, id []byte) (p *packageRecord, err error) {
bucket := tx.Bucket(bucketNamePackages)
if bucket == nil {
err = packages.ErrPackageNotFound
return
}
data := bucket.Get(id)
if data == nil {
err = packages.ErrPackageNotFound
return
}
if err = json.Unmarshal(data, &p); err != nil {
return
}
p.id = string(id)
return
}
func getPackageIDByPath(tx *bolt.Tx, domainID, path []byte) (id []byte, err error) {
bucket := tx.Bucket(bucketNameIndexDomainIDPathPackageID)
if bucket == nil {
err = packages.ErrPackageNotFound
return
}
bucket = bucket.Bucket(domainID)
if bucket == nil {
err = packages.ErrPackageNotFound
return
}
id = bucket.Get(path)
if id == nil {
err = packages.ErrPackageNotFound
return
}
return
}
func (p *packageRecord) save(tx *bolt.Tx) (err error) {
p.Path = strings.TrimRight(strings.TrimSpace(p.Path), "/")
p.RepoRoot = strings.TrimSpace(p.RepoRoot)
// Required fields
if p.DomainID == "" {
return packages.ErrPackageDomainRequired
}
if p.Path == "" {
return packages.ErrPackagePathRequired
}
if p.VCS == "" {
return packages.ErrPackageVCSRequired
}
if p.RepoRoot == "" {
return packages.ErrPackageRepoRootRequired
}
repoRoot, err := url.Parse(p.RepoRoot)
if err != nil {
return packages.ErrPackageRepoRootInvalid
}
if repoRoot.Scheme == "" {
return packages.ErrPackageRepoRootSchemeRequired
}
ok := false
for _, s := range packages.VCSSchemes[p.VCS] {
if repoRoot.Scheme == s {
ok = true
break
}
}
if !ok {
return packages.ErrPackageRepoRootSchemeInvalid
}
if !hostAndPortRegex.MatchString(repoRoot.Host) {
return packages.ErrPackageRepoRootHostInvalid
}
if p.RefName != "" && (p.VCS != packages.VCSGit || (p.VCS == packages.VCSGit && !(repoRoot.Scheme == "http" || repoRoot.Scheme == "https"))) {
return packages.ErrPackageRefChangeRejected
}
// existing package record
ep := &packageRecord{}
if p.id == "" {
// Generate new id
id, err := newPackageID(tx)
if err != nil {
return fmt.Errorf("generate unique ID: %s", err)
}
p.id = id
} else {
// Check if package with p.ID exists
cp, err := getPackageRecord(tx, []byte(p.id))
if err != nil {
return fmt.Errorf("package record save get package record %s: %s", p.id, err)
}
if cp != nil {
ep = cp
}
}
// Address must be unique
checkID, err := getPackageIDByPath(tx, []byte(p.DomainID), []byte(p.Path))
switch err {
case packages.ErrPackageNotFound:
case nil:
if p.id == "" || string(checkID) != p.id {
return packages.ErrPackageAlreadyExists
}
default:
return fmt.Errorf("get package id by path: %s", err)
}
id := []byte(p.id)
var bucket *bolt.Bucket
// Domain ID index
if p.DomainID != ep.DomainID {
if ep.DomainID != "" {
if err := boltutils.BoltDeepDelete(
tx,
bucketNameIndexPackageIDDomainID,
id,
); err != nil {
return fmt.Errorf("bolt deep delete: %s", err)
}
}
if p.DomainID != "" {
if err := boltutils.BoltDeepPut(
tx,
bucketNameIndexPackageIDDomainID,
id,
[]byte(p.DomainID),
); err != nil {
return fmt.Errorf("bolt deep put: %s", err)
}
}
}
// Domain ID and Path index
if p.DomainID != ep.DomainID || p.Path != ep.Path {
if ep.DomainID != "" && ep.Path != "" {
if err := boltutils.BoltDeepDelete(
tx,
bucketNameIndexDomainIDPathPackageID,
[]byte(ep.DomainID),
[]byte(ep.Path),
); err != nil {
return fmt.Errorf("bolt deep delete: %s", err)
}
}
if p.DomainID != "" && p.Path != "" {
if err := boltutils.BoltDeepPut(
tx,
bucketNameIndexDomainIDPathPackageID,
[]byte(p.DomainID),
[]byte(p.Path),
id,
); err != nil {
return fmt.Errorf("bolt deep put: %s", err)
}
}
}
// Disabled index
if p.Disabled == false && ep.Disabled == true {
bucket = tx.Bucket(bucketNameIndexDisabledPackageIDs)
if bucket != nil {
if err := bucket.Delete(id); err != nil {
return fmt.Errorf("bucket(%s).Delete(%s): %s", bucketNameIndexDisabledPackageIDs, ep.id, err)
}
}
}
if p.Disabled == true && ep.Disabled == false {
bucket = tx.Bucket(bucketNameIndexDisabledPackageIDs)
if bucket != nil {
if err := bucket.Put(id, flagBytes); err != nil {
return fmt.Errorf("bucket(%s).Put(%s, %s) %s", bucketNameIndexDisabledPackageIDs, p.id, flagBytes, err)
}
}
}
// Save the package record data
value, err := json.Marshal(p)
if err != nil {
return fmt.Errorf("json marshal: %s", err)
}
bucket, err = tx.CreateBucketIfNotExists(bucketNamePackages)
if err != nil {
return fmt.Errorf("CreateBucketIfNotExists(%s) %s", bucketNamePackages, err)
}
if err := bucket.Put(id, value); err != nil {
return fmt.Errorf("bucket(%s).Put(%s) %s", bucketNamePackages, p.id, err)
}
return nil
}
func (p packageRecord) delete(tx *bolt.Tx) (err error) {
id := []byte(p.id)
// Domain ID indexes
if p.DomainID != "" {
if err := boltutils.BoltDeepDelete(
tx,
bucketNameIndexPackageIDDomainID,
id,
); err != nil {
return fmt.Errorf("bolt deep delete: %s", err)
}
if err := boltutils.BoltDeepDelete(
tx,
bucketNameIndexDomainIDPathPackageID,
[]byte(p.DomainID),
[]byte(p.Path),
); err != nil {
return fmt.Errorf("bolt deep delete: %s", err)
}
}
// Disabled index
bucket := tx.Bucket(bucketNameIndexDisabledPackageIDs)
if bucket != nil {
if err := bucket.Delete(id); err != nil {
return fmt.Errorf("bucket(%s).Delete(%s): %s", bucketNameIndexDisabledPackageIDs, p.id, err)
}
}
// Package data
bucket, err = tx.CreateBucketIfNotExists(bucketNamePackages)
if err != nil {
return
}
return bucket.Delete(id)
}
func newPackageID(tx *bolt.Tx) (id string, err error) {
bp := make([]byte, 2)
binary.LittleEndian.PutUint16(bp, uint16(os.Getpid()))
br := make([]byte, 19)
bt := make([]byte, 4)
binary.LittleEndian.PutUint32(bt, uint32(time.Now().UTC().Unix()))
bucket, err := tx.CreateBucketIfNotExists(bucketNamePackages)
if err != nil {
return
}
for i := 0; i < 100; i++ {
_, err = rand.Read(br)
if err != nil {
return
}
b := append(bt, append(bp, br...)...)
id = strings.TrimRight(base32Encoding.EncodeToString(b), "=")
if v := bucket.Get([]byte(id)); v == nil {
return
}
}
return "", errors.New("unable to generate unique domain id")
}