From b9c75581e4bd74d2142b8b0f6f18d8188af3a1fa Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Mon, 14 Nov 2022 13:31:41 +1100 Subject: [PATCH] Use the Release API instead of checkpoint. --- checkpoint.go | 83 --------------------------------------- example/terraform.pkr.hcl | 2 +- go.mod | 50 +++++++++++------------ release.go | 82 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 109 deletions(-) delete mode 100644 checkpoint.go create mode 100644 release.go diff --git a/checkpoint.go b/checkpoint.go deleted file mode 100644 index 3397028..0000000 --- a/checkpoint.go +++ /dev/null @@ -1,83 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "log" - "net/http" - "net/url" - "runtime" - "time" - - "github.com/hashicorp/go-cleanhttp" -) - -// https://checkpoint-api.hashicorp.com/v1/check/terraform -// {"product":"terraform","current_version":"0.12.23","current_release":1583443574,"current_download_url":"https://releases.hashicorp.com/terraform/0.12.23/","current_changelog_url":"https://github.com/hashicorp/terraform/blob/v0.12.23/CHANGELOG.md","project_website":"https://www.terraform.io","alerts":[]} - -// CheckResponse is the response for a check request. -type CheckResponse struct { - Product string `json:"product"` - CurrentVersion string `json:"current_version"` - CurrentReleaseDate int `json:"current_release_date"` - CurrentDownloadURL string `json:"current_download_url"` - CurrentChangelogURL string `json:"current_changelog_url"` - ProjectWebsite string `json:"project_website"` - Outdated bool `json:"outdated"` - Alerts []*CheckAlert `json:"alerts"` -} - -// CheckAlert is used in the error message. -type CheckAlert struct { - ID int - Date int - Message string - URL string - Level string -} - -// FetchLatestTerraform grabs the latest verions of Terraform from Hashicorp -func FetchLatestTerraform() (string, error) { - var u url.URL - v := u.Query() - - v.Set("arch", runtime.GOARCH) - v.Set("os", runtime.GOOS) - u.Scheme = "https" - u.Host = "checkpoint-api.hashicorp.com" - u.Path = "/v1/check/terraform" - u.RawQuery = v.Encode() - req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { - return "", err - } - req.Header.Set("Accept", "application/json") - req.Header.Set("User-Agent", "HashiCorp/go-checkpoint") - - client := cleanhttp.DefaultClient() - - // We use a short timeout since checking for new versions is not critical - // enough to block on if checkpoint is broken/slow. - client.Timeout = time.Duration(3000) * time.Millisecond - - log.Println(fmt.Sprintf("About to fetch from URL: %s", u.String())) - resp, err := client.Do(req) - if err != nil { - return "", err - } - defer resp.Body.Close() - - if resp.StatusCode != 200 { - return "", fmt.Errorf("Unknown status: %d", resp.StatusCode) - } - - var r io.Reader = resp.Body - - var result CheckResponse - if err := json.NewDecoder(r).Decode(&result); err != nil { - return "", err - } - log.Println(fmt.Sprintf("Got version response: %s", result.CurrentVersion)) - return result.CurrentVersion, nil -} diff --git a/example/terraform.pkr.hcl b/example/terraform.pkr.hcl index fa2b1b6..60d087d 100644 --- a/example/terraform.pkr.hcl +++ b/example/terraform.pkr.hcl @@ -1,7 +1,7 @@ packer { required_plugins { terraform = { - version = ">= 0.0.7" + version = ">= 0.0.8" source = "github.com/servian/terraform" } } diff --git a/go.mod b/go.mod index d86aa34..8aff95e 100644 --- a/go.mod +++ b/go.mod @@ -1,52 +1,52 @@ module github.com/servian/packer-plugin-terraform -go 1.18 +go 1.19 require ( github.com/hashicorp/go-cleanhttp v0.5.2 - github.com/hashicorp/hcl/v2 v2.13.0 - github.com/hashicorp/packer-plugin-sdk v0.3.0 + github.com/hashicorp/hcl/v2 v2.14.1 + github.com/hashicorp/packer-plugin-sdk v0.3.2 github.com/zclconf/go-cty v1.10.0 ) require ( github.com/agext/levenshtein v1.2.3 // indirect github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect - github.com/armon/go-metrics v0.4.0 // indirect + github.com/armon/go-metrics v0.4.1 // indirect github.com/armon/go-radix v1.0.0 // indirect - github.com/aws/aws-sdk-go v1.44.43 // indirect + github.com/aws/aws-sdk-go v1.44.136 // indirect github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect github.com/cenkalti/backoff/v3 v3.2.2 // indirect github.com/fatih/color v1.13.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/hashicorp/consul/api v1.13.0 // indirect + github.com/hashicorp/consul/api v1.15.3 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-getter/v2 v2.1.0 // indirect - github.com/hashicorp/go-hclog v1.2.1 // indirect + github.com/hashicorp/go-hclog v1.3.1 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect - github.com/hashicorp/go-plugin v1.4.4 // indirect + github.com/hashicorp/go-plugin v1.4.6 // indirect github.com/hashicorp/go-retryablehttp v0.7.1 // indirect github.com/hashicorp/go-rootcerts v1.0.2 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect github.com/hashicorp/go-secure-stdlib/mlock v0.1.2 // indirect - github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect + github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7 // indirect github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect github.com/hashicorp/go-sockaddr v1.0.2 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect - github.com/hashicorp/go-version v1.5.0 // indirect + github.com/hashicorp/go-version v1.6.0 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/hashicorp/serf v0.9.8 // indirect - github.com/hashicorp/vault/api v1.7.2 // indirect - github.com/hashicorp/vault/sdk v0.5.2 // indirect - github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect + github.com/hashicorp/serf v0.10.1 // indirect + github.com/hashicorp/vault/api v1.8.2 // indirect + github.com/hashicorp/vault/sdk v0.6.1 // indirect + github.com/hashicorp/yamux v0.1.1 // indirect github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect @@ -59,14 +59,14 @@ require ( github.com/ryanuber/go-glob v1.0.0 // indirect github.com/ugorji/go/codec v1.2.7 // indirect github.com/ulikunitz/xz v0.5.10 // indirect - go.uber.org/atomic v1.9.0 // indirect - golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect - golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect - golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect - golang.org/x/text v0.3.7 // indirect - golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect - google.golang.org/genproto v0.0.0-20220627200112-0a929928cb33 // indirect - google.golang.org/grpc v1.47.0 // indirect - google.golang.org/protobuf v1.28.0 // indirect + go.uber.org/atomic v1.10.0 // indirect + golang.org/x/crypto v0.2.0 // indirect + golang.org/x/net v0.2.0 // indirect + golang.org/x/sys v0.2.0 // indirect + golang.org/x/text v0.4.0 // indirect + golang.org/x/time v0.2.0 // indirect + google.golang.org/genproto v0.0.0-20221111202108-142d8a6fa32e // indirect + google.golang.org/grpc v1.50.1 // indirect + google.golang.org/protobuf v1.28.1 // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect ) diff --git a/release.go b/release.go new file mode 100644 index 0000000..952ce27 --- /dev/null +++ b/release.go @@ -0,0 +1,82 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "net/url" + "time" + + "github.com/hashicorp/go-cleanhttp" +) + +// https://api.releases.hashicorp.com/v1/releases/terraform/latest + +// CheckResponse is the response for a check request. +type CheckResponse struct { + Builds []*Build `json:"builds"` + LicenseClass string `json:"license_class"` + Name string `json:"name"` + TimestampCreated string `json:"timestamp_created"` + TimestampUpdated string `json:"timestamp_updated"` + UrlChangelog string `json:"url_changelog"` + UrlDockerRegistryDockerhub string `json:"url_docker_registry_dockerhub"` + UrlDockerRegistryEcr string `json:"url_docker_registry_ecr"` + UrlLicense string `json:"url_license"` + UrlProjectWebsite string `json:"url_project_website"` + UrlShasums string `json:"url_shasums"` + UrlSourceRepository string `json:"url_source_repository"` + Version string `json:"version"` +} + +// Build is the different OS Arch builds. +type Build struct { + Arch string + OS string + Url string +} + +// FetchLatestTerraform grabs the latest verions of Terraform from Hashicorp +func FetchLatestTerraform() (string, error) { + var u url.URL + v := u.Query() + + u.Scheme = "https" + u.Host = "api.releases.hashicorp.com" + u.Path = "/v1/releases/terraform/latest" + u.RawQuery = v.Encode() + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return "", err + } + req.Header.Set("Accept", "application/json") + req.Header.Set("User-Agent", "Servian/Packer-Plugin-Terraform") + + client := cleanhttp.DefaultClient() + + // We use a short timeout since checking for new versions is not critical + // enough to block on if the release api is broken/slow. + client.Timeout = time.Duration(3000) * time.Millisecond + + log.Println(fmt.Sprintf("About to fetch from URL: %s", u.String())) + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return "", fmt.Errorf("Unknown status: %d", resp.StatusCode) + } + + var r io.Reader = resp.Body + + var result CheckResponse + if err := json.NewDecoder(r).Decode(&result); err != nil { + return "", err + } + log.Println(fmt.Sprintf("Got version response: %s", result.Version)) + return result.Version, nil +}