mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-11 15:08:09 -05:00
Compare commits
1 Commits
env1-sampl
...
fix/upgrad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db9d62f89f |
138
common/docker/container.go
Normal file
138
common/docker/container.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Container state.
|
||||
type dockerState string
|
||||
|
||||
var (
|
||||
runningState dockerState = "running"
|
||||
exitedState dockerState = "exited"
|
||||
)
|
||||
|
||||
func getSpecifiedContainer(image string, keyword string) (string, bool) {
|
||||
containers, _ := cli.ContainerList(context.Background(), types.ContainerListOptions{
|
||||
All: true,
|
||||
})
|
||||
var ID string
|
||||
for _, container := range containers {
|
||||
// Just use the exited container.
|
||||
if container.Image != image ||
|
||||
!strings.Contains(container.Names[0], keyword) {
|
||||
continue
|
||||
}
|
||||
|
||||
// If container state is running choose and return.
|
||||
if container.State == string(runningState) {
|
||||
return container.ID, true
|
||||
}
|
||||
// If state is exited just set id.
|
||||
if container.State == string(exitedState) {
|
||||
ID = container.ID
|
||||
}
|
||||
}
|
||||
|
||||
return ID, ID != ""
|
||||
}
|
||||
|
||||
func startContainer(id string, stdout io.Writer) (io.Closer, uint16, error) {
|
||||
// Get container again, check state and handle public port.
|
||||
ct, err := getContainerByID(id)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// start the exited container.
|
||||
if ct.State == string(exitedState) {
|
||||
// Start the exist container.
|
||||
err = cli.ContainerStart(context.Background(), id, types.ContainerStartOptions{})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
}
|
||||
|
||||
// Get container again, check state and handle public port.
|
||||
ct, err = getContainerByID(id)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if ct.State != string(runningState) {
|
||||
return nil, 0, fmt.Errorf("container state is not running, id: %s", id)
|
||||
}
|
||||
|
||||
readCloser, err := cli.ContainerLogs(context.Background(), id, types.ContainerLogsOptions{
|
||||
ShowStdout: true,
|
||||
ShowStderr: true,
|
||||
Follow: true,
|
||||
})
|
||||
|
||||
// Handle stdout.
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
_, err2 := stdcopy.StdCopy(stdout, stdout, readCloser)
|
||||
errCh <- err2
|
||||
}()
|
||||
select {
|
||||
case <-time.After(time.Millisecond * 200):
|
||||
case err = <-errCh:
|
||||
}
|
||||
return readCloser, ct.Ports[0].PublicPort, err
|
||||
}
|
||||
|
||||
func getContainerByID(id string) (*types.Container, error) {
|
||||
filter := filters.NewArgs()
|
||||
filter.Add("id", id)
|
||||
lst, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
|
||||
All: true,
|
||||
Filters: filter,
|
||||
})
|
||||
|
||||
if len(lst) == 1 {
|
||||
return &lst[0], err
|
||||
}
|
||||
return nil, fmt.Errorf("the container is not exist, id: %s", id)
|
||||
}
|
||||
|
||||
func InitConfig(name string) (string, error) {
|
||||
res, err := cli.ConfigCreate(context.Background(), swarm.ConfigSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Name: name,
|
||||
Labels: map[string]string{},
|
||||
},
|
||||
Data: []byte("postgres config for " + name),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return res.ID, nil
|
||||
}
|
||||
|
||||
func SetConfigLabel(id string, keyword string, setOrRm bool) (int, error) {
|
||||
filter := filters.NewArgs()
|
||||
filter.Add("id", id)
|
||||
cfgs, err := cli.ConfigList(context.Background(), types.ConfigListOptions{
|
||||
Filters: filter,
|
||||
})
|
||||
if err != nil || len(cfgs) == 0 {
|
||||
return 0, err
|
||||
}
|
||||
cfg := cfgs[0]
|
||||
if setOrRm {
|
||||
cfg.Spec.Labels[keyword] = ""
|
||||
} else {
|
||||
delete(cfg.Spec.Labels, keyword)
|
||||
}
|
||||
|
||||
// Update config and return count of labels.
|
||||
return len(cfg.Spec.Labels), cli.ConfigUpdate(context.Background(), id, cfg.Version, cfg.Spec)
|
||||
}
|
||||
64
common/docker/container_test.go
Normal file
64
common/docker/container_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package docker_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
cli *client.Client
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
cli, err = client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cli.NegotiateAPIVersion(context.Background())
|
||||
}
|
||||
|
||||
func TestPP(t *testing.T) {
|
||||
res, err := cli.ConfigCreate(context.Background(), swarm.ConfigSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Name: "zz",
|
||||
Labels: map[string]string{},
|
||||
},
|
||||
Data: []byte("const postgres config"),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
filter := filters.NewArgs()
|
||||
filter.Add("id", res.ID)
|
||||
lst, err := cli.ConfigList(context.Background(), types.ConfigListOptions{
|
||||
Filters: filter,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
t.Logf("ID: %s, version: %d\n", res.ID, lst[0].Version.Index)
|
||||
}
|
||||
|
||||
func TestCC(t *testing.T) {
|
||||
//err := cli.ConfigUpdate(context.Background(), "zma9k3nsfcmt9saimdd42p9ii", swarm.Version{Index: 166}, swarm.ConfigSpec{
|
||||
// Annotations: swarm.Annotations{
|
||||
// Name: "xx",
|
||||
// Labels: map[string]string{"1": "3"},
|
||||
// },
|
||||
//})
|
||||
//assert.NoError(t, err)
|
||||
|
||||
filter := filters.NewArgs()
|
||||
filter.Add("name", "zz")
|
||||
lst, err := cli.ConfigList(context.Background(), types.ConfigListOptions{
|
||||
Filters: filter,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
for _, l := range lst {
|
||||
t.Log(l.ID, l.Spec.Labels, l.Spec)
|
||||
err = cli.ConfigRemove(context.Background(), l.ID)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func NewDockerApp() *App {
|
||||
Timestamp: timestamp,
|
||||
L1gethImg: newTestL1Docker(),
|
||||
L2gethImg: newTestL2Docker(),
|
||||
DBImg: newTestDBDocker("postgres"),
|
||||
DBImg: newTestDBDocker(),
|
||||
DBConfigFile: fmt.Sprintf("/tmp/%d_db-config.json", timestamp),
|
||||
}
|
||||
if err := app.mockDBConfig(); err != nil {
|
||||
@@ -189,7 +189,7 @@ func newTestL2Docker() GethImgInstance {
|
||||
return NewImgGeth("scroll_l2geth", "", "", 0, l2StartPort+int(id.Int64()))
|
||||
}
|
||||
|
||||
func newTestDBDocker(driverName string) ImgInstance {
|
||||
func newTestDBDocker() ImgInstance {
|
||||
id, _ := rand.Int(rand.Reader, big.NewInt(2000))
|
||||
return NewImgDB(driverName, "123456", "test_db", dbStartPort+int(id.Int64()))
|
||||
return NewImgDB("123456", dbStartPort+int(id.Int64()))
|
||||
}
|
||||
|
||||
@@ -27,15 +27,17 @@ type ImgDB struct {
|
||||
}
|
||||
|
||||
// NewImgDB return postgres db img instance.
|
||||
func NewImgDB(image, password, dbName string, port int) ImgInstance {
|
||||
func NewImgDB(password string, port int) ImgInstance {
|
||||
image := "postgres"
|
||||
dbName := "test_db"
|
||||
img := &ImgDB{
|
||||
image: image,
|
||||
name: fmt.Sprintf("%s-%s_%d", image, dbName, port),
|
||||
name: fmt.Sprintf("%s-v1-%s_%d", image, dbName, port),
|
||||
password: password,
|
||||
dbName: dbName,
|
||||
port: port,
|
||||
}
|
||||
img.cmd = cmd.NewCmd(img.name, img.prepare()...)
|
||||
//img.cmd = cmd.NewCmd(img.name, img.prepare()...)
|
||||
return img
|
||||
}
|
||||
|
||||
@@ -84,10 +86,15 @@ func (i *ImgDB) IsRunning() bool {
|
||||
}
|
||||
|
||||
func (i *ImgDB) prepare() []string {
|
||||
cmd := []string{"docker", "run", "--rm", "--name", i.name, "-p", fmt.Sprintf("%d:5432", i.port)}
|
||||
cmd := []string{"docker", "run", "--rm", "-v", fmt.Sprintf("%s/pg-init-scripts:/docker-entrypoint-initdb.d", absPath), "--name", i.name, "-p", fmt.Sprintf("%d:5432", i.port)}
|
||||
|
||||
dbNames := i.dbName + "_0"
|
||||
for idx := 1; idx < 10; idx++ {
|
||||
dbNames += fmt.Sprintf(",%s_%d", i.dbName, idx)
|
||||
}
|
||||
envs := []string{
|
||||
"-e", "POSTGRES_PASSWORD=" + i.password,
|
||||
"-e", fmt.Sprintf("POSTGRES_DB=%s", i.dbName),
|
||||
"-e", fmt.Sprintf("POSTGRES_MULTIPLE_DATABASES=%s", dbNames),
|
||||
}
|
||||
|
||||
cmd = append(cmd, envs...)
|
||||
|
||||
@@ -3,6 +3,7 @@ package docker
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
@@ -10,7 +11,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
cli *client.Client
|
||||
cli *client.Client
|
||||
absPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -20,6 +22,8 @@ func init() {
|
||||
panic(err)
|
||||
}
|
||||
cli.NegotiateAPIVersion(context.Background())
|
||||
|
||||
absPath, _ = filepath.Abs("./")
|
||||
}
|
||||
|
||||
// ImgInstance is an img instance.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set -u
|
||||
|
||||
function create_user_and_database() {
|
||||
local database=$1
|
||||
echo " Creating user and database '$database'"
|
||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
|
||||
CREATE USER $database;
|
||||
CREATE DATABASE $database;
|
||||
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
|
||||
EOSQL
|
||||
}
|
||||
|
||||
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
|
||||
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
|
||||
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
|
||||
create_user_and_database $db
|
||||
done
|
||||
echo "Multiple databases created"
|
||||
fi
|
||||
Reference in New Issue
Block a user