Skip to content

Commit

Permalink
Merge pull request #12 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added capability to automatically download binaries (fixes #4, #5, #11)
  • Loading branch information
aandryashin committed Jun 3, 2017
2 parents d064038 + c34ece0 commit e11bb6d
Show file tree
Hide file tree
Showing 25 changed files with 2,331 additions and 424 deletions.
113 changes: 113 additions & 0 deletions browsers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"chrome": {
"command": "%s --whitelisted-ips='' --verbose",
"files": {
"linux": {
"386": {
"url": "http://chromedriver.storage.googleapis.com/2.29/chromedriver_linux32.zip",
"filename": "chromedriver"
},
"amd64": {
"url": "http://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip",
"filename": "chromedriver"
}
},
"darwin": {
"amd64": {
"url": "http://chromedriver.storage.googleapis.com/2.29/chromedriver_mac64.zip",
"filename": "chromedriver"
}
},
"windows": {
"386": {
"url": "http://chromedriver.storage.googleapis.com/2.29/chromedriver_win32.zip",
"filename": "chromedriver.exe"
},
"amd64": {
"url": "http://chromedriver.storage.googleapis.com/2.29/chromedriver_win32.zip",
"filename": "chromedriver.exe"
}
}
}
},

"firefox": {
"command": "%s --host :: --log debug",
"files": {
"linux": {
"amd64": {
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-linux64.tar.gz",
"filename": "geckodriver"
}
},
"darwin": {
"amd64": {
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-macos.tar.gz",
"filename": "geckodriver"
}
},
"windows": {
"386": {
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-win32.zip",
"filename": "geckodriver.exe"
},
"amd64": {
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-win64.zip",
"filename": "geckodriver.exe"
}
}
}
},

"opera": {
"command": "%s --port=4444 --whitelisted-ips='' --verbose",
"files": {
"linux": {
"386": {
"url": "https://github.com/operasoftware/operachromiumdriver/releases/download/v.2.27/operadriver_linux32.zip",
"filename": "operadriver_linux32/operadriver"
},
"amd64": {
"url": "https://github.com/operasoftware/operachromiumdriver/releases/download/v.2.27/operadriver_linux64.zip",
"filename": "operadriver_linux64/operadriver"
}
},
"darwin": {
"amd64": {
"url": "https://github.com/operasoftware/operachromiumdriver/releases/download/v.2.27/operadriver_mac64.zip",
"filename": "operadriver_mac64/operadriver"
}
},
"windows": {
"386": {
"url": "https://github.com/operasoftware/operachromiumdriver/releases/download/v.2.27/operadriver_win32.zip",
"filename": "operadriver_win32/operadriver.exe"
},
"amd64": {
"url": "https://github.com/operasoftware/operachromiumdriver/releases/download/v.2.27/operadriver_win64.zip",
"filename": "operadriver_win64/operadriver.exe"
}
}

}
},

"internet_explorer": {
"command": "%s",
"files": {
"windows": {
"386": {
"url": "http://selenium-release.storage.googleapis.com/3.4/IEDriverServer_Win32_3.4.0.zip",
"filename": "IEDriverServer.exe"
},
"amd64": {
"url": "http://selenium-release.storage.googleapis.com/3.4/IEDriverServer_x64_3.4.0.zip",
"filename": "IEDriverServer.exe"
}
}
}
}
}



2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
quiet bool
quiet bool
registry string
rootCmd = &cobra.Command{
Use: "cm",
Expand Down
127 changes: 99 additions & 28 deletions cmd/selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,117 @@ import (
"github.com/aerokube/cm/selenoid"
"github.com/spf13/cobra"
"os"
"os/user"
"path/filepath"
"runtime"
)

const (
registryUrl = "https://registry.hub.docker.com"
registryUrl = "https://registry.hub.docker.com"
defaultBrowsersJsonURL = "https://raw.githubusercontent.com/aerokube/cm/master/browsers.json"
)


var (
lastVersions int
pull bool
tmpfs int
lastVersions int
tmpfs int
operatingSystem string
arch string
version string
browsers string
browsersJSONUrl string
configDir string
skipDownload bool
force bool
)

func init() {
selenoidCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "suppress output")
selenoidCmd.Flags().StringVarP(&registry, "registry", "r", registryUrl, "Docker registry to use")
selenoidCmd.Flags().IntVarP(&lastVersions, "last-versions", "l", 5, "process only last N versions")
selenoidCmd.Flags().BoolVarP(&pull, "pull", "p", false, "pull images if not present")
selenoidCmd.Flags().IntVarP(&tmpfs, "tmpfs", "t", 0, "add tmpfs volume sized in megabytes")
initFlags()
selenoidCmd.AddCommand(selenoidDownloadCmd)
selenoidCmd.AddCommand(selenoidConfigureCmd)
selenoidCmd.AddCommand(selenoidStartCmd)
selenoidCmd.AddCommand(selenoidStopCmd)
selenoidCmd.AddCommand(selenoidUpdateCmd)
selenoidCmd.AddCommand(selenoidCleanupCmd)
}

func initFlags() {
for _, c := range []*cobra.Command{
selenoidDownloadCmd,
selenoidConfigureCmd,
selenoidStartCmd,
selenoidStopCmd,
selenoidUpdateCmd,
selenoidCleanupCmd,
} {
c.Flags().BoolVarP(&quiet, "quiet", "q", false, "suppress output")
c.Flags().StringVarP(&configDir, "config-dir", "c", getSelenoidConfigDir(), "directory to save files")
}
for _, c := range []*cobra.Command{
selenoidDownloadCmd,
selenoidConfigureCmd,
selenoidStartCmd,
selenoidUpdateCmd,
} {
c.Flags().StringVarP(&operatingSystem, "operating-system", "o", runtime.GOOS, "target operating system (drivers only)")
c.Flags().StringVarP(&arch, "architecture", "a", runtime.GOARCH, "target architecture (drivers only)")
c.Flags().StringVarP(&version, "version", "v", selenoid.Latest, "desired version; default is latest release")
c.Flags().StringVarP(&browsers, "browsers", "b", "", "comma separated list of browser names to process")
c.Flags().StringVarP(&browsersJSONUrl, "browsers-json", "j", defaultBrowsersJsonURL, "browsers JSON data URL (in most cases never need to be set manually)")
c.Flags().BoolVarP(&skipDownload, "no-download", "n", false, "only output config file without downloading images or drivers")
c.Flags().StringVarP(&registry, "registry", "r", registryUrl, "Docker registry to use")
c.Flags().IntVarP(&lastVersions, "last-versions", "l", 2, "process only last N versions (Docker only)")
c.Flags().IntVarP(&tmpfs, "tmpfs", "t", 0, "add tmpfs volume sized in megabytes (Docker only)")
}
for _, c := range []*cobra.Command{
selenoidDownloadCmd,
selenoidConfigureCmd,
selenoidStartCmd,
} {
c.Flags().BoolVarP(&force, "force", "f", false, "force action")
}

}

func createLifecycle() (*selenoid.Lifecycle, error) {
config := selenoid.LifecycleConfig{
Quiet: quiet,
Force: force,
ConfigDir: configDir,
Browsers: browsers,
Download: !skipDownload,

LastVersions: lastVersions,
RegistryUrl: registry,
Tmpfs: tmpfs,

BrowsersJsonUrl: browsersJSONUrl,
OS: operatingSystem,
Arch: arch,
Version: version,
}
return selenoid.NewLifecycle(&config)
}

var selenoidCmd = &cobra.Command{
Use: "selenoid",
Short: "Generate JSON configuration for Selenoid",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := selenoid.NewConfigurator(registry, quiet)
cfg.LastVersions = lastVersions
cfg.Pull = pull
cfg.Tmpfs = tmpfs
if err != nil {
fmt.Printf("Failed to initialize: %v\n", err)
os.Exit(1)
}
defer cfg.Close()
data, err := cfg.Configure()
if err != nil {
fmt.Printf("Failed to configure: %v\n", err)
os.Exit(1)
}
fmt.Println(data)
os.Exit(0)
Short: "Download, configure and run Selenoid",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}

func getConfigDir(elem ...string) string {
usr, err := user.Current()
if err != nil {
return filepath.Join(elem...)
}
return filepath.Join(append([]string{usr.HomeDir}, elem...)...)
}

func getSelenoidConfigDir() string {
return getConfigDir(".aerokube", "selenoid")
}

func stderr(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a)
}
32 changes: 32 additions & 0 deletions cmd/selenoid_cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"github.com/spf13/cobra"
"os"
)

var selenoidCleanupCmd = &cobra.Command{
Use: "cleanup",
Short: "Remove Selenoid traces",
Run: func(cmd *cobra.Command, args []string) {
lifecycle, err := createLifecycle()
if err != nil {
stderr("Failed to initialize: %v\n", err)
os.Exit(1)
}

err = lifecycle.Stop()
if err != nil {
stderr("Failed to stop Selenoid: %v\n", err)
os.Exit(1)
}

err = os.RemoveAll(configDir)
if err != nil {
lifecycle.Printf("Failed to remove configuration directory: %v\n", err)
os.Exit(1)
}
lifecycle.Printf("Successfully removed configuration directory\n")
os.Exit(0)
},
}
24 changes: 24 additions & 0 deletions cmd/selenoid_configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/spf13/cobra"
"os"
)

var selenoidConfigureCmd = &cobra.Command{
Use: "configure",
Short: "Create Selenoid configuration file and download dependencies",
Run: func(cmd *cobra.Command, args []string) {
lifecycle, err := createLifecycle()
if err != nil {
stderr("Failed to initialize: %v\n", err)
os.Exit(1)
}
err = lifecycle.Configure()
if err != nil {
lifecycle.Printf("Failed to configure Selenoid: %v\n", err)
os.Exit(1)
}
os.Exit(0)
},
}
24 changes: 24 additions & 0 deletions cmd/selenoid_download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/spf13/cobra"
"os"
)

var selenoidDownloadCmd = &cobra.Command{
Use: "download",
Short: "Download Selenoid latest or specified release",
Run: func(cmd *cobra.Command, args []string) {
lifecycle, err := createLifecycle()
if err != nil {
stderr("Failed to initialize: %v\n", err)
os.Exit(1)
}
err = lifecycle.Download()
if err != nil {
lifecycle.Printf("Failed to download Selenoid release: %v\n", err)
os.Exit(1)
}
os.Exit(0)
},
}
29 changes: 29 additions & 0 deletions cmd/selenoid_start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/spf13/cobra"
"os"
)

var selenoidStartCmd = &cobra.Command{
Use: "start",
Short: "Start Selenoid",
Run: func(cmd *cobra.Command, args []string) {
startImpl(force)
},
}

func startImpl(force bool) {
lifecycle, err := createLifecycle()
lifecycle.Force = force
if err != nil {
stderr("Failed to initialize: %v\n", err)
os.Exit(1)
}
err = lifecycle.Start()
if err != nil {
lifecycle.Printf("Failed to start Selenoid: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
Loading

0 comments on commit e11bb6d

Please sign in to comment.