112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/client"
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
nattypes "github.com/docker/go-connections/nat"
|
|
networktypes "github.com/docker/docker/api/types/network"
|
|
)
|
|
|
|
type Module struct {
|
|
ID string `json:"id"`
|
|
Image string `json:"image"`
|
|
Name string `json:"name"`
|
|
User string `json:"user"`
|
|
Env []struct {
|
|
Var string `json:"var"`
|
|
Val string `json:"val"`
|
|
} `json:"env"`
|
|
Volumes []struct {
|
|
Host string `json:"host"`
|
|
Local string `json:"local"`
|
|
Tag string `json:"tag"`
|
|
} `json:"volumes"`
|
|
UrlsAllowed string `json:"urls_allowed"`
|
|
InternalAccess bool `json:"internal_access"`
|
|
}
|
|
|
|
func ContainerConfig(module Module, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkConfig *networktypes.NetworkingConfig) {
|
|
config.Image = module.Image
|
|
config.Hostname = module.Name
|
|
config.User = module.User
|
|
|
|
var env []string
|
|
for _, e := range module.Env {
|
|
v := fmt.Sprintf("%s=%s", e.Var, e.Val)
|
|
env = append(env, v)
|
|
}
|
|
config.Env = env
|
|
|
|
var vols []string
|
|
for _, v := range module.Volumes {
|
|
vol := fmt.Sprintf("%s:%s:%s", v.Host, v.Local, v.Tag)
|
|
vols = append(vols, vol)
|
|
}
|
|
|
|
hostConfig.Binds = vols
|
|
exposedPorts := nattypes.PortSet{
|
|
nattypes.Port("8080/tcp"): struct{}{},
|
|
}
|
|
config.ExposedPorts = exposedPorts
|
|
|
|
networkConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{
|
|
module.ID: &networktypes.EndpointSettings{},
|
|
}
|
|
}
|
|
|
|
func CreateModule(module Module) string {
|
|
ctx := context.Background()
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var container containertypes.Config
|
|
host := containertypes.HostConfig{}
|
|
network := networktypes.NetworkingConfig{}
|
|
ContainerConfig(module, &container, &host, &network)
|
|
|
|
image_out, err := cli.ImagePull(ctx, module.Image, types.ImagePullOptions{})
|
|
if err == nil {
|
|
defer image_out.Close()
|
|
io.Copy(ioutil.Discard, image_out)
|
|
}
|
|
fmt.Println("test1")
|
|
|
|
resp, err := cli.ContainerCreate(ctx, &container, &host, &network, nil, module.Name)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("test2")
|
|
|
|
if _, err := cli.NetworkInspect(ctx, module.ID, types.NetworkInspectOptions{}); err != nil {
|
|
networkConfig := types.NetworkCreate{
|
|
Internal: module.InternalAccess,
|
|
}
|
|
if _, err := cli.NetworkCreate(ctx, module.ID, networkConfig); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
fmt.Println("test3")
|
|
|
|
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("test3")
|
|
|
|
if err := cli.NetworkConnect(ctx, module.ID, os.Getenv("hostname"), &networktypes.EndpointSettings{}); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("test4")
|
|
|
|
return resp.ID
|
|
}
|