소스 검색

add urbit config state management

reid 2 년 전
부모
커밋
b2c1e842db
5개의 변경된 파일50개의 추가작업 그리고 4개의 파일을 삭제
  1. 3 0
      config/config.go
  2. 33 1
      config/urbit.go
  3. 6 0
      docker/urbit.go
  4. 7 2
      docker/wireguard.go
  5. 1 1
      rectify/rectify.go

+ 3 - 0
config/config.go

@@ -125,6 +125,9 @@ func UpdateConf(values map[string]interface{}) error {
 	return nil
 }
 
+// we keep map[string]structs.ContainerState in memory to keep track of the containers
+// eg if they're running and whether they should be
+
 // modify the desired/actual state of containers
 func UpdateContainerState(name string, containerState structs.ContainerState) {
 	contMutex.Lock()

+ 33 - 1
config/urbit.go

@@ -1,10 +1,13 @@
 package config
 
+// functions related to managing urbit config jsons & corresponding structs
+
 import (
 	"encoding/json"
 	"fmt"
 	"goseg/structs"
 	"io/ioutil"
+	"os"
 	"path/filepath"
 	"sync"
 )
@@ -14,12 +17,14 @@ var (
 	urbitMutex   sync.RWMutex
 )
 
+// retrieve struct corresponding with urbit json file
 func UrbitConf(pier string) structs.UrbitDocker {
 	urbitMutex.Lock()
 	defer urbitMutex.Unlock()
 	return UrbitsConfig[pier]
 }
 
+// load urbit conf json into memory
 func LoadUrbitConfig(pier string) error {
 	urbitMutex.Lock()
 	defer urbitMutex.Unlock()
@@ -28,7 +33,8 @@ func LoadUrbitConfig(pier string) error {
 	file, err := ioutil.ReadFile(confPath)
 	if err != nil {
 		errmsg := fmt.Sprintf("Unable to load %s config: %v", pier, err)
-		return fmt.Errorf(errmsg) // Return an error instead of a string
+		return fmt.Errorf(errmsg)
+		// todo: write a new conf
 	}
 	// Unmarshal JSON
 	var targetStruct structs.UrbitDocker
@@ -40,3 +46,29 @@ func LoadUrbitConfig(pier string) error {
 	UrbitsConfig[pier] = targetStruct
 	return nil
 }
+
+// update the in-memory struct and save it to json
+func UpdateUrbitConfig(inputConfig map[string]structs.UrbitDocker) error {
+	urbitMutex.Lock()
+	defer urbitMutex.Unlock()
+	// update UrbitsConfig with the values from inputConfig
+	for pier, config := range inputConfig {
+		UrbitsConfig[pier] = config
+		// also update the corresponding json files
+		path := filepath.Join(BasePath, "settings", "pier", pier+".json")
+		if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
+			return err
+		}
+		file, err := os.Create(path)
+		if err != nil {
+			return err
+		}
+		defer file.Close()
+		encoder := json.NewEncoder(file)
+		encoder.SetIndent("", "    ")
+		if err := encoder.Encode(&config); err != nil {
+			return err
+		}
+	}
+	return nil
+}

+ 6 - 0
docker/urbit.go

@@ -11,6 +11,12 @@ func LoadUrbits() error {
 	conf := config.Conf()
 	for _, pier := range conf.Piers {
 		logger.Info(fmt.Sprintf("Loading pier %s", pier))
+		// load json into struct
+		err := config.LoadUrbitConfig(pier)
+		if err != nil {
+			logger.Error(fmt.Sprintf("Error loading %s config: %v", pier, err))
+			continue
+		}
 		info, err := StartContainer(pier, "vere")
 		if err != nil {
 			logger.Error(fmt.Sprintf("Error starting %s: %v", pier, err))

+ 7 - 2
docker/wireguard.go

@@ -21,7 +21,12 @@ func LoadWireguard() error {
 			panic(errmsg)
 		}
 	}
-	// apply latest version info
-	// start container
+	logger.Info("Running Wireguard")
+	info, err := StartContainer("wireguard", "wireguard")
+	if err != nil {
+		logger.Error(fmt.Sprintf("Error starting wireguard: %v", err))
+		return err
+	}
+	config.UpdateContainerState("wireguard", info)
 	return nil
 }

+ 1 - 1
rectify/rectify.go

@@ -52,7 +52,7 @@ func DockerSubscriptionHandler() {
 			}
 
 		case "die":
-			logger.Info(fmt.Sprintf("Docker: %s died!", contName))
+			logger.Warn(fmt.Sprintf("Docker: %s died!", contName))
 			if containerState, exists := config.GetContainerState()[contName]; exists {
 				containerState.ActualStatus = "died"
 				// we don't want infinite restart loop