ソースを参照

add local-only host portmapping for urbit

reid 2 年 前
コミット
b25f1072f3
2 ファイル変更19 行追加9 行削除
  1. 3 6
      docker/docker.go
  2. 16 3
      docker/urbit.go

+ 3 - 6
docker/docker.go

@@ -178,10 +178,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 			break
 		}
 	}
-	desiredTag := containerInfo["tag"]
-	desiredHash := containerInfo["hash"]
-	desiredRepo := containerInfo["repo"]
-	desiredImage := fmt.Sprintf("%s:%s@sha256:%s", desiredRepo, desiredTag, desiredHash)
+	desiredImage := fmt.Sprintf("%s:%s@sha256:%s", containerInfo["repo"], containerInfo["tag"], containerInfo["hash"])
 	desiredStatus := "running"
 	// check if the desired image is available locally
 	images, err := cli.ImageList(ctx, types.ImageListOptions{})
@@ -190,7 +187,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 	}
 	imageExistsLocally := false
 	for _, img := range images {
-		if img.ID == desiredHash {
+		if img.ID == containerInfo["hash"] {
 			imageExistsLocally = true
 			break
 		}
@@ -234,7 +231,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 		if len(digestParts) > 1 {
 			currentDigest = digestParts[1]
 		}
-		if currentDigest != desiredHash {
+		if currentDigest != containerInfo["hash"] {
 			// if the hashes don't match, recreate the container with the new one
 			err := cli.ContainerRemove(ctx, containerName, types.ContainerRemoveOptions{Force: true})
 			if err != nil {

+ 16 - 3
docker/urbit.go

@@ -12,6 +12,7 @@ import (
 
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/mount"
+	"github.com/docker/go-connections/nat"
 )
 
 // load existing urbits from config json
@@ -62,7 +63,7 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
 	shipConf := config.UrbitConf(containerName)
 	// todo: this BootStatus doesnt actually have anythin to do with pack and meld right now
 	act := shipConf.BootStatus
-	// get the correct startup script based on act
+	// get the correct startup script based on BootStatus val
 	switch act {
 	case "boot":
 		scriptContent = defaults.StartScript
@@ -73,7 +74,7 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
 	case "prep":
 		scriptContent = defaults.PrepScript
 	case "noboot":
-		return containerConfig, hostConfig, fmt.Errorf("%s marked noboot!",containerName)
+		return containerConfig, hostConfig, fmt.Errorf("%s marked noboot!", containerName)
 	default:
 		return containerConfig, hostConfig, fmt.Errorf("Unknown action: %s", act)
 	}
@@ -107,6 +108,7 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
 	var httpPort string
 	var amesPort string
 	var network string
+	var portMap nat.PortMap
 	if shipConf.Network == "wireguard" {
 		httpPort = string(shipConf.WgHTTPPort)
 		amesPort = string(shipConf.WgAmesPort)
@@ -115,11 +117,21 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
 		httpPort = string(shipConf.HTTPPort)
 		amesPort = string(shipConf.AmesPort)
 		network = "default"
+		httpPortStr := nat.Port(fmt.Sprintf(httpPort+"/tcp"))
+		amesPortStr := nat.Port(fmt.Sprintf(amesPort+"/udp"))
+		portMap = nat.PortMap{
+			httpPortStr: []nat.PortBinding{
+				{HostIP: "0.0.0.0", HostPort: httpPort},
+			},
+			amesPortStr: []nat.PortBinding{
+				{HostIP: "0.0.0.0", HostPort: amesPort},
+			},
+		}
 	}
 	// finally construct the container config structs
 	containerConfig = container.Config{
 		Image:      desiredImage,
-		Entrypoint: []string{scriptPath, shipName, "--loom=" + loomValue, "--dirname=" + dirnameValue, "--dev-mode="+ devMode, "--http-port=" + httpPort, "--port=" + amesPort},
+		Entrypoint: []string{scriptPath, shipName, "--loom=" + loomValue, "--dirname=" + dirnameValue, "--dev-mode=" + devMode, "--http-port=" + httpPort, "--port=" + amesPort},
 	}
 	mounts := []mount.Mount{
 		{
@@ -131,6 +143,7 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
 	hostConfig = container.HostConfig{
 		NetworkMode: container.NetworkMode(network),
 		Mounts:      mounts,
+		PortBindings: portMap,
 	}
 	return containerConfig, hostConfig, nil
 }