@@ -63,15 +63,10 @@ func init() {
63
63
const complementLabel = "complement_context"
64
64
65
65
type Builder struct {
66
- BaseImage string
67
- ImageArgs []string
68
- KeepBlueprints []string
66
+ Config * config.Complement
69
67
CSAPIPort int
70
68
FederationPort int
71
69
Docker * client.Client
72
- debugLogging bool
73
- bestEffort bool
74
- config * config.Complement
75
70
}
76
71
77
72
func NewBuilder (cfg * config.Complement ) (* Builder , error ) {
@@ -81,19 +76,14 @@ func NewBuilder(cfg *config.Complement) (*Builder, error) {
81
76
}
82
77
return & Builder {
83
78
Docker : cli ,
84
- BaseImage : cfg .BaseImageURI ,
85
- ImageArgs : cfg .BaseImageArgs ,
86
- KeepBlueprints : cfg .KeepBlueprints ,
79
+ Config : cfg ,
87
80
CSAPIPort : 8008 ,
88
81
FederationPort : 8448 ,
89
- debugLogging : cfg .DebugLoggingEnabled ,
90
- bestEffort : cfg .BestEffort ,
91
- config : cfg ,
92
82
}, nil
93
83
}
94
84
95
85
func (d * Builder ) log (str string , args ... interface {}) {
96
- if ! d .debugLogging {
86
+ if ! d .Config . DebugLoggingEnabled {
97
87
return
98
88
}
99
89
log .Printf (str , args ... )
@@ -117,7 +107,10 @@ func (d *Builder) Cleanup() {
117
107
// removeImages removes all images with `complementLabel`.
118
108
func (d * Builder ) removeNetworks () error {
119
109
networks , err := d .Docker .NetworkList (context .Background (), types.NetworkListOptions {
120
- Filters : label (complementLabel ),
110
+ Filters : label (
111
+ complementLabel ,
112
+ "complement_pkg=" + d .Config .PackageNamespace ,
113
+ ),
121
114
})
122
115
if err != nil {
123
116
return err
@@ -134,7 +127,10 @@ func (d *Builder) removeNetworks() error {
134
127
// removeImages removes all images with `complementLabel`.
135
128
func (d * Builder ) removeImages () error {
136
129
images , err := d .Docker .ImageList (context .Background (), types.ImageListOptions {
137
- Filters : label (complementLabel ),
130
+ Filters : label (
131
+ complementLabel ,
132
+ "complement_pkg=" + d .Config .PackageNamespace ,
133
+ ),
138
134
})
139
135
if err != nil {
140
136
return err
@@ -156,7 +152,7 @@ func (d *Builder) removeImages() error {
156
152
}
157
153
bprintName := img .Labels ["complement_blueprint" ]
158
154
keep := false
159
- for _ , keepBprint := range d .KeepBlueprints {
155
+ for _ , keepBprint := range d .Config . KeepBlueprints {
160
156
if bprintName == keepBprint {
161
157
keep = true
162
158
break
@@ -180,8 +176,11 @@ func (d *Builder) removeImages() error {
180
176
// removeContainers removes all containers with `complementLabel`.
181
177
func (d * Builder ) removeContainers () error {
182
178
containers , err := d .Docker .ContainerList (context .Background (), types.ContainerListOptions {
183
- All : true ,
184
- Filters : label (complementLabel ),
179
+ All : true ,
180
+ Filters : label (
181
+ complementLabel ,
182
+ "complement_pkg=" + d .Config .PackageNamespace ,
183
+ ),
185
184
})
186
185
if err != nil {
187
186
return err
@@ -201,7 +200,10 @@ func (d *Builder) ConstructBlueprintsIfNotExist(bs []b.Blueprint) error {
201
200
var blueprintsToBuild []b.Blueprint
202
201
for _ , bprint := range bs {
203
202
images , err := d .Docker .ImageList (context .Background (), types.ImageListOptions {
204
- Filters : label ("complement_blueprint=" + bprint .Name ),
203
+ Filters : label (
204
+ "complement_blueprint=" + bprint .Name ,
205
+ "complement_pkg=" + d .Config .PackageNamespace ,
206
+ ),
205
207
})
206
208
if err != nil {
207
209
return fmt .Errorf ("ConstructBlueprintsIfNotExist: failed to ImageList: %w" , err )
@@ -242,7 +244,10 @@ func (d *Builder) ConstructBlueprints(bs []b.Blueprint) error {
242
244
foundImages := false
243
245
for i := 0 ; i < 50 ; i ++ { // max 5s
244
246
images , err := d .Docker .ImageList (context .Background (), types.ImageListOptions {
245
- Filters : label (complementLabel ),
247
+ Filters : label (
248
+ complementLabel ,
249
+ "complement_pkg=" + d .Config .PackageNamespace ,
250
+ ),
246
251
})
247
252
if err != nil {
248
253
return err
@@ -265,12 +270,12 @@ func (d *Builder) ConstructBlueprints(bs []b.Blueprint) error {
265
270
266
271
// construct all Homeservers sequentially then commits them
267
272
func (d * Builder ) construct (bprint b.Blueprint ) (errs []error ) {
268
- networkID , err := CreateNetworkIfNotExists (d .Docker , bprint .Name )
273
+ networkID , err := createNetworkIfNotExists (d .Docker , d . Config . PackageNamespace , bprint .Name )
269
274
if err != nil {
270
275
return []error {err }
271
276
}
272
277
273
- runner := instruction .NewRunner (bprint .Name , d .bestEffort , d .debugLogging )
278
+ runner := instruction .NewRunner (bprint .Name , d .Config . BestEffort , d .Config . DebugLoggingEnabled )
274
279
results := make ([]result , len (bprint .Homeservers ))
275
280
for i , hs := range bprint .Homeservers {
276
281
res := d .constructHomeserver (bprint .Name , runner , hs , networkID )
@@ -342,7 +347,7 @@ func (d *Builder) construct(bprint b.Blueprint) (errs []error) {
342
347
343
348
// construct this homeserver and execute its instructions, keeping the container alive.
344
349
func (d * Builder ) constructHomeserver (blueprintName string , runner * instruction.Runner , hs b.Homeserver , networkID string ) result {
345
- contextStr := fmt .Sprintf ("%s.%s" , blueprintName , hs .Name )
350
+ contextStr := fmt .Sprintf ("%s.%s.%s" , d . Config . PackageNamespace , blueprintName , hs .Name )
346
351
d .log ("%s : constructing homeserver...\n " , contextStr )
347
352
dep , err := d .deployBaseImage (blueprintName , hs , contextStr , networkID )
348
353
if err != nil {
@@ -376,15 +381,17 @@ func (d *Builder) deployBaseImage(blueprintName string, hs b.Homeserver, context
376
381
asIDToRegistrationMap := asIDToRegistrationFromLabels (labelsForApplicationServices (hs ))
377
382
378
383
return deployImage (
379
- d .Docker , d .BaseImage , d .CSAPIPort , fmt .Sprintf ("complement_%s" , contextStr ), blueprintName , hs .Name , asIDToRegistrationMap , contextStr ,
380
- networkID , d .config .VersionCheckIterations ,
384
+ d .Docker , d .Config .BaseImageURI , d .CSAPIPort , fmt .Sprintf ("complement_%s" , contextStr ),
385
+ d .Config .PackageNamespace , blueprintName , hs .Name , asIDToRegistrationMap , contextStr ,
386
+ networkID , d .Config .VersionCheckIterations ,
381
387
)
382
388
}
383
389
384
390
// getCaVolume returns the correct volume mount for providing a CA to homeserver containers.
385
391
// If running CI, returns an error if it's unable to find a volume that has /ca
386
392
// Otherwise, returns an error if we're unable to find the <cwd>/ca directory on the local host
387
393
func getCaVolume (ctx context.Context , docker * client.Client ) (caMount mount.Mount , err error ) {
394
+ // TODO: wrap in a lockfile
388
395
if os .Getenv ("CI" ) == "true" {
389
396
// When in CI, Complement itself is a container with the CA volume mounted at /ca.
390
397
// We need to mount this volume to all homeserver containers to synchronize the CA cert.
@@ -484,7 +491,7 @@ func generateASRegistrationYaml(as b.ApplicationService) string {
484
491
}
485
492
486
493
func deployImage (
487
- docker * client.Client , imageID string , csPort int , containerName , blueprintName , hsName string , asIDToRegistrationMap map [string ]string , contextStr , networkID string , versionCheckIterations int ,
494
+ docker * client.Client , imageID string , csPort int , containerName , pkgNamespace , blueprintName , hsName string , asIDToRegistrationMap map [string ]string , contextStr , networkID string , versionCheckIterations int ,
488
495
) (* HomeserverDeployment , error ) {
489
496
ctx := context .Background ()
490
497
var extraHosts []string
@@ -526,6 +533,7 @@ func deployImage(
526
533
Labels : map [string ]string {
527
534
complementLabel : contextStr ,
528
535
"complement_blueprint" : blueprintName ,
536
+ "complement_pkg" : pkgNamespace ,
529
537
"complement_hs_name" : hsName ,
530
538
},
531
539
}, & container.HostConfig {
@@ -616,12 +624,15 @@ func deployImage(
616
624
return d , nil
617
625
}
618
626
619
- // CreateNetworkIfNotExists creates a docker network and returns its id.
627
+ // createNetworkIfNotExists creates a docker network and returns its id.
620
628
// ID is guaranteed not to be empty when err == nil
621
- func CreateNetworkIfNotExists (docker * client.Client , blueprintName string ) (networkID string , err error ) {
629
+ func createNetworkIfNotExists (docker * client.Client , pkgNamespace , blueprintName string ) (networkID string , err error ) {
622
630
// check if a network already exists for this blueprint
623
631
nws , err := docker .NetworkList (context .Background (), types.NetworkListOptions {
624
- Filters : label ("complement_blueprint=" + blueprintName ),
632
+ Filters : label (
633
+ "complement_pkg=" + pkgNamespace ,
634
+ "complement_blueprint=" + blueprintName ,
635
+ ),
625
636
})
626
637
if err != nil {
627
638
return "" , fmt .Errorf ("%s: failed to list networks. %w" , blueprintName , err )
@@ -631,10 +642,11 @@ func CreateNetworkIfNotExists(docker *client.Client, blueprintName string) (netw
631
642
return nws [0 ].ID , nil
632
643
}
633
644
// make a user-defined network so we get DNS based on the container name
634
- nw , err := docker .NetworkCreate (context .Background (), "complement_" + blueprintName , types.NetworkCreate {
645
+ nw , err := docker .NetworkCreate (context .Background (), "complement_" + pkgNamespace + "_" + blueprintName , types.NetworkCreate {
635
646
Labels : map [string ]string {
636
647
complementLabel : blueprintName ,
637
648
"complement_blueprint" : blueprintName ,
649
+ "complement_pkg" : pkgNamespace ,
638
650
},
639
651
})
640
652
if err != nil {
@@ -668,9 +680,14 @@ func printLogs(docker *client.Client, containerID, contextStr string) {
668
680
log .Printf ("============== %s : END LOGS ==============\n \n \n " , contextStr )
669
681
}
670
682
671
- func label (in string ) filters.Args {
683
+ // label returns a filter for the presence of certain labels ("complement_context") or a match of
684
+ // labels ("complement_blueprint=foo").
685
+ func label (labelFilters ... string ) filters.Args {
672
686
f := filters .NewArgs ()
673
- f .Add ("label" , in )
687
+ // label=<key> or label=<key>=<value>
688
+ for _ , in := range labelFilters {
689
+ f .Add ("label" , in )
690
+ }
674
691
return f
675
692
}
676
693
0 commit comments