Merge pull request #16 from getamis/feature/image-pull-bugs

container, tests: refactoring and new features
This commit is contained in:
Alan Chen 2017-08-16 11:35:59 +08:00 committed by GitHub
commit 1620003cc8
6 changed files with 138 additions and 44 deletions

View File

@ -22,7 +22,6 @@ services:
- docker
install:
- docker pull quay.io/amis/geth:istanbul_develop
- go get github.com/Masterminds/glide
script:

51
container/container.go Normal file
View File

@ -0,0 +1,51 @@
// 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 (
"log"
"net"
"net/url"
)
func (eth *ethereum) Image() string {
if eth.imageTag == "" {
return eth.imageRepository + ":latest"
}
return eth.imageRepository + ":" + eth.imageTag
}
func (eth *ethereum) Host() string {
var host string
daemonHost := eth.client.DaemonHost()
url, err := url.Parse(daemonHost)
if err != nil {
log.Printf("Failed to parse daemon host, err: %v", err)
return host
}
if url.Scheme == "unix" {
host = "localhost"
} else {
host, _, err = net.SplitHostPort(url.Host)
if err != nil {
log.Printf("Failed to split host and port, err: %v", err)
}
}
return host
}

View File

@ -20,6 +20,7 @@ import (
"context"
"errors"
"io"
"io/ioutil"
"log"
"math/big"
"os"
@ -28,6 +29,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/getamis/go-ethereum/cmd/utils"
@ -44,18 +46,41 @@ type Ethereum interface {
Init(string) error
Start() error
Stop() error
Host() string
NewClient() *ethclient.Client
}
func NewEthereum(c *client.Client, options ...Option) *ethereum {
geth := &ethereum{
eth := &ethereum{
client: c,
}
for _, opt := range options {
opt(geth)
opt(eth)
}
return geth
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 {
io.Copy(ioutil.Discard, out)
}
}
return eth
}
type ethereum struct {
@ -67,36 +92,18 @@ type ethereum struct {
rpcPort string
hostName string
containerID string
imageName string
logging bool
client *client.Client
imageRepository string
imageTag string
logging bool
client *client.Client
}
func (eth *ethereum) Init(genesisFile string) error {
results, err := eth.client.ImageSearch(context.Background(), eth.imageName, types.ImageSearchOptions{
Limit: 1,
})
if err != nil {
log.Printf("Cannot search %s, err: %v", eth.imageName, err)
return err
}
if len(results) == 0 {
out, err := eth.client.ImagePull(context.Background(), eth.imageName, types.ImagePullOptions{})
if err != nil {
log.Printf("Cannot pull %s, err: %v", eth.imageName, err)
return err
}
if eth.logging {
io.Copy(os.Stdout, out)
} else {
_ = out
}
}
resp, err := eth.client.ContainerCreate(context.Background(),
&container.Config{
Image: eth.imageName,
Image: eth.Image(),
Cmd: []string{
"init",
"--" + utils.DataDirFlag.Name,
@ -120,16 +127,36 @@ func (eth *ethereum) Init(genesisFile string) error {
go eth.showLog(context.Background())
}
}()
eth.containerID = resp.ID
return eth.client.ContainerStart(context.Background(), eth.containerID, types.ContainerStartOptions{})
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,
})
}
func (eth *ethereum) Start() error {
resp, err := eth.client.ContainerCreate(context.Background(),
&container.Config{
Hostname: "geth-" + eth.hostName,
Image: eth.imageName,
Image: eth.Image(),
Cmd: eth.flags,
ExposedPorts: map[nat.Port]struct{}{
nat.Port(eth.port): {},
@ -174,8 +201,8 @@ func (eth *ethereum) Start() error {
}
for i := 0; i < healthCheckRetryCount; i++ {
cli, err := ethclient.Dial("http://localhost:" + eth.rpcPort)
if err != nil {
cli := eth.NewClient()
if cli == nil {
time.Sleep(healthCheckRetryDelay)
continue
}
@ -231,6 +258,17 @@ func (eth *ethereum) Running() bool {
return false
}
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
}
// ----------------------------------------------------------------------------
func (eth *ethereum) showLog(context context.Context) {
if readCloser, err := eth.client.ContainerLogs(context, eth.containerID,
types.ContainerLogsOptions{ShowStderr: true, Follow: true}); err == nil {

View File

@ -36,7 +36,8 @@ func TestEthereumContainer(t *testing.T) {
for _, env := range envs {
geth := NewEthereum(
env.Client,
ImageName("quay.io/amis/geth:istanbul_develop"),
ImageRepository("quay.io/amis/geth"),
ImageTag("istanbul_develop"),
HostDataDir(env.DataDir),
DataDir("/data"),
Port(fmt.Sprintf("%d", env.P2PPort)),
@ -44,7 +45,6 @@ func TestEthereumContainer(t *testing.T) {
RPCAddress("0.0.0.0"),
RPCAPI("eth,net,web3,personal"),
RPCPort(fmt.Sprintf("%d", env.RpcPort)),
Logging(true),
)
err := geth.Init(filepath.Join(env.DataDir, genesis.FileName))

View File

@ -24,9 +24,15 @@ import (
type Option func(*ethereum)
func ImageName(imageName string) Option {
func ImageRepository(repository string) Option {
return func(eth *ethereum) {
eth.imageName = imageName
eth.imageRepository = repository
}
}
func ImageTag(tag string) Option {
return func(eth *ethereum) {
eth.imageTag = tag
}
}

View File

@ -26,7 +26,6 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/getamis/go-ethereum/ethclient"
"github.com/getamis/istanbul-tools/container"
"github.com/getamis/istanbul-tools/core"
"github.com/getamis/istanbul-tools/core/genesis"
@ -51,7 +50,8 @@ var _ = Describe("4 validators Istanbul", func() {
for _, env := range envs {
geth := container.NewEthereum(
env.Client,
container.ImageName("quay.io/amis/geth:istanbul_develop"),
container.ImageRepository("quay.io/amis/geth"),
container.ImageTag("istanbul_develop"),
container.HostDataDir(env.DataDir),
container.DataDir("/data"),
container.Port(fmt.Sprintf("%d", env.P2PPort)),
@ -83,11 +83,11 @@ var _ = Describe("4 validators Istanbul", func() {
})
It("Blockchain creation", func() {
for _, env := range envs {
cli, err := ethclient.Dial("http://localhost:" + fmt.Sprintf("%d", env.RpcPort))
Expect(err).To(BeNil())
for _, geth := range geths {
client := geth.NewClient()
Expect(client).NotTo(BeNil())
block, err := cli.BlockByNumber(context.Background(), big.NewInt(0))
block, err := client.BlockByNumber(context.Background(), big.NewInt(0))
Expect(err).To(BeNil())
Expect(block).NotTo(BeNil())
}