Skip to content

Commit 5098e36

Browse files
authored
Respect ames ports in local mode; linting and golang update; unit tests (#752)
* respect ames port in local mode * Add unit tests * linting and tidying * bump critical issue
1 parent f1caac3 commit 5098e36

41 files changed

Lines changed: 454 additions & 1647 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

goseg/auth/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ func AddSession(tokenID string, hash string, created string, authorized bool) er
312312
Created: created,
313313
}
314314
if authorized {
315-
update := map[string]interface{}{
316-
"sessions": map[string]interface{}{
315+
update := map[string]any{
316+
"sessions": map[string]any{
317317
"authorized": map[string]structs.SessionInfo{
318318
tokenID: session,
319319
},
@@ -324,8 +324,8 @@ func AddSession(tokenID string, hash string, created string, authorized bool) er
324324
}
325325
RemoveFromAuthMap(tokenID, false)
326326
} else {
327-
update := map[string]interface{}{
328-
"sessions": map[string]interface{}{
327+
update := map[string]any{
328+
"sessions": map[string]any{
329329
"unauthorized": map[string]structs.SessionInfo{
330330
tokenID: session,
331331
},

goseg/broadcast/broadcast.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
"groundseg/startram"
1212
"groundseg/structs"
1313
"groundseg/system"
14+
"maps"
1415
"path"
1516
"path/filepath"
1617
"regexp"
1718
"runtime"
19+
"slices"
1820
"strconv"
1921
"strings"
2022
"sync"
@@ -150,9 +152,7 @@ func ConstructPierInfo() (map[string]structs.Urbit, error) {
150152
remoteBackups := config.StartramConfig.Backups
151153
remoteBackupMap := make(structs.Backup)
152154
for _, backup := range remoteBackups {
153-
for ship, backupInfo := range backup {
154-
remoteBackupMap[ship] = backupInfo
155-
}
155+
maps.Copy(remoteBackupMap, backup)
156156
}
157157
localDailyBackups := make(structs.Backup)
158158
localWeeklyBackups := make(structs.Backup)
@@ -325,10 +325,7 @@ func ConstructPierInfo() (map[string]structs.Urbit, error) {
325325
}
326326

327327
// pack date
328-
packDate := 1
329-
if dockerConfig.MeldDate > 1 {
330-
packDate = dockerConfig.MeldDate
331-
}
328+
packDate := max(dockerConfig.MeldDate, 1)
332329

333330
// collate all the info from our sources into the struct
334331
bootCommandBase := ""
@@ -462,13 +459,7 @@ func constructProfileInfo() structs.Profile {
462459
patp := parts[len(parts)-3]
463460

464461
// Put ships in slice
465-
shipExists := false
466-
for _, ship := range startramServices {
467-
if ship == patp {
468-
shipExists = true
469-
break
470-
}
471-
}
462+
shipExists := slices.Contains(startramServices, patp)
472463
if !shipExists {
473464
startramServices = append(startramServices, patp)
474465
}

goseg/click/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func getStorageEndpoint(patp string) (string, error) {
121121
}
122122

123123
func parseStorageEndpoint(response string) (string, error) {
124-
for _, line := range strings.Split(response, "\n") {
124+
for line := range strings.SplitSeq(response, "\n") {
125125
if !strings.Contains(line, "%avow") {
126126
continue
127127
}

goseg/config/config.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"groundseg/system"
1414
"io"
1515
"io/ioutil"
16+
"maps"
1617
"math/rand"
1718
"net"
1819
"os"
@@ -82,7 +83,7 @@ func init() {
8283
if err != nil {
8384
zap.L().Error(fmt.Sprintf("%v", err))
8485
} else {
85-
if err = UpdateConf(map[string]interface{}{
86+
if err = UpdateConf(map[string]any{
8687
"pubkey": wgPub,
8788
"privkey": wgPriv,
8889
"salt": salt,
@@ -124,7 +125,7 @@ func init() {
124125
globalConfig.BinHash = hash
125126
zap.L().Info(fmt.Sprintf("Binary sha256 hash: %v", hash))
126127

127-
configMap := make(map[string]interface{})
128+
configMap := make(map[string]any)
128129
configBytes, err := json.Marshal(globalConfig)
129130
if err != nil {
130131
errmsg := fmt.Sprintf("Error marshaling JSON: %v", err)
@@ -163,7 +164,7 @@ func init() {
163164
}
164165
}
165166
file, _ = os.Open(confPath)
166-
if err = UpdateConf(map[string]interface{}{
167+
if err = UpdateConf(map[string]any{
167168
"keyfile": keyPath,
168169
}); err != nil {
169170
zap.L().Error(fmt.Sprintf("%v", err))
@@ -266,7 +267,7 @@ func ConfChannel() {
266267
case "c2cInterval":
267268
conf := Conf()
268269
if conf.C2cInterval == 0 {
269-
if err := UpdateConf(map[string]interface{}{
270+
if err := UpdateConf(map[string]any{
270271
"c2cInterval": 600,
271272
}); err != nil {
272273
zap.L().Error(fmt.Sprintf("Couldn't set C2C interval: %v", err))
@@ -277,7 +278,7 @@ func ConfChannel() {
277278
}
278279

279280
// update by passing in a map of key:values you want to modify
280-
func UpdateConf(values map[string]interface{}) error {
281+
func UpdateConf(values map[string]any) error {
281282
// mutex lock to avoid race conditions
282283
confMutex.Lock()
283284
defer confMutex.Unlock()
@@ -286,21 +287,19 @@ func UpdateConf(values map[string]interface{}) error {
286287
return fmt.Errorf("Unable to load config: %v", err)
287288
}
288289
// unmarshal the config to struct
289-
var configMap map[string]interface{}
290+
var configMap map[string]any
290291
if err := json.Unmarshal(file, &configMap); err != nil {
291292
return fmt.Errorf("Error decoding JSON: %v", err)
292293
}
293294
// update our unmarshaled struct
294-
for key, value := range values {
295-
configMap[key] = value
296-
}
295+
maps.Copy(configMap, values)
297296
if err = persistConf(configMap); err != nil {
298297
return fmt.Errorf("Unable to persist config update: %v", err)
299298
}
300299
return nil
301300
}
302301

303-
func persistConf(configMap map[string]interface{}) error {
302+
func persistConf(configMap map[string]any) error {
304303
BasePath := getBasePath()
305304
confPath := filepath.Join(BasePath, "settings", "system.json")
306305
tmpFile, err := os.CreateTemp(filepath.Dir(confPath), "system.json.*")

goseg/config/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func CheckVersion() (structs.Channel, bool) {
2929
const delay = time.Second
3030
url := globalConfig.UpdateUrl
3131
var fetchedVersion structs.Version
32-
for i := 0; i < retries; i++ {
32+
for i := range retries {
3333
req, err := http.NewRequest("GET", url, nil)
3434
if err != nil {
3535
}

goseg/config/wireguard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func CycleWgKey() error {
107107
if err != nil {
108108
return fmt.Errorf("Couldn't reset WG keys: %w", err)
109109
}
110-
if err := UpdateConf(map[string]interface{}{
110+
if err := UpdateConf(map[string]any{
111111
"pubkey": pub,
112112
"privkey": priv,
113113
}); err != nil {

goseg/docker/docker.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"io/ioutil"
1212
"path/filepath"
13+
"slices"
1314
"strings"
1415
"time"
1516

@@ -147,10 +148,8 @@ func GetContainerRunningStatus(containerName string) (string, error) {
147148
}
148149
// Loop through containers to find the one with the given name
149150
for _, container := range containers {
150-
for _, name := range container.Names {
151-
if name == "/"+containerName {
152-
return container.Status, nil
153-
}
151+
if slices.Contains(container.Names, "/"+containerName) {
152+
return container.Status, nil
154153
}
155154
}
156155
return status, fmt.Errorf("Unable to get container running status: %v", containerName)
@@ -535,12 +534,12 @@ func GetLatestContainerInfo(containerType string) (map[string]string, error) {
535534
return res, err
536535
}
537536
// Convert JSON to map
538-
var m map[string]interface{}
537+
var m map[string]any
539538
err = json.Unmarshal(jsonData, &m)
540539
if err != nil {
541540
return res, err
542541
}
543-
containerData, ok := m[containerType].(map[string]interface{})
542+
containerData, ok := m[containerType].(map[string]any)
544543
if !ok {
545544
return nil, fmt.Errorf("%s data is not a map", containerType)
546545
}
@@ -606,10 +605,8 @@ func PullImageIfNotExist(desiredImage string, imageInfo map[string]string) (bool
606605
return false, err
607606
}
608607
for _, img := range images {
609-
for _, digest := range img.RepoDigests {
610-
if digest == fmt.Sprintf("%s@sha256:%s", imageInfo["repo"], imageInfo["hash"]) {
611-
return true, nil
612-
}
608+
if slices.Contains(img.RepoDigests, fmt.Sprintf("%s@sha256:%s", imageInfo["repo"], imageInfo["hash"])) {
609+
return true, nil
613610
}
614611
}
615612
resp, err := cli.ImagePull(ctx, fmt.Sprintf("%s@sha256:%s", imageInfo["repo"], imageInfo["hash"]), imagetypes.PullOptions{})
@@ -635,15 +632,11 @@ func PullImageByRef(imageRef string) error {
635632
return err
636633
}
637634
for _, img := range images {
638-
for _, tag := range img.RepoTags {
639-
if tag == imageRef {
640-
return nil
641-
}
635+
if slices.Contains(img.RepoTags, imageRef) {
636+
return nil
642637
}
643-
for _, digest := range img.RepoDigests {
644-
if digest == imageRef {
645-
return nil
646-
}
638+
if slices.Contains(img.RepoDigests, imageRef) {
639+
return nil
647640
}
648641
}
649642

@@ -751,10 +744,8 @@ func GetContainerIDByName(ctx context.Context, cli *client.Client, name string)
751744
return "", err
752745
}
753746
for _, container := range containers {
754-
for _, n := range container.Names {
755-
if n == "/"+name {
756-
return container.ID, nil
757-
}
747+
if slices.Contains(container.Names, "/"+name) {
748+
return container.ID, nil
758749
}
759750
}
760751
return "", fmt.Errorf("Container not found")
@@ -783,12 +774,7 @@ func RestartContainer(name string) error {
783774
}
784775

785776
func contains(slice []string, str string) bool {
786-
for _, item := range slice {
787-
if item == str {
788-
return true
789-
}
790-
}
791-
return false
777+
return slices.Contains(slice, str)
792778
}
793779

794780
func volumeExists(volumeName string) (bool, error) {

goseg/docker/minio.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ func newS3Client(endpoint string, accessKey string, secretKey string) (*s3v2.Cli
533533
awscfg.WithRegion("us-east-1"),
534534
awscfg.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
535535
awscfg.WithEndpointResolverWithOptions(awsv2.EndpointResolverWithOptionsFunc(
536-
func(service, region string, options ...interface{}) (awsv2.Endpoint, error) {
536+
func(service, region string, options ...any) (awsv2.Endpoint, error) {
537537
if service == s3v2.ServiceID {
538538
return awsv2.Endpoint{
539539
URL: endpoint,
@@ -627,7 +627,7 @@ func setPublicReadPolicy(client *s3v2.Client, bucket string) error {
627627

628628
func waitForS3Ready(client *s3v2.Client) error {
629629
var lastErr error
630-
for i := 0; i < 30; i++ {
630+
for range 30 {
631631
_, err := client.ListBuckets(context.Background(), &s3v2.ListBucketsInput{})
632632
if err == nil {
633633
return nil

goseg/docker/urbit.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,14 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
167167
Cmd: bootCommand.ScriptArgs,
168168
}
169169
} else {
170-
httpPort := fmt.Sprintf("%v", shipConf.HTTPPort)
171-
amesPort := fmt.Sprintf("%v", shipConf.AmesPort)
172170
network = "default"
173-
//httpPortStr := nat.Port(fmt.Sprintf(httpPort + "/tcp"))
174-
//amesPortStr := nat.Port(fmt.Sprintf(amesPort + "/udp"))
175-
// Port mapping
176-
portMap = nat.PortMap{
177-
"80/tcp": []nat.PortBinding{
178-
{HostIP: "0.0.0.0", HostPort: httpPort},
179-
},
180-
"34343/udp": []nat.PortBinding{
181-
{HostIP: "0.0.0.0", HostPort: amesPort},
182-
},
183-
}
171+
var exposedPorts nat.PortSet
172+
portMap, exposedPorts = localUrbitPortBindings(shipConf)
184173
// finally construct the container config structs
185174
containerConfig = container.Config{
186-
Image: desiredImage,
187-
ExposedPorts: nat.PortSet{
188-
"80/tcp": struct{}{},
189-
"34343/udp": struct{}{},
190-
},
191-
Cmd: bootCommand.ScriptArgs,
175+
Image: desiredImage,
176+
ExposedPorts: exposedPorts,
177+
Cmd: bootCommand.ScriptArgs,
192178
}
193179
}
194180
mountType := mount.TypeVolume
@@ -219,6 +205,26 @@ func urbitContainerConf(containerName string) (container.Config, container.HostC
219205
return containerConfig, hostConfig, nil
220206
}
221207

208+
func localUrbitPortBindings(shipConf structs.UrbitDocker) (nat.PortMap, nat.PortSet) {
209+
httpPort := fmt.Sprintf("%v", shipConf.HTTPPort)
210+
amesPort := resolvedLocalAmesPort(shipConf)
211+
amesPortProto := nat.Port(amesPort + "/udp")
212+
213+
portMap := nat.PortMap{
214+
"80/tcp": []nat.PortBinding{
215+
{HostIP: "0.0.0.0", HostPort: httpPort},
216+
},
217+
amesPortProto: []nat.PortBinding{
218+
{HostIP: "0.0.0.0", HostPort: amesPort},
219+
},
220+
}
221+
exposedPorts := nat.PortSet{
222+
"80/tcp": struct{}{},
223+
amesPortProto: struct{}{},
224+
}
225+
return portMap, exposedPorts
226+
}
227+
222228
func shipUsesDevMode(bootStatus string) bool {
223229
switch bootStatus {
224230
case "boot", "noboot", "ignore":

goseg/docker/urbit_boot.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func BuildUrbitBootCommand(shipConf structs.UrbitDocker, systemConf structs.SysC
6565
"--dirname=" + shipConf.PierName,
6666
"--devmode=" + devMode,
6767
}
68-
if shipConf.Network == "wireguard" {
68+
if shipConf.Network == "wireguard" || amesPort != "34343" {
6969
scriptArgs = append(scriptArgs,
7070
"--http-port="+httpPort,
7171
"--port="+amesPort,
@@ -247,7 +247,14 @@ func resolvedRuntimePorts(shipConf structs.UrbitDocker) (string, string) {
247247
if shipConf.Network == "wireguard" {
248248
return fmt.Sprintf("%v", shipConf.WgHTTPPort), fmt.Sprintf("%v", shipConf.WgAmesPort)
249249
}
250-
return "80", "34343"
250+
return "80", resolvedLocalAmesPort(shipConf)
251+
}
252+
253+
func resolvedLocalAmesPort(shipConf structs.UrbitDocker) string {
254+
if shipConf.AmesPort > 0 {
255+
return fmt.Sprintf("%v", shipConf.AmesPort)
256+
}
257+
return "34343"
251258
}
252259

253260
func formatShellCommand(command string, args []string) string {

0 commit comments

Comments
 (0)