Skip to content

Commit d496ef3

Browse files
committed
start using config file instead of cmdline flags
1 parent 7523771 commit d496ef3

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

.golangci.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# https://golangci-lint.run/usage/configuration/
22
run:
33
timeout: 10m
4-
# skip auto-generated files.
5-
skip-files:
6-
- ".*\\.pb\\.go$"
7-
- ".*mock.*"
84

95
issues:
106
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
117
max-same-issues: 0
8+
# skip auto-generated files.
9+
exclude-files:
10+
- ".*\\.pb\\.go$"
11+
- ".*mock.*"
1212

1313
linters:
1414
# please, do not use `enable-all`: it's deprecated and will be removed soon.

local/helpers.go

+34-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package local
22

33
import (
44
"encoding/base64"
5+
"encoding/json"
56
"fmt"
67
"math/rand"
78
"net"
@@ -45,40 +46,32 @@ func writeFiles(networkID uint32, genesis []byte, nodeRootDir string, nodeConfig
4546
}
4647
files := []file{
4748
{
48-
flagValue: filepath.Join(nodeRootDir, stakingKeyFileName),
49-
path: filepath.Join(nodeRootDir, stakingKeyFileName),
49+
flagValue: filepath.Join(nodeRootDir, "staking", stakingKeyFileName),
50+
path: filepath.Join(nodeRootDir, "staking", stakingKeyFileName),
5051
pathKey: config.StakingTLSKeyPathKey,
5152
contents: []byte(nodeConfig.StakingKey),
5253
},
5354
{
54-
flagValue: filepath.Join(nodeRootDir, stakingCertFileName),
55-
path: filepath.Join(nodeRootDir, stakingCertFileName),
55+
flagValue: filepath.Join(nodeRootDir, "staking", stakingCertFileName),
56+
path: filepath.Join(nodeRootDir, "staking", stakingCertFileName),
5657
pathKey: config.StakingCertPathKey,
5758
contents: []byte(nodeConfig.StakingCert),
5859
},
5960
{
60-
flagValue: filepath.Join(nodeRootDir, stakingSigningKeyFileName),
61-
path: filepath.Join(nodeRootDir, stakingSigningKeyFileName),
61+
flagValue: filepath.Join(nodeRootDir, "staking", stakingSigningKeyFileName),
62+
path: filepath.Join(nodeRootDir, "staking", stakingSigningKeyFileName),
6263
pathKey: config.StakingSignerKeyPathKey,
6364
contents: decodedStakingSigningKey,
6465
},
6566
}
6667
if networkID != constants.LocalID {
6768
files = append(files, file{
68-
flagValue: filepath.Join(nodeRootDir, genesisFileName),
69-
path: filepath.Join(nodeRootDir, genesisFileName),
69+
flagValue: filepath.Join(nodeRootDir, "configs", genesisFileName),
70+
path: filepath.Join(nodeRootDir, "configs", genesisFileName),
7071
pathKey: config.GenesisFileKey,
7172
contents: genesis,
7273
})
7374
}
74-
if len(nodeConfig.ConfigFile) != 0 {
75-
files = append(files, file{
76-
flagValue: filepath.Join(nodeRootDir, configFileName),
77-
path: filepath.Join(nodeRootDir, configFileName),
78-
pathKey: config.ConfigFileKey,
79-
contents: []byte(nodeConfig.ConfigFile),
80-
})
81-
}
8275
flags := map[string]string{}
8376
for _, f := range files {
8477
flags[f.pathKey] = f.flagValue
@@ -122,6 +115,31 @@ func writeFiles(networkID uint32, genesis []byte, nodeRootDir string, nodeConfig
122115
return flags, nil
123116
}
124117

118+
func writeConfigFile(nodeRootDir string, nodeConfig *node.Config, flags map[string]string) (string, error) {
119+
if len(nodeConfig.ConfigFile) != 0 {
120+
newFlags := map[string]interface{}{}
121+
if err := json.Unmarshal([]byte(nodeConfig.ConfigFile), &newFlags); err != nil {
122+
return "", err
123+
}
124+
for k, vI := range newFlags {
125+
v, ok := vI.(string)
126+
if !ok {
127+
return "", fmt.Errorf("expected string for key %q, found %T", k, vI)
128+
}
129+
flags[k] = v
130+
}
131+
}
132+
configFileBytes, err := json.MarshalIndent(flags, "", " ")
133+
if err != nil {
134+
return "", err
135+
}
136+
configFilePath := filepath.Join(nodeRootDir, "configs", configFileName)
137+
if err := createFileAndWrite(configFilePath, configFileBytes); err != nil {
138+
return "", err
139+
}
140+
return configFilePath, nil
141+
}
142+
125143
// getConfigEntry returns an entry in the config file if it is found, otherwise returns the default value
126144
func getConfigEntry(
127145
nodeConfigFlags map[string]interface{},

local/network.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,14 @@ func (ln *localNetwork) buildArgs(
11151115
// old avago versions
11161116
flagsForAvagoVersion := getFlagsForAvagoVersion(nodeSemVer, flags)
11171117

1118+
configFilePath, err := writeConfigFile(dataDir, nodeConfig, flagsForAvagoVersion)
1119+
if err != nil {
1120+
return buildArgsReturn{}, err
1121+
}
1122+
11181123
// create args
1119-
args := []string{}
1120-
for k, v := range flagsForAvagoVersion {
1121-
args = append(args, fmt.Sprintf("--%s=%s", k, v))
1124+
args := []string{
1125+
fmt.Sprintf("--%s=%s", config.ConfigFileKey, configFilePath),
11221126
}
11231127

11241128
return buildArgsReturn{

local/snapshot.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ava-labs/avalanche-network-runner/utils"
1616
"github.com/ava-labs/avalanchego/config"
1717
"github.com/ava-labs/avalanchego/ids"
18-
"github.com/ava-labs/avalanchego/utils/constants"
1918
"github.com/ava-labs/avalanchego/utils/logging"
2019
dircopy "github.com/otiai10/copy"
2120
"golang.org/x/exp/maps"
@@ -125,14 +124,14 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
125124
}
126125
// keep copy of node info that will be removed by stop
127126
nodesConfig := map[string]node.Config{}
128-
nodesDBDir := map[string]string{}
127+
nodesDataDir := map[string]string{}
129128
for nodeName, node := range ln.nodes {
130129
nodeConfig := node.config
131130
// depending on how the user generated the config, different nodes config flags
132131
// may point to the same map, so we made a copy to avoid always modifying the same value
133132
nodeConfig.Flags = maps.Clone(nodeConfig.Flags)
134133
nodesConfig[nodeName] = nodeConfig
135-
nodesDBDir[nodeName] = node.GetDbDir()
134+
nodesDataDir[nodeName] = node.GetDataDir()
136135
}
137136
// we change nodeConfig.Flags so as to preserve in snapshot the current node ports
138137
for nodeName, nodeConfig := range nodesConfig {
@@ -162,19 +161,17 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
162161
return "", err
163162
}
164163
// create main snapshot dirs
165-
snapshotDBDir := filepath.Join(snapshotDir, defaultDBSubdir)
166-
if err := os.MkdirAll(snapshotDBDir, os.ModePerm); err != nil {
164+
if err := os.MkdirAll(snapshotDir, os.ModePerm); err != nil {
167165
return "", err
168166
}
169167
// save db
170168
for _, nodeConfig := range nodesConfig {
171-
sourceDBDir, ok := nodesDBDir[nodeConfig.Name]
169+
sourceDataDir, ok := nodesDataDir[nodeConfig.Name]
172170
if !ok {
173-
return "", fmt.Errorf("failure obtaining db path for node %q", nodeConfig.Name)
171+
return "", fmt.Errorf("failure obtaining datadir path for node %q", nodeConfig.Name)
174172
}
175-
sourceDBDir = filepath.Join(sourceDBDir, constants.NetworkName(ln.networkID))
176-
targetDBDir := filepath.Join(filepath.Join(snapshotDBDir, nodeConfig.Name), constants.NetworkName(ln.networkID))
177-
if err := dircopy.Copy(sourceDBDir, targetDBDir); err != nil {
173+
targetDataDir := filepath.Join(snapshotDir, nodeConfig.Name)
174+
if err := dircopy.Copy(sourceDataDir, targetDataDir); err != nil {
178175
return "", fmt.Errorf("failure saving node %q db dir: %w", nodeConfig.Name, err)
179176
}
180177
}

0 commit comments

Comments
 (0)