@@ -63,15 +63,10 @@ func init() {
6363const complementLabel = "complement_context"
6464
6565type Builder struct {
66- BaseImage string
67- ImageArgs []string
68- KeepBlueprints []string
66+ Config * config.Complement
6967 CSAPIPort int
7068 FederationPort int
7169 Docker * client.Client
72- debugLogging bool
73- bestEffort bool
74- config * config.Complement
7570}
7671
7772func NewBuilder (cfg * config.Complement ) (* Builder , error ) {
@@ -81,19 +76,14 @@ func NewBuilder(cfg *config.Complement) (*Builder, error) {
8176 }
8277 return & Builder {
8378 Docker : cli ,
84- BaseImage : cfg .BaseImageURI ,
85- ImageArgs : cfg .BaseImageArgs ,
86- KeepBlueprints : cfg .KeepBlueprints ,
79+ Config : cfg ,
8780 CSAPIPort : 8008 ,
8881 FederationPort : 8448 ,
89- debugLogging : cfg .DebugLoggingEnabled ,
90- bestEffort : cfg .BestEffort ,
91- config : cfg ,
9282 }, nil
9383}
9484
9585func (d * Builder ) log (str string , args ... interface {}) {
96- if ! d .debugLogging {
86+ if ! d .Config . DebugLoggingEnabled {
9787 return
9888 }
9989 log .Printf (str , args ... )
@@ -117,7 +107,10 @@ func (d *Builder) Cleanup() {
117107// removeImages removes all images with `complementLabel`.
118108func (d * Builder ) removeNetworks () error {
119109 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+ ),
121114 })
122115 if err != nil {
123116 return err
@@ -134,7 +127,10 @@ func (d *Builder) removeNetworks() error {
134127// removeImages removes all images with `complementLabel`.
135128func (d * Builder ) removeImages () error {
136129 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+ ),
138134 })
139135 if err != nil {
140136 return err
@@ -156,7 +152,7 @@ func (d *Builder) removeImages() error {
156152 }
157153 bprintName := img .Labels ["complement_blueprint" ]
158154 keep := false
159- for _ , keepBprint := range d .KeepBlueprints {
155+ for _ , keepBprint := range d .Config . KeepBlueprints {
160156 if bprintName == keepBprint {
161157 keep = true
162158 break
@@ -180,8 +176,11 @@ func (d *Builder) removeImages() error {
180176// removeContainers removes all containers with `complementLabel`.
181177func (d * Builder ) removeContainers () error {
182178 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+ ),
185184 })
186185 if err != nil {
187186 return err
@@ -201,7 +200,10 @@ func (d *Builder) ConstructBlueprintsIfNotExist(bs []b.Blueprint) error {
201200 var blueprintsToBuild []b.Blueprint
202201 for _ , bprint := range bs {
203202 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+ ),
205207 })
206208 if err != nil {
207209 return fmt .Errorf ("ConstructBlueprintsIfNotExist: failed to ImageList: %w" , err )
@@ -242,7 +244,10 @@ func (d *Builder) ConstructBlueprints(bs []b.Blueprint) error {
242244 foundImages := false
243245 for i := 0 ; i < 50 ; i ++ { // max 5s
244246 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+ ),
246251 })
247252 if err != nil {
248253 return err
@@ -265,12 +270,12 @@ func (d *Builder) ConstructBlueprints(bs []b.Blueprint) error {
265270
266271// construct all Homeservers sequentially then commits them
267272func (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 )
269274 if err != nil {
270275 return []error {err }
271276 }
272277
273- runner := instruction .NewRunner (bprint .Name , d .bestEffort , d .debugLogging )
278+ runner := instruction .NewRunner (bprint .Name , d .Config . BestEffort , d .Config . DebugLoggingEnabled )
274279 results := make ([]result , len (bprint .Homeservers ))
275280 for i , hs := range bprint .Homeservers {
276281 res := d .constructHomeserver (bprint .Name , runner , hs , networkID )
@@ -342,7 +347,7 @@ func (d *Builder) construct(bprint b.Blueprint) (errs []error) {
342347
343348// construct this homeserver and execute its instructions, keeping the container alive.
344349func (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 )
346351 d .log ("%s : constructing homeserver...\n " , contextStr )
347352 dep , err := d .deployBaseImage (blueprintName , hs , contextStr , networkID )
348353 if err != nil {
@@ -376,15 +381,17 @@ func (d *Builder) deployBaseImage(blueprintName string, hs b.Homeserver, context
376381 asIDToRegistrationMap := asIDToRegistrationFromLabels (labelsForApplicationServices (hs ))
377382
378383 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 ,
381387 )
382388}
383389
384390// getCaVolume returns the correct volume mount for providing a CA to homeserver containers.
385391// If running CI, returns an error if it's unable to find a volume that has /ca
386392// Otherwise, returns an error if we're unable to find the <cwd>/ca directory on the local host
387393func getCaVolume (ctx context.Context , docker * client.Client ) (caMount mount.Mount , err error ) {
394+ // TODO: wrap in a lockfile
388395 if os .Getenv ("CI" ) == "true" {
389396 // When in CI, Complement itself is a container with the CA volume mounted at /ca.
390397 // 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 {
484491}
485492
486493func 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 ,
488495) (* HomeserverDeployment , error ) {
489496 ctx := context .Background ()
490497 var extraHosts []string
@@ -526,6 +533,7 @@ func deployImage(
526533 Labels : map [string ]string {
527534 complementLabel : contextStr ,
528535 "complement_blueprint" : blueprintName ,
536+ "complement_pkg" : pkgNamespace ,
529537 "complement_hs_name" : hsName ,
530538 },
531539 }, & container.HostConfig {
@@ -616,12 +624,15 @@ func deployImage(
616624 return d , nil
617625}
618626
619- // CreateNetworkIfNotExists creates a docker network and returns its id.
627+ // createNetworkIfNotExists creates a docker network and returns its id.
620628// 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 ) {
622630 // check if a network already exists for this blueprint
623631 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+ ),
625636 })
626637 if err != nil {
627638 return "" , fmt .Errorf ("%s: failed to list networks. %w" , blueprintName , err )
@@ -631,10 +642,11 @@ func CreateNetworkIfNotExists(docker *client.Client, blueprintName string) (netw
631642 return nws [0 ].ID , nil
632643 }
633644 // 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 {
635646 Labels : map [string ]string {
636647 complementLabel : blueprintName ,
637648 "complement_blueprint" : blueprintName ,
649+ "complement_pkg" : pkgNamespace ,
638650 },
639651 })
640652 if err != nil {
@@ -668,9 +680,14 @@ func printLogs(docker *client.Client, containerID, contextStr string) {
668680 log .Printf ("============== %s : END LOGS ==============\n \n \n " , contextStr )
669681}
670682
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 {
672686 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+ }
674691 return f
675692}
676693
0 commit comments