Skip to content

Commit

Permalink
fix faucet script
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Nov 22, 2024
1 parent 61ed28d commit e3e8cb5
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 45 deletions.
34 changes: 27 additions & 7 deletions spaceship/cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func ExecuteSSHStatus(ctx context.Context, cmd *plugin.ExecutedCommand, chain *p
}
_ = session.Println(chainStatus)

if !c.HasFaucetScript(ctx) {
return ErrServerNotInitialized
}

stopStatus, err := c.FaucetStatus(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -70,7 +74,15 @@ func ExecuteSSHRestart(ctx context.Context, cmd *plugin.ExecutedCommand, chain *
}
_ = session.Println(chainRestart)

faucetRestart, err := c.FaucetRestart(ctx)
if !c.HasFaucetScript(ctx) {
return ErrServerNotInitialized
}

faucetPort, err := faucetPort(cmd.Flags)
if err != nil {
return err
}
faucetRestart, err := c.FaucetRestart(ctx, faucetPort)
if err != nil {
return err
}
Expand Down Expand Up @@ -99,6 +111,10 @@ func ExecuteSSHSStop(ctx context.Context, cmd *plugin.ExecutedCommand, chain *pl
}
_ = session.Println(chainStop)

if !c.HasFaucetScript(ctx) {
return ErrServerNotInitialized
}

faucetStop, err := c.FaucetStop(ctx)
if err != nil {
return err
Expand All @@ -123,9 +139,8 @@ func ExecuteSSHDeploy(ctx context.Context, cmd *plugin.ExecutedCommand, chain *p
}()

var (
initChain, _ = flags.GetBool(flagInitChain)
faucet, _ = flags.GetBool(flagFaucet)
faucetPort, _ = flags.GetUint64(flagFaucetPort)
initChain, _ = flags.GetBool(flagInitChain)
faucet, _ = flags.GetBool(flagFaucet)

localChainHome = filepath.Join(localDir, "home")
localBinOutput = filepath.Join(localDir, "bin")
Expand Down Expand Up @@ -198,12 +213,12 @@ func ExecuteSSHDeploy(ctx context.Context, cmd *plugin.ExecutedCommand, chain *p
}

bar.Describe("Uploading chain binary")
binPath, err := c.UploadBinary(extracted[0], progressCallback)
chainBinPath, err := c.UploadBinary(extracted[0], progressCallback)
if err != nil {
return err
}
_ = session.Println()
_ = session.Println(color.Yellow.Sprintf("Chain binary uploaded to '%s'\n", binPath))
_ = session.Println(color.Yellow.Sprintf("Chain binary uploaded to '%s'\n", chainBinPath))

bar.Describe("Uploading faucet binary")
faucetBin, err := c.UploadFaucetBinary(ctx, targetName, progressCallback)
Expand Down Expand Up @@ -252,7 +267,8 @@ func ExecuteSSHDeploy(ctx context.Context, cmd *plugin.ExecutedCommand, chain *p
c.Workspace(),
c.Log(),
home,
binPath,
c.Bin(),
chainBinPath,
faucetBin,
*chainCfg.Faucet.Name,
denom,
Expand All @@ -276,6 +292,10 @@ func ExecuteSSHDeploy(ctx context.Context, cmd *plugin.ExecutedCommand, chain *p

if faucet {
_ = session.Println(color.Yellow.Sprintf("Running chain %s faucet", chain.ChainId))
faucetPort, err := faucetPort(cmd.Flags)
if err != nil {
return err
}
faucetStart, err := c.FaucetStart(ctx, faucetPort)
if err != nil {
return err
Expand Down
21 changes: 9 additions & 12 deletions spaceship/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ func GetCommands() []*plugin.Command {
DefaultValue: "false",
},
&plugin.Flag{
Name: flagFaucetPort,
Usage: "chain faucet port",
Type: plugin.FlagTypeUint64,
DefaultValue: "8009",
Name: flagFaucetPort,
Usage: "chain faucet port",
Type: plugin.FlagTypeUint64,
},
),
},
Expand Down Expand Up @@ -142,10 +141,9 @@ func GetCommands() []*plugin.Command {
Short: "start the faucet",
Flags: append(defaultFlags,
&plugin.Flag{
Name: flagFaucetPort,
Usage: "chain faucet port",
Type: plugin.FlagTypeUint64,
DefaultValue: "8009",
Name: flagFaucetPort,
Usage: "chain faucet port",
Type: plugin.FlagTypeUint64,
},
),
},
Expand All @@ -154,10 +152,9 @@ func GetCommands() []*plugin.Command {
Short: "restart the faucet",
Flags: append(defaultFlags,
&plugin.Flag{
Name: flagFaucetPort,
Usage: "chain faucet port",
Type: plugin.FlagTypeUint64,
DefaultValue: "8009",
Name: flagFaucetPort,
Usage: "chain faucet port",
Type: plugin.FlagTypeUint64,
},
),
},
Expand Down
41 changes: 38 additions & 3 deletions spaceship/cmd/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,39 @@ package cmd
import (
"context"

"github.com/ignite/cli/v28/ignite/pkg/availableport"
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/services/plugin"
)

func faucetPort(f []*plugin.Flag) (uint64, error) {
flags := plugin.Flags(f)
port, err := flags.GetUint64(flagFaucetPort)
if err != nil {
return 0, err
}
if port == 0 {
return availablePort()
}
return port, nil
}

func availablePort() (uint64, error) {
ports, err := availableport.Find(
1,
availableport.WithMinPort(8000),
availableport.WithMaxPort(9000),
)
if err != nil {
return 0, err
}
if len(ports) == 0 {
return 0, errors.New("no available ports")
}
return uint64(ports[0]), nil
}

// ExecuteSSHFaucetStatus executes the ssh faucet status subcommand.
func ExecuteSSHFaucetStatus(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
session := cliui.New(cliui.StartSpinnerWithText(statusConnecting))
Expand Down Expand Up @@ -45,8 +74,10 @@ func ExecuteSSHFaucetStart(ctx context.Context, cmd *plugin.ExecutedCommand, cha
return ErrServerNotInitialized
}

flags := plugin.Flags(cmd.Flags)
faucetPort, _ := flags.GetUint64(flagFaucetPort)
faucetPort, err := faucetPort(cmd.Flags)
if err != nil {
return err
}
faucetRestart, err := c.FaucetStart(ctx, faucetPort)
if err != nil {
return err
Expand All @@ -70,7 +101,11 @@ func ExecuteSSHFaucetRestart(ctx context.Context, cmd *plugin.ExecutedCommand, c
return ErrServerNotInitialized
}

faucetRestart, err := c.FaucetRestart(ctx)
faucetPort, err := faucetPort(cmd.Flags)
if err != nil {
return err
}
faucetRestart, err := c.FaucetRestart(ctx, faucetPort)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions spaceship/pkg/ssh/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// faucet returns the path to the faucet script within the workspace.
func (s *SSH) faucet() string {
return filepath.Join(s.bin(), faucet.BinaryName())
return filepath.Join(s.Bin(), faucet.BinaryName())
}

// faucetScript returns the path to the faucet runner script within the workspace.
Expand All @@ -29,8 +29,8 @@ func (s *SSH) FaucetStart(ctx context.Context, port uint64) (string, error) {
}

// FaucetRestart runs the faucet "restart" script on the remote server.
func (s *SSH) FaucetRestart(ctx context.Context) (string, error) {
return s.runFaucetScript(ctx, "restart")
func (s *SSH) FaucetRestart(ctx context.Context, port uint64) (string, error) {
return s.runFaucetScript(ctx, "restart", strconv.FormatUint(port, 10))
}

// FaucetStop runs the faucet "stop" script on the remote server.
Expand Down
8 changes: 4 additions & 4 deletions spaceship/pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ func (s *SSH) auth() (goph.Auth, error) {

// ensureEnvironment ensures that the necessary directories exist on the remote server.
func (s *SSH) ensureEnvironment() error {
if err := s.sftpClient.MkdirAll(s.bin()); err != nil {
return errors.Wrapf(err, "failed to create bin dir %s", s.bin())
if err := s.sftpClient.MkdirAll(s.Bin()); err != nil {
return errors.Wrapf(err, "failed to create bin dir %s", s.Bin())
}
if err := s.sftpClient.MkdirAll(s.Home()); err != nil {
return errors.Wrapf(err, "failed to create home dir %s", s.Home())
Expand All @@ -204,8 +204,8 @@ func (s *SSH) ensureEnvironment() error {
return nil
}

// bin returns the binary directory within the workspace.
func (s *SSH) bin() string {
// Bin returns the binary directory within the workspace.
func (s *SSH) Bin() string {
return filepath.Join(s.Workspace(), "bin")
}

Expand Down
4 changes: 2 additions & 2 deletions spaceship/pkg/ssh/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ func (s *SSH) UploadFile(filePath, dstPath string, progressCallback ProgressCall
return dstPath, nil
}

// UploadBinary uploads a binary file to the remote server's bin directory
// UploadBinary uploads a binary file to the remote server's Bin directory
// and sets the appropriate permissions.
func (s *SSH) UploadBinary(srcPath string, progressCallback ProgressCallback) (string, error) {
var (
filename = filepath.Base(srcPath)
binPath = filepath.Join(s.bin(), filename)
binPath = filepath.Join(s.Bin(), filename)
)
if _, err := s.UploadFile(srcPath, binPath, progressCallback); err != nil {
return "", err
Expand Down
6 changes: 4 additions & 2 deletions spaceship/templates/script/files/faucet.sh.plush
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash

PORT=${2:-8009}
COMMAND="$HOME/<%= faucetPath %> --home <%= home %> --cli-name <%= binary %> --account-name <%= account %> --denoms <%= denoms %> --port $PORT"
COMMAND="$HOME/<%= faucetBinPath %> --home <%= home %> --cli-name <%= binary %> --account-name <%= account %> --denoms <%= denoms %> --port $PORT"
PID_FILE="$HOME/<%= path %>/faucet.pid"

export PATH="$PATH:$HOME/<%= binDirPath %>"

# Function to get the current date and time formatted for the log file
get_log_file_name() {
echo "$HOME/<%= log %>/faucet_$(date '+%Y-%m-%d_%H-%M-%S').log"
Expand All @@ -22,7 +24,7 @@ start() {
LOG_FILE=$(get_log_file_name)
ensure_directory_exists "$LOG_FILE"

echo "Starting faucet..."
echo "Starting faucet on port $PORT..."
nohup $COMMAND > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "faucet started with PID $(cat $PID_FILE)."
Expand Down
19 changes: 11 additions & 8 deletions spaceship/templates/script/files/run.sh.plush
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/bin/bash

HOME_PATH="$HOME/<%= home %>"
COMMAND="$HOME/<%= binaryPath %> start --home $HOME_PATH"
BINARY="<%= binary %>"
COMMAND="$HOME/<%= chainBinPath %> start --home $HOME_PATH"
PID_FILE="$HOME/<%= path %>/spaceship.pid"

export PATH="$PATH:$HOME/<%= binDirPath %>"

# Function to get the current date and time formatted for the log file
get_log_file_name() {
echo "$HOME/<%= log %>/chain_$(date '+%Y-%m-%d_%H-%M-%S').log"
Expand All @@ -25,32 +28,32 @@ start() {
echo "Starting $COMMAND..."
nohup $COMMAND > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "$COMMAND started with PID $(cat $PID_FILE)."
echo "$BINARY started with PID $(cat $PID_FILE)."
echo "Logs are being written to $LOG_FILE"
}

stop() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
echo "Stopping $COMMAND with PID $PID..."
echo "Stopping $BINARY with PID $PID..."
kill "$PID"
rm "$PID_FILE"
echo "$COMMAND stopped."
echo "$BINARY stopped."
else
echo "$COMMAND is not running."
echo "$BINARY is not running."
fi
}

status() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
echo "$COMMAND is running with PID $PID."
echo "$BINARY is running with PID $PID."
else
echo "$COMMAND is not running, but PID file exists."
echo "$BINARY is not running, but PID file exists."
fi
else
echo "$COMMAND is not running."
echo "$BINARY is not running."
fi
}

Expand Down
9 changes: 5 additions & 4 deletions spaceship/templates/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var fsRunScript embed.FS

// NewRunScripts returns the generator to scaffold a chain and faucet run script.
func NewRunScripts(path, log, home, binaryPath, faucetPath, account, denoms, output string) error {
func NewRunScripts(path, log, home, binDirPath, chainBinPath, faucetBinPath, account, denoms, output string) error {
var (
g = genny.New()
runScript = xgenny.NewEmbedWalker(
Expand All @@ -32,9 +32,10 @@ func NewRunScripts(path, log, home, binaryPath, faucetPath, account, denoms, out
ctx.Set("path", path)
ctx.Set("log", log)
ctx.Set("home", home)
ctx.Set("binaryPath", binaryPath)
ctx.Set("binary", filepath.Base(binaryPath))
ctx.Set("faucetPath", faucetPath)
ctx.Set("chainBinPath", chainBinPath)
ctx.Set("faucetBinPath", faucetBinPath)
ctx.Set("binDirPath", binDirPath)
ctx.Set("binary", filepath.Base(chainBinPath))
ctx.Set("account", account)
ctx.Set("denoms", denoms)

Expand Down

0 comments on commit e3e8cb5

Please sign in to comment.