Skip to content

Commit

Permalink
feat: adds node start and node stop commands
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Nov 6, 2024
1 parent 633a2e4 commit fb3c471
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 75 deletions.
252 changes: 177 additions & 75 deletions cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"runtime"
"time"

"github.com/algorandfoundation/hack-tui/ui/style"
"github.com/manifoldco/promptui"
Expand Down Expand Up @@ -35,14 +36,33 @@ var configureCmd = &cobra.Command{
},
}

var startCmd = &cobra.Command{
Use: "start",
Short: "Start Algod",
Long: "Start Algod on your system (the one on your PATH).",
Run: func(cmd *cobra.Command, args []string) {
startNode()
},
}

var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop Algod",
Long: "Stop the Algod process on your system.",
Run: func(cmd *cobra.Command, args []string) {
stopNode()
},
}

func init() {
rootCmd.AddCommand(nodeCmd)
nodeCmd.AddCommand(installCmd)
nodeCmd.AddCommand(configureCmd)
nodeCmd.AddCommand(startCmd)
nodeCmd.AddCommand(stopCmd)
}

func installNode() {

fmt.Println("Checking if Algod is installed...")

// Check if Algod is installed
Expand All @@ -65,7 +85,102 @@ func installNode() {

}

// TODO: Instead of just setting as env variable in the current terminal, set it in algorun.yaml
func installNodeLinux() {
fmt.Println("Installing Algod on Linux")

var installCmd string
var postInstallHint string

// Based off of https://developer.algorand.org/docs/run-a-node/setup/install/#installation-with-a-package-manager

if checkCmdToolExists("apt") { // On Ubuntu and Debian we use the apt package manager
installCmd = `sudo apt update
sudo apt install -y gnupg2 curl software-properties-common
curl -o - https://releases.algorand.com/key.pub | sudo tee /etc/apt/trusted.gpg.d/algorand.asc
sudo add-apt-repository "deb [arch=amd64] https://releases.algorand.com/deb/ stable main"
sudo apt update
# To get both algorand and the devtools:
sudo apt install -y algorand-devtools`
} else if checkCmdToolExists("apt-get") { // On some Debian systems we use apt-get
installCmd = `sudo apt-get update
sudo apt-get install -y gnupg2 curl software-properties-common
curl -o - https://releases.algorand.com/key.pub | sudo tee /etc/apt/trusted.gpg.d/algorand.asc
sudo add-apt-repository "deb [arch=amd64] https://releases.algorand.com/deb/ stable main"
sudo apt-get update
# To get both algorand and the devtools:
sudo apt-get install -y algorand-devtools`
}

// On Fedora and CentOs8 there's the dnf package manager
if checkCmdToolExists("dnf") {
installCmd = `curl -O https://releases.algorand.com/rpm/rpm_algorand.pub
sudo rpmkeys --import rpm_algorand.pub
dnf install -y 'dnf-command(config-manager)'
dnf config-manager --add-repo=https://releases.algorand.com/rpm/stable/algorand.repo
dnf install algorand`
} else if checkCmdToolExists("yum") { // On CentOs7 we use the yum package manager
installCmd = `curl -O https://releases.algorand.com/rpm/rpm_algorand.pub
sudo rpmkeys --import rpm_algorand.pub
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://releases.algorand.com/rpm/stable/algorand.repo
# To get both algorand and the devtools:
sudo yum install algorand-devtools
`
}

if installCmd == "" {
fmt.Println("Unsupported package manager, possibly due to non-Debian or non-Red Hat based Linux distribution. Will attempt to install using updater script.")
// https://developer.algorand.org/docs/run-a-node/setup/install/#installing-on-linux
installCmd = `mkdir ~/node
cd ~/node
wget https://raw.githubusercontent.com/algorand/go-algorand/rel/stable/cmd/updater/update.sh
chmod 744 update.sh
./update.sh -i -c stable -p ~/node -d ~/node/data -n`

postInstallHint = `You may need to add the Algorand binaries to your PATH:
export ALGORAND_DATA="$HOME/node/data"
export PATH="$HOME/node:$PATH"
`
}

// Run the installation command
err := exec.Command("/bin/sh", "-c", installCmd).Run()
cobra.CheckErr(err)

if postInstallHint != "" {
fmt.Println(postInstallHint)
}
}

func installNodeMac() {
fmt.Println("Installing Algod on macOS...")

// Based off of the macOS installation instructions
// https://developer.algorand.org/docs/run-a-node/setup/install/#installing-on-mac

installCmd := `mkdir ~/node
cd ~/node
wget https://raw.githubusercontent.com/algorand/go-algorand/rel/stable/cmd/updater/update.sh
chmod 744 update.sh
./update.sh -i -c stable -p ~/node -d ~/node/data -n`

postInstallHint := `You may need to add the Algorand binaries to your PATH:
export ALGORAND_DATA="$HOME/node/data"
export PATH="$HOME/node:$PATH"
`

// Run the installation command
err := exec.Command("/bin/sh", "-c", installCmd).Run()
cobra.CheckErr(err)

if postInstallHint != "" {
fmt.Println(postInstallHint)
}
}

func configureNode() {
fmt.Println("Configuring Data directory for algod started through Algorun...")

Expand Down Expand Up @@ -168,98 +283,85 @@ func configureNode() {
os.Exit(0)
}

func installNodeLinux() {
fmt.Println("Installing Algod on Linux")
// Start Algod on your system (the one on your PATH).
func startNode() {
fmt.Println("Attempting to start Algod...")

var installCmd string
var postInstallHint string

// Based off of https://developer.algorand.org/docs/run-a-node/setup/install/#installation-with-a-package-manager
if !isAlgodInstalled() {
fmt.Println("Algod is not installed. Please run the node install command.")
os.Exit(1)
}

if checkCmdToolExists("apt") { // On Ubuntu and Debian we use the apt package manager
installCmd = `sudo apt update
sudo apt install -y gnupg2 curl software-properties-common
curl -o - https://releases.algorand.com/key.pub | sudo tee /etc/apt/trusted.gpg.d/algorand.asc
sudo add-apt-repository "deb [arch=amd64] https://releases.algorand.com/deb/ stable main"
sudo apt update
// Check if Algod is already running
if isAlgodRunning() {
fmt.Println("Algod is already running.")
os.Exit(0)
}

# To get both algorand and the devtools:
sudo apt install -y algorand-devtools`
} else if checkCmdToolExists("apt-get") { // On some Debian systems we use apt-get
installCmd = `sudo apt-get update
sudo apt-get install -y gnupg2 curl software-properties-common
curl -o - https://releases.algorand.com/key.pub | sudo tee /etc/apt/trusted.gpg.d/algorand.asc
sudo add-apt-repository "deb [arch=amd64] https://releases.algorand.com/deb/ stable main"
sudo apt-get update
startAlgodProcess()
}

# To get both algorand and the devtools:
sudo apt-get install -y algorand-devtools`
func isAlgodRunning() bool {
// Check if Algod is already running
err := exec.Command("pgrep", "algod").Run()
if err != nil {
return false
}
return true
}

// On Fedora and CentOs8 there's the dnf package manager
if checkCmdToolExists("dnf") {
installCmd = `curl -O https://releases.algorand.com/rpm/rpm_algorand.pub
sudo rpmkeys --import rpm_algorand.pub
dnf install -y 'dnf-command(config-manager)'
dnf config-manager --add-repo=https://releases.algorand.com/rpm/stable/algorand.repo
dnf install algorand`
} else if checkCmdToolExists("yum") { // On CentOs7 we use the yum package manager
installCmd = `curl -O https://releases.algorand.com/rpm/rpm_algorand.pub
sudo rpmkeys --import rpm_algorand.pub
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://releases.algorand.com/rpm/stable/algorand.repo
func startAlgodProcess() {
// Check if ALGORAND_DATA environment variable is set
fmt.Println("Checking if ALGORAND_DATA env var is set...")
algorandData := os.Getenv("ALGORAND_DATA")

# To get both algorand and the devtools:
sudo yum install algorand-devtools
`
if !validateAlgorandDataDir(algorandData) {
fmt.Println("ALGORAND_DATA environment variable is not set or is invalid. Please run node configure and follow the instructions.")
os.Exit(1)
}

if installCmd == "" {
fmt.Println("Unsupported package manager, possibly due to non-Debian or non-Red Hat based Linux distribution. Will attempt to install using updater script.")
// https://developer.algorand.org/docs/run-a-node/setup/install/#installing-on-linux
installCmd = `mkdir ~/node
cd ~/node
wget https://raw.githubusercontent.com/algorand/go-algorand/rel/stable/cmd/updater/update.sh
chmod 744 update.sh
./update.sh -i -c stable -p ~/node -d ~/node/data -n`
fmt.Println("ALGORAND_DATA env var set to valid directory: " + algorandData)

postInstallHint = `You may need to add the Algorand binaries to your PATH:
export ALGORAND_DATA="$HOME/node/data"
export PATH="$HOME/node:$PATH"
`
// Start algod in the background using nohup and &
cmd := exec.Command("sh", "-c", "nohup algod > /dev/null 2>&1 &")
err := cmd.Start()
if err != nil {
fmt.Printf("Failed to start algod: %v\n", err)
os.Exit(1)
}

// Run the installation command
err := exec.Command("/bin/sh", "-c", installCmd).Run()
cobra.CheckErr(err)
time.Sleep(5 * time.Second)

if postInstallHint != "" {
fmt.Println(postInstallHint)
if isAlgodRunning() {
fmt.Println("Algod is running.")
} else {
fmt.Println("Algod failed to start.")
}
}

func installNodeMac() {
fmt.Println("Installing Algod on macOS...")
// Stop the Algod process on your system.
func stopNode() {
fmt.Println("Attempting to stop Algod...")

// Based off of the macOS installation instructions
// https://developer.algorand.org/docs/run-a-node/setup/install/#installing-on-mac

installCmd := `mkdir ~/node
cd ~/node
wget https://raw.githubusercontent.com/algorand/go-algorand/rel/stable/cmd/updater/update.sh
chmod 744 update.sh
./update.sh -i -c stable -p ~/node -d ~/node/data -n`
if !isAlgodRunning() {
fmt.Println("Algod was not running.")
os.Exit(0)
}

postInstallHint := `You may need to add the Algorand binaries to your PATH:
export ALGORAND_DATA="$HOME/node/data"
export PATH="$HOME/node:$PATH"
`
stopAlgodProcess()

// Run the installation command
err := exec.Command("/bin/sh", "-c", installCmd).Run()
cobra.CheckErr(err)
time.Sleep(5 * time.Second)

if postInstallHint != "" {
fmt.Println(postInstallHint)
if !isAlgodRunning() {
fmt.Println("Algod is no longer running.")
os.Exit(0)
}

fmt.Println("Failed to stop Algod.")
os.Exit(1)
}

func stopAlgodProcess() {
err := exec.Command("pkill", "algod").Run()
cobra.CheckErr(err)
}
4 changes: 4 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func findPathToFile(startDir string, targetFileName string) []string {
}

func validateAlgorandDataDir(path string) bool {
if path == "" {
return false
}

paths := findPathToFile(path, "algod.token")
if len(paths) == 1 {
return true
Expand Down

0 comments on commit fb3c471

Please sign in to comment.