Skip to content

Commit 5b30cd7

Browse files
authored
v2.7.4 Boot lifecycle logic fixes (#758)
* lifecycle logic fixes * lifecycle logic fixes * add precommit hooks
1 parent 3e1e7b5 commit 5b30cd7

24 files changed

Lines changed: 294 additions & 105 deletions

goseg/accesspoint/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package accesspoint
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66

77
"go.uber.org/zap"
88
)
@@ -13,7 +13,7 @@ func writeHostapdConfig() error {
1313
return err
1414
}
1515
// Write to file
16-
err = ioutil.WriteFile(hostapdConfigPath, []byte(config), 0644)
16+
err = os.WriteFile(hostapdConfigPath, []byte(config), 0644)
1717
if err != nil {
1818
return err
1919
}

goseg/auth/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ import (
4343
"fmt"
4444
"groundseg/config"
4545
"groundseg/structs"
46-
"io/ioutil"
4746
"net"
4847
"net/http"
48+
"os"
4949
"strings"
5050
"time"
5151

@@ -393,7 +393,7 @@ func AddSession(tokenID string, hash string, created string, authorized bool) er
393393

394394
// encrypt the token contents using stored keyfile val
395395
func KeyfileEncrypt(contents map[string]string, keyStr string) (string, error) {
396-
fileBytes, err := ioutil.ReadFile(keyStr)
396+
fileBytes, err := os.ReadFile(keyStr)
397397
if err != nil {
398398
return "", err
399399
}
@@ -413,7 +413,7 @@ func KeyfileEncrypt(contents map[string]string, keyStr string) (string, error) {
413413
}
414414

415415
func KeyfileDecrypt(tokenStr string, keyStr string) (map[string]string, error) {
416-
fileBytes, err := ioutil.ReadFile(keyStr)
416+
fileBytes, err := os.ReadFile(keyStr)
417417
if err != nil {
418418
return nil, err
419419
}

goseg/broadcast/sse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func writeStreamEvent(w io.Writer, event streamEvent) error {
304304
return err
305305
}
306306
}
307-
for _, line := range strings.Split(string(event.Data), "\n") {
307+
for line := range strings.SplitSeq(string(event.Data), "\n") {
308308
if _, err := fmt.Fprintf(w, "data: %s\n", line); err != nil {
309309
return err
310310
}

goseg/click/hoon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"groundseg/config"
66
"groundseg/docker"
7-
"io/ioutil"
7+
88
"os"
99
"path/filepath"
1010
"strings"
@@ -19,7 +19,7 @@ func createHoon(patp, file, hoon string) error {
1919
}
2020
}
2121
hoonFile := filepath.Join(location, fmt.Sprintf("%s.hoon", file))
22-
if err := ioutil.WriteFile(hoonFile, []byte(hoon), 0644); err != nil {
22+
if err := os.WriteFile(hoonFile, []byte(hoon), 0644); err != nil {
2323
return err
2424
}
2525
ClearLusCode(patp)

goseg/config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"groundseg/structs"
1313
"groundseg/system"
1414
"io"
15-
"io/ioutil"
1615
"maps"
1716
"math/rand"
1817
"net"
@@ -100,7 +99,7 @@ func init() {
10099
keyfile, err := os.Stat(keyPath)
101100
if err != nil || keyfile.Size() == 0 {
102101
keyContent := RandString(32)
103-
if err := ioutil.WriteFile(keyPath, []byte(keyContent), 0644); err != nil {
102+
if err := os.WriteFile(keyPath, []byte(keyContent), 0644); err != nil {
104103
zap.L().Error(fmt.Sprintf("Couldn't write keyfile! %v", err))
105104
}
106105
}
@@ -159,7 +158,7 @@ func init() {
159158
keyfile, err := os.Stat(keyPath)
160159
if err != nil || keyfile.Size() == 0 {
161160
keyContent := RandString(32)
162-
if err := ioutil.WriteFile(keyPath, []byte(keyContent), 0644); err != nil {
161+
if err := os.WriteFile(keyPath, []byte(keyContent), 0644); err != nil {
163162
zap.L().Error(fmt.Sprintf("Couldn't write keyfile! %v", err))
164163
}
165164
}
@@ -282,7 +281,7 @@ func UpdateConf(values map[string]any) error {
282281
// mutex lock to avoid race conditions
283282
confMutex.Lock()
284283
defer confMutex.Unlock()
285-
file, err := ioutil.ReadFile(confPath)
284+
file, err := os.ReadFile(confPath)
286285
if err != nil {
287286
return fmt.Errorf("Unable to load config: %v", err)
288287
}

goseg/config/urbit.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"groundseg/defaults"
1010
"groundseg/dockerclient"
1111
"groundseg/structs"
12-
"io/ioutil"
12+
1313
"os"
1414
"path/filepath"
1515
"strings"
@@ -50,7 +50,7 @@ func LoadUrbitConfig(pier string) error {
5050
defer urbitMutex.Unlock()
5151
// pull docker info from json
5252
confPath := filepath.Join(BasePath, "settings", "pier", pier+".json")
53-
file, err := ioutil.ReadFile(confPath)
53+
file, err := os.ReadFile(confPath)
5454
if err != nil {
5555
return fmt.Errorf("Unable to load %s config: %w", pier, err)
5656
// todo: write a new conf
@@ -129,6 +129,15 @@ func UpdateUrbitConfig(inputConfig map[string]structs.UrbitDocker) error {
129129
return nil
130130
}
131131

132+
func UpdateUrbitConfigForPier(pier string, mutate func(*structs.UrbitDocker)) error {
133+
if err := LoadUrbitConfig(pier); err != nil {
134+
return err
135+
}
136+
urbConf := UrbitConf(pier)
137+
mutate(&urbConf)
138+
return UpdateUrbitConfig(map[string]structs.UrbitDocker{pier: urbConf})
139+
}
140+
132141
func getImageTagByContainerName(containerName string) (string, error) {
133142
ctx := context.Background()
134143

goseg/config/version.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"groundseg/defaults"
77
"groundseg/structs"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"os"
1111
"path/filepath"
@@ -48,7 +48,7 @@ func CheckVersion() (structs.Channel, bool) {
4848
}
4949
}
5050
// read the body bytes
51-
body, err := ioutil.ReadAll(resp.Body)
51+
body, err := io.ReadAll(resp.Body)
5252
resp.Body.Close()
5353
if err != nil {
5454
errmsg := fmt.Sprintf("Error reading version info: %v", err)
@@ -110,7 +110,7 @@ func CreateDefaultVersion() error {
110110
return err
111111
}
112112
filePath := filepath.Join(BasePath, "settings", "version_info.json")
113-
err = ioutil.WriteFile(filePath, prettyJSON, 0644)
113+
err = os.WriteFile(filePath, prettyJSON, 0644)
114114
if err != nil {
115115
return err
116116
}
@@ -131,7 +131,7 @@ func LocalVersion() structs.Version {
131131
panic(errmsg)
132132
}
133133
}
134-
file, err := ioutil.ReadFile(confPath)
134+
file, err := os.ReadFile(confPath)
135135
if err != nil {
136136
errmsg := fmt.Sprintf("Unable to load version info: %v", err)
137137
panic(errmsg)

goseg/config/wireguard.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"fmt"
77
"groundseg/defaults"
88
"groundseg/structs"
9-
"io/ioutil"
9+
"io"
10+
1011
"os"
1112
"path/filepath"
1213

@@ -24,7 +25,7 @@ func GetWgConf() (structs.WgConfig, error) {
2425
defer configFile.Close()
2526

2627
// Read file contents into byte slice
27-
byteValue, _ := ioutil.ReadAll(configFile)
28+
byteValue, _ := io.ReadAll(configFile)
2829

2930
if err := json.Unmarshal(byteValue, &wgConf); err != nil {
3031
return wgConf, err

goseg/docker/boot_status.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package docker
2+
3+
func IsMaintenanceBootStatus(status string) bool {
4+
switch status {
5+
case "pack", "meld", "chop", "rollchop", "prep", "roll":
6+
return true
7+
default:
8+
return false
9+
}
10+
}
11+
12+
func PersistentBootStatusAfterContainerBuild(status string) string {
13+
switch status {
14+
case "pack", "meld", "chop", "rollchop", "noboot":
15+
return "noboot"
16+
case "ignore":
17+
return "ignore"
18+
default:
19+
return "boot"
20+
}
21+
}

goseg/docker/boot_status_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package docker
2+
3+
import "testing"
4+
5+
func TestIsMaintenanceBootStatus(t *testing.T) {
6+
tests := []struct {
7+
status string
8+
want bool
9+
}{
10+
{status: "pack", want: true},
11+
{status: "meld", want: true},
12+
{status: "chop", want: true},
13+
{status: "rollchop", want: true},
14+
{status: "prep", want: true},
15+
{status: "roll", want: true},
16+
{status: "boot", want: false},
17+
{status: "noboot", want: false},
18+
{status: "ignore", want: false},
19+
}
20+
21+
for _, test := range tests {
22+
if got := IsMaintenanceBootStatus(test.status); got != test.want {
23+
t.Fatalf("IsMaintenanceBootStatus(%q) = %v, want %v", test.status, got, test.want)
24+
}
25+
}
26+
}
27+
28+
func TestPersistentBootStatusAfterContainerBuild(t *testing.T) {
29+
tests := []struct {
30+
status string
31+
want string
32+
}{
33+
{status: "pack", want: "noboot"},
34+
{status: "meld", want: "noboot"},
35+
{status: "chop", want: "noboot"},
36+
{status: "rollchop", want: "noboot"},
37+
{status: "noboot", want: "noboot"},
38+
{status: "ignore", want: "ignore"},
39+
{status: "boot", want: "boot"},
40+
{status: "prep", want: "boot"},
41+
{status: "roll", want: "boot"},
42+
}
43+
44+
for _, test := range tests {
45+
if got := PersistentBootStatusAfterContainerBuild(test.status); got != test.want {
46+
t.Fatalf("PersistentBootStatusAfterContainerBuild(%q) = %q, want %q", test.status, got, test.want)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)