From c044088313810a5bdbf983223ad91526e2863e06 Mon Sep 17 00:00:00 2001 From: Sebastian Spaink <3441183+sspaink@users.noreply.github.com> Date: Wed, 12 Oct 2022 15:19:50 -0500 Subject: [PATCH] chore: Tool to update Telegraf to use new Go version (#11968) Co-authored-by: Joshua Powers --- scripts/installgo_mac.sh | 10 +- tools/update_goversion/README.md | 12 ++ tools/update_goversion/main.go | 195 ++++++++++++++++++ tools/update_goversion/main_test.go | 55 +++++ .../testdata/godev_minor.html | 192 +++++++++++++++++ .../testdata/godev_patch.html | 192 +++++++++++++++++ 6 files changed, 654 insertions(+), 2 deletions(-) create mode 100644 tools/update_goversion/README.md create mode 100644 tools/update_goversion/main.go create mode 100644 tools/update_goversion/main_test.go create mode 100644 tools/update_goversion/testdata/godev_minor.html create mode 100644 tools/update_goversion/testdata/godev_patch.html diff --git a/scripts/installgo_mac.sh b/scripts/installgo_mac.sh index 02de30638..04f99b894 100644 --- a/scripts/installgo_mac.sh +++ b/scripts/installgo_mac.sh @@ -4,12 +4,18 @@ set -eux ARCH=$(uname -m) GO_VERSION="1.19.2" +# To easily find and replace the hashes +# shellcheck disable=SC2034 +GO_VERSION_SHA_arm64="35d819df25197c0be45f36ce849b994bba3b0559b76d4538b910d28f6395c00d" # from https://golang.org/dl +# shellcheck disable=SC2034 +GO_VERSION_SHA_amd64="16f8047d7b627699b3773680098fbaf7cc962b7db02b3e02726f78c4db26dfde" # from https://golang.org/dl + if [ "$ARCH" = 'arm64' ]; then GO_ARCH="darwin-arm64" - GO_VERSION_SHA="35d819df25197c0be45f36ce849b994bba3b0559b76d4538b910d28f6395c00d" # from https://golang.org/dl + GO_VERSION_SHA=GO_VERSION_SHA_arm64 elif [ "$ARCH" = 'x86_64' ]; then GO_ARCH="darwin-amd64" - GO_VERSION_SHA="16f8047d7b627699b3773680098fbaf7cc962b7db02b3e02726f78c4db26dfde" # from https://golang.org/dl + GO_VERSION_SHA=GO_VERSION_SHA_amd64 fi # This path is cachable. (Saving in /usr/local/ would cause issues restoring the cache.) diff --git a/tools/update_goversion/README.md b/tools/update_goversion/README.md new file mode 100644 index 000000000..37288f1fd --- /dev/null +++ b/tools/update_goversion/README.md @@ -0,0 +1,12 @@ +# Update Go Version + +The version doesn't require a leading "v" and minor versions don't need +a trailing ".0". The tool will still will work correctly if they are provided. + +`go run tools/update_goversion/main.go 1.19.2` +`go run tools/update_goversion/main.go 1.19` + +This tool is meant to be used to create a pull request that will update the +Telegraf project to use the latest version of Go. +The Dockerfile `quay.io/influxdb/telegraf-ci` used by the CI will have to be +pushed to the quay repository by a maintainer with `make ci`. diff --git a/tools/update_goversion/main.go b/tools/update_goversion/main.go new file mode 100644 index 000000000..1e2af0438 --- /dev/null +++ b/tools/update_goversion/main.go @@ -0,0 +1,195 @@ +package main + +import ( + "fmt" + "io" + "log" //nolint:revive + "net/http" + "os" + "regexp" + "strings" + + "golang.org/x/net/html" +) + +type FileInfo struct { + FileName string + Regex string + Replace string +} + +func (f FileInfo) Update() error { + b, err := os.ReadFile(f.FileName) + if err != nil { + return err + } + + re := regexp.MustCompile(f.Regex) + newContents := re.ReplaceAll(b, []byte(f.Replace)) + + err = os.WriteFile(f.FileName, newContents, 0664) + if err != nil { + return err + } + + return nil +} + +// removeZeroPatch cleans version in case the user provides the minor version as "1.19.0" but require "1.19" +func removeZeroPatch(version string) string { + if strings.HasSuffix(version, ".0") { + return strings.Trim(version, ".0") + } + return version +} + +// findHash will search the downloads table for the hashes matching the artifacts list +func findHashes(body io.Reader, version string) (map[string]string, error) { + version = removeZeroPatch(version) + + htmlTokens := html.NewTokenizer(body) + artifacts := []string{ + fmt.Sprintf("go%s.linux-amd64.tar.gz", version), + fmt.Sprintf("go%s.darwin-arm64.tar.gz", version), + fmt.Sprintf("go%s.darwin-amd64.tar.gz", version), + } + + var insideDownloadTable bool + var currentRow string + hashes := make(map[string]string) + + for { + tokenType := htmlTokens.Next() + + //if it's an error token, we either reached + //the end of the file, or the HTML was malformed + if tokenType == html.ErrorToken { + err := htmlTokens.Err() + if err == io.EOF { + //end of the file, break out of the loop + break + } + return nil, htmlTokens.Err() + } + + if tokenType == html.StartTagToken { + //get the token + token := htmlTokens.Token() + if "table" == token.Data && len(token.Attr) == 1 && token.Attr[0].Val == "downloadtable" { + insideDownloadTable = true + } + + if insideDownloadTable && token.Data == "a" && len(token.Attr) == 2 { + for _, f := range artifacts { + // Check if the current row matches a desired file + if strings.Contains(token.Attr[1].Val, f) { + currentRow = f + break + } + } + } + + if currentRow != "" && token.Data == "tt" { + //the next token should be the page title + tokenType = htmlTokens.Next() + //just make sure it's actually a text token + if tokenType == html.TextToken { + hashes[currentRow] = htmlTokens.Token().Data + currentRow = "" + } + } + } + + // Found a hash for each filename + if len(hashes) == len(artifacts) { + break + } + + // Reached end of table + if tokenType == html.EndTagToken && htmlTokens.Token().Data == "table" { + return nil, fmt.Errorf("only found %d hashes expected %d: %v", len(hashes), len(artifacts), hashes) + } + } + + return hashes, nil +} + +func getHashes(version string) (map[string]string, error) { + resp, err := http.Get(`https://go.dev/dl/`) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + return findHashes(resp.Body, version) +} + +func main() { + version := os.Args[1] + // Handle situation user accidentally provides version as "v1.19.2" + if strings.HasPrefix(version, "v") { + version = strings.TrimLeft(version, "v") + } + zeroPatchVersion := removeZeroPatch(version) + + hashes, err := getHashes(version) + if err != nil { + log.Panic(err) + } + + files := []FileInfo{ + { + FileName: ".circleci/config.yml", + Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`, + Replace: fmt.Sprintf("$1:%s", version), + }, + { + FileName: "Makefile", + Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`, + Replace: fmt.Sprintf("$1:%s", version), + }, + { + FileName: "scripts/ci.docker", + Regex: `(FROM golang):(\d.\d*.\d)`, + Replace: fmt.Sprintf("$1:%s", zeroPatchVersion), + }, + { + FileName: "scripts/installgo_linux.sh", + Regex: `(GO_VERSION)=("\d.\d*.\d")`, + Replace: fmt.Sprintf("$1=\"%s\"", zeroPatchVersion), + }, + { + FileName: "scripts/installgo_mac.sh", + Regex: `(GO_VERSION)=("\d.\d*.\d")`, + Replace: fmt.Sprintf("$1=\"%s\"", zeroPatchVersion), + }, + { + FileName: "scripts/installgo_windows.sh", + Regex: `(GO_VERSION)=("\d.\d*.\d")`, + Replace: fmt.Sprintf("$1=\"%s\"", zeroPatchVersion), + }, + { + FileName: "scripts/installgo_linux.sh", + Regex: `(GO_VERSION_SHA)=".*"`, + Replace: fmt.Sprintf("$1=\"%s\"", hashes[fmt.Sprintf("go%s.linux-amd64.tar.gz", zeroPatchVersion)]), + }, + { + FileName: "scripts/installgo_mac.sh", + Regex: `(GO_VERSION_SHA_arm64)=".*"`, + Replace: fmt.Sprintf("$1=\"%s\"", hashes[fmt.Sprintf("go%s.darwin-arm64.tar.gz", zeroPatchVersion)]), + }, + { + FileName: "scripts/installgo_mac.sh", + Regex: `(GO_VERSION_SHA_amd64)=".*"`, + Replace: fmt.Sprintf("$1=\"%s\"", hashes[fmt.Sprintf("go%s.darwin-amd64.tar.gz", zeroPatchVersion)]), + }, + } + + for _, f := range files { + fmt.Printf("Updating %s \n", f.FileName) + err := f.Update() + if err != nil { + log.Panic(err) + } + } +} diff --git a/tools/update_goversion/main_test.go b/tools/update_goversion/main_test.go new file mode 100644 index 000000000..de9197e0e --- /dev/null +++ b/tools/update_goversion/main_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFindHash(t *testing.T) { + tests := []struct { + testFile string + version string + expectedHases map[string]string + }{ + { + "testdata/godev_patch.html", + "1.19.2", + map[string]string{ + "go1.19.2.linux-amd64.tar.gz": "5e8c5a74fe6470dd7e055a461acda8bb4050ead8c2df70f227e3ff7d8eb7eeb6", + "go1.19.2.darwin-arm64.tar.gz": "35d819df25197c0be45f36ce849b994bba3b0559b76d4538b910d28f6395c00d", + "go1.19.2.darwin-amd64.tar.gz": "16f8047d7b627699b3773680098fbaf7cc962b7db02b3e02726f78c4db26dfde", + }, + }, + { + "testdata/godev_minor.html", + "1.19", + map[string]string{ + "go1.19.linux-amd64.tar.gz": "464b6b66591f6cf055bc5df90a9750bf5fbc9d038722bb84a9d56a2bea974be6", + "go1.19.darwin-arm64.tar.gz": "859e0a54b7fcea89d9dd1ec52aab415ac8f169999e5fdfb0f0c15b577c4ead5e", + "go1.19.darwin-amd64.tar.gz": "df6509885f65f0d7a4eaf3dfbe7dda327569787e8a0a31cbf99ae3a6e23e9ea8", + }, + }, + { + "testdata/godev_minor.html", + "1.19.0", + map[string]string{ + "go1.19.linux-amd64.tar.gz": "464b6b66591f6cf055bc5df90a9750bf5fbc9d038722bb84a9d56a2bea974be6", + "go1.19.darwin-arm64.tar.gz": "859e0a54b7fcea89d9dd1ec52aab415ac8f169999e5fdfb0f0c15b577c4ead5e", + "go1.19.darwin-amd64.tar.gz": "df6509885f65f0d7a4eaf3dfbe7dda327569787e8a0a31cbf99ae3a6e23e9ea8", + }, + }, + } + + for _, test := range tests { + b, err := os.ReadFile(test.testFile) + require.NoError(t, err) + + hashes, err := findHashes(bytes.NewReader(b), test.version) + require.NoError(t, err) + + require.Equal(t, test.expectedHases, hashes) + } +} diff --git a/tools/update_goversion/testdata/godev_minor.html b/tools/update_goversion/testdata/godev_minor.html new file mode 100644 index 000000000..0bf2744bd --- /dev/null +++ b/tools/update_goversion/testdata/godev_minor.html @@ -0,0 +1,192 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
File nameKindOSArchSizeSHA256 Checksum
go1.19.src.tar.gzSource25MB9419cc70dc5a2523f29a77053cafff658ed21ef3561d9b6b020280ebceab28b9
go1.19.darwin-amd64.tar.gzArchivemacOSx86-64144MBdf6509885f65f0d7a4eaf3dfbe7dda327569787e8a0a31cbf99ae3a6e23e9ea8
go1.19.darwin-amd64.pkgInstallermacOSx86-64145MB61cb2268c4b3e6662a19c2dda58d43bfcf89d3649c205bcdb32c148e9048b1ba
go1.19.darwin-arm64.tar.gzArchivemacOSARM64138MB859e0a54b7fcea89d9dd1ec52aab415ac8f169999e5fdfb0f0c15b577c4ead5e
go1.19.darwin-arm64.pkgInstallermacOSARM64139MB167cd1107886c36fda76404e82d1447ebd3efc472fd9a9fe3b2e872d41adf981
go1.19.linux-386.tar.gzArchiveLinuxx86114MB6f721fa3e8f823827b875b73579d8ceadd9053ad1db8eaa2393c084865fb4873
go1.19.linux-amd64.tar.gzArchiveLinuxx86-64142MB464b6b66591f6cf055bc5df90a9750bf5fbc9d038722bb84a9d56a2bea974be6
go1.19.linux-arm64.tar.gzArchiveLinuxARM64110MBefa97fac9574fc6ef6c9ff3e3758fb85f1439b046573bf434cccb5e012bd00c8
go1.19.linux-armv6l.tar.gzArchiveLinuxARMv6111MB25197c7d70c6bf2b34d7d7c29a2ff92ba1c393f0fb395218f1147aac2948fb93
go1.19.windows-386.zipArchiveWindowsx86128MB45b80c0aca6a5a1f87f111d375db5afee3ce0a9fd5834041c39116e643ba1df2
go1.19.windows-386.msiInstallerWindowsx86112MB39ed9b03c42b0ee99477377c27b1a809a73b96c627d86c4aedd133e92df5bd43
go1.19.windows-amd64.zipArchiveWindowsx86-64156MBbcaaf966f91980d35ae93c37a8fe890e4ddfca19448c0d9f66c027d287e2823a
go1.19.windows-amd64.msiInstallerWindowsx86-64136MB0743b5fe0c6e5c67c7d131a8e24d4e7bdd5ef272dd13205dd7ae30cc2f464123
Other Ports
go1.19.freebsd-386.tar.gzArchiveFreeBSDx86114MB3989e2336dbb3dcf9197b8c0ef9227cdd1a134789d83095d20ebdc1d88edb9f0
go1.19.freebsd-amd64.tar.gzArchiveFreeBSDx86-64142MBeca1a8f7b6ff6146efc285eed581096b12b59c1f0488bfe98ed053ab205267ca
go1.19.linux-ppc64le.tar.gzArchiveLinuxppc64le110MB92bf5aa598a01b279d03847c32788a3a7e0a247a029dedb7c759811c2a4241fc
go1.19.linux-s390x.tar.gzArchiveLinuxs390x113MB58723eb8e3c7b9e8f5e97b2d38ace8fd62d9e5423eaa6cdb7ffe5f881cb11875
go1.19.windows-arm64.zipArchiveWindowsARM64122MB032f1f75a85bc595bf5eb8b48ec8e490121047915803ad62277586b2e13608f2
go1.19.windows-arm64.msiInstallerWindowsARM64107MB6319ab5b50f0462efcb092eec3ac8c23a548652cdaa2755282aed4513b6188ee
diff --git a/tools/update_goversion/testdata/godev_patch.html b/tools/update_goversion/testdata/godev_patch.html new file mode 100644 index 000000000..80d8f5486 --- /dev/null +++ b/tools/update_goversion/testdata/godev_patch.html @@ -0,0 +1,192 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
File nameKindOSArchSizeSHA256 Checksum
go1.19.2.src.tar.gzSource25MB2ce930d70a931de660fdaf271d70192793b1b240272645bf0275779f6704df6b
go1.19.2.darwin-amd64.tar.gzArchivemacOSx86-64144MB16f8047d7b627699b3773680098fbaf7cc962b7db02b3e02726f78c4db26dfde
go1.19.2.darwin-amd64.pkgInstallermacOSx86-64145MB2633f62c0b259a8fa4fb1fc967d28817816acca50b1fc354364cb381e1ccd2e5
go1.19.2.darwin-arm64.tar.gzArchivemacOSARM64138MB35d819df25197c0be45f36ce849b994bba3b0559b76d4538b910d28f6395c00d
go1.19.2.darwin-arm64.pkgInstallermacOSARM64139MB6f985e50497ff3cba8f216e49c6c767f6ad52c8e505f375983714c14749cb954
go1.19.2.linux-386.tar.gzArchiveLinuxx86114MBba8c97965e0856c69c9ca2c86f96bec5bb21de43e6533e25494bb211d85cda1b
go1.19.2.linux-amd64.tar.gzArchiveLinuxx86-64142MB5e8c5a74fe6470dd7e055a461acda8bb4050ead8c2df70f227e3ff7d8eb7eeb6
go1.19.2.linux-arm64.tar.gzArchiveLinuxARM64110MBb62a8d9654436c67c14a0c91e931d50440541f09eb991a987536cb982903126d
go1.19.2.linux-armv6l.tar.gzArchiveLinuxARMv6111MBf3ccec7816ecd704ebafd130b08b8ad97c55402a8193a107b63e9de12ab90118
go1.19.2.windows-386.zipArchiveWindowsx86128MB9355b09b23e9db33945a7ba45bb75981ab0bb6006713099732167722cf081b53
go1.19.2.windows-386.msiInstallerWindowsx86111MB98b0f92a6c74469353917afff914457d6809de164251ddb45831c3f0efc269b6
go1.19.2.windows-amd64.zipArchiveWindowsx86-64156MBe132d4f0518b0d417eb6cc5f182c3385f6d24bb2eebee2566cd1a7ab6097e3f2
go1.19.2.windows-amd64.msiInstallerWindowsx86-64135MB249aba207df30133deadb3419b2476479189a2c0d324e72faee4e1f1a6209eca
Other Ports
go1.19.2.freebsd-386.tar.gzArchiveFreeBSDx86114MB7831a406447a14d964212d07f68e77cf7fe7fb7286bade6eeb9fbea39b192984
go1.19.2.freebsd-amd64.tar.gzArchiveFreeBSDx86-64142MBd74c88430484d14826ec21161e3b9336bd021f502b6594c4dd00e9ec730ee51d
go1.19.2.linux-ppc64le.tar.gzArchiveLinuxppc64le110MB37e1d4342f7103aeb9babeabe8c71ef3dba23db28db525071119e94b2aa21d7d
go1.19.2.linux-s390x.tar.gzArchiveLinuxs390x114MB51b45dec41295215df17f78e67d1a373b9dda97a5e539bed440974da5ffc97de
go1.19.2.windows-arm64.zipArchiveWindowsARM64122MB4049435f77fb2a0642fd8740c588aadbcc446056e637e835a8e223fdb897cb3e
go1.19.2.windows-arm64.msiInstallerWindowsARM64106MB979e8bf619c6dc25d27bd62c3d2325e730ccb486849c274b35fdffe9bd0bb827