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

Commit 7499b3e

Browse files
committed
Added downloading Chromedriver from the new URLs
1 parent 8dd2917 commit 7499b3e

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

build/chrome.go

+97-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package build
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
7+
hv "github.com/hashicorp/go-version"
8+
"net/http"
69
"os"
710
"path/filepath"
11+
"sort"
812
"strconv"
13+
"strings"
914
)
1015

1116
const (
12-
chromeDriverBinary = "chromedriver"
17+
chromeDriverBinary = "chromedriver"
18+
newChromeDriverBinary = "chromedriver-linux64/chromedriver"
1319
)
1420

1521
type Chrome struct {
@@ -25,7 +31,12 @@ func (c *Chrome) Build() error {
2531

2632
pkgTagVersion := extractVersion(pkgVersion)
2733

28-
driverVersion, err := c.parseChromeDriverVersion(pkgTagVersion)
34+
chromeDriverVersions, err := fetchChromeDriverVersions()
35+
if err != nil {
36+
return fmt.Errorf("fetch chromedriver versions: %v", err)
37+
}
38+
39+
driverVersion, err := c.parseChromeDriverVersion(pkgTagVersion, chromeDriverVersions)
2940
if err != nil {
3041
return fmt.Errorf("parse chromedriver version: %v", err)
3142
}
@@ -82,7 +93,7 @@ func (c *Chrome) Build() error {
8293
}
8394
image.BuildArgs = append(image.BuildArgs, fmt.Sprintf("VERSION=%s", pkgTagVersion))
8495

85-
err = c.downloadChromeDriver(image.Dir, driverVersion)
96+
err = c.downloadChromeDriver(image.Dir, driverVersion, chromeDriverVersions)
8697
if err != nil {
8798
return fmt.Errorf("failed to download chromedriver: %v", err)
8899
}
@@ -117,25 +128,60 @@ func (c *Chrome) channelToBuildArgs() []string {
117128
}
118129
}
119130

120-
func (c *Chrome) parseChromeDriverVersion(pkgVersion string) (string, error) {
131+
func (c *Chrome) parseChromeDriverVersion(pkgVersion string, chromeDriverVersions map[string]string) (string, error) {
121132
version := c.DriverVersion
122133
if version == LatestVersion {
134+
135+
var matchingVersions []string
136+
for mv := range chromeDriverVersions {
137+
if strings.Contains(mv, pkgVersion) {
138+
matchingVersions = append(matchingVersions, mv)
139+
}
140+
}
141+
if len(matchingVersions) > 0 {
142+
sort.SliceStable(matchingVersions, func(i, j int) bool {
143+
l := matchingVersions[i]
144+
r := matchingVersions[j]
145+
lv, err := hv.NewVersion(l)
146+
if err != nil {
147+
return false
148+
}
149+
rv, err := hv.NewVersion(r)
150+
if err != nil {
151+
return false
152+
}
153+
return lv.LessThan(rv)
154+
})
155+
return matchingVersions[0], nil
156+
}
157+
123158
const baseUrl = "https://chromedriver.storage.googleapis.com/"
124159
v, err := c.getLatestChromeDriver(baseUrl, pkgVersion)
125160
if err != nil {
126161
return "", err
127162
}
128-
version = v
163+
return v, nil
129164
}
130165
return version, nil
131166
}
132167

133-
func (c *Chrome) downloadChromeDriver(dir string, version string) error {
134-
u := fmt.Sprintf("http://chromedriver.storage.googleapis.com/%s/chromedriver_linux64.zip", version)
135-
_, err := downloadDriver(u, chromeDriverBinary, dir)
168+
func (c *Chrome) downloadChromeDriver(dir string, version string, chromeDriverVersions map[string]string) error {
169+
u := fmt.Sprintf("https://chromedriver.storage.googleapis.com/%s/chromedriver_linux64.zip", version)
170+
fn := chromeDriverBinary
171+
if cdu, ok := chromeDriverVersions[version]; ok {
172+
u = cdu
173+
fn = newChromeDriverBinary
174+
}
175+
outputPath, err := downloadDriver(u, fn, dir)
136176
if err != nil {
137177
return fmt.Errorf("download chromedriver: %v", err)
138178
}
179+
if fn == newChromeDriverBinary {
180+
err = os.Rename(outputPath, filepath.Join(dir, chromeDriverBinary))
181+
if err != nil {
182+
return fmt.Errorf("rename chromedriver: %v", err)
183+
}
184+
}
139185
return nil
140186
}
141187

@@ -176,3 +222,46 @@ func (c *Chrome) getLatestChromeDriver(baseUrl string, pkgVersion string) (strin
176222
}
177223
}
178224
}
225+
226+
func fetchChromeDriverVersions() (map[string]string, error) {
227+
const versionsURL = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
228+
resp, err := http.Get(versionsURL)
229+
if err != nil {
230+
return nil, fmt.Errorf("fetch chrome versions: %v", err)
231+
}
232+
defer resp.Body.Close()
233+
var cv ChromeVersions
234+
err = json.NewDecoder(resp.Body).Decode(&cv)
235+
if err != nil {
236+
return nil, fmt.Errorf("decode json: %v", err)
237+
}
238+
ret := make(map[string]string)
239+
const platformLinux64 = "linux64"
240+
const chromeDriver = "chromedriver"
241+
for _, v := range cv.Versions {
242+
version := v.Version
243+
if cd, ok := v.Downloads[chromeDriver]; ok {
244+
for _, d := range cd {
245+
u := d.URL
246+
if u != "" && d.Platform == platformLinux64 {
247+
ret[version] = u
248+
}
249+
}
250+
}
251+
}
252+
return ret, nil
253+
}
254+
255+
type ChromeVersions struct {
256+
Versions []ChromeVersion `json:"versions"`
257+
}
258+
259+
type ChromeVersion struct {
260+
Version string `json:"version"`
261+
Downloads map[string][]ChromeDownload `json:"downloads"`
262+
}
263+
264+
type ChromeDownload struct {
265+
Platform string `json:"platform"`
266+
URL string `json:"url"`
267+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ require (
1212
gopkg.in/cheggaaa/pb.v1 v1.0.28
1313
)
1414

15+
require github.com/hashicorp/go-version v1.6.0
16+
1517
require (
1618
github.com/gobuffalo/here v0.6.0 // indirect
1719
github.com/inconshreveable/mousetrap v1.0.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
8787
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
8888
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
8989
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
90+
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
91+
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
9092
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
9193
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
9294
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

0 commit comments

Comments
 (0)