mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-02-21 10:27:26 +08:00
update commands
This commit is contained in:
parent
7980450e2c
commit
c0141da756
@ -3,6 +3,8 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"source.quilibrium.com/quilibrium/monorepo/client/utils"
|
||||
@ -31,6 +33,33 @@ Example: qclient link`,
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Check if the current executable is in the expected location
|
||||
expectedPrefix := utils.ClientDataPath
|
||||
|
||||
// Check if the current executable is in the expected location
|
||||
if !strings.HasPrefix(execPath, expectedPrefix) {
|
||||
fmt.Printf("Current executable is not in the expected location: %s\n", execPath)
|
||||
fmt.Printf("Expected location should start with: %s\n", expectedPrefix)
|
||||
|
||||
// Ask user if they want to move it
|
||||
fmt.Print("Would you like to move the executable to the standard location? (y/n): ")
|
||||
var response string
|
||||
fmt.Scanln(&response)
|
||||
|
||||
if strings.ToLower(response) == "y" || strings.ToLower(response) == "yes" {
|
||||
if err := moveExecutableToStandardLocation(execPath); err != nil {
|
||||
return fmt.Errorf("failed to move executable: %w", err)
|
||||
}
|
||||
// Update execPath to the new location
|
||||
execPath, err = os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get new executable path: %w", err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Continuing with current location...")
|
||||
}
|
||||
}
|
||||
|
||||
// Create the symlink (handles existing symlinks)
|
||||
if err := utils.CreateSymlink(execPath, symlinkPath); err != nil {
|
||||
return err
|
||||
@ -41,6 +70,31 @@ Example: qclient link`,
|
||||
},
|
||||
}
|
||||
|
||||
func moveExecutableToStandardLocation(execPath string) error {
|
||||
// Get the directory of the current executable
|
||||
version, err := GetVersionInfo(false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get version info: %w", err)
|
||||
}
|
||||
destDir := filepath.Join(utils.ClientDataPath, "bin", version.Version)
|
||||
|
||||
// Create the standard location directory if it doesn't exist
|
||||
currentUser, err := utils.GetCurrentSudoUser()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get current user: %w", err)
|
||||
}
|
||||
if err := utils.ValidateAndCreateDir(destDir, currentUser); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
// Move the executable to the standard location
|
||||
if err := os.Rename(execPath, filepath.Join(destDir, StandardizedQClientFileName)); err != nil {
|
||||
return fmt.Errorf("failed to move executable: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(linkCmd)
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ var (
|
||||
NodeUser *user.User
|
||||
ConfigDirs string
|
||||
NodeConfigToRun string
|
||||
SetDefault bool
|
||||
)
|
||||
|
||||
// ConfigCmd represents the node config command
|
||||
@ -47,7 +48,12 @@ This command provides utilities for configuring your Quilibrium node, such as:
|
||||
}
|
||||
|
||||
func init() {
|
||||
importCmd.Flags().BoolVarP(&SetDefault, "default", "d", false, "Select this config as the default")
|
||||
ConfigCmd.AddCommand(importCmd)
|
||||
|
||||
ConfigCmd.AddCommand(SwitchConfigCmd)
|
||||
|
||||
createCmd.Flags().BoolVarP(&SetDefault, "default", "d", false, "Select this config as the default")
|
||||
ConfigCmd.AddCommand(createCmd)
|
||||
ConfigCmd.AddCommand(setCmd)
|
||||
}
|
||||
|
||||
@ -10,8 +10,6 @@ import (
|
||||
"source.quilibrium.com/quilibrium/monorepo/client/utils"
|
||||
)
|
||||
|
||||
var setDefault bool
|
||||
|
||||
var createCmd = &cobra.Command{
|
||||
Use: "create [name]",
|
||||
Short: "Create a default configuration file set for a node",
|
||||
@ -87,7 +85,7 @@ The third example will create a new configuration at %s/myconfig and symlink it
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if setDefault {
|
||||
if SetDefault {
|
||||
// Create the symlink
|
||||
if err := utils.CreateSymlink(configDir, NodeConfigToRun); err != nil {
|
||||
fmt.Printf("Failed to create symlink: %s\n", err)
|
||||
@ -101,7 +99,3 @@ The third example will create a new configuration at %s/myconfig and symlink it
|
||||
fmt.Println("The keys.yml file will only contain 'null:' until the node is started.")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
createCmd.Flags().BoolVarP(&setDefault, "default", "d", false, "Select this config as the default")
|
||||
}
|
||||
|
||||
@ -59,6 +59,20 @@ This will copy config.yml and keys.yml from /path/to/source to /home/quilibrium/
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Successfully imported config files to %s\n", targetDir)
|
||||
if SetDefault {
|
||||
// Create the symlink
|
||||
if err := utils.CreateSymlink(targetDir, NodeConfigToRun); err != nil {
|
||||
fmt.Printf("Failed to create symlink: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Successfully imported config files to %s and symlinked to default\n", name)
|
||||
} else {
|
||||
fmt.Printf("Successfully imported config files to %s\n", targetDir)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
}
|
||||
|
||||
@ -80,7 +80,3 @@ Example:
|
||||
fmt.Printf("Successfully updated %s to %s in %s\n", key, value, configFile)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ConfigCmd.AddCommand(setCmd)
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
// installCmd represents the command to install the Quilibrium node
|
||||
var installCmd = &cobra.Command{
|
||||
var InstallCmd = &cobra.Command{
|
||||
Use: "install [version]",
|
||||
Short: "Install Quilibrium node",
|
||||
Long: `Install Quilibrium node binary and create a service to run it.
|
||||
@ -136,17 +136,12 @@ Examples:
|
||||
fmt.Fprintf(os.Stdout, "Installing Quilibrium node for %s-%s, version: %s\n", osType, arch, version)
|
||||
|
||||
// Install the node
|
||||
installNode(version)
|
||||
InstallNode(version)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Add the install command to the node command
|
||||
NodeCmd.AddCommand(installCmd)
|
||||
}
|
||||
|
||||
// installNode installs the Quilibrium node
|
||||
func installNode(version string) {
|
||||
func InstallNode(version string) {
|
||||
// Create installation directory
|
||||
if err := utils.ValidateAndCreateDir(utils.NodeDataPath, NodeUser); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating installation directory: %v\n", err)
|
||||
@ -158,7 +153,7 @@ func installNode(version string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := installByVersion(version); err != nil {
|
||||
if err := InstallByVersion(version); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error installing specific version: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@ -169,7 +164,7 @@ func installNode(version string) {
|
||||
}
|
||||
|
||||
// installByVersion installs a specific version of the Quilibrium node
|
||||
func installByVersion(version string) error {
|
||||
func InstallByVersion(version string) error {
|
||||
|
||||
versionDir := filepath.Join(utils.NodeDataPath, version)
|
||||
if err := utils.ValidateAndCreateDir(versionDir, NodeUser); err != nil {
|
||||
|
||||
@ -66,7 +66,7 @@ func init() {
|
||||
NodeCmd.PersistentFlags().StringVar(&configDirectory, "config", ".config", "config directory (default is .config/)")
|
||||
|
||||
// Add subcommands
|
||||
NodeCmd.AddCommand(installCmd)
|
||||
NodeCmd.AddCommand(InstallCmd)
|
||||
NodeCmd.AddCommand(clientNodeConfig.ConfigCmd)
|
||||
NodeCmd.AddCommand(updateNodeCmd)
|
||||
NodeCmd.AddCommand(nodeServiceCmd)
|
||||
|
||||
@ -3,8 +3,6 @@ package node
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"source.quilibrium.com/quilibrium/monorepo/client/utils"
|
||||
@ -30,6 +28,23 @@ Examples:
|
||||
// Determine version to install
|
||||
version := determineVersion(args)
|
||||
|
||||
// Download and install the node
|
||||
if version == "latest" {
|
||||
latestVersion, err := utils.GetLatestVersion(utils.ReleaseTypeNode)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error getting latest version: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
version = latestVersion
|
||||
fmt.Fprintf(os.Stdout, "Found latest version: %s\n", version)
|
||||
}
|
||||
|
||||
if IsExistingNodeVersion(version) {
|
||||
fmt.Fprintf(os.Stderr, "Error: Node version %s already exists\n", version)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stdout, "Updating Quilibrium node for %s-%s, version: %s\n", OsType, Arch, version)
|
||||
|
||||
// Update the node
|
||||
@ -49,43 +64,5 @@ func updateNode(version string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Create new binary version directory
|
||||
versionDataDir := filepath.Join(utils.NodeDataPath, version)
|
||||
if err := utils.ValidateAndCreateDir(versionDataDir, nil); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating data directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Construct the expected filename for the specified version
|
||||
// Remove 'v' prefix if present for filename construction
|
||||
versionWithoutV := strings.TrimPrefix(version, "v")
|
||||
|
||||
if IsExistingNodeVersion(versionWithoutV) {
|
||||
fmt.Fprintf(os.Stderr, "Error: Node version %s already exists\n", versionWithoutV)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Download the release directly
|
||||
err := utils.DownloadRelease(utils.ReleaseTypeNode, versionWithoutV)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error downloading version %s: %v\n", version, err)
|
||||
fmt.Fprintf(os.Stderr, "The specified version %s does not exist for %s-%s\n", version, OsType, Arch)
|
||||
// Clean up the created directories since installation failed
|
||||
os.RemoveAll(versionDataDir)
|
||||
return
|
||||
}
|
||||
|
||||
// Download signature files
|
||||
if err := utils.DownloadReleaseSignatures(utils.ReleaseTypeNode, versionWithoutV); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: Failed to download signature files: %v\n", err)
|
||||
fmt.Fprintf(os.Stdout, "Continuing with installation...\n")
|
||||
}
|
||||
|
||||
// Ensure log rotation is set up
|
||||
if err := setupLogRotation(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: Failed to set up log rotation: %v\n", err)
|
||||
}
|
||||
|
||||
// Successfully downloaded the specific version
|
||||
finishInstallation(version)
|
||||
InstallNode(version)
|
||||
}
|
||||
|
||||
@ -72,11 +72,13 @@ var versionCmd = &cobra.Command{
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("qclient %s\n", info.Version)
|
||||
fmt.Printf("%s\n", info.Version)
|
||||
|
||||
if info.SHA256 != "" && info.MD5 != "" {
|
||||
fmt.Printf("SHA256: %s\n", info.SHA256)
|
||||
fmt.Printf("MD5: %s\n", info.MD5)
|
||||
if showChecksum {
|
||||
if info.SHA256 != "" && info.MD5 != "" {
|
||||
fmt.Printf("SHA256: %s\n", info.SHA256)
|
||||
fmt.Printf("MD5: %s\n", info.MD5)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@ -13,9 +13,6 @@ import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// DefaultNodeUser is the default user name for node operations
|
||||
var DefaultNodeUser = "quilibrium"
|
||||
|
||||
var ClientInstallPath = filepath.Join("/opt/quilibrium/", string(ReleaseTypeQClient))
|
||||
var RootQuilibriumPath = filepath.Join("/var/quilibrium/")
|
||||
var BinaryPath = filepath.Join(RootQuilibriumPath, "bin")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user