lotus/lotuspond/main.go

138 lines
2.5 KiB
Go
Raw Normal View History

2019-07-24 17:10:44 +00:00
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
2019-08-12 23:09:08 +00:00
"strconv"
2019-07-24 17:10:44 +00:00
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
2019-08-12 23:09:08 +00:00
"gopkg.in/urfave/cli.v2"
2019-07-24 17:10:44 +00:00
)
const listenAddr = "127.0.0.1:2222"
type runningNode struct {
2019-07-25 00:55:19 +00:00
cmd *exec.Cmd
2019-07-24 17:10:44 +00:00
meta nodeInfo
2019-07-26 00:40:06 +00:00
stop func()
2019-07-24 17:10:44 +00:00
}
2019-08-12 23:09:08 +00:00
var onCmd = &cli.Command{
Name: "on",
Usage: "run a command on a given node",
Action: func(cctx *cli.Context) error {
client, err := apiClient()
2019-07-25 23:25:46 +00:00
if err != nil {
2019-08-12 23:09:08 +00:00
return err
2019-07-25 23:25:46 +00:00
}
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
nd, err := strconv.ParseInt(cctx.Args().Get(0), 10, 32)
if err != nil {
return err
2019-07-25 23:25:46 +00:00
}
2019-08-12 23:53:59 +00:00
node := nodeById(client.Nodes(), int(nd))
2019-08-12 23:09:08 +00:00
var cmd *exec.Cmd
if !node.Storage {
cmd = exec.Command("./lotus", cctx.Args().Slice()[1:]...)
cmd.Env = []string{
"LOTUS_PATH=" + node.Repo,
}
} else {
cmd = exec.Command("./lotus-storage-miner")
cmd.Env = []string{
"LOTUS_STORAGE_PATH=" + node.Repo,
"LOTUS_PATH=" + node.FullNode,
}
}
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
err = cmd.Run()
return err
},
2019-07-24 17:10:44 +00:00
}
2019-08-12 23:09:08 +00:00
var shCmd = &cli.Command{
Name: "sh",
Usage: "spawn shell with node shell variables set",
Action: func(cctx *cli.Context) error {
client, err := apiClient()
if err != nil {
return err
}
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
nd, err := strconv.ParseInt(cctx.Args().Get(0), 10, 32)
if err != nil {
return err
}
2019-07-24 17:10:44 +00:00
2019-08-12 23:53:59 +00:00
node := nodeById(client.Nodes(), int(nd))
2019-08-12 23:09:08 +00:00
shcmd := exec.Command("/bin/bash")
if !node.Storage {
shcmd.Env = []string{
"LOTUS_PATH=" + node.Repo,
}
} else {
shcmd.Env = []string{
"LOTUS_STORAGE_PATH=" + node.Repo,
"LOTUS_PATH=" + node.FullNode,
}
}
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
shcmd.Stdin = os.Stdin
shcmd.Stdout = os.Stdout
shcmd.Stderr = os.Stderr
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
fmt.Printf("Entering shell for Node %d\n", nd)
err = shcmd.Run()
fmt.Printf("Closed pond shell\n")
2019-07-24 17:10:44 +00:00
2019-08-12 23:09:08 +00:00
return err
},
2019-07-24 17:10:44 +00:00
}
2019-08-12 23:53:59 +00:00
func nodeById(nodes []nodeInfo, i int) nodeInfo {
for _, n := range nodes {
if n.ID == int32(i) {
return n
}
}
panic("no node with this id")
}
2019-08-12 23:09:08 +00:00
var runCmd = &cli.Command{
Name: "run",
Usage: "run lotuspond daemon",
Action: func(cctx *cli.Context) error {
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Pond", &api{running: map[int32]runningNode{}})
2019-07-30 23:18:21 +00:00
2019-08-12 23:09:08 +00:00
http.Handle("/", http.FileServer(http.Dir("lotuspond/front/build")))
http.Handle("/rpc/v0", rpcServer)
2019-07-30 23:18:21 +00:00
2019-08-12 23:09:08 +00:00
fmt.Printf("Listening on http://%s\n", listenAddr)
return http.ListenAndServe(listenAddr, nil)
},
2019-07-30 23:18:21 +00:00
}
2019-07-24 17:10:44 +00:00
func main() {
2019-08-12 23:09:08 +00:00
app := &cli.App{
Name: "pond",
Commands: []*cli.Command{
runCmd,
shCmd,
onCmd,
},
}
if err := app.Run(os.Args); err != nil {
panic(err)
}
2019-07-24 17:10:44 +00:00
}