Skip to content

Commit

Permalink
Merge pull request #25 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added --limit flag (fixes #23)
  • Loading branch information
aandryashin committed Jun 16, 2017
2 parents 6089583 + d081957 commit 069ceec
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
9 changes: 8 additions & 1 deletion cmd/selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
skipDownload bool
vnc bool
force bool
limit int
)

func init() {
Expand Down Expand Up @@ -77,7 +78,12 @@ func initFlags() {
} {
c.Flags().BoolVarP(&force, "force", "f", false, "force action")
}

for _, c := range []*cobra.Command{
selenoidStartCmd,
selenoidUpdateCmd,
} {
c.Flags().IntVarP(&limit, "limit", "m", 0, "max parallel sessions limit (-limit parameter in Selenoid)")
}
}

func createLifecycle() (*selenoid.Lifecycle, error) {
Expand All @@ -87,6 +93,7 @@ func createLifecycle() (*selenoid.Lifecycle, error) {
ConfigDir: configDir,
Browsers: browsers,
Download: !skipDownload,
Limit: limit,

LastVersions: lastVersions,
RegistryUrl: registry,
Expand Down
4 changes: 4 additions & 0 deletions selenoid/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ type DownloadAware struct {
type RequestedBrowsersAware struct {
Browsers string
}

type LimitAware struct {
Limit int
}
23 changes: 17 additions & 6 deletions selenoid/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/heroku/docker-registry-client/registry"
Expand Down Expand Up @@ -42,6 +43,7 @@ type DockerConfigurator struct {
VersionAware
DownloadAware
RequestedBrowsersAware
LimitAware
LastVersions int
Pull bool
RegistryUrl string
Expand All @@ -58,6 +60,7 @@ func NewDockerConfigurator(config *LifecycleConfig) (*DockerConfigurator, error)
VersionAware: VersionAware{Version: config.Version},
DownloadAware: DownloadAware{DownloadNeeded: config.Download},
RequestedBrowsersAware: RequestedBrowsersAware{Browsers: config.Browsers},
LimitAware: LimitAware{Limit: config.Limit},
RegistryUrl: config.RegistryUrl,
LastVersions: config.LastVersions,
Tmpfs: config.Tmpfs,
Expand Down Expand Up @@ -396,13 +399,21 @@ func (c *DockerConfigurator) Start() error {
volumes = append(volumes, fmt.Sprintf("%s:%s", dockerSocket, dockerSocket))
}
ctx := context.Background()
containerConfig := container.Config{
Hostname: "localhost",
Image: image.RepoTags[0],
Env: env,
ExposedPorts: exposedPorts,
}
cmd := []string{}
if c.Limit > 0 {
cmd = append(cmd, "-limit", string(c.Limit))
}
if len(cmd) > 0 {
containerConfig.Cmd = strslice.StrSlice(cmd)
}
ctr, err := c.docker.ContainerCreate(ctx,
&container.Config{
Hostname: "localhost",
Image: image.RepoTags[0],
Env: env,
ExposedPorts: exposedPorts,
},
&containerConfig,
&container.HostConfig{
Binds: volumes,
PortBindings: portBindings,
Expand Down
1 change: 1 addition & 0 deletions selenoid/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func testConfigure(t *testing.T, download bool) {
LastVersions: 2,
Tmpfs: 512,
Browsers: "firefox,opera",
Limit: 42,
}
c, err := NewDockerConfigurator(&lcConfig)
AssertThat(t, err, Is{nil})
Expand Down
10 changes: 8 additions & 2 deletions selenoid/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type DriversConfigurator struct {
ConfigDirAware
VersionAware
DownloadAware
LimitAware
RequestedBrowsersAware
Browsers string
BrowsersJsonUrl string
Expand All @@ -75,6 +76,7 @@ func NewDriversConfigurator(config *LifecycleConfig) *DriversConfigurator {
Logger: Logger{Quiet: config.Quiet},
ConfigDirAware: ConfigDirAware{ConfigDir: config.ConfigDir},
VersionAware: VersionAware{Version: config.Version},
LimitAware: LimitAware{Limit: config.Limit},
DownloadAware: DownloadAware{DownloadNeeded: config.Download},
RequestedBrowsersAware: RequestedBrowsersAware{Browsers: config.Browsers},
BrowsersJsonUrl: config.BrowsersJsonUrl,
Expand Down Expand Up @@ -463,10 +465,14 @@ func (d *DriversConfigurator) IsRunning() bool {
}

func (d *DriversConfigurator) Start() error {
return runCommand(d.getSelenoidBinaryPath(), []string{
args := []string{
"-conf", getSelenoidConfigPath(d.ConfigDir),
"-disable-docker",
})
}
if d.Limit > 0 {
args = append(args, "-limit", string(d.Limit))
}
return runCommand(d.getSelenoidBinaryPath(), args)
}

var killFunc func(os.Process) error = func(p os.Process) error {
Expand Down
1 change: 1 addition & 0 deletions selenoid/drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ func TestStartStopProcess(t *testing.T) {
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Version: Latest,
Limit: 42,
}
configurator := NewDriversConfigurator(&lcConfig)
AssertThat(t, configurator.IsRunning(), Is{true})
Expand Down
1 change: 1 addition & 0 deletions selenoid/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type LifecycleConfig struct {
ConfigDir string
Browsers string
Download bool
Limit int

// Docker specific
LastVersions int
Expand Down

0 comments on commit 069ceec

Please sign in to comment.