2019-07-24 17:10:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-26 00:40:06 +00:00
|
|
|
"io"
|
2019-07-24 17:10:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
|
|
|
|
"github.com/filecoin-project/go-lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type api struct {
|
2019-07-25 00:55:19 +00:00
|
|
|
cmds int32
|
|
|
|
running map[int32]runningNode
|
2019-07-24 17:10:44 +00:00
|
|
|
runningLk sync.Mutex
|
2019-07-25 23:25:46 +00:00
|
|
|
genesis string
|
2019-07-24 17:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nodeInfo struct {
|
2019-07-25 00:55:19 +00:00
|
|
|
Repo string
|
|
|
|
ID int32
|
2019-07-24 17:10:44 +00:00
|
|
|
ApiPort int32
|
2019-07-30 23:18:21 +00:00
|
|
|
|
|
|
|
Storage bool
|
2019-07-24 17:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *api) Spawn() (nodeInfo, error) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "lotus-")
|
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:25:46 +00:00
|
|
|
genParam := "--genesis=" + api.genesis
|
2019-07-24 17:10:44 +00:00
|
|
|
id := atomic.AddInt32(&api.cmds, 1)
|
2019-07-25 23:25:46 +00:00
|
|
|
if id == 1 {
|
|
|
|
// make genesis
|
|
|
|
genf, err := ioutil.TempFile(os.TempDir(), "lotus-genesis-")
|
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
2019-07-24 17:10:44 +00:00
|
|
|
|
2019-07-25 23:25:46 +00:00
|
|
|
api.genesis = genf.Name()
|
|
|
|
genParam = "--lotus-make-random-genesis=" + api.genesis
|
|
|
|
|
|
|
|
if err := genf.Close(); err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:18:21 +00:00
|
|
|
errlogfile, err := os.OpenFile(dir+".err.log", os.O_CREATE|os.O_WRONLY, 0644)
|
2019-07-26 00:40:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
2019-07-30 23:18:21 +00:00
|
|
|
logfile, err := os.OpenFile(dir+".out.log", os.O_CREATE|os.O_WRONLY, 0644)
|
2019-07-26 00:40:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:25:46 +00:00
|
|
|
cmd := exec.Command("./lotus", "daemon", genParam, "--api", fmt.Sprintf("%d", 2500+id))
|
2019-07-26 00:40:06 +00:00
|
|
|
cmd.Stderr = io.MultiWriter(os.Stderr, errlogfile)
|
|
|
|
cmd.Stdout = io.MultiWriter(os.Stdout, logfile)
|
2019-07-24 17:10:44 +00:00
|
|
|
cmd.Env = []string{"LOTUS_PATH=" + dir}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info := nodeInfo{
|
2019-07-25 00:55:19 +00:00
|
|
|
Repo: dir,
|
|
|
|
ID: id,
|
2019-07-24 17:10:44 +00:00
|
|
|
ApiPort: 2500 + id,
|
|
|
|
}
|
|
|
|
|
|
|
|
api.runningLk.Lock()
|
|
|
|
api.running[id] = runningNode{
|
2019-07-25 00:55:19 +00:00
|
|
|
cmd: cmd,
|
2019-07-24 17:10:44 +00:00
|
|
|
meta: info,
|
2019-07-26 00:40:06 +00:00
|
|
|
|
|
|
|
stop: func() {
|
|
|
|
defer errlogfile.Close()
|
|
|
|
defer logfile.Close()
|
|
|
|
},
|
2019-07-24 17:10:44 +00:00
|
|
|
}
|
|
|
|
api.runningLk.Unlock()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 750) // TODO: Something less terrible
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *api) Nodes() []nodeInfo {
|
|
|
|
api.runningLk.Lock()
|
|
|
|
out := make([]nodeInfo, 0, len(api.running))
|
|
|
|
for _, node := range api.running {
|
|
|
|
out = append(out, node.meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
api.runningLk.Unlock()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *api) TokenFor(id int32) (string, error) {
|
|
|
|
api.runningLk.Lock()
|
|
|
|
defer api.runningLk.Unlock()
|
|
|
|
|
|
|
|
rnd, ok := api.running[id]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("no running node with this ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := repo.NewFS(rnd.meta.Repo)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := r.APIToken()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(t), nil
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:18:21 +00:00
|
|
|
func (api *api) SpawnStorage(fullNodeRepo string) (nodeInfo, error) {
|
|
|
|
dir, err := ioutil.TempDir(os.TempDir(), "lotus-storage-")
|
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
errlogfile, err := os.OpenFile(dir+".err.log", os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
logfile, err := os.OpenFile(dir+".out.log", os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := atomic.AddInt32(&api.cmds, 1)
|
|
|
|
cmd := exec.Command("./lotus-storage-miner", "init")
|
|
|
|
cmd.Stderr = io.MultiWriter(os.Stderr, errlogfile)
|
|
|
|
cmd.Stdout = io.MultiWriter(os.Stdout, logfile)
|
|
|
|
cmd.Env = []string{"LOTUS_STORAGE_PATH=" + dir, "LOTUS_PATH=" + fullNodeRepo}
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 300)
|
|
|
|
|
|
|
|
cmd = exec.Command("./lotus-storage-miner", "run", "--api", fmt.Sprintf("%d", 2500+id))
|
|
|
|
cmd.Stderr = io.MultiWriter(os.Stderr, errlogfile)
|
|
|
|
cmd.Stdout = io.MultiWriter(os.Stdout, logfile)
|
|
|
|
cmd.Env = []string{"LOTUS_STORAGE_PATH=" + dir, "LOTUS_PATH=" + fullNodeRepo}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info := nodeInfo{
|
|
|
|
Repo: dir,
|
|
|
|
ID: id,
|
|
|
|
ApiPort: 2500 + id,
|
|
|
|
|
|
|
|
Storage: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
api.runningLk.Lock()
|
|
|
|
api.running[id] = runningNode{
|
|
|
|
cmd: cmd,
|
|
|
|
meta: info,
|
|
|
|
|
|
|
|
stop: func() {
|
|
|
|
defer errlogfile.Close()
|
|
|
|
defer logfile.Close()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
api.runningLk.Unlock()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 750) // TODO: Something less terrible
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:10:44 +00:00
|
|
|
func main() {
|
|
|
|
rpcServer := jsonrpc.NewServer()
|
|
|
|
rpcServer.Register("Pond", &api{running: map[int32]runningNode{}})
|
|
|
|
|
2019-07-24 21:45:01 +00:00
|
|
|
http.Handle("/", http.FileServer(http.Dir("lotuspond/front/build")))
|
2019-07-24 17:10:44 +00:00
|
|
|
http.Handle("/rpc/v0", rpcServer)
|
|
|
|
|
|
|
|
fmt.Printf("Listening on http://%s\n", listenAddr)
|
|
|
|
http.ListenAndServe(listenAddr, nil)
|
|
|
|
}
|