Skip to content

Commit

Permalink
Merge pull request #338 from vania-pooh/master
Browse files Browse the repository at this point in the history
Correctly working video recording in custom networks (fixes #331)
  • Loading branch information
aandryashin committed Jan 20, 2018
2 parents 53a44d5 + fb08341 commit 890e34a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func init() {
flag.BoolVar(&version, "version", false, "Show version and exit")
flag.Var(&mem, "mem", "Containers memory limit e.g. 128m or 1g")
flag.Var(&cpu, "cpu", "Containers cpu limit as float e.g. 0.2 or 1.0")
flag.StringVar(&containerNetwork, "container-network", "default", "Network to be used for containers")
flag.StringVar(&containerNetwork, "container-network", service.DefaultContainerNetwork, "Network to be used for containers")
flag.BoolVar(&captureDriverLogs, "capture-driver-logs", false, "Whether to add driver process logs to Selenoid output")
flag.BoolVar(&disablePrivileged, "disable-privileged", false, "Whether to disable privileged container mode")
flag.StringVar(&videoOutputDir, "video-output-dir", "video", "Directory to save recorded video to")
Expand Down
27 changes: 17 additions & 10 deletions service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (d *Docker) StartWithCancel() (*StartedService, error) {
u := &url.URL{Scheme: "http", Host: seleniumHostPort, Path: d.Service.Path}

if d.Video {
videoContainerId, err = startVideoContainer(ctx, cl, requestId, browserContainerId, d.Environment, d.Caps)
videoContainerId, err = startVideoContainer(ctx, cl, requestId, stat, d.Environment, d.Caps)
if err != nil {
return nil, fmt.Errorf("start video container: %v", err)
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func getContainerIP(networkName string, stat types.ContainerJSON) string {
return ""
}

func startVideoContainer(ctx context.Context, cl *client.Client, requestId uint64, browserContainerId string, environ Environment, caps session.Caps) (string, error) {
func startVideoContainer(ctx context.Context, cl *client.Client, requestId uint64, browserContainer types.ContainerJSON, environ Environment, caps session.Caps) (string, error) {
videoContainerStartTime := time.Now()
videoContainerImage := environ.VideoContainerImage
env := []string{fmt.Sprintf("FILE_NAME=%s", caps.VideoName)}
Expand All @@ -287,29 +287,36 @@ func startVideoContainer(ctx context.Context, cl *client.Client, requestId uint6
if videoFrameRate > 0 {
env = append(env, fmt.Sprintf("FRAME_RATE=%d", videoFrameRate))
}
hostConfig := &ctr.HostConfig{
Binds: []string{fmt.Sprintf("%s:/data:rw", getVideoOutputDir(environ))},
AutoRemove: true,
NetworkMode: ctr.NetworkMode(environ.Network),
}
browserContainerName := getContainerIP(environ.Network, browserContainer)
if environ.Network == DefaultContainerNetwork {
const defaultBrowserContainerName = "browser"
hostConfig.Links = []string{fmt.Sprintf("%s:%s", browserContainer.ID, defaultBrowserContainerName)}
browserContainerName = defaultBrowserContainerName
}
env = append(env, fmt.Sprintf("BROWSER_CONTAINER_NAME=%s", browserContainerName))
log.Printf("[%d] [CREATING_VIDEO_CONTAINER] [%s]\n", requestId, videoContainerImage)
videoContainer, err := cl.ContainerCreate(ctx,
&ctr.Config{
Image: videoContainerImage,
Env: env,
},
&ctr.HostConfig{
Binds: []string{fmt.Sprintf("%s:/data:rw", getVideoOutputDir(environ))},
Links: []string{fmt.Sprintf("%s:browser", browserContainerId)},
AutoRemove: true,
NetworkMode: ctr.NetworkMode(environ.Network),
},
hostConfig,
&network.NetworkingConfig{}, "")
if err != nil {
removeContainer(ctx, cl, requestId, browserContainerId)
removeContainer(ctx, cl, requestId, browserContainer.ID)
return "", fmt.Errorf("create video container: %v", err)
}

videoContainerId := videoContainer.ID
log.Printf("[%d] [STARTING_VIDEO_CONTAINER] [%s] [%s]\n", requestId, videoContainerImage, videoContainerId)
err = cl.ContainerStart(ctx, videoContainerId, types.ContainerStartOptions{})
if err != nil {
removeContainer(ctx, cl, requestId, browserContainerId)
removeContainer(ctx, cl, requestId, browserContainer.ID)
removeContainer(ctx, cl, requestId, videoContainerId)
return "", fmt.Errorf("start video container: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type Environment struct {
Privileged bool
}

const (
DefaultContainerNetwork = "default"
)

// ServiceBase - stores fields required by all services
type ServiceBase struct {
RequestId uint64
Expand Down

0 comments on commit 890e34a

Please sign in to comment.