Explorar o código

return hostconfig for urbit

reid %!s(int64=2) %!d(string=hai) anos
pai
achega
62e9e332f0
Modificáronse 6 ficheiros con 67 adicións e 38 borrados
  1. 1 1
      docker/docker.go
  2. 2 5
      docker/minio.go
  3. 1 4
      docker/netdata.go
  4. 45 12
      docker/urbit.go
  5. 10 10
      docker/wireguard.go
  6. 8 6
      main.go

+ 1 - 1
docker/docker.go

@@ -123,7 +123,7 @@ func StartContainer(containerName string, containerType string) (structs.Contain
 	switch containerType {
 	case "vere":
 		// containerConfig, HostConfig, err := urbitContainerConf(containerName)
-		_, err := urbitContainerConf(containerName)
+		_, _, err := urbitContainerConf(containerName)
 		if err != nil {
 			return containerState, err
 		}

+ 2 - 5
docker/minio.go

@@ -78,7 +78,7 @@ func minioContainerConf(containerName string) (container.Config, container.HostC
 	mounts := []mount.Mount{
 		{
 			Type:   mount.TypeBind,
-			Source: shipName,
+			Source: containerName,
 			Target: "/data",
 		},
 	}
@@ -104,10 +104,7 @@ func mcContainerConf() (container.Config, container.HostConfig, error) {
 	if err != nil {
 		return containerConfig, hostConfig, err
 	}
-	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"])
 	// construct the container config struct
 	containerConfig = container.Config{
 		Image:      desiredImage,

+ 1 - 4
docker/netdata.go

@@ -42,10 +42,7 @@ func netdataContainerConf() (container.Config, container.HostConfig, error) {
 	if err != nil {
 		return containerConfig, hostConfig, err
 	}
-	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"])
 	// construct the container config struct
 	containerConfig = container.Config{
 		Image:        desiredImage,

+ 45 - 12
docker/urbit.go

@@ -6,10 +6,12 @@ import (
 	"fmt"
 	"goseg/config"
 	"goseg/defaults"
+	"goseg/structs"
 	"io/ioutil"
 	"path/filepath"
 
 	"github.com/docker/docker/api/types/container"
+	"github.com/docker/docker/api/types/mount"
 )
 
 // load existing urbits from config json
@@ -40,23 +42,21 @@ func LoadUrbits() error {
 }
 
 // urbit container config builder
-func urbitContainerConf(containerName string) (container.Config, error) {
+func urbitContainerConf(containerName string) (container.Config, container.HostConfig, error) {
 	var containerConfig container.Config
+	var hostConfig container.HostConfig
 	var scriptContent string
 	// construct the container metadata from version server info
 	containerInfo, err := GetLatestContainerInfo("vere")
 	if err != nil {
-		return containerConfig, err
+		return containerConfig, hostConfig, err
 	}
-	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"])
 	// reload urbit conf from disk
 	err = config.LoadUrbitConfig(containerName)
 	if err != nil {
 		errmsg := fmt.Errorf("Error loading %s config: %v", containerName, err)
-		return containerConfig, errmsg
+		return containerConfig, hostConfig, errmsg
 	}
 	// put in memory
 	shipConf := config.UrbitConf(containerName)
@@ -72,32 +72,65 @@ func urbitContainerConf(containerName string) (container.Config, error) {
 		scriptContent = defaults.MeldScript
 	case "prep":
 		scriptContent = defaults.PrepScript
+	case "noboot":
+		return containerConfig, hostConfig, fmt.Errorf("%s marked noboot!",containerName)
 	default:
-		return containerConfig, fmt.Errorf("Unknown action: %s", act)
+		return containerConfig, hostConfig, fmt.Errorf("Unknown action: %s", act)
+	}
+	// reset ship status to boot for next time
+	if act != "boot" {
+		updateUrbitConf := shipConf
+		updateUrbitConf.BootStatus = "boot"
+		var newConfig map[string]structs.UrbitDocker
+		newConfig[containerName] = updateUrbitConf
+		err = config.UpdateUrbitConfig(newConfig)
+		if err != nil {
+			logger.Warn("Unable to reset %s boot script!", containerName)
+		}
 	}
 	// write the script
 	scriptPath := filepath.Join(config.DockerDir, containerName, "_data", containerName, "start_urbit.sh")
 	err = ioutil.WriteFile(scriptPath, []byte(scriptContent), 0755) // make the script executable
 	if err != nil {
-		return containerConfig, fmt.Errorf("Failed to write script: %v", err)
+		return containerConfig, hostConfig, fmt.Errorf("Failed to write script: %v", err)
 	}
 	// gather boot option values
 	shipName := shipConf.PierName
 	loomValue := string(shipConf.LoomSize)
 	dirnameValue := shipConf.PierName
+	var devMode string
+	if shipConf.DevMode == true {
+		devMode = "True"
+	} else {
+		devMode = "False"
+	}
 	var httpPort string
 	var amesPort string
+	var network string
 	if shipConf.Network == "wireguard" {
 		httpPort = string(shipConf.WgHTTPPort)
 		amesPort = string(shipConf.WgAmesPort)
+		network = "container:wireguard"
 	} else {
 		httpPort = string(shipConf.HTTPPort)
 		amesPort = string(shipConf.AmesPort)
+		network = "default"
 	}
-	// finally construct the container config struct
+	// finally construct the container config structs
 	containerConfig = container.Config{
 		Image:      desiredImage,
-		Entrypoint: []string{scriptPath, shipName, "--loom=" + loomValue, "--dirname=" + dirnameValue, "--http-port=" + httpPort, "--ames-port=" + amesPort},
+		Entrypoint: []string{scriptPath, shipName, "--loom=" + loomValue, "--dirname=" + dirnameValue, "--dev-mode="+ devMode, "--http-port=" + httpPort, "--port=" + amesPort},
+	}
+	mounts := []mount.Mount{
+		{
+			Type:   mount.TypeBind,
+			Source: shipName,
+			Target: "/data",
+		},
+	}
+	hostConfig = container.HostConfig{
+		NetworkMode: container.NetworkMode(network),
+		Mounts:      mounts,
 	}
-	return containerConfig, nil
+	return containerConfig, hostConfig, nil
 }

+ 10 - 10
docker/wireguard.go

@@ -20,15 +20,18 @@ func LoadWireguard() error {
 	confPath := filepath.Join(config.BasePath, "settings", "wireguard.json")
 	_, err := os.Open(confPath)
 	if err != nil {
-		// create a default if it doesn't exist
+		// create a default container conf if it doesn't exist
 		err = config.CreateDefaultWGConf()
 		if err != nil {
 			// error if we can't create it
-			errmsg := fmt.Sprintf("Unable to create WG config! %v", err)
-			logger.Error(errmsg)
-			panic(errmsg)
+			return err
 		}
 	}
+	// create wg0.conf or update it
+	err = WriteWgConf()
+	if err != nil {
+		return err
+	}
 	logger.Info("Running Wireguard")
 	info, err := StartContainer("wireguard", "wireguard")
 	if err != nil {
@@ -48,10 +51,7 @@ func wgContainerConf() (container.Config, container.HostConfig, error) {
 	if err != nil {
 		return containerConfig, hostConfig, err
 	}
-	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"])
 	// construct the container config struct
 	containerConfig = container.Config{
 		Image:      desiredImage,
@@ -66,7 +66,7 @@ func wgContainerConf() (container.Config, container.HostConfig, error) {
 	return containerConfig, hostConfig, nil
 }
 
-// wg client config builder
+// wg0.conf builder
 func buildWgConf() (string, error) {
 	confB64 := config.StartramConfig.Conf
 	confBytes, err := base64.StdEncoding.DecodeString(confB64)
@@ -80,7 +80,7 @@ func buildWgConf() (string, error) {
 }
 
 // write latest conf
-func writeWgConf() error {
+func WriteWgConf() error {
 	newConf, err := buildWgConf()
 	if err != nil {
 		return err

+ 8 - 6
main.go

@@ -100,16 +100,18 @@ func main() {
 			config.VersionInfo = targetChan
 		}
 	}
+	if conf.WgRegistered == true {
+		// Load Wireguard
+		loadService(docker.LoadWireguard, "Unable to load Wireguard!")
+		// Load MC
+		loadService(docker.LoadMC, "Unable to load MinIO Client!")
+		// Load MinIOs
+		loadService(docker.LoadMinIOs, "Unable to load MinIO containers!")
+	}
 	// Load Netdata
 	loadService(docker.LoadNetdata, "Unable to load Netdata!")
-	// Load Wireguard
-	loadService(docker.LoadWireguard, "Unable to load Wireguard!")
 	// Load Urbits
 	loadService(docker.LoadUrbits, "Unable to load Urbit ships!")
-	// Load MC
-	loadService(docker.LoadMC, "Unable to load MinIO Client!")
-	// Load MinIOs
-	loadService(docker.LoadMinIOs, "Unable to load MinIO containers!")
 
 	// Websocket
 	r := mux.NewRouter()