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

Commit

Permalink
Merge pull request #174 from vania-pooh/master
Browse files Browse the repository at this point in the history
Automatically setting up saving container logs
  • Loading branch information
aandryashin authored Jul 18, 2018
2 parents 038f3ff + 9571e4d commit d3b09ef
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
60 changes: 41 additions & 19 deletions selenoid/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,30 +588,26 @@ func (c *DockerConfigurator) IsRunning() bool {
}

func (c *DockerConfigurator) getSelenoidContainer() *types.Container {
return c.getContainer(selenoidContainerName, c.Port)
return c.getContainer(selenoidContainerName)
}

func (c *DockerConfigurator) IsUIRunning() bool {
return c.getSelenoidUIContainer() != nil
}

func (c *DockerConfigurator) getSelenoidUIContainer() *types.Container {
return c.getContainer(selenoidUIContainerName, c.Port)
return c.getContainer(selenoidUIContainerName)
}

func (c *DockerConfigurator) getContainer(name string, port int) *types.Container {
func (c *DockerConfigurator) getContainer(name string) *types.Container {
f := filters.NewArgs()
f.Add("name", name)
containers, err := c.docker.ContainerList(context.Background(), types.ContainerListOptions{Filters: f})
if err != nil {
return nil
}
for _, c := range containers {
for _, p := range c.Ports {
if p.PublicPort == uint16(port) {
return &c
}
}
if len(containers) > 0 {
return &containers[0]
}
return nil
}
Expand All @@ -624,18 +620,24 @@ func (c *DockerConfigurator) PrintArgs() error {
return c.startContainer("", image, 0, 0, []string{}, []string{}, []string{"--help"}, []string{}, true)
}

const (
videoDirName = "video"
logsDirName = "logs"
)

func (c *DockerConfigurator) Start() error {
image := c.getSelenoidImage()
if image == nil {
return errors.New("Selenoid image is not downloaded: this is probably a bug")
}

const videoDirName = "video"
volumeConfigDir := getVolumeConfigDir(c.ConfigDir, selenoidConfigDirElem)
videoConfigDir := getVolumeConfigDir(filepath.Join(c.ConfigDir, videoDirName), append(selenoidConfigDirElem, videoDirName))
logsConfigDir := getVolumeConfigDir(filepath.Join(c.ConfigDir, logsDirName), append(selenoidConfigDirElem, logsDirName))
volumes := []string{
fmt.Sprintf("%s:/etc/selenoid:ro", volumeConfigDir),
fmt.Sprintf("%s:/opt/selenoid/video", videoConfigDir),
fmt.Sprintf("%s:/opt/selenoid/logs", logsConfigDir),
}
const dockerSocket = "/var/run/docker.sock"
if c.isDockerForWindows() {
Expand All @@ -657,6 +659,10 @@ func (c *DockerConfigurator) Start() error {
cmd = append(cmd, "-video-output-dir", "/opt/selenoid/video/")
}

if !contains(cmd, "-log-output-dir") && isLogSavingSupported(c.Logger, c.Version) {
cmd = append(cmd, "-log-output-dir", "/opt/selenoid/logs/")
}

overrideEnv := strings.Fields(c.Env)
if !strings.Contains(c.Env, "OVERRIDE_VIDEO_OUTPUT_DIR") {
overrideEnv = append(overrideEnv, fmt.Sprintf("OVERRIDE_VIDEO_OUTPUT_DIR=%s", videoConfigDir))
Expand All @@ -674,18 +680,30 @@ func (c *DockerConfigurator) isDockerForWindows() bool {
}

func isVideoRecordingSupported(logger Logger, version string) bool {
return isVersion(version, ">= 1.4.0", func(version string) {
logger.Pointf(`Not enabling video feature because specified version "%s" is not semantic`, version)
})
}

func isVersion(version string, condition string, notSemanticVersionCallback func(string)) bool {
if version == Latest {
return true
}
constraint, _ := ver.NewConstraint(">= 1.4.0")
constraint, _ := ver.NewConstraint(condition)
v, err := ver.NewVersion(version)
if err != nil {
logger.Pointf(`Not enabling video feature because specified version "%s" is not semantic`, version)
notSemanticVersionCallback(version)
return false
}
return constraint.Check(v)
}

func isLogSavingSupported(logger Logger, version string) bool {
return isVersion(version, ">= 1.7.0", func(version string) {
logger.Pointf(`Not enabling log saving feature because specified version "%s" is not semantic`, version)
})
}

func getVolumeConfigDir(defaultConfigDir string, elem []string) string {
configDir := chooseVolumeConfigDir(defaultConfigDir, elem)
if runtime.GOOS == "windows" { //A bit ugly, but conditional compilation is even worse
Expand Down Expand Up @@ -730,14 +748,18 @@ func (c *DockerConfigurator) StartUI() error {

var cmd, links []string
var selenoidUri string
for containerName, port := range map[string]int{
selenoidContainerName: SelenoidDefaultPort,
ggrUIContainerName: GgrUIDefaultPort,
containers:
for _, containerName := range []string{
selenoidContainerName, ggrUIContainerName,
} {
if c.getContainer(containerName, port) != nil {
selenoidUri = fmt.Sprintf("--selenoid-uri=http://%s:%d", containerName, port)
links = []string{containerName}
break
if ctr := c.getContainer(containerName); ctr != nil {
for _, p := range ctr.Ports {
if p.PublicPort != 0 {
selenoidUri = fmt.Sprintf("--selenoid-uri=http://%s:%d", containerName, p.PublicPort)
links = []string{containerName}
break containers
}
}
}
}
overrideCmd := strings.Fields(c.Args)
Expand Down
1 change: 1 addition & 0 deletions selenoid/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func TestStartStopContainer(t *testing.T) {
c, err := NewDockerConfigurator(&LifecycleConfig{
RegistryUrl: mockDockerServer.URL,
Port: SelenoidDefaultPort,
Version: Latest,
})
AssertThat(t, err, Is{nil})
AssertThat(t, c.IsRunning(), Is{true})
Expand Down
5 changes: 5 additions & 0 deletions selenoid/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ func (d *DriversConfigurator) Start() error {
if !contains(args, "-disable-docker") {
args = append(args, "-disable-docker")
}
if !contains(args, "-log-output-dir") && isLogSavingSupported(d.Logger, d.Version) {
logsConfigDir := getVolumeConfigDir(filepath.Join(d.ConfigDir, logsDirName), append(selenoidConfigDirElem, logsDirName))
args = append(args, "-log-output-dir", logsConfigDir)
}

env := strings.Fields(d.Env)
return runCommand(d.getSelenoidBinaryPath(), args, env)
}
Expand Down

0 comments on commit d3b09ef

Please sign in to comment.