Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #260 from vania-pooh/master
Browse files Browse the repository at this point in the history
Starting everything on custom Docker network (fixes #257)
  • Loading branch information
aandryashin authored Mar 1, 2020
2 parents b8dd6c5 + f04b476 commit 0e66d86
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
36 changes: 29 additions & 7 deletions selenoid/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ func (c *DockerConfigurator) PrintArgs() error {
const (
videoDirName = "video"
logsDirName = "logs"
networkName = "selenoid"
)

func (c *DockerConfigurator) Start() error {
Expand Down Expand Up @@ -725,6 +726,9 @@ func (c *DockerConfigurator) Start() error {
if !c.DisableLogs && !contains(cmd, "-log-output-dir") && isLogSavingSupported(c.Logger, c.Version) {
cmd = append(cmd, "-log-output-dir", "/opt/selenoid/logs/")
}
if !contains(cmd, "-container-network") {
cmd = append(cmd, "-container-network", networkName)
}

overrideEnv := strings.Fields(c.Env)
if !strings.Contains(c.Env, "OVERRIDE_VIDEO_OUTPUT_DIR") {
Expand All @@ -736,6 +740,7 @@ func (c *DockerConfigurator) Start() error {
HostPort: c.Port,
ServicePort: SelenoidDefaultPort,
Volumes: volumes,
Network: networkName,
Cmd: cmd,
OverrideEnv: overrideEnv,
UserNS: c.UserNS,
Expand Down Expand Up @@ -819,7 +824,7 @@ func (c *DockerConfigurator) StartUI() error {
return errors.New("Selenoid UI image is not downloaded: this is probably a bug")
}

var cmd, links []string
var cmd, candidates []string
var selenoidUri string
containers:
for _, containerName := range []string{
Expand All @@ -829,7 +834,7 @@ containers:
for _, p := range ctr.Ports {
if p.PublicPort != 0 {
selenoidUri = fmt.Sprintf("--selenoid-uri=http://%s:%d", containerName, p.PublicPort)
links = []string{containerName}
candidates = []string{containerName}
break containers
}
}
Expand All @@ -843,7 +848,7 @@ containers:
cmd = append(cmd, selenoidUri)
}

if len(links) == 0 {
if len(candidates) == 0 {
c.Errorf("Neither Selenoid nor Ggr UI is started. Selenoid UI may not work.")
}

Expand All @@ -853,7 +858,7 @@ containers:
Image: image,
HostPort: c.Port,
ServicePort: SelenoidUIDefaultPort,
Links: links,
Network: networkName,
Cmd: cmd,
OverrideEnv: overrideEnv,
UserNS: c.UserNS,
Expand All @@ -878,7 +883,7 @@ type containerConfig struct {
HostPort int
ServicePort int
Volumes []string
Links []string
Network string
Cmd []string
OverrideEnv []string
UserNS string
Expand All @@ -900,6 +905,11 @@ func (c *DockerConfigurator) startContainer(cfg *containerConfig) error {
if err != nil {
return fmt.Errorf("failed to init port: %v", err)
}

err = c.createNetworkIfNeeded(cfg.Network)
if err != nil {
return fmt.Errorf("failed to configure container network: %v", err)
}
containerConfig := container.Config{
Hostname: "localhost",
Image: cfg.Image.RepoTags[0],
Expand All @@ -912,8 +922,8 @@ func (c *DockerConfigurator) startContainer(cfg *containerConfig) error {
containerConfig.Cmd = strslice.StrSlice(cfg.Cmd)
}
hostConfig := container.HostConfig{
Binds: cfg.Volumes,
Links: cfg.Links,
Binds: cfg.Volumes,
NetworkMode: networkName,
}
if cfg.UserNS != "" {
mode := container.UsernsMode(cfg.UserNS)
Expand Down Expand Up @@ -962,6 +972,18 @@ func (c *DockerConfigurator) startContainer(cfg *containerConfig) error {
return nil
}

func (c *DockerConfigurator) createNetworkIfNeeded(networkName string) error {
ctx := context.Background()
_, err := c.docker.NetworkInspect(ctx, networkName, types.NetworkInspectOptions{})
if err != nil {
_, err = c.docker.NetworkCreate(ctx, networkName, types.NetworkCreate{CheckDuplicate: true})
if err != nil {
return fmt.Errorf("failed to create custom network %s: %v", networkName, err)
}
}
return nil
}

func (c *DockerConfigurator) removeContainer(id string) error {
return c.docker.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true})
}
Expand Down
41 changes: 41 additions & 0 deletions selenoid/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,47 @@ func mux() http.Handler {
w.Write([]byte(output))
},
))
mux.HandleFunc("/v1.29/networks/selenoid", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`
[{
"Name": "selenoid",
"Id": "39d591dabe313ed90b599e6d6515301e879c088b449a260cc02981bd25b52a6f",
"Created": "2020-02-29T14:41:12.960257Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}]`))
},
))
mux.HandleFunc("/v1.29/networks/create", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
output := `{"id": "39d591dabe31", "warnings": []}`
w.Write([]byte(output))
},
))
mux.HandleFunc("/v1.29/containers/create", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
Expand Down

0 comments on commit 0e66d86

Please sign in to comment.