summaryrefslogtreecommitdiff
path: root/cmd/algocfg/profileCommand.go
blob: 4cd9bf5c9e3240f14873efb515f959a8a34ed12d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/spf13/cobra"

	"github.com/algorand/go-algorand/cmd/util/datadir"
	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/util/codecs"
)

// configUpdater updates the provided config for non-defaults in a given profile
type configUpdater struct {
	updateFunc  func(cfg config.Local) config.Local
	description string
}

var (
	development = configUpdater{
		description: "Build on Algorand.",
		updateFunc: func(cfg config.Local) config.Local {
			cfg.EnableExperimentalAPI = true
			cfg.EnableDeveloperAPI = true
			cfg.MaxAcctLookback = 256
			cfg.EnableTxnEvalTracer = true
			cfg.DisableAPIAuth = true
			return cfg
		},
	}

	conduit = configUpdater{
		description: "Provide data for the Conduit tool.",
		updateFunc: func(cfg config.Local) config.Local {
			cfg.EnableFollowMode = true
			cfg.MaxAcctLookback = 64
			cfg.CatchupParallelBlocks = 64
			return cfg
		},
	}

	participation = configUpdater{
		description: "Participate in consensus or simply ensure chain health by validating blocks.",
		updateFunc: func(cfg config.Local) config.Local {
			return cfg
		},
	}

	relay = configUpdater{
		description: "Relay consensus messages across the network and support catchup.",
		updateFunc: func(cfg config.Local) config.Local {
			cfg.MaxBlockHistoryLookback = 22000 // Enough to support 2 catchpoints with some wiggle room for nodes to catch up from the older one
			cfg.CatchpointFileHistoryLength = 3
			cfg.CatchpointTracking = 2
			cfg.EnableLedgerService = true
			cfg.EnableBlockService = true
			cfg.NetAddress = ":4160"
			return cfg
		},
	}

	archival = configUpdater{
		description: "Store the full chain history and support catchup.",
		updateFunc: func(cfg config.Local) config.Local {
			cfg.Archival = true
			cfg.EnableLedgerService = true
			cfg.EnableBlockService = true
			cfg.NetAddress = ":4160"
			cfg.EnableGossipService = false
			return cfg
		},
	}

	// profileNames are the supported pre-configurations of config values
	profileNames = map[string]configUpdater{
		"participation": participation,
		"conduit":       conduit,
		"relay":         relay,
		"archival":      archival,
		"development":   development,
	}

	forceUpdate bool
)

func init() {
	rootCmd.AddCommand(profileCmd)
	profileCmd.AddCommand(setProfileCmd)
	setProfileCmd.Flags().BoolVarP(&forceUpdate, "yes", "y", false, "Force updates to be written")
	profileCmd.AddCommand(printProfileCmd)
	profileCmd.AddCommand(listProfileCmd)
}

var profileCmd = &cobra.Command{
	Use:   "profile",
	Short: "Generate config.json file from a profile.",
	Long: `Initialize algod config.json files based on a usage profile.

The config file generated by these profiles can be used as a starting point
for a nodes configuration. The defaults for a given profile should be treated
as supplemental to the documentation, you should review the documentation to
understand what the settings are doing.

For more details about configuration settings refer to the developer portal:
https://developer.algorand.org/docs/run-a-node/reference/config/

Profiles are subject to change or removal.`,
	Args: cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.HelpFunc()(cmd, args)
	},
}

var listProfileCmd = &cobra.Command{
	Use:   "list",
	Short: "A list of valid config profiles and a short description.",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		longest := 0
		for key := range profileNames {
			if len(key) > longest {
				longest = len(key)
			}
		}

		for key, value := range profileNames {
			reportInfof("%-*s  %s", longest, key, value.description)
		}
	},
}

var printProfileCmd = &cobra.Command{
	Use:   "print",
	Short: "Print config.json to stdout.",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		cfg, err := getConfigForArg(args[0])
		if err != nil {
			reportErrorf("%v", err)
		}
		err = codecs.WriteNonDefaultValues(os.Stdout, cfg, config.GetDefaultLocal(), nil)
		if err != nil {
			reportErrorf("Error writing config file to stdout: %s", err)
		}
		fmt.Fprintf(os.Stdout, "\n")
	},
}

var setProfileCmd = &cobra.Command{
	Use:   "set",
	Short: "Set config.json file from a profile.",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		datadir.OnDataDirs(func(dataDir string) {
			cfg, err := getConfigForArg(args[0])
			if err != nil {
				reportErrorf("%v", err)
			}
			file := filepath.Join(dataDir, config.ConfigFilename)
			if _, statErr := os.Stat(file); !forceUpdate && statErr == nil {
				fmt.Printf("A config.json file already exists at %s\nWould you like to overwrite it? (Y/n)", file)
				reader := bufio.NewReader(os.Stdin)
				resp, readErr := reader.ReadString('\n')
				resp = strings.TrimSpace(resp)
				if readErr != nil {
					reportErrorf("Failed to read response: %v", readErr)
				}
				if strings.ToLower(resp) == "n" {
					reportInfof("Exiting without overwriting existing config.")
					return
				}
			}
			err = codecs.SaveNonDefaultValuesToFile(file, cfg, config.GetDefaultLocal(), nil)
			if err != nil {
				reportErrorf("Error saving updated config file '%s' - %s", file, err)
			}
		})
	},
}

// getConfigForArg returns a Local config w/ options updated acorrding to the profil specified by configType
func getConfigForArg(configType string) (config.Local, error) {
	cfg := config.GetDefaultLocal()
	if updater, ok := profileNames[configType]; ok {
		return updater.updateFunc(cfg), nil
	}

	var names []string
	for name := range profileNames {
		names = append(names, name)
	}
	return config.Local{}, fmt.Errorf("unknown profile provided: '%s' is not in list of valid profiles: %s", configType, strings.Join(names, ", "))
}