about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/docker.go27
-rw-r--r--src/http.go6
2 files changed, 28 insertions, 5 deletions
diff --git a/src/docker.go b/src/docker.go
index df89e52..26b0b67 100644
--- a/src/docker.go
+++ b/src/docker.go
@@ -36,22 +36,31 @@ func setupDockerCLI() (err error) {
 	return err
 }
 
-func spawnCompanion(username string, accesscode string) string {
+func spawnCompanion(username string, accesscode string) {
 	log.Println("Spwaning a new companion container")
 	// setup the context and the docker cli connection
 	setupContext()
 	setupDockerCLI()
 
 	sessionSalt := generateSessionSalt()
+	vpnRemoteAddress := "127.0.0.1"
+	vpnRemotePort := "1193"
+
+	log.Println("Generating the container config")
 
 	Config := &container.Config{
 		Image: "circus-companion:latest",
 		Cmd:   []string{fmt.Sprintf("-username %s -accesscode %s -sessionSalt %s -vpnRemoteAddress %s -vpnRemotePort %s", username, accesscode, sessionSalt, vpnRemoteAddress, vpnRemotePort)},
+		// Container labels used by traefik
+		Labels: map[string]string{
+			"traefik.enable": "true",
+			"traefik.http.routers.companion.entrypoints":               "web",
+			"traefik.http.routers.companion.rule":                      "Host(`companion.docker.localhost`)",
+			"traefik.http.services.companion.loadbalancer.server.port": "8080",
+		},
 	}
 
 	HostConfig := &container.HostConfig{
-		// ports (nat.PortMap)
-		// docker socket
 		Mounts: []mount.Mount{
 			{
 				Type:   mount.TypeBind,
@@ -66,16 +75,24 @@ func spawnCompanion(username string, accesscode string) string {
 		},
 	}
 
+	containerNetworkID, err := setupNetwork("circus", true)
+
 	NetworkingConfig := &network.NetworkingConfig{
-		// network
+		EndpointsConfig: map[string]*network.EndpointSettings{
+			"circus": {
+				NetworkID: containerNetworkID,
+			},
+		},
 	}
 
+	log.Println("Starting the container")
+
 	_, err := dockerCLI.ContainerCreate(dockerCtx, Config, HostConfig, NetworkingConfig, "")
 	if err != nil {
 		panic(err)
 	}
 
-	return accesscode
+	log.Println("Container started")
 }
 
 func generateSessionSalt() string {
diff --git a/src/http.go b/src/http.go
index 5ed4957..1b5db1a 100644
--- a/src/http.go
+++ b/src/http.go
@@ -14,6 +14,9 @@ import (
 var (
 	port      *int     // port the http server listens on
 	usernames []string // list of usernames
+	// map usernames to their companion containers
+	// the companion containers get assigned a random string used to identify itself
+	companion map[string]string
 )
 
 func registerHTTPFlags() {
@@ -63,6 +66,9 @@ func registerPostHandler(w http.ResponseWriter, r *http.Request) {
 	accesscode := newAccessCode()
 	log.Printf("Generated a new AccessCode for user %s: %s", username, accesscode)
 
+	// generate a new companion
+	spawnCompanion(username, accesscode)
+
 	// redirect the user to the front page
 	http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
 }