-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
440d784
commit f80331d
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mysql | ||
|
||
import ( | ||
"docker.io/go-docker" | ||
"docker.io/go-docker/api/types/container" | ||
"fmt" | ||
"upper.io/db.v3/lib/sqlbuilder" | ||
) | ||
|
||
func NewUnitV2() (sqlbuilder.Database, func()) { | ||
var cp ContainerProperties | ||
var err error | ||
|
||
cp.CLI, err = docker.NewEnvClient() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cp.Config = &container.Config{ | ||
Image: "mysql:5.6", | ||
Env: []string{ | ||
"MYSQL_ROOT_PASSWORD=root", | ||
"MYSQL_DATABASE=test_database_v2", | ||
}, | ||
} | ||
cp.HostPort = "3305" | ||
cp.TCP = "3306" | ||
cpr := cp.Creator(&DockerContainer{}) | ||
|
||
fmt.Println("container id ******************") | ||
fmt.Println(cpr.ID) | ||
|
||
fmt.Println("connection start ******************") | ||
connection, err := Connections.ConnectOrReuse("test_database_v2", "127.0.0.1:3305") | ||
|
||
fmt.Println("connection end ******************") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
tearDown := func() { | ||
_ = connection | ||
_ = connection.Close() | ||
} | ||
|
||
return connection, tearDown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mysql | ||
|
||
import ( | ||
"docker.io/go-docker" | ||
"docker.io/go-docker/api/types/container" | ||
) | ||
|
||
type ContainerProperties struct { | ||
Config *container.Config | ||
HostPort string | ||
CLI *docker.Client | ||
TCP string | ||
Name string | ||
} | ||
|
||
func (cp *ContainerProperties) Creator(d Docker) *DockerContainer { | ||
d.SetConfig(cp.Config) | ||
d.SetContainerHostPort(cp.HostPort) | ||
d.SetTCP(cp.TCP) | ||
d.SetContainerCLI(cp.CLI) | ||
d.SetContainerName(cp.Name) | ||
|
||
return d.CreateContainer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
"docker.io/go-docker" | ||
"docker.io/go-docker/api/types" | ||
"docker.io/go-docker/api/types/container" | ||
"github.com/docker/go-connections/nat" | ||
) | ||
|
||
type Docker interface { | ||
SetContainerID(id string) | ||
SetContainerName(name string) | ||
SetContainerHost(host string) | ||
SetContainerHostIP(hostIp string) | ||
SetContainerHostPort(hostPort string) | ||
SetTCP(tcp string) | ||
SetConfig(config *container.Config) | ||
SetContainerCLI(client *docker.Client) | ||
CreateContainer() *DockerContainer | ||
} | ||
|
||
type DockerContainer struct { | ||
ID string | ||
Host string | ||
Name string | ||
HostIP string | ||
HostPort string | ||
TCP string | ||
Config *container.Config | ||
CLI *docker.Client | ||
CustomArgs map[string]interface{} | ||
} | ||
|
||
func (d *DockerContainer) SetContainerID(id string) { | ||
d.ID = id | ||
} | ||
|
||
func (d *DockerContainer) SetContainerName(name string) { | ||
d.Name = name | ||
} | ||
|
||
func (d *DockerContainer) SetContainerHost(host string) { | ||
d.Host = host | ||
} | ||
|
||
func (d *DockerContainer) SetContainerHostIP(hostIp string) { | ||
d.HostIP = hostIp | ||
} | ||
|
||
func (d *DockerContainer) SetContainerHostPort(hostPort string) { | ||
d.HostPort = hostPort | ||
} | ||
|
||
func (d *DockerContainer) SetTCP(tcp string) { | ||
d.TCP = tcp | ||
} | ||
|
||
func (d *DockerContainer) SetConfig(config *container.Config) { | ||
d.Config = config | ||
} | ||
|
||
func (d *DockerContainer) SetContainerCLI(client *docker.Client) { | ||
d.CLI = client | ||
} | ||
|
||
func (d *DockerContainer) CreateContainer() *DockerContainer { | ||
ctx := context.Background() | ||
hostBinding := nat.PortBinding{ | ||
HostIP: d.HostIP, | ||
HostPort: d.HostPort, | ||
} | ||
containerPort, err := nat.NewPort("tcp", d.TCP) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
portBinding := nat.PortMap{containerPort: []nat.PortBinding{hostBinding}} | ||
hcfg := &container.HostConfig{ | ||
PortBindings: portBinding, | ||
} | ||
cnt, err := d.CLI.ContainerCreate(ctx, d.Config, hcfg, nil, d.Name) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err = d.CLI.ContainerStart(ctx, cnt.ID, types.ContainerStartOptions{}); err != nil { | ||
panic(err) | ||
} | ||
|
||
d.SetContainerID(cnt.ID) | ||
|
||
return d | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.