github.com/aquanetwork/aquachain@v1.7.8/p2p/simulations/adapters/docker.go (about) 1 // Copyright 2017 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package adapters 18 19 import ( 20 "errors" 21 "fmt" 22 "io" 23 "io/ioutil" 24 "os" 25 "os/exec" 26 "path/filepath" 27 "runtime" 28 "strings" 29 30 "github.com/docker/docker/pkg/reexec" 31 "gitlab.com/aquachain/aquachain/common/log" 32 "gitlab.com/aquachain/aquachain/node" 33 "gitlab.com/aquachain/aquachain/p2p/discover" 34 ) 35 36 // DockerAdapter is a NodeAdapter which runs simulation nodes inside Docker 37 // containers. 38 // 39 // A Docker image is built which contains the current binary at /bin/p2p-node 40 // which when executed runs the underlying service (see the description 41 // of the execP2PNode function for more details) 42 type DockerAdapter struct { 43 ExecAdapter 44 } 45 46 // NewDockerAdapter builds the p2p-node Docker image containing the current 47 // binary and returns a DockerAdapter 48 func NewDockerAdapter() (*DockerAdapter, error) { 49 // Since Docker containers run on Linux and this adapter runs the 50 // current binary in the container, it must be compiled for Linux. 51 // 52 // It is reasonable to require this because the caller can just 53 // compile the current binary in a Docker container. 54 if runtime.GOOS != "linux" { 55 return nil, errors.New("DockerAdapter can only be used on Linux as it uses the current binary (which must be a Linux binary)") 56 } 57 58 if err := buildDockerImage(); err != nil { 59 return nil, err 60 } 61 62 return &DockerAdapter{ 63 ExecAdapter{ 64 nodes: make(map[discover.NodeID]*ExecNode), 65 }, 66 }, nil 67 } 68 69 // Name returns the name of the adapter for logging purposes 70 func (d *DockerAdapter) Name() string { 71 return "docker-adapter" 72 } 73 74 // NewNode returns a new DockerNode using the given config 75 func (d *DockerAdapter) NewNode(config *NodeConfig) (Node, error) { 76 if len(config.Services) == 0 { 77 return nil, errors.New("node must have at least one service") 78 } 79 for _, service := range config.Services { 80 if _, exists := serviceFuncs[service]; !exists { 81 return nil, fmt.Errorf("unknown node service %q", service) 82 } 83 } 84 85 // generate the config 86 conf := &execNodeConfig{ 87 Stack: node.DefaultConfig, 88 Node: config, 89 } 90 conf.Stack.DataDir = "/data" 91 conf.Stack.WSHost = "0.0.0.0" 92 conf.Stack.WSOrigins = []string{"*"} 93 conf.Stack.WSExposeAll = true 94 conf.Stack.P2P.EnableMsgEvents = false 95 conf.Stack.P2P.NoDiscovery = true 96 conf.Stack.P2P.NAT = nil 97 conf.Stack.Logger = log.New("node.id", config.ID.String()) 98 99 node := &DockerNode{ 100 ExecNode: ExecNode{ 101 ID: config.ID, 102 Config: conf, 103 adapter: &d.ExecAdapter, 104 }, 105 } 106 node.newCmd = node.dockerCommand 107 d.ExecAdapter.nodes[node.ID] = &node.ExecNode 108 return node, nil 109 } 110 111 // DockerNode wraps an ExecNode but exec's the current binary in a docker 112 // container rather than locally 113 type DockerNode struct { 114 ExecNode 115 } 116 117 // dockerCommand returns a command which exec's the binary in a Docker 118 // container. 119 // 120 // It uses a shell so that we can pass the _P2P_NODE_CONFIG environment 121 // variable to the container using the --env flag. 122 func (n *DockerNode) dockerCommand() *exec.Cmd { 123 return exec.Command( 124 "sh", "-c", 125 fmt.Sprintf( 126 `exec docker run --interactive --env _P2P_NODE_CONFIG="${_P2P_NODE_CONFIG}" %s p2p-node %s %s`, 127 dockerImage, strings.Join(n.Config.Node.Services, ","), n.ID.String(), 128 ), 129 ) 130 } 131 132 // dockerImage is the name of the Docker image which gets built to run the 133 // simulation node 134 const dockerImage = "p2p-node" 135 136 // buildDockerImage builds the Docker image which is used to run the simulation 137 // node in a Docker container. 138 // 139 // It adds the current binary as "p2p-node" so that it runs execP2PNode 140 // when executed. 141 func buildDockerImage() error { 142 // create a directory to use as the build context 143 dir, err := ioutil.TempDir("", "p2p-docker") 144 if err != nil { 145 return err 146 } 147 defer os.RemoveAll(dir) 148 149 // copy the current binary into the build context 150 bin, err := os.Open(reexec.Self()) 151 if err != nil { 152 return err 153 } 154 defer bin.Close() 155 dst, err := os.OpenFile(filepath.Join(dir, "self.bin"), os.O_WRONLY|os.O_CREATE, 0755) 156 if err != nil { 157 return err 158 } 159 defer dst.Close() 160 if _, err := io.Copy(dst, bin); err != nil { 161 return err 162 } 163 164 // create the Dockerfile 165 dockerfile := []byte(` 166 FROM ubuntu:16.04 167 RUN mkdir /data 168 ADD self.bin /bin/p2p-node 169 `) 170 if err := ioutil.WriteFile(filepath.Join(dir, "Dockerfile"), dockerfile, 0644); err != nil { 171 return err 172 } 173 174 // run 'docker build' 175 cmd := exec.Command("docker", "build", "-t", dockerImage, dir) 176 cmd.Stdout = os.Stdout 177 cmd.Stderr = os.Stderr 178 if err := cmd.Run(); err != nil { 179 return fmt.Errorf("error building docker image: %s", err) 180 } 181 182 return nil 183 }