code.vegaprotocol.io/vega@v0.79.0/visor/init.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package visor
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path"
    22  
    23  	"code.vegaprotocol.io/vega/logging"
    24  	"code.vegaprotocol.io/vega/visor/config"
    25  	"code.vegaprotocol.io/vega/visor/utils"
    26  )
    27  
    28  func Init(log *logging.Logger, homeFolder string, withDataNode bool) error {
    29  	homePath, err := utils.AbsPath(homeFolder)
    30  	if err != nil {
    31  		return fmt.Errorf("failed to get absolute path for %q: %w", homeFolder, err)
    32  	}
    33  
    34  	homeExists, err := utils.PathExists(homePath)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	if homeExists {
    40  		return fmt.Errorf("home folder %q already exists", homePath)
    41  	}
    42  
    43  	visorConf := config.DefaultVisorConfig(log, homePath)
    44  
    45  	log.Info("Initiating genesis folder")
    46  	if err := initDefaultFolder(visorConf.GenesisFolder(), "genesis", withDataNode); err != nil {
    47  		return err
    48  	}
    49  
    50  	log.Info("Initiating vX.X.X upgrade folder")
    51  	if err := initDefaultFolder(visorConf.UpgradeFolder("vX.X.X"), "vX.X.X", withDataNode); err != nil {
    52  		return err
    53  	}
    54  
    55  	log.Info("Saving default config file")
    56  	if err := visorConf.WriteToFile(); err != nil {
    57  		return fmt.Errorf("failed to write config to file: %w", err)
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func initDefaultFolder(folderPath, name string, withDataNode bool) error {
    64  	if err := os.MkdirAll(folderPath, 0o755); err != nil {
    65  		return fmt.Errorf("failed to create %q folder: %w", name, err)
    66  	}
    67  
    68  	if err := config.ExampleRunConfig(name, withDataNode).WriteToFile(path.Join(folderPath, config.RunConfigFileName)); err != nil {
    69  		return fmt.Errorf("failed to write example config file for %q: %w", name, err)
    70  	}
    71  
    72  	return nil
    73  }