@@ -21,7 +21,6 @@ import (
2121 "time"
2222
2323 "github.com/docker/docker/api/types"
24- "github.com/docker/docker/api/types/container"
2524 "github.com/docker/docker/client"
2625 "github.com/docker/docker/pkg/stdcopy"
2726 "github.com/docker/go-connections/nat"
@@ -32,8 +31,6 @@ import (
3231)
3332
3433var (
35- // HostnameRunningComplement is the hostname of Complement from the perspective of a Homeserver.
36- HostnameRunningComplement = "host.docker.internal"
3734 // HostnameRunningDocker is the hostname of the docker daemon from the perspective of Complement.
3835 HostnameRunningDocker = "localhost"
3936)
@@ -240,15 +237,15 @@ func (d *Builder) ConstructBlueprint(bprint b.Blueprint) error {
240237func (d * Builder ) construct (bprint b.Blueprint ) (errs []error ) {
241238 d .log ("Constructing blueprint '%s'" , bprint .Name )
242239
243- networkID , err := createNetworkIfNotExists (d .Docker , d .Config .PackageNamespace , bprint .Name )
240+ networkName , err := createNetworkIfNotExists (d .Docker , d .Config .PackageNamespace , bprint .Name )
244241 if err != nil {
245242 return []error {err }
246243 }
247244
248245 runner := instruction .NewRunner (bprint .Name , d .Config .BestEffort , d .Config .DebugLoggingEnabled )
249246 results := make ([]result , len (bprint .Homeservers ))
250247 for i , hs := range bprint .Homeservers {
251- res := d .constructHomeserver (bprint .Name , runner , hs , networkID )
248+ res := d .constructHomeserver (bprint .Name , runner , hs , networkName )
252249 if res .err != nil {
253250 errs = append (errs , res .err )
254251 if res .containerID != "" {
@@ -336,9 +333,7 @@ func (d *Builder) construct(bprint b.Blueprint) (errs []error) {
336333 Author : "Complement" ,
337334 Pause : true ,
338335 Reference : "localhost/complement:" + res .contextStr ,
339- Config : & container.Config {
340- Labels : labels ,
341- },
336+ Changes : toChanges (labels ),
342337 })
343338 if err != nil {
344339 d .log ("%s : failed to ContainerCommit: %s\n " , res .contextStr , err )
@@ -351,11 +346,22 @@ func (d *Builder) construct(bprint b.Blueprint) (errs []error) {
351346 return errs
352347}
353348
349+ // Convert a map of labels to a list of changes directive in Dockerfile format.
350+ // Labels keys and values can't be multiline (eg. can't contain `\n` character)
351+ // neither can they contain unescaped `"` character.
352+ func toChanges (labels map [string ]string ) []string {
353+ var changes []string
354+ for k , v := range labels {
355+ changes = append (changes , fmt .Sprintf ("LABEL \" %s\" =\" %s\" " , k , v ))
356+ }
357+ return changes
358+ }
359+
354360// construct this homeserver and execute its instructions, keeping the container alive.
355- func (d * Builder ) constructHomeserver (blueprintName string , runner * instruction.Runner , hs b.Homeserver , networkID string ) result {
361+ func (d * Builder ) constructHomeserver (blueprintName string , runner * instruction.Runner , hs b.Homeserver , networkName string ) result {
356362 contextStr := fmt .Sprintf ("%s.%s.%s" , d .Config .PackageNamespace , blueprintName , hs .Name )
357363 d .log ("%s : constructing homeserver...\n " , contextStr )
358- dep , err := d .deployBaseImage (blueprintName , hs , contextStr , networkID )
364+ dep , err := d .deployBaseImage (blueprintName , hs , contextStr , networkName )
359365 if err != nil {
360366 log .Printf ("%s : failed to deployBaseImage: %s\n " , contextStr , err )
361367 containerID := ""
@@ -383,7 +389,7 @@ func (d *Builder) constructHomeserver(blueprintName string, runner *instruction.
383389}
384390
385391// deployBaseImage runs the base image and returns the baseURL, containerID or an error.
386- func (d * Builder ) deployBaseImage (blueprintName string , hs b.Homeserver , contextStr , networkID string ) (* HomeserverDeployment , error ) {
392+ func (d * Builder ) deployBaseImage (blueprintName string , hs b.Homeserver , contextStr , networkName string ) (* HomeserverDeployment , error ) {
387393 asIDToRegistrationMap := asIDToRegistrationFromLabels (labelsForApplicationServices (hs ))
388394 var baseImageURI string
389395 if hs .BaseImageURI == nil {
@@ -399,28 +405,29 @@ func (d *Builder) deployBaseImage(blueprintName string, hs b.Homeserver, context
399405 return deployImage (
400406 d .Docker , baseImageURI , fmt .Sprintf ("complement_%s" , contextStr ),
401407 d .Config .PackageNamespace , blueprintName , hs .Name , asIDToRegistrationMap , contextStr ,
402- networkID , d .Config ,
408+ networkName , d .Config ,
403409 )
404410}
405411
412+ // Multilines label using Dockerfile syntax is unsupported, let's inline \n instead
406413func generateASRegistrationYaml (as b.ApplicationService ) string {
407- return fmt .Sprintf ("id: %s\n " , as .ID ) +
408- fmt .Sprintf ("hs_token: %s\n " , as .HSToken ) +
409- fmt .Sprintf ("as_token: %s\n " , as .ASToken ) +
410- fmt .Sprintf ("url: '%s'\n " , as .URL ) +
411- fmt .Sprintf ("sender_localpart: %s\n " , as .SenderLocalpart ) +
412- fmt .Sprintf ("rate_limited: %v\n " , as .RateLimited ) +
413- "namespaces:\n " +
414- " users:\n " +
415- " - exclusive: false\n " +
416- " regex: .*\n " +
417- " rooms: []\n " +
418- " aliases: []\n "
414+ return fmt .Sprintf ("id: %s\\ n" , as .ID ) +
415+ fmt .Sprintf ("hs_token: %s\\ n" , as .HSToken ) +
416+ fmt .Sprintf ("as_token: %s\\ n" , as .ASToken ) +
417+ fmt .Sprintf ("url: '%s'\\ n" , as .URL ) +
418+ fmt .Sprintf ("sender_localpart: %s\\ n" , as .SenderLocalpart ) +
419+ fmt .Sprintf ("rate_limited: %v\\ n" , as .RateLimited ) +
420+ "namespaces:\\ n" +
421+ " users:\\ n" +
422+ " - exclusive: false\\ n" +
423+ " regex: .*\\ n" +
424+ " rooms: []\\ n" +
425+ " aliases: []\\ n"
419426}
420427
421- // createNetworkIfNotExists creates a docker network and returns its id .
422- // ID is guaranteed not to be empty when err == nil
423- func createNetworkIfNotExists (docker * client.Client , pkgNamespace , blueprintName string ) (networkID string , err error ) {
428+ // createNetworkIfNotExists creates a docker network and returns its name .
429+ // Name is guaranteed not to be empty when err == nil
430+ func createNetworkIfNotExists (docker * client.Client , pkgNamespace , blueprintName string ) (networkName string , err error ) {
424431 // check if a network already exists for this blueprint
425432 nws , err := docker .NetworkList (context .Background (), types.NetworkListOptions {
426433 Filters : label (
@@ -436,10 +443,11 @@ func createNetworkIfNotExists(docker *client.Client, pkgNamespace, blueprintName
436443 if len (nws ) > 1 {
437444 log .Printf ("WARNING: createNetworkIfNotExists got %d networks for pkg=%s blueprint=%s" , len (nws ), pkgNamespace , blueprintName )
438445 }
439- return nws [0 ].ID , nil
446+ return nws [0 ].Name , nil
440447 }
448+ networkName = "complement_" + pkgNamespace + "_" + blueprintName
441449 // make a user-defined network so we get DNS based on the container name
442- nw , err := docker .NetworkCreate (context .Background (), "complement_" + pkgNamespace + "_" + blueprintName , types.NetworkCreate {
450+ nw , err := docker .NetworkCreate (context .Background (), networkName , types.NetworkCreate {
443451 Labels : map [string ]string {
444452 complementLabel : blueprintName ,
445453 "complement_blueprint" : blueprintName ,
@@ -458,7 +466,7 @@ func createNetworkIfNotExists(docker *client.Client, pkgNamespace, blueprintName
458466 if nw .ID == "" {
459467 return "" , fmt .Errorf ("%s: unexpected empty ID while creating networkID" , blueprintName )
460468 }
461- return nw . ID , nil
469+ return networkName , nil
462470}
463471
464472func printLogs (docker * client.Client , containerID , contextStr string ) {
0 commit comments