|
|
@@ -7,8 +7,11 @@ import (
|
|
|
"goseg/docker"
|
|
|
"goseg/startram"
|
|
|
"goseg/structs"
|
|
|
+ "io/ioutil"
|
|
|
"log/slog"
|
|
|
+ "math"
|
|
|
"os"
|
|
|
+ "path/filepath"
|
|
|
"reflect"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
@@ -57,29 +60,14 @@ func UnregisterClient(conn *websocket.Conn) {
|
|
|
func bootstrapBroadcastState(config structs.SysConfig) (structs.AuthBroadcast, error) {
|
|
|
logger.Info("Bootstrapping state")
|
|
|
var res structs.AuthBroadcast
|
|
|
- currentState := GetState()
|
|
|
// get a list of piers from config
|
|
|
piers := config.Piers
|
|
|
// this returns a map of ship:running status
|
|
|
logger.Info("Resolving pier status")
|
|
|
- pierStatus, err := docker.GetShipStatus(piers)
|
|
|
+ updates, err := constructPierInfo(piers)
|
|
|
if err != nil {
|
|
|
- errmsg := fmt.Sprintf("Unable to bootstrap urbit states: %v", err)
|
|
|
- logger.Error(errmsg)
|
|
|
return res, err
|
|
|
}
|
|
|
- updates := make(map[string]structs.Urbit)
|
|
|
- // convert the running status into bools
|
|
|
- for pier, status := range pierStatus {
|
|
|
- urbit := structs.Urbit{}
|
|
|
- if existingUrbit, exists := currentState.Urbits[pier]; exists {
|
|
|
- // If the ship already exists in broadcastState, use its current state
|
|
|
- urbit = existingUrbit
|
|
|
- }
|
|
|
- isRunning := (status == "Up" || strings.HasPrefix(status, "Up "))
|
|
|
- urbit.Info.Running = isRunning
|
|
|
- updates[pier] = urbit
|
|
|
- }
|
|
|
// update broadcastState
|
|
|
err = UpdateBroadcastState(map[string]interface{}{
|
|
|
"Urbits": updates,
|
|
|
@@ -89,11 +77,10 @@ func bootstrapBroadcastState(config structs.SysConfig) (structs.AuthBroadcast, e
|
|
|
logger.Error(errmsg)
|
|
|
return res, err
|
|
|
}
|
|
|
- currentState = GetState()
|
|
|
+ // wgRegistered := config.WgRegistered
|
|
|
+ // wgOn := config.WgOn
|
|
|
// get startram regions
|
|
|
logger.Info("Retrieving StarTram region info")
|
|
|
- //wgRegistered := config.WgRegistered
|
|
|
- //wgOn := config.WgOn
|
|
|
regions, err := startram.GetRegions()
|
|
|
if err != nil {
|
|
|
logger.Warn("Couldn't get StarTram regions")
|
|
|
@@ -118,6 +105,69 @@ func bootstrapBroadcastState(config structs.SysConfig) (structs.AuthBroadcast, e
|
|
|
return res, nil
|
|
|
}
|
|
|
|
|
|
+func constructPierInfo(piers []string) (map[string]structs.Urbit, error) {
|
|
|
+ updates := make(map[string]structs.Urbit)
|
|
|
+ currentState := GetState()
|
|
|
+ shipNetworks := GetContainerNetworks(piers)
|
|
|
+ pierStatus, err := docker.GetShipStatus(piers)
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Unable to bootstrap urbit states: %v", err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ return updates, err
|
|
|
+ }
|
|
|
+ hostName, err := os.Hostname()
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Error getting hostname, defaulting to `nativeplanet`: %v", err)
|
|
|
+ logger.Warn(errmsg)
|
|
|
+ hostName = "nativeplanet"
|
|
|
+ }
|
|
|
+ // convert the running status into bools
|
|
|
+ for pier, status := range pierStatus {
|
|
|
+ // pull docker info from json
|
|
|
+ var dockerConfig structs.UrbitDocker
|
|
|
+ confPath := filepath.Join(config.BasePath, "settings", "pier", pier+".json")
|
|
|
+ file, err := ioutil.ReadFile(confPath)
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Unable to load %s config: %v", pier, err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if err := json.Unmarshal(file, &dockerConfig); err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Error decoding %s JSON: %v", pier, err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ urbit := structs.Urbit{}
|
|
|
+ if existingUrbit, exists := currentState.Urbits[pier]; exists {
|
|
|
+ // If the ship already exists in broadcastState, use its current state
|
|
|
+ urbit = existingUrbit
|
|
|
+ }
|
|
|
+ isRunning := (status == "Up" || strings.HasPrefix(status, "Up "))
|
|
|
+ urbit.Info.Running = isRunning
|
|
|
+ urbit.Info.Network = shipNetworks[pier]
|
|
|
+ urbit.Info.URL = "http://" + hostName + ":" + string(dockerConfig.HTTPPort)
|
|
|
+ urbit.Info.LoomSize = int(math.Pow(2, float64(dockerConfig.LoomSize)) / math.Pow(1024, 2))
|
|
|
+ updates[pier] = urbit
|
|
|
+ }
|
|
|
+ return updates, nil
|
|
|
+}
|
|
|
+
|
|
|
+// return a map of ships and their networks
|
|
|
+func GetContainerNetworks(containers []string) map[string]string {
|
|
|
+ res := make(map[string]string)
|
|
|
+ for _, container := range containers {
|
|
|
+ network, err := docker.GetContainerNetwork(container)
|
|
|
+ if err != nil {
|
|
|
+ errmsg := fmt.Sprintf("Error getting container network: %v", err)
|
|
|
+ logger.Error(errmsg)
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
+ res[container] = network
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
// update broadcastState with a map of items
|
|
|
func UpdateBroadcastState(values map[string]interface{}) error {
|
|
|
mu.Lock()
|