p2p/simulations/adapters: fix docker adapter

This commit is contained in:
Felix Lange 2018-10-11 01:57:09 +02:00
parent bfa7e3fe75
commit d8c86d737d
2 changed files with 19 additions and 20 deletions

View file

@ -121,18 +121,13 @@ type DockerNode struct {
ExecNode ExecNode
} }
// dockerCommand returns a command which exec's the binary in a Docker // dockerCommand returns a command which exec's the binary in a Docker container.
// container.
//
// It uses a shell so that we can pass the _P2P_NODE_CONFIG environment
// variable to the container using the --env flag.
func (n *DockerNode) dockerCommand() *exec.Cmd { func (n *DockerNode) dockerCommand() *exec.Cmd {
return exec.Command( return exec.Command(
"sh", "-c", "docker", "run", "--interactive", "--env", envStatusURL, "--env", envNodeConfig,
fmt.Sprintf( dockerImage, "/bin/p2p-node",
`exec docker run --interactive --env _P2P_NODE_CONFIG="${_P2P_NODE_CONFIG}" %s p2p-node %s %s`, strings.Join(n.Config.Node.Services, ","),
dockerImage, strings.Join(n.Config.Node.Services, ","), n.ID.String(), n.ID.String(),
),
) )
} }

View file

@ -151,8 +151,7 @@ func (n *ExecNode) Client() (*rpc.Client, error) {
} }
// Start exec's the node passing the ID and service as command line arguments // Start exec's the node passing the ID and service as command line arguments
// and the node config encoded as JSON in the _P2P_NODE_CONFIG environment // and the node config encoded as JSON in an environment variable.
// variable
func (n *ExecNode) Start(snapshots map[string][]byte) (err error) { func (n *ExecNode) Start(snapshots map[string][]byte) (err error) {
if n.Cmd != nil { if n.Cmd != nil {
return errors.New("already started") return errors.New("already started")
@ -185,8 +184,8 @@ func (n *ExecNode) Start(snapshots map[string][]byte) (err error) {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), cmd.Env = append(os.Environ(),
"_P2P_STATUS_URL="+statusURL, envStatusURL+"="+statusURL,
"_P2P_NODE_CONFIG="+string(confData), envNodeConfig+"="+string(confData),
) )
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting node: %s", err) return fmt.Errorf("error starting node: %s", err)
@ -370,14 +369,14 @@ func ExternalIP() net.IP {
// execP2PNode starts a devp2p node when the current binary is executed with // execP2PNode starts a devp2p node when the current binary is executed with
// argv[0] being "p2p-node", reading the service / ID from argv[1] / argv[2] // argv[0] being "p2p-node", reading the service / ID from argv[1] / argv[2]
// and the node config from the _P2P_NODE_CONFIG environment variable // and the node config from an environment variable.
func execP2PNode() { func execP2PNode() {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat())) glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
glogger.Verbosity(log.LvlInfo) glogger.Verbosity(log.LvlInfo)
log.Root().SetHandler(glogger) log.Root().SetHandler(glogger)
statusURL := os.Getenv("_P2P_STATUS_URL") statusURL := os.Getenv(envStatusURL)
if statusURL == "" { if statusURL == "" {
log.Crit("missing _P2P_STATUS_URL") log.Crit("missing " + envStatusURL)
} }
// Start the node and gather startup report. // Start the node and gather startup report.
@ -416,13 +415,13 @@ func startExecNodeStack() (*node.Node, error) {
serviceNames := strings.Split(os.Args[1], ",") serviceNames := strings.Split(os.Args[1], ",")
// decode the config // decode the config
confEnv := os.Getenv("_P2P_NODE_CONFIG") confEnv := os.Getenv(envNodeConfig)
if confEnv == "" { if confEnv == "" {
return nil, fmt.Errorf("missing _P2P_NODE_CONFIG") return nil, fmt.Errorf("missing " + envNodeConfig)
} }
var conf execNodeConfig var conf execNodeConfig
if err := json.Unmarshal([]byte(confEnv), &conf); err != nil { if err := json.Unmarshal([]byte(confEnv), &conf); err != nil {
return nil, fmt.Errorf("error decoding _P2P_NODE_CONFIG: %v", err) return nil, fmt.Errorf("error decoding %s: %v", envNodeConfig, err)
} }
conf.Stack.P2P.PrivateKey = conf.Node.PrivateKey conf.Stack.P2P.PrivateKey = conf.Node.PrivateKey
conf.Stack.Logger = log.New("node.id", conf.Node.ID.String()) conf.Stack.Logger = log.New("node.id", conf.Node.ID.String())
@ -484,6 +483,11 @@ func startExecNodeStack() (*node.Node, error) {
return stack, err return stack, err
} }
const (
envStatusURL = "_P2P_STATUS_URL"
envNodeConfig = "_P2P_NODE_CONFIG"
)
// nodeStartupJSON is sent to the simulation host after startup. // nodeStartupJSON is sent to the simulation host after startup.
type nodeStartupJSON struct { type nodeStartupJSON struct {
Err string Err string