Skip to content

Commit

Permalink
refactor: algod commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 12, 2024
1 parent 4f30b6a commit bae2d6f
Show file tree
Hide file tree
Showing 20 changed files with 1,280 additions and 1,241 deletions.
48 changes: 25 additions & 23 deletions cmd/node/configure.go → cmd/configure/configure.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package node
package configure

import (
"bytes"
"fmt"
"github.com/algorandfoundation/algorun-tui/cmd/node"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/utils"
"os"
"os/exec"
"runtime"
Expand All @@ -12,35 +15,34 @@ import (
"github.com/spf13/cobra"
)

var configureCmd = &cobra.Command{
Use: "configure",
Short: "Configure Algod",
Long: "Configure Algod settings",
Run: func(cmd *cobra.Command, args []string) {
configureNode()
},
var Cmd = &cobra.Command{
Use: "configure",
Short: "Configure Algod",
Long: "Configure Algod settings",
SilenceUsage: true,
PersistentPreRunE: node.NeedsToBeStopped,
//RunE: func(cmd *cobra.Command, args []string) error {
// return configureNode()
//},
}

// TODO: configure not just data directory but algod path
func configureNode() {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
panic("Unsupported OS: " + runtime.GOOS)
}
func init() {
Cmd.AddCommand(serviceCmd)
}

const ConfigureRunningErrorMsg = "algorand is currently running. Please stop the node with *node stop* before configuring"

// TODO: configure not just data directory but algod path
func configureNode() error {
var systemServiceConfigure bool

if !isRunningWithSudo() {
fmt.Println("This command must be run with super-user priviledges (sudo).")
os.Exit(1)
if algod.IsRunning() {
return fmt.Errorf(ConfigureRunningErrorMsg)
}

// Check systemctl first
if checkAlgorandServiceCreated() {
if algod.IsService() {
if promptWrapperYes("Algorand is installed as a service. Do you wish to edit the service file to change the data directory? (y/n)") {
if checkAlgorandServiceActive() {
fmt.Println("Algorand service is currently running. Please stop the service with *node stop* before editing the service file.")
os.Exit(1)
}
// Edit the service file with the user's new data directory
systemServiceConfigure = true
} else {
Expand Down Expand Up @@ -105,7 +107,7 @@ func configureNode() {
}

// Do quick "lazy" check for existing Algorand Data directories
paths := lazyCheckAlgorandDataDirs()
paths := utils.GetKnownDataPaths()

if len(paths) != 0 {

Expand Down Expand Up @@ -169,7 +171,7 @@ func configureNode() {
} else {
affectALGORAND_DATA(selectedPath)
}
os.Exit(0)
return nil
}

func editAlgorandServiceFile(dataDirectoryPath string) {
Expand Down
26 changes: 26 additions & 0 deletions cmd/configure/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package configure

import (
"errors"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/system"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/spf13/cobra"
)

var serviceCmd = &cobra.Command{
Use: "service",
Short: "Configure the node service",
Long: style.Purple(style.BANNER) + "\n" + style.LightBlue("Configure the service that runs the node."),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !system.IsSudo() {
return errors.New(
"you need to be root to run this command. Please run this command with sudo")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: Combine this with algod.UpdateService and algod.SetNetwork
return algod.EnsureService()
},
}
97 changes: 97 additions & 0 deletions cmd/configure/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package configure

import (
"fmt"
"github.com/algorandfoundation/algorun-tui/internal/system"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"os"
)

type Release struct {
Name string `json:"name"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Commit struct {
Sha string `json:"sha"`
URL string `json:"url"`
} `json:"commit"`
NodeID string `json:"node_id"`
}

// Queries user on the provided prompt and returns the user input
func promptWrapperInput(promptLabel string) string {
prompt := promptui.Prompt{
Label: promptLabel,
}

result, err := prompt.Run()
cobra.CheckErr(err)

return result
}

// Queries user on the provided prompt and returns true if user inputs "y"
func promptWrapperYes(promptLabel string) bool {
return promptWrapperInput(promptLabel) == "y"
}

// Queries user on the provided prompt and returns true if user does not input "y"
// Included for improved readability of decision tree, despite being redundant.
func promptWrapperNo(promptLabel string) bool {
return promptWrapperInput(promptLabel) != "y"
}

// Queries user on the provided prompt and returns the selected item
func promptWrapperSelection(promptLabel string, items []string) string {
prompt := promptui.Select{
Label: promptLabel,
Items: items,
}

_, result, err := prompt.Run()
cobra.CheckErr(err)

fmt.Printf("You selected: %s\n", result)

return result
}

// TODO: consider replacing with a method that does more for the user
func affectALGORAND_DATA(path string) {
fmt.Println("Please execute the following in your terminal to set the environment variable:")
fmt.Println("")
fmt.Println("export ALGORAND_DATA=" + path)
fmt.Println("")
}

func validateAlgorandDataDir(path string) bool {
info, err := os.Stat(path)

// Check if the path exists
if os.IsNotExist(err) {
return false
}

// Check if the path is a directory
if !info.IsDir() {
return false
}

paths := system.FindPathToFile(path, "algod.token")
if len(paths) == 1 {
return true
}
return false
}

// Checks if Algorand data directories exist, based off of existence of the "algod.token" file
func deepSearchAlgorandDataDirs() []string {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// TODO: consider a better way to identify an Algorand data directory
paths := system.FindPathToFile(home, "algod.token")

return paths
}
25 changes: 25 additions & 0 deletions cmd/node/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package node

import (
"fmt"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/utils"
"github.com/algorandfoundation/algorun-tui/internal/system"
"github.com/spf13/cobra"
)

var debugCmd = &cobra.Command{
Use: "debug",
Short: "Display debug information for developers",
Long: "Prints debug data to be copy and pasted to a bug report.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
paths := utils.GetKnownDataPaths()
fmt.Printf("Algod in PATH: %v\n", system.CmdExists("algod"))
fmt.Printf("Algod is installed: %v\n", algod.IsInstalled())
fmt.Printf("Algod is running: %v\n", algod.IsRunning())
fmt.Printf("Algod is service: %v\n", algod.IsService())
fmt.Printf("Algod paths: %+v\n", paths)
return nil
},
}
Loading

0 comments on commit bae2d6f

Please sign in to comment.