-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* stub daemon flags * regions controller * daemons api * local exec spawner * tidy * kubernetes spawner constructor * use templating * default in view func * output daemon on create * templating * remove wallets api * use static slice length on Get * use lock on read-operations List and Get * use sigs yaml * bug, wallet nil pointer * remove extra log info * tags * include linux headers in docker file
- Loading branch information
Cory Schwartz
authored
Jul 8, 2021
1 parent
e400dc9
commit cff66ff
Showing
12 changed files
with
1,077 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package controller | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/filecoin-project/dealbot/controller/spawn" | ||
"github.com/google/uuid" | ||
|
||
"github.com/filecoin-project/lotus/chain/types" | ||
"github.com/filecoin-project/lotus/chain/wallet" | ||
_ "github.com/filecoin-project/lotus/lib/sigs/bls" | ||
_ "github.com/filecoin-project/lotus/lib/sigs/secp" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type DaemonList struct { | ||
Daemons []*spawn.Daemon `json:"daemons"` | ||
} | ||
|
||
func (c *Controller) getDaemonsHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := log.With("req_id", r.Header.Get("X-Request-ID")) | ||
logger.Debugw("handle request", "command", "list tasks") | ||
defer logger.Debugw("request handled", "command", "list tasks") | ||
w.Header().Set("Content-Type", "application/json") | ||
enableCors(&w, r) | ||
|
||
regionid := mux.Vars(r)["regionid"] | ||
daemons, err := c.spawner.List(regionid) | ||
if err != nil { | ||
if errors.Is(err, spawn.RegionNotFound) { | ||
w.WriteHeader(http.StatusNotFound) | ||
} else { | ||
log.Errorw("error listing region", "err", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
json.NewEncoder(w).Encode(&DaemonList{ | ||
Daemons: daemons, | ||
}) | ||
} | ||
|
||
func (c *Controller) getDaemonHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := log.With("req_id", r.Header.Get("X-Request-ID")) | ||
logger.Debugw("handle request", "command", "list tasks") | ||
defer logger.Debugw("request handled", "command", "list tasks") | ||
w.Header().Set("Content-Type", "application/json") | ||
enableCors(&w, r) | ||
|
||
regionid := mux.Vars(r)["regionid"] | ||
daemonid := mux.Vars(r)["daemonid"] | ||
daemon, err := c.spawner.Get(regionid, daemonid) | ||
if err != nil { | ||
if errors.Is(err, spawn.DaemonNotFound) { | ||
w.WriteHeader(http.StatusNotFound) | ||
} else { | ||
log.Errorw("error getting daemon", "err", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
json.NewEncoder(w).Encode(daemon) | ||
} | ||
|
||
func (c *Controller) newDaemonHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := log.With("req_id", r.Header.Get("X-Request-ID")) | ||
logger.Debugw("handle request", "command", "list tasks") | ||
defer logger.Debugw("request handled", "command", "list tasks") | ||
w.Header().Set("Content-Type", "application/json") | ||
enableCors(&w, r) | ||
|
||
daemon := new(spawn.Daemon) | ||
if err := json.NewDecoder(r.Body).Decode(daemon); err != nil { | ||
log.Info("could not decode daemon request", "err", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
regionid := mux.Vars(r)["regionid"] | ||
daemon.Region = regionid | ||
// generate random values if they aren't already setup | ||
daemonDefaults(daemon) | ||
if err := c.spawner.Spawn(daemon); err != nil { | ||
log.Errorw("could not spawn daemon", "daemonid", daemon.Id, "err", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
// daemon is already created; sanatize output | ||
daemon.Wallet.Exported = "" | ||
json.NewEncoder(w).Encode(daemon) | ||
} | ||
|
||
func daemonDefaults(d *spawn.Daemon) { | ||
if !(d.Wallet != nil && d.Wallet.Address != "" && d.Wallet.Exported != "") { | ||
k, _ := wallet.GenerateKey(types.KTSecp256k1) | ||
b, _ := json.Marshal(k.KeyInfo) | ||
d.Wallet = &spawn.Wallet{ | ||
Address: k.Address.String(), | ||
Exported: hex.EncodeToString(b), | ||
} | ||
} | ||
if d.Id == "" { | ||
d.Id = "daemon-" + uuid.New().String()[:8] | ||
} | ||
if d.Workers == 0 { | ||
d.Workers = 1 | ||
} | ||
if d.DockerRepo == "" { | ||
d.DockerRepo = "filecoin/dealbot" | ||
} | ||
if d.DockerTag == "" { | ||
d.DockerTag = "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package controller | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type RegionList struct { | ||
Regions []string `json:"regions"` | ||
} | ||
|
||
func (c *Controller) getRegionsHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := log.With("req_id", r.Header.Get("X-Request-ID")) | ||
|
||
logger.Debugw("handle request", "command", "list tasks") | ||
defer logger.Debugw("request handled", "command", "list tasks") | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
enableCors(&w, r) | ||
|
||
json.NewEncoder(w).Encode(&RegionList{ | ||
Regions: c.spawner.Regions(), | ||
}) | ||
} |
Oops, something went wrong.