|
|
@@ -3,6 +3,7 @@ package config
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "goseg/defaults"
|
|
|
"goseg/structs"
|
|
|
"io/ioutil"
|
|
|
"log/slog"
|
|
|
@@ -18,13 +19,12 @@ import (
|
|
|
var (
|
|
|
globalConfig structs.SysConfig
|
|
|
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
|
|
- BasePath = "/opt/nativeplanet/groundseg"
|
|
|
- Version = "v2.0.0"
|
|
|
+ BasePath = os.Getenv("GS_BASE_PATH")
|
|
|
Architecture = getArchitecture()
|
|
|
DebugMode = false
|
|
|
Ready = false
|
|
|
VersionServerReady = false
|
|
|
- VersionInfo structs.Version
|
|
|
+ VersionInfo structs.Channel
|
|
|
GSContainers = make(map[string]structs.ContainerState)
|
|
|
checkInterval = 5 * time.Minute
|
|
|
confMutex sync.Mutex
|
|
|
@@ -41,6 +41,10 @@ func init() {
|
|
|
DebugMode = true
|
|
|
}
|
|
|
}
|
|
|
+ if BasePath == "" {
|
|
|
+ // default base path
|
|
|
+ BasePath = "/opt/nativeplanet/groundseg"
|
|
|
+ }
|
|
|
pathMsg := fmt.Sprintf("Loading configs from %s", BasePath)
|
|
|
logger.Info(pathMsg)
|
|
|
confPath := filepath.Join(BasePath, "settings", "system.json")
|
|
|
@@ -50,12 +54,13 @@ func init() {
|
|
|
err = createDefaultConf()
|
|
|
if err != nil {
|
|
|
// panic if we can't create it
|
|
|
- errmsg := fmt.Sprintf("Unable to create config! %v", err)
|
|
|
+ errmsg := fmt.Sprintf("Unable to create config! Please elevate permissions. %v", err)
|
|
|
logger.Error(errmsg)
|
|
|
panic(errmsg)
|
|
|
}
|
|
|
}
|
|
|
defer file.Close()
|
|
|
+ // read the sysconfig to memory
|
|
|
decoder := json.NewDecoder(file)
|
|
|
err = decoder.Decode(&globalConfig)
|
|
|
if err != nil {
|
|
|
@@ -146,48 +151,7 @@ func GetContainerState() map[string]structs.ContainerState {
|
|
|
|
|
|
// write a default conf to disk
|
|
|
func createDefaultConf() error {
|
|
|
- defaultConfig := structs.SysConfig{
|
|
|
- Setup: "start",
|
|
|
- EndpointUrl: "api.startram.io",
|
|
|
- ApiVersion: "v1",
|
|
|
- Piers: []string{},
|
|
|
- NetCheck: "1.1.1.1:53",
|
|
|
- UpdateMode: "auto",
|
|
|
- UpdateUrl: "https://version.groundseg.app",
|
|
|
- UpdateBranch: "latest",
|
|
|
- SwapVal: 16,
|
|
|
- SwapFile: filepath.Join(BasePath, "settings", "swapfile"),
|
|
|
- KeyFile: filepath.Join(BasePath, "settings", "session.key"),
|
|
|
- Sessions: struct {
|
|
|
- Authorized map[string]structs.SessionInfo `json:"authorized"`
|
|
|
- Unauthorized map[string]structs.SessionInfo `json:"unauthorized"`
|
|
|
- }{
|
|
|
- Authorized: make(map[string]structs.SessionInfo),
|
|
|
- Unauthorized: make(map[string]structs.SessionInfo),
|
|
|
- },
|
|
|
- LinuxUpdates: struct {
|
|
|
- Value int `json:"value"`
|
|
|
- Interval string `json:"interval"`
|
|
|
- Previous bool `json:"previous"`
|
|
|
- }{
|
|
|
- Value: 1,
|
|
|
- Interval: "week",
|
|
|
- Previous: false,
|
|
|
- },
|
|
|
- DockerData: "/var/lib/docker",
|
|
|
- WgOn: false,
|
|
|
- WgRegistered: false,
|
|
|
- PwHash: "",
|
|
|
- C2cInterval: 0,
|
|
|
- FirstBoot: true,
|
|
|
- GsVersion: Version,
|
|
|
- CfgDir: "",
|
|
|
- UpdateInterval: 0,
|
|
|
- BinHash: "",
|
|
|
- Pubkey: "",
|
|
|
- Privkey: "",
|
|
|
- Salt: "",
|
|
|
- }
|
|
|
+ defaultConfig := defaults.SysConfig(BasePath)
|
|
|
path := filepath.Join(BasePath, "settings", "system.json")
|
|
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
|
return err
|
|
|
@@ -222,13 +186,16 @@ func NetCheck(netCheck string) bool {
|
|
|
return internet
|
|
|
}
|
|
|
|
|
|
-// check the version server and return unmarshaled result
|
|
|
-func CheckVersion() (structs.Version, bool) {
|
|
|
+// check the version server and return struct
|
|
|
+func CheckVersion() (structs.Channel, bool) {
|
|
|
versMutex.Lock()
|
|
|
defer versMutex.Unlock()
|
|
|
+ conf := Conf()
|
|
|
+ releaseChannel := conf.UpdateBranch
|
|
|
const retries = 10
|
|
|
const delay = time.Second
|
|
|
url := globalConfig.UpdateUrl
|
|
|
+ var fetchedVersion structs.Version
|
|
|
for i := 0; i < retries; i++ {
|
|
|
resp, err := http.Get(url)
|
|
|
if err != nil {
|
|
|
@@ -238,6 +205,7 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
time.Sleep(delay)
|
|
|
continue
|
|
|
} else {
|
|
|
+ VersionServerReady = false
|
|
|
return VersionInfo, false
|
|
|
}
|
|
|
}
|
|
|
@@ -251,11 +219,12 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
time.Sleep(delay)
|
|
|
continue
|
|
|
} else {
|
|
|
+ VersionServerReady = false
|
|
|
return VersionInfo, false
|
|
|
}
|
|
|
}
|
|
|
// unmarshal values into Version struct
|
|
|
- err = json.Unmarshal(body, &VersionInfo)
|
|
|
+ err = json.Unmarshal(body, &fetchedVersion)
|
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Error unmarshalling JSON: %v", err)
|
|
|
logger.Warn(errmsg)
|
|
|
@@ -263,38 +232,46 @@ func CheckVersion() (structs.Version, bool) {
|
|
|
time.Sleep(delay)
|
|
|
continue
|
|
|
} else {
|
|
|
+ VersionServerReady = false
|
|
|
return VersionInfo, false
|
|
|
}
|
|
|
}
|
|
|
- // debug: re-marshal and write to disk
|
|
|
+ VersionInfo = fetchedVersion.Groundseg[releaseChannel]
|
|
|
+ // debug: re-marshal and write the entire fetched version to disk
|
|
|
confPath := filepath.Join(BasePath, "settings", "version_info.json")
|
|
|
file, err := os.Create(confPath)
|
|
|
if err != nil {
|
|
|
errmsg := fmt.Sprintf("Failed to create file: %v", err)
|
|
|
logger.Error(errmsg)
|
|
|
+ VersionServerReady = false
|
|
|
return VersionInfo, false
|
|
|
}
|
|
|
defer file.Close()
|
|
|
encoder := json.NewEncoder(file)
|
|
|
encoder.SetIndent("", " ")
|
|
|
- if err := encoder.Encode(&VersionInfo); err != nil {
|
|
|
+ if err := encoder.Encode(&fetchedVersion); err != nil {
|
|
|
errmsg := fmt.Sprintf("Failed to write JSON: %v", err)
|
|
|
logger.Error(errmsg)
|
|
|
}
|
|
|
+ VersionServerReady = true
|
|
|
return VersionInfo, true
|
|
|
}
|
|
|
+ VersionServerReady = false
|
|
|
return VersionInfo, false
|
|
|
}
|
|
|
|
|
|
func CheckVersionLoop() {
|
|
|
ticker := time.NewTicker(checkInterval)
|
|
|
+ conf := Conf()
|
|
|
+ releaseChannel := conf.UpdateBranch
|
|
|
for {
|
|
|
select {
|
|
|
case <-ticker.C:
|
|
|
latestVersion, _ := CheckVersion()
|
|
|
- currentVersion := VersionInfo
|
|
|
- if latestVersion != currentVersion {
|
|
|
- fmt.Printf("New version available! Current: %s, Latest: %s\n", currentVersion, latestVersion)
|
|
|
+ currentChannelVersion := VersionInfo
|
|
|
+ latestChannelVersion := latestVersion
|
|
|
+ if latestChannelVersion != currentChannelVersion {
|
|
|
+ fmt.Printf("New version available in %s channel! Current: %+v, Latest: %+v\n", releaseChannel, currentChannelVersion, latestChannelVersion)
|
|
|
VersionInfo = latestVersion
|
|
|
// Handle the update logic here
|
|
|
}
|