2020-05-04 10:28:42 +02:00
|
|
|
package run
|
2020-05-01 15:28:44 +02:00
|
|
|
|
|
|
|
import (
|
2020-05-18 10:33:01 +02:00
|
|
|
"fmt"
|
2020-05-01 16:03:33 +02:00
|
|
|
"strconv"
|
2020-05-18 10:33:01 +02:00
|
|
|
"strings"
|
2020-05-14 12:17:06 +02:00
|
|
|
|
2020-05-21 19:28:42 +02:00
|
|
|
"github.com/docker/docker/pkg/namesgenerator"
|
2020-05-14 12:17:06 +02:00
|
|
|
"github.com/docker/go-connections/nat"
|
2020-05-01 15:28:44 +02:00
|
|
|
|
|
|
|
"github.com/docker/api/containers"
|
|
|
|
)
|
|
|
|
|
2020-05-15 17:52:19 +02:00
|
|
|
// Opts contain run command options
|
|
|
|
type Opts struct {
|
|
|
|
Name string
|
|
|
|
Publish []string
|
2020-05-18 10:33:01 +02:00
|
|
|
Labels []string
|
2020-05-07 04:58:04 +02:00
|
|
|
Volumes []string
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-15 17:52:19 +02:00
|
|
|
// ToContainerConfig convert run options to a container configuration
|
|
|
|
func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
|
2020-05-21 19:28:42 +02:00
|
|
|
if r.Name == "" {
|
|
|
|
r.Name = getRandomName()
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:52:19 +02:00
|
|
|
publish, err := r.toPorts()
|
|
|
|
if err != nil {
|
|
|
|
return containers.ContainerConfig{}, err
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:33:01 +02:00
|
|
|
labels, err := toLabels(r.Labels)
|
|
|
|
if err != nil {
|
|
|
|
return containers.ContainerConfig{}, err
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:52:19 +02:00
|
|
|
return containers.ContainerConfig{
|
2020-05-19 11:29:48 +02:00
|
|
|
ID: r.Name,
|
|
|
|
Image: image,
|
|
|
|
Ports: publish,
|
|
|
|
Labels: labels,
|
|
|
|
Volumes: r.Volumes,
|
2020-05-15 17:52:19 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Opts) toPorts() ([]containers.Port, error) {
|
|
|
|
_, bindings, err := nat.ParsePortSpecs(r.Publish)
|
2020-05-14 12:17:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
var result []containers.Port
|
|
|
|
|
2020-05-14 12:17:06 +02:00
|
|
|
for port, bind := range bindings {
|
|
|
|
for _, portbind := range bind {
|
|
|
|
var hostPort uint32
|
|
|
|
if portbind.HostPort != "" {
|
|
|
|
hp, err := strconv.Atoi(portbind.HostPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hostPort = uint32(hp)
|
|
|
|
} else {
|
|
|
|
hostPort = uint32(port.Int())
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, containers.Port{
|
|
|
|
HostPort: hostPort,
|
|
|
|
ContainerPort: uint32(port.Int()),
|
|
|
|
Protocol: port.Proto(),
|
|
|
|
HostIP: portbind.HostIP,
|
|
|
|
})
|
2020-05-01 16:03:33 +02:00
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
}
|
2020-05-14 12:17:06 +02:00
|
|
|
|
2020-05-01 15:28:44 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
2020-05-18 10:33:01 +02:00
|
|
|
|
|
|
|
func toLabels(labels []string) (map[string]string, error) {
|
|
|
|
result := map[string]string{}
|
|
|
|
for _, label := range labels {
|
|
|
|
parts := strings.Split(label, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, fmt.Errorf("wrong label format %q", label)
|
|
|
|
}
|
|
|
|
result[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2020-05-21 19:28:42 +02:00
|
|
|
|
|
|
|
func getRandomName() string {
|
|
|
|
// Azure supports hyphen but not underscore in names
|
|
|
|
return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
|
|
|
|
}
|