Skip to content

Commit

Permalink
check os and arch and get logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Jul 20, 2024
1 parent 9e6b581 commit 7b0f67f
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 182 deletions.
14 changes: 0 additions & 14 deletions spaceship/cmd/aws.go

This file was deleted.

73 changes: 66 additions & 7 deletions spaceship/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,43 @@ package cmd

import "github.com/ignite/cli/v28/ignite/services/plugin"

var defaultFlags = []*plugin.Flag{
{
Name: flagUser,
Shorthand: "u",
Usage: "ssh user",
Type: plugin.FlagTypeString,
},
{
Name: flagPort,
Shorthand: "p",
Usage: "ssh port",
Type: plugin.FlagTypeString,
},
{
Name: flagPassword,
Usage: "ssh user password",
Type: plugin.FlagTypeString,
},
{
Name: flagKey,
Shorthand: "k",
Usage: "ssh key",
Type: plugin.FlagTypeString,
},
{
Name: flagRawKey,
Shorthand: "r",
Usage: "ssh raw key",
Type: plugin.FlagTypeString,
},
{
Name: flagKeyPassword,
Usage: "ssh key password",
Type: plugin.FlagTypeString,
},
}

// GetCommands returns the list of spaceship app commands.
func GetCommands() []*plugin.Command {
return []*plugin.Command{
Expand All @@ -10,14 +47,36 @@ func GetCommands() []*plugin.Command {
Short: "spaceship is an awesome Ignite application!",
Commands: []*plugin.Command{
{
Use: "ssh",
Short: "deploy your chain trough ssh",
Commands: []*plugin.Command{
{
Use: "deploy",
Short: "deploy your chain into a production mode",
Use: "deploy",
Short: "deploy your chain",
Flags: append(defaultFlags,
&plugin.Flag{
Name: flagInitChain,
Shorthand: "i",
Usage: "ssh user",
Type: plugin.FlagTypeBool,
},
},
),
},
{
Use: "log",
Short: "get chain logs if its running",
Flags: defaultFlags,
},
{
Use: "status",
Short: "get chain status if its running",
Flags: defaultFlags,
},
{
Use: "restart",
Short: "restart your chain",
Flags: defaultFlags,
},
{
Use: "stop",
Short: "stop your chain",
Flags: defaultFlags,
},
},
},
Expand Down
47 changes: 36 additions & 11 deletions spaceship/cmd/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,47 @@ func main() {
AppPath: filepath.Join(home, "Desktop/go/src/github.com/ignite/mars"),
ChainId: "mars",
}
c = &plugin.ExecutedCommand{
Use: args[1],
Path: "ignite spaceship " + args[1],
Args: []string{"[email protected]"},
OsArgs: os.Args,
With: nil,
Flags: []*plugin.Flag{
{
Name: "key",
Shorthand: "k",
Usage: "ssh key",
Type: plugin.FlagTypeString,
Value: "/Users/danilopantani/.ssh/id_rsa",
},
},
}
)
switch args[1] {
case "aws":
if err := cmd.ExecuteAWS(ctx, nil); err != nil {
case "deploy":
if err := cmd.ExecuteSSHDeploy(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "log":
if err := cmd.ExecuteSSHLog(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "ssh":
switch args[2] {
case "deploy":
if err := cmd.ExecuteSSHDeploy(ctx, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
default:
fmt.Fprintf(os.Stderr, "unknown ssh command: %s", args[2])
case "status":
if err := cmd.ExecuteSSHStatus(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "restart":
if err := cmd.ExecuteSSHRestart(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
case "stop":
if err := cmd.ExecuteSSHSStop(ctx, c, chainInfo); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
default:
Expand Down
139 changes: 123 additions & 16 deletions spaceship/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,150 @@ import (
"path/filepath"

ignitecmd "github.com/ignite/cli/v28/ignite/cmd"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/services/plugin"

"github.com/ignite/apps/spaceship/pkg/ssh"
"github.com/ignite/apps/spaceship/templates/script"
)

const (
flagPort = "port"
flagUser = "user"
flagPassword = "password"
flagKey = "key"
flagRawKey = "raw-key"
flagKeyPassword = "key-password"
flagInitChain = "init-chain"
)

func executeSSH(cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) (*ssh.SSH, error) {
args := cmd.Args
if len(args) < 1 {
return nil, errors.New("must specify unless a uri host")
}
flags, err := cmd.NewFlags()
if err != nil {
return nil, err
}

var (
host = args[0]

user, _ = flags.GetString(flagUser)
port, _ = flags.GetString(flagPort)
password, _ = flags.GetString(flagPassword)
key, _ = flags.GetString(flagKey)
rawKey, _ = flags.GetString(flagRawKey)
keyPassword, _ = flags.GetString(flagKeyPassword)
)

// Connect to the SSH.
c, err := ssh.New(
host,
ssh.WithUser(user),
ssh.WithPort(port),
ssh.WithPassword(password),
ssh.WithKey(key),
ssh.WithRawKey(rawKey),
ssh.WithKeyPassword(keyPassword),
ssh.WithWorkspace(chain.ChainId),
)
if err != nil {
return nil, err
}

return c, c.Connect()
}

// ExecuteSSHStatus executes the ssh status subcommand.
func ExecuteSSHStatus(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
c, err := executeSSH(cmd, chain)
if err != nil {
return err
}
defer c.Close()
status, err := c.Status(ctx)
if err != nil {
return err
}
fmt.Println(status)
return nil
}

// ExecuteSSHSStop executes the ssh stop subcommand.
func ExecuteSSHSStop(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
c, err := executeSSH(cmd, chain)
if err != nil {
return err
}
defer c.Close()
stop, err := c.Stop(ctx)
if err != nil {
return err
}
fmt.Println(stop)
return nil
}

// ExecuteSSHRestart executes the ssh restart subcommand.
func ExecuteSSHRestart(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
c, err := executeSSH(cmd, chain)
if err != nil {
return err
}
defer c.Close()
restart, err := c.Restart(ctx)
if err != nil {
return err
}
fmt.Println(restart)
return nil
}

// ExecuteSSHLog executes the ssh log subcommand.
func ExecuteSSHLog(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
c, err := executeSSH(cmd, chain)
if err != nil {
return err
}
defer c.Close()
log, err := c.LatestLog()
if err != nil {
return err
}
fmt.Println(string(log))
return nil
}

// ExecuteSSHDeploy executes the ssh deploy subcommand.
func ExecuteSSHDeploy(ctx context.Context, chain *plugin.ChainInfo) error {
// args := os.Args[2:]
func ExecuteSSHDeploy(ctx context.Context, cmd *plugin.ExecutedCommand, chain *plugin.ChainInfo) error {
flags, err := cmd.NewFlags()
if err != nil {
return err
}
var (
host = "[email protected]" // arg host or URI
key = "/Users/danilopantani/.ssh/id_rsa" // flag key
// user = "danilopantani" // flag user
// password = "" // flag password
// port = "22" // flag port
// keyPassword = args[5] // flag key password
// keyRaw = args[6] // flag key raw
initChain = true // flag key raw
initChain, _ = flags.GetBool(flagInitChain)

localDir = filepath.Join(os.TempDir(), "spaceship", chain.ChainId)
localChainHome = filepath.Join(localDir, "home")
localBinOutput = filepath.Join(localDir, "bin")
localChainBin = fmt.Sprintf("%s/%sd", localBinOutput, chain.ChainId)
)

// Connect to the SSH.
c, err := ssh.New(host, ssh.WithKey(key), ssh.WithWorkspace(chain.ChainId))
c, err := executeSSH(cmd, chain)
if err != nil {
return err
}
if err := c.Connect(); err != nil {
return err
}
defer c.Close()

os, err := c.Target(ctx)
if err != nil {
return err
}
// We are using the ignite chain build command to build the app.
igniteChainBuildCmd := ignitecmd.NewChainBuild()
igniteChainBuildCmd.SetArgs([]string{"-p", chain.AppPath, "-o", localBinOutput})
igniteChainBuildCmd.SetArgs([]string{"-p", chain.AppPath, "-o", localBinOutput, "--release", "--release.targets", os})
if err := igniteChainBuildCmd.ExecuteContext(ctx); err != nil {
return err
}
Expand Down Expand Up @@ -91,5 +197,6 @@ func ExecuteSSHDeploy(ctx context.Context, chain *plugin.ChainInfo) error {
return err
}
fmt.Println(status)

return nil
}
21 changes: 10 additions & 11 deletions spaceship/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, api plugin.Cl
// Remove the first two elements "ignite" and "spaceship" from OsArgs.
args := c.OsArgs[2:]
switch args[0] {
case "aws":
return cmd.ExecuteAWS(ctx, c)
case "ssh":
switch args[1] {
case "dev":
return cmd.ExecuteSSHDevelopment(ctx, chainInfo)
case "deploy":
return cmd.ExecuteSSHDeploy(ctx, chainInfo)
default:
return fmt.Errorf("unknown ssh command: %s", args[1])
}
case "deploy":
return cmd.ExecuteSSHDeploy(ctx, c, chainInfo)
case "log":
return cmd.ExecuteSSHLog(ctx, c, chainInfo)
case "status":
return cmd.ExecuteSSHStatus(ctx, c, chainInfo)
case "restart":
return cmd.ExecuteSSHRestart(ctx, c, chainInfo)
case "stop":
return cmd.ExecuteSSHSStop(ctx, c, chainInfo)
default:
return fmt.Errorf("unknown command: %s", args[0])
}
Expand Down
Loading

0 comments on commit 7b0f67f

Please sign in to comment.