From 5b0ec6eea21589a9a8f1cd111b0233e29d8ee2a5 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 25 Nov 2024 21:49:56 -0300 Subject: [PATCH] feat(spaceship): improve logs (#145) * improve the log command * improve readme * improve readme * add changelog * improve readme readbilitty --- spaceship/CHANGELOG.md | 5 +++ spaceship/README.md | 86 +++++++++++++++++++++---------------- spaceship/cmd/chain.go | 4 ++ spaceship/cmd/cmd.go | 65 +++++++++++++--------------- spaceship/cmd/debug/main.go | 41 ++++++++++-------- spaceship/cmd/faucet.go | 5 +++ spaceship/cmd/log.go | 20 +++++---- spaceship/cmd/ssh.go | 5 --- spaceship/main.go | 9 +--- spaceship/pkg/ssh/log.go | 24 ++++++++++- 10 files changed, 151 insertions(+), 113 deletions(-) diff --git a/spaceship/CHANGELOG.md b/spaceship/CHANGELOG.md index 9d4594a3..faf2747c 100644 --- a/spaceship/CHANGELOG.md +++ b/spaceship/CHANGELOG.md @@ -1,5 +1,10 @@ # Spaceship App Changelog +## [`v0.1.1`](https://github.com/ignite/apps/releases/tag/spaceship/v0.1.1) + +* [#144](https://github.com/ignite/apps/pull/144) Spaceship faucet +* [#145](https://github.com/ignite/apps/pull/145) Improve logs + ## [`v0.1.0`](https://github.com/ignite/apps/releases/tag/spaceship/v0.1.0) * First release of the Spaceship app compatible with Ignite >= v28.x.y diff --git a/spaceship/README.md b/spaceship/README.md index d1165e12..8443ad20 100644 --- a/spaceship/README.md +++ b/spaceship/README.md @@ -1,83 +1,97 @@ # Spaceship -Spaceship is an Ignite App designed to extend the [Ignite CLI](https://github.com/ignite/cli) by providing tools to -deploy blockchain applications via SSH. +Spaceship is an Ignite App designed to extend the [Ignite CLI](https://github.com/ignite/cli) by providing tools to deploy blockchain applications via SSH. ## Prerequisites -* Ignite CLI: Version `v28.4.0` or higher is required. -* Blockchain Scaffold: A blockchain scaffolded using Ignite +* **Ignite CLI**: Version `v28.4.0` or higher is required. +* **Blockchain Scaffold**: A blockchain scaffolded using Ignite. ## Usage Spaceship provides multiple ways to connect to your SSH server for deployment: ```sh -ignite spaceship deploy root@127.0.0.1 --key $HOME/.ssh/id_rsa -ignite spaceship deploy 127.0.0.1 --user root --key $HOME/.ssh/id_rsa -ignite spaceship deploy 127.0.0.1 --user root --password password -ignite spaceship deploy root@127.0.0.1 --key $HOME/.ssh/id_rsa --key-password key_password +ignite spaceship deploy @ --key $HOME/.ssh/id_rsa +ignite spaceship deploy --user root --key $HOME/.ssh/id_rsa +ignite spaceship deploy --user root --password password +ignite spaceship deploy @ --key $HOME/.ssh/id_rsa --key-password key_password ``` -Each command initiates a build of the blockchain binary and sets up the chain's home directory based on the -configuration. The app then connects to the specified SSH server, establishes workspaces, transfers the binary, and -executes it using a runner script. The workspaces are organized under `$HOME/workspace/` and include: +Each command initiates a build of the blockchain binary and sets up the chain's home directory based on the configuration. The app then connects to the specified SSH server, establishes workspaces, transfers the binary, and executes it using a runner script. -- Binary Directory: `$HOME/workspace//bin` - Contains the chain binary. -- Home Directory: `$HOME/workspace//home` - Stores chain data. -- Log Directory: `$HOME/workspace//log` - Holds logs of the running chain. -- Runner Script: `$HOME/workspace//run.sh` - A script to start the binary in the background using nohup. -- PID File: `$HOME/workspace//spaceship.pid` - Stores the PID of the currently running chain instance. +The workspaces are organized under `$HOME/workspace/` and include: +- **Binary Directory**: `$HOME/workspace//bin` - Contains the chain binary. +- **Home Directory**: `$HOME/workspace//home` - Stores chain data. +- **Log Directory**: `$HOME/workspace//log` - Holds logs of the running chain. +- **Runner Script**: `$HOME/workspace//run.sh` - A script to start the binary in the background using `nohup`. +- **PID File**: `$HOME/workspace//spaceship.pid` - Stores the PID of the currently running chain instance. ### Managing the Chain To manage your blockchain deployment, use the following commands: -- Check status: +- **Check status**: ```sh -ignite spaceship status root@127.0.0.1 --key $HOME/.ssh/id_rsa +ignite spaceship status @ --key $HOME/.ssh/id_rsa ``` -- View logs: +- **View logs**: ```sh -ignite spaceship log root@127.0.0.1 --key $HOME/.ssh/id_rsa +ignite spaceship log @ --key $HOME/.ssh/id_rsa ``` -- Watch the logs in real time: +- **Watch the logs in real time**: ```sh -ignite spaceship log root@127.0.0.1 --key $HOME/.ssh/id_rsa --real-time +ignite spaceship log @ --key $HOME/.ssh/id_rsa --real-time ``` -- Restart the chain: +- **Restart the chain**: ```sh -ignite spaceship restart root@127.0.0.1 --key $HOME/.ssh/id_rsa +ignite spaceship restart @ --key $HOME/.ssh/id_rsa ``` -- Stop the Chain: +- **Stop the chain**: ```sh -ignite spaceship stop root@127.0.0.1 --key $HOME/.ssh/id_rsa +ignite spaceship stop @ --key $HOME/.ssh/id_rsa ``` -To redeploy the chain on the same server without overwriting the home directory, use the `--init-chain` flag to -reinitialize the chain if necessary. +To redeploy the chain on the same server without overwriting the home directory, use the `--init-chain` flag to reinitialize the chain if necessary. -### Config +## Faucet + +You can deploy your chain along with a faucet application by passing the faucet flag to the deploy command: -You can override the default [chain configuration](https://docs.ignite.com/references/config#validators) by using the -Ignite configuration file. Validators' node configuration files are stored in the data directory. By default, Spaceship -initializes the chain locally in a temporary folder using the Ignite config file and then copies the configuration to -the remote machine at `$HOME/workspace//home`. -Configuration resets are performed by Ignite when necessary, especially when using the `--init-chain` flag or if the chain -was not previously initialized. +```sh +ignite spaceship deploy @ --key $HOME/.ssh/id_rsa --faucet +``` -Example Ignite config: +You can also specify the faucet port: +```sh +ignite spaceship deploy @ --key $HOME/.ssh/id_rsa --faucet --faucet-port 8077 ``` + +To check the faucet logs, pass the parameter `faucet` to the `--app` flag in the log command: + +```sh +ignite spaceship log @ --key $HOME/.ssh/id_rsa --app faucet +``` + +### Config + +You can override the default [chain configuration](https://docs.ignite.com/references/config#validators) by using the Ignite configuration file. Validators' node configuration files are stored in the data directory. By default, Spaceship initializes the chain locally in a temporary folder using the Ignite config file and then copies the configuration to the remote machine at `$HOME/workspace//home`. + +Configuration resets are performed by Ignite when necessary, especially when using the `--init-chain` flag or if the chain was not previously initialized. + +**Example Ignite config**: + +```yaml validators: - name: alice bonded: '100000000stake' diff --git a/spaceship/cmd/chain.go b/spaceship/cmd/chain.go index 7bf34d8a..491ba7ba 100644 --- a/spaceship/cmd/chain.go +++ b/spaceship/cmd/chain.go @@ -20,6 +20,10 @@ import ( "github.com/ignite/apps/spaceship/templates/script" ) +const ( + flagInitChain = "init-chain" +) + // ExecuteSSHStatus executes the ssh status subcommand. func ExecuteSSHStatus(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error { session := cliui.New(cliui.StartSpinnerWithText(statusConnecting)) diff --git a/spaceship/cmd/cmd.go b/spaceship/cmd/cmd.go index 0189f90c..23cbaa8c 100644 --- a/spaceship/cmd/cmd.go +++ b/spaceship/cmd/cmd.go @@ -1,6 +1,13 @@ package cmd -import "github.com/ignite/cli/v28/ignite/services/plugin" +import ( + "fmt" + "strings" + + "github.com/ignite/cli/v28/ignite/services/plugin" + + "github.com/ignite/apps/spaceship/pkg/ssh" +) var defaultFlags = []*plugin.Flag{ { @@ -73,44 +80,30 @@ func GetCommands() []*plugin.Command { { Use: "log [host]", Short: "get remote logs", - Commands: []*plugin.Command{ - { - Use: "chain", - Short: "get chain logs if its running", - Flags: append(defaultFlags, - &plugin.Flag{ - Name: flagLines, - Shorthand: "l", - Usage: "number of lines of chain logs", - Type: plugin.FlagTypeInt, - DefaultValue: "100", - }, - &plugin.Flag{ - Name: flagRealTime, - Usage: "show the logs in the real time", - Type: plugin.FlagTypeBool, - }, - ), + Flags: append(defaultFlags, + &plugin.Flag{ + Name: flagLines, + Shorthand: "l", + Usage: "number of lines of chain logs", + Type: plugin.FlagTypeInt, + DefaultValue: "100", }, - { - Use: "faucet", - Short: "get faucet logs if its running", - Flags: append(defaultFlags, - &plugin.Flag{ - Name: flagLines, - Shorthand: "l", - Usage: "number of lines of chain logs", - Type: plugin.FlagTypeInt, - DefaultValue: "100", - }, - &plugin.Flag{ - Name: flagRealTime, - Usage: "show the logs in the real time", - Type: plugin.FlagTypeBool, - }, + &plugin.Flag{ + Name: flagRealTime, + Usage: "show the logs in the real time", + Type: plugin.FlagTypeBool, + }, + &plugin.Flag{ + Name: flagAppLog, + Shorthand: "a", + Usage: fmt.Sprintf( + "the app to show the log (%s)", + strings.Join(ssh.LogTypes(), ","), ), + Type: plugin.FlagTypeString, + DefaultValue: ssh.LogChain.String(), }, - }, + ), }, { Use: "status [host]", diff --git a/spaceship/cmd/debug/main.go b/spaceship/cmd/debug/main.go index 4fd7c4f6..c85b0cd7 100644 --- a/spaceship/cmd/debug/main.go +++ b/spaceship/cmd/debug/main.go @@ -5,10 +5,12 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/ignite/apps/spaceship/cmd" + "github.com/ignite/apps/spaceship/pkg/ssh" ) func main() { @@ -72,24 +74,27 @@ func main() { return } case "log": - c.Flags = append(c.Flags, &plugin.Flag{ - Name: "real-time", - Usage: "show the logs in the real time", - Type: plugin.FlagTypeBool, - Value: "true", - }) - switch args[2] { - case "chain": - if err := cmd.ExecuteChainSSHLog(ctx, c, chainInfo); err != nil { - fmt.Fprintln(os.Stderr, err) - return - } - case "faucet": - if err := cmd.ExecuteFaucetSSHLog(ctx, c, chainInfo); err != nil { - fmt.Fprintln(os.Stderr, err) - return - } - fmt.Fprintf(os.Stderr, "unknown log command: %s", args[2]) + c.Flags = append(c.Flags, + &plugin.Flag{ + Name: "real-time", + Usage: "show the logs in the real time", + Type: plugin.FlagTypeBool, + Value: "true", + }, + &plugin.Flag{ + Name: "app", + Shorthand: "a", + Usage: fmt.Sprintf( + "the app to show the log (%s)", + strings.Join(ssh.LogTypes(), ","), + ), + Type: plugin.FlagTypeString, + DefaultValue: ssh.LogChain.String(), + Value: ssh.LogChain.String(), + }, + ) + if err := cmd.ExecuteSSHLog(ctx, c, chainInfo); err != nil { + fmt.Fprintln(os.Stderr, err) return } case "status": diff --git a/spaceship/cmd/faucet.go b/spaceship/cmd/faucet.go index a63a4b13..efe73e9c 100644 --- a/spaceship/cmd/faucet.go +++ b/spaceship/cmd/faucet.go @@ -9,6 +9,11 @@ import ( "github.com/ignite/cli/v28/ignite/services/plugin" ) +const ( + flagFaucet = "faucet" + flagFaucetPort = "faucet-port" +) + func faucetPort(f []*plugin.Flag) (uint64, error) { flags := plugin.Flags(f) port, err := flags.GetUint64(flagFaucetPort) diff --git a/spaceship/cmd/log.go b/spaceship/cmd/log.go index 6a5f33bd..bbcef1bc 100644 --- a/spaceship/cmd/log.go +++ b/spaceship/cmd/log.go @@ -11,16 +11,14 @@ import ( "github.com/ignite/apps/spaceship/pkg/ssh" ) -func ExecuteChainSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error { - return ExecuteSSHLog(ctx, ssh.LogChain, cmd, chain) -} - -func ExecuteFaucetSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error { - return ExecuteSSHLog(ctx, ssh.LogFaucet, cmd, chain) -} +const ( + flagLines = "lines" + flagRealTime = "real-time" + flagAppLog = "app" +) // ExecuteSSHLog executes the ssh log subcommand. -func ExecuteSSHLog(ctx context.Context, logType ssh.LogType, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error { +func ExecuteSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error { session := cliui.New(cliui.StartSpinnerWithText(statusConnecting)) defer session.End() @@ -28,8 +26,14 @@ func ExecuteSSHLog(ctx context.Context, logType ssh.LogType, cmd *plugin.Execute flags = plugin.Flags(cmd.Flags) lines, _ = flags.GetInt(flagLines) realTime, _ = flags.GetBool(flagRealTime) + appLog, _ = flags.GetString(flagAppLog) ) + logType, err := ssh.ParseLogType(appLog) + if err != nil { + return err + } + c, err := executeSSH(cmd, chain) if err != nil { return err diff --git a/spaceship/cmd/ssh.go b/spaceship/cmd/ssh.go index 11b7ca6c..2f49de21 100644 --- a/spaceship/cmd/ssh.go +++ b/spaceship/cmd/ssh.go @@ -16,11 +16,6 @@ const ( flagKey = "key" flagRawKey = "raw-key" flagKeyPassword = "key-password" - flagInitChain = "init-chain" - flagFaucet = "faucet" - flagFaucetPort = "faucet-port" - flagLines = "lines" - flagRealTime = "real-time" statusConnecting = "Connecting..." ) diff --git a/spaceship/main.go b/spaceship/main.go index 7b4d8edd..88db5020 100644 --- a/spaceship/main.go +++ b/spaceship/main.go @@ -38,14 +38,7 @@ func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, api plugin.Cl case "stop": return cmd.ExecuteSSHSStop(ctx, c, chainInfo) case "log": - switch args[1] { - case "chain": - return cmd.ExecuteChainSSHLog(ctx, c, chainInfo) - case "faucet": - return cmd.ExecuteFaucetSSHLog(ctx, c, chainInfo) - default: - return fmt.Errorf("unknown log command: %s", args[1]) - } + return cmd.ExecuteSSHLog(ctx, c, chainInfo) case "faucet": switch args[1] { case "status": diff --git a/spaceship/pkg/ssh/log.go b/spaceship/pkg/ssh/log.go index 1be0f945..8f32e7bc 100644 --- a/spaceship/pkg/ssh/log.go +++ b/spaceship/pkg/ssh/log.go @@ -30,10 +30,30 @@ type ( const ( logExtension = ".log" - LogChain LogType = "chain_" - LogFaucet LogType = "faucet_" + LogChain LogType = "chain" + LogFaucet LogType = "faucet" ) +func (l LogType) String() string { + return string(l) +} + +func LogTypes() []string { + return []string{LogChain.String(), LogFaucet.String()} +} + +// ParseLogType parses the log type from a string. +func ParseLogType(logType string) (LogType, error) { + switch LogType(strings.ToLower(logType)) { + case LogChain: + return LogChain, nil + case LogFaucet: + return LogFaucet, nil + default: + return "", errors.New("invalid log type: " + logType) + } +} + func (a logs) Len() int { return len(a) } func (a logs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a logs) Less(i, j int) bool { return a[i].time.Before(a[j].time) }