2017-08-08 00:31:13 -07:00
|
|
|
// Copyright 2017 AMIS Technologies
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-08-11 01:18:35 -07:00
|
|
|
"errors"
|
2017-08-08 00:31:13 -07:00
|
|
|
"io"
|
2017-08-15 20:22:29 -07:00
|
|
|
"io/ioutil"
|
2017-08-08 00:31:13 -07:00
|
|
|
"log"
|
2017-08-11 01:18:35 -07:00
|
|
|
"math/big"
|
2017-08-08 00:31:13 -07:00
|
|
|
"os"
|
2017-08-10 23:03:12 -07:00
|
|
|
"path/filepath"
|
2017-08-08 00:31:13 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-08-14 23:50:56 -07:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2017-08-09 01:53:14 -07:00
|
|
|
"github.com/docker/docker/client"
|
2017-08-11 01:18:35 -07:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-08-10 23:03:12 -07:00
|
|
|
"github.com/getamis/go-ethereum/cmd/utils"
|
2017-08-11 01:18:35 -07:00
|
|
|
"github.com/getamis/go-ethereum/ethclient"
|
2017-08-13 23:05:36 -07:00
|
|
|
"github.com/getamis/istanbul-tools/core/genesis"
|
2017-08-08 00:31:13 -07:00
|
|
|
)
|
|
|
|
|
2017-08-11 01:18:35 -07:00
|
|
|
const (
|
|
|
|
healthCheckRetryCount = 5
|
|
|
|
healthCheckRetryDelay = 2 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type Ethereum interface {
|
|
|
|
Init(string) error
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
2017-08-15 00:19:43 -07:00
|
|
|
|
|
|
|
Host() string
|
|
|
|
NewClient() *ethclient.Client
|
2017-08-11 01:18:35 -07:00
|
|
|
}
|
|
|
|
|
2017-08-13 20:45:03 -07:00
|
|
|
func NewEthereum(c *client.Client, options ...Option) *ethereum {
|
2017-08-15 03:24:06 -07:00
|
|
|
eth := ðereum{
|
2017-08-13 20:45:03 -07:00
|
|
|
client: c,
|
2017-08-09 01:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range options {
|
2017-08-15 03:24:06 -07:00
|
|
|
opt(eth)
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
2017-08-09 01:53:14 -07:00
|
|
|
|
2017-08-15 03:24:06 -07:00
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("reference", eth.Image())
|
|
|
|
|
|
|
|
images, err := c.ImageList(context.Background(), types.ImageListOptions{
|
|
|
|
Filters: filters,
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(images) == 0 || err != nil {
|
|
|
|
out, err := eth.client.ImagePull(context.Background(), eth.Image(), types.ImagePullOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot pull %s, err: %v", eth.Image(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if eth.logging {
|
|
|
|
io.Copy(os.Stdout, out)
|
|
|
|
} else {
|
2017-08-15 20:22:29 -07:00
|
|
|
io.Copy(ioutil.Discard, out)
|
2017-08-15 03:24:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eth
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type ethereum struct {
|
2017-08-11 01:18:35 -07:00
|
|
|
ok bool
|
2017-08-09 01:53:14 -07:00
|
|
|
flags []string
|
2017-08-10 23:03:12 -07:00
|
|
|
hostDataDir string
|
|
|
|
dataDir string
|
2017-08-11 01:18:35 -07:00
|
|
|
port string
|
|
|
|
rpcPort string
|
2017-08-09 01:53:14 -07:00
|
|
|
hostName string
|
2017-08-08 00:31:13 -07:00
|
|
|
containerID string
|
2017-08-15 00:09:41 -07:00
|
|
|
|
|
|
|
imageRepository string
|
|
|
|
imageTag string
|
|
|
|
|
|
|
|
logging bool
|
|
|
|
client *client.Client
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
|
|
|
|
2017-08-10 23:03:12 -07:00
|
|
|
func (eth *ethereum) Init(genesisFile string) error {
|
|
|
|
resp, err := eth.client.ContainerCreate(context.Background(),
|
|
|
|
&container.Config{
|
2017-08-15 00:09:41 -07:00
|
|
|
Image: eth.Image(),
|
2017-08-10 23:03:12 -07:00
|
|
|
Cmd: []string{
|
|
|
|
"init",
|
|
|
|
"--" + utils.DataDirFlag.Name,
|
|
|
|
eth.dataDir,
|
2017-08-13 23:05:36 -07:00
|
|
|
filepath.Join("/", genesis.FileName),
|
2017-08-10 23:03:12 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&container.HostConfig{
|
|
|
|
Binds: []string{
|
2017-08-13 23:05:36 -07:00
|
|
|
genesisFile + ":" + filepath.Join("/", genesis.FileName),
|
2017-08-10 23:03:12 -07:00
|
|
|
eth.hostDataDir + ":" + eth.dataDir,
|
|
|
|
},
|
|
|
|
}, nil, "")
|
2017-08-08 00:31:13 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to create container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2017-08-10 23:03:12 -07:00
|
|
|
if eth.logging {
|
|
|
|
go eth.showLog(context.Background())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-15 03:24:06 -07:00
|
|
|
id := resp.ID
|
|
|
|
|
|
|
|
if err := eth.client.ContainerStart(context.Background(), id, types.ContainerStartOptions{}); err != nil {
|
|
|
|
log.Printf("Failed to start container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resC, errC := eth.client.ContainerWait(ctx, id, container.WaitConditionNotRunning)
|
|
|
|
select {
|
|
|
|
case <-resC:
|
|
|
|
case <-errC:
|
|
|
|
log.Printf("Failed to wait container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return eth.client.ContainerRemove(context.Background(), id,
|
|
|
|
types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
2017-08-10 23:03:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Start() error {
|
|
|
|
resp, err := eth.client.ContainerCreate(context.Background(),
|
|
|
|
&container.Config{
|
|
|
|
Hostname: "geth-" + eth.hostName,
|
2017-08-15 00:09:41 -07:00
|
|
|
Image: eth.Image(),
|
2017-08-10 23:03:12 -07:00
|
|
|
Cmd: eth.flags,
|
2017-08-11 01:18:35 -07:00
|
|
|
ExposedPorts: map[nat.Port]struct{}{
|
|
|
|
nat.Port(eth.port): {},
|
|
|
|
nat.Port(eth.rpcPort): {},
|
|
|
|
},
|
2017-08-10 23:03:12 -07:00
|
|
|
},
|
|
|
|
&container.HostConfig{
|
|
|
|
Binds: []string{
|
|
|
|
eth.hostDataDir + ":" + eth.dataDir,
|
|
|
|
},
|
2017-08-11 01:18:35 -07:00
|
|
|
PortBindings: nat.PortMap{
|
|
|
|
nat.Port(eth.port): []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: eth.port,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nat.Port(eth.rpcPort): []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: eth.rpcPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-08-10 23:03:12 -07:00
|
|
|
}, nil, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to create container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if eth.logging {
|
2017-08-08 00:31:13 -07:00
|
|
|
go eth.showLog(context.Background())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
eth.containerID = resp.ID
|
|
|
|
|
2017-08-11 01:18:35 -07:00
|
|
|
err = eth.client.ContainerStart(context.Background(), eth.containerID, types.ContainerStartOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to start container, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < healthCheckRetryCount; i++ {
|
2017-08-15 00:19:43 -07:00
|
|
|
cli := eth.NewClient()
|
|
|
|
if cli == nil {
|
2017-08-11 01:18:35 -07:00
|
|
|
time.Sleep(healthCheckRetryDelay)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = cli.BlockByNumber(context.Background(), big.NewInt(0))
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(healthCheckRetryDelay)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
eth.ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !eth.ok {
|
|
|
|
return errors.New("Failed to start geth")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-08-08 00:31:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Stop() error {
|
|
|
|
timeout := 10 * time.Second
|
|
|
|
err := eth.client.ContainerStop(context.Background(), eth.containerID, &timeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return eth.client.ContainerRemove(context.Background(), eth.containerID,
|
|
|
|
types.ContainerRemoveOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Wait(t time.Duration) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t)
|
|
|
|
defer cancel()
|
|
|
|
_, errCh := eth.client.ContainerWait(ctx, eth.containerID, "")
|
|
|
|
return <-errCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eth *ethereum) Running() bool {
|
|
|
|
containers, err := eth.client.ContainerList(context.Background(), types.ContainerListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to list containers, err: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
if c.ID == eth.containerID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:19:43 -07:00
|
|
|
func (eth *ethereum) NewClient() *ethclient.Client {
|
|
|
|
client, err := ethclient.Dial("http://" + eth.Host() + ":" + eth.rpcPort)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to dial to geth, err: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2017-08-15 00:09:41 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-08-08 00:31:13 -07:00
|
|
|
func (eth *ethereum) showLog(context context.Context) {
|
|
|
|
if readCloser, err := eth.client.ContainerLogs(context, eth.containerID,
|
|
|
|
types.ContainerLogsOptions{ShowStderr: true, Follow: true}); err == nil {
|
|
|
|
defer readCloser.Close()
|
|
|
|
_, err = io.Copy(os.Stdout, readCloser)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|