summaryrefslogtreecommitdiff
path: root/cmd/netgoal/network.go
blob: 69fecbac263f040ffdaeb529bb0a7c2d7e785784 (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
214
215
216
217
218
219
220
221
222
223
224
225
// 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 (
	"fmt"
	"os"
	"path/filepath"
	"runtime/pprof"
	"strings"

	"github.com/spf13/cobra"

	"github.com/algorand/go-algorand/netdeploy/remote"
	"github.com/algorand/go-algorand/util"
	"github.com/algorand/go-algorand/util/codecs"
)

var networkRootDir string
var networkRecipeFile string
var networkName string
var networkGenesisVersionModifier string
var miscStringStringTokens []string

var cpuprofilePath string

var networkUseGenesisFiles bool
var networkIgnoreExistingDir bool
var bootstrapLoadingFile bool

func init() {
	rootCmd.AddCommand(networkBuildCmd)

	rootCmd.PersistentFlags().StringVarP(&networkRootDir, "rootdir", "r", "", "Root directory for the private network directories")
	rootCmd.MarkPersistentFlagRequired("rootdir")
	networkBuildCmd.Flags().StringVarP(&networkName, "network", "n", "", "Specify the name to use for the network (overrides config file)")
	rootCmd.MarkPersistentFlagRequired("network")

	networkBuildCmd.Flags().StringVar(&networkRecipeFile, "recipe", "", "Specify the path of a Recipe file to use")
	networkBuildCmd.MarkFlagRequired("recipe")

	networkBuildCmd.Flags().BoolVarP(&networkUseGenesisFiles, "use-existing-files", "e", false, "Use existing genesis files.")
	networkBuildCmd.Flags().BoolVarP(&bootstrapLoadingFile, "gen-db-files", "b", false, "Generate database files.")
	networkBuildCmd.Flags().BoolVarP(&networkIgnoreExistingDir, "force", "f", false, "Force generation into existing directory.")
	networkBuildCmd.Flags().StringSliceVarP(&miscStringStringTokens, "val", "v", nil, "name=value, may be reapeated")
	networkBuildCmd.Flags().StringVar(&cpuprofilePath, "cpuprofile", "", "write cpu profile to path")

	rootCmd.PersistentFlags().StringVarP(&networkGenesisVersionModifier, "modifier", "m", "", "Override Genesis Version Modifier (eg 'v1')")
}

var networkBuildCmd = &cobra.Command{
	Use:   "build",
	Short: "Build network deployment artifacts",
	Long:  `Build network deployment artifacts for modifying before deploying`,
	Run: func(cmd *cobra.Command, args []string) {
		// Similar to `goal network create`, we need to generate a genesis.json and wallets and store somewhere.
		// We have a lot more parameters to define, so we'll support a subset of parameters on cmdline but
		// support a config file with all parameters for complex configurations.

		err := runBuildNetwork()
		if err != nil {
			reportErrorf("error building network files: %v\n", err)
		}
	},
}

func runBuildNetwork() error {
	if cpuprofilePath != "" {
		f, err := os.Create(cpuprofilePath)
		if err != nil {
			log.Fatalf("%s: could not create CPU profile, %v", cpuprofilePath, err)
		}
		defer f.Close() // error handling omitted for example
		if err := pprof.StartCPUProfile(f); err != nil {
			log.Fatalf("%s: could not start CPU profile, %v", cpuprofilePath, err)
		}
		defer pprof.StopCPUProfile()
	}

	networkRootDir, err := filepath.Abs(networkRootDir)
	if err != nil {
		return err
	}
	// Make sure target directory doesn't already exist
	exists := util.FileExists(networkRootDir)
	if exists {
		if !networkIgnoreExistingDir {
			return fmt.Errorf(errDirectoryAlreadyExists, networkRootDir)
		}

		// If directory exists but we're not reusing its files, delete it.
		if !networkUseGenesisFiles {
			os.RemoveAll(networkRootDir)
		}
	}

	if networkRecipeFile, err = filepath.Abs(networkRecipeFile); err != nil {
		return err
	}

	var r recipe
	if err = codecs.LoadObjectFromFile(networkRecipeFile, &r); err != nil {
		return fmt.Errorf("unable to parse recipe file '%s' : %v", networkRecipeFile, err)
	}

	templateBaseDir := filepath.Dir(networkRecipeFile)

	configFile := resolveFile(r.ConfigFile, templateBaseDir)

	buildConfig, err := remote.LoadBuildConfig(configFile)
	if err != nil {
		return fmt.Errorf("error loading Build Config file: %v", err)
	}
	for _, kev := range miscStringStringTokens {
		k, v, _ := strings.Cut(kev, "=")
		buildConfig.MiscStringString = append(buildConfig.MiscStringString, "{{"+k+"}}", v)
	}

	networkTemplateFile := resolveFile(r.NetworkFile, templateBaseDir)
	networkTemplateFile, err = filepath.Abs(networkTemplateFile)
	if err != nil {
		return fmt.Errorf("error resolving network template file '%s' to full path: %v", networkTemplateFile, err)
	}

	netCfg, err := remote.InitDeployedNetworkConfig(networkTemplateFile, buildConfig)
	if err != nil {
		return fmt.Errorf("error loading Network Config file '%s': %v", networkTemplateFile, err)
	}

	genesisDataFile := resolveFile(r.GenesisFile, templateBaseDir)
	topologyFile := resolveFile(r.TopologyFile, templateBaseDir)
	net, err := netCfg.ResolveDeployedNetworkConfig(genesisDataFile, topologyFile)
	if err != nil {
		return fmt.Errorf("error resolving Network Config file: %v", err)
	}

	// If network name specified, use that
	if networkName != "" {
		buildConfig.NetworkName = networkName
		net.GenesisData.NetworkName = networkName
	}

	if networkGenesisVersionModifier != "" {
		net.GenesisData.VersionModifier = networkGenesisVersionModifier
	}

	var bootstrappedFile string
	if r.BootstrappedFile != "" {
		bootstrappedFile = resolveFile(r.BootstrappedFile, templateBaseDir)
	}
	if util.FileExists(bootstrappedFile) && bootstrapLoadingFile {
		fileTemplate, err := remote.LoadBootstrappedData(bootstrappedFile)
		if err != nil {
			return fmt.Errorf("error resolving bootstrap file: %v", err)
		}
		net.BootstrappedNet = fileTemplate
		net.SetUseBoostrappedFiles(bootstrapLoadingFile)
	} else {
		net.SetUseBoostrappedFiles(false)
	}

	net.SetUseExistingGenesisFiles(networkUseGenesisFiles)
	err = net.Validate(buildConfig, networkRootDir)
	if err != nil {
		return fmt.Errorf("error validating Network Config file: %v", err)
	}

	hostTemplatesFile := resolveFile(r.HostTemplatesFile, templateBaseDir)
	hostTemplates, err := remote.LoadHostTemplates(hostTemplatesFile)
	if err != nil {
		return fmt.Errorf("error loading HostTemplates file '%s': %v", hostTemplatesFile, err)
	}

	err = net.ValidateTopology(hostTemplates)
	if err != nil {
		return fmt.Errorf("error validating Topology: %v", err)
	}

	// OK, everything has been validated.  Time to generate.

	defer func() {
		if err != nil {
			os.RemoveAll(networkRootDir) // Don't leave partial network directory if create failed
		}
	}()

	err = net.BuildNetworkFromTemplate(buildConfig, networkRootDir)
	if err != nil {
		return fmt.Errorf(errorCreateNetwork, err)
	}

	// Write the processed template file (without genesisdata / topology instances)
	netCfg.SaveToDisk(networkRootDir)

	err = net.GenerateCloudTemplate(hostTemplates, networkRootDir)
	if err != nil {
		return fmt.Errorf("error generating cloud template file: %v", err)
	}

	reportInfof(infoNetworkCreated, networkName, networkRootDir)

	return nil
}

func resolveFile(filename string, baseDir string) string {
	if filepath.IsAbs(filename) {
		return filename
	}
	// Assume path is relative to the directory of the template file
	return filepath.Join(baseDir, filename)
}