2017-05-03 17:02:47 +02:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/html"
|
|
|
|
|
|
|
|
getter "github.com/hashicorp/go-getter"
|
|
|
|
)
|
|
|
|
|
|
|
|
const releasesURL = "https://releases.hashicorp.com/"
|
|
|
|
|
|
|
|
// pluginURL generates URLs to lookup the versions of a plugin, or the file path.
|
|
|
|
//
|
|
|
|
// The URL for releases follows the pattern:
|
|
|
|
// https://releases.hashicorp.com/terraform-providers/terraform-provider-name/ +
|
|
|
|
// terraform-provider-name_<x.y.z>/terraform-provider-name_<x.y.z>_<os>_<arch>.<ext>
|
2017-05-04 20:32:18 +02:00
|
|
|
type pluginURL struct {
|
|
|
|
// the base url to search for releases
|
|
|
|
releases string
|
|
|
|
// The name prefix common to all plugins of this type.
|
|
|
|
// This is either `terraform-provider` or `terraform-provisioner`.
|
|
|
|
baseName string
|
|
|
|
}
|
2017-05-03 17:02:47 +02:00
|
|
|
|
2017-05-04 20:32:18 +02:00
|
|
|
// releasesURL returns the top level directory for all plugins of this type
|
|
|
|
func (p pluginURL) releasesURL() string {
|
2017-05-03 17:02:47 +02:00
|
|
|
// the top level directory is the plural form of the plugin type
|
2017-05-04 20:32:18 +02:00
|
|
|
return p.releases + p.baseName + "s"
|
2017-05-03 17:02:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 20:32:18 +02:00
|
|
|
// versionsURL returns the url to the directory to list available versionsURL for this plugin
|
|
|
|
func (p pluginURL) versionsURL(name string) string {
|
|
|
|
return fmt.Sprintf("%s/%s-%s", p.releasesURL(), p.baseName, name)
|
2017-05-03 17:02:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 20:32:18 +02:00
|
|
|
// fileURL returns the full path to a plugin based on the plugin name,
|
2017-05-03 17:02:47 +02:00
|
|
|
// version, GOOS and GOARCH.
|
2017-05-04 20:32:18 +02:00
|
|
|
func (p pluginURL) fileURL(name, version string) string {
|
|
|
|
releasesDir := fmt.Sprintf("%s-%s_%s/", p.baseName, name, version)
|
|
|
|
fileName := fmt.Sprintf("%s-%s_%s_%s_%s.zip", p.baseName, name, version, runtime.GOOS, runtime.GOARCH)
|
|
|
|
return fmt.Sprintf("%s/%s/%s", p.versionsURL(name), releasesDir, fileName)
|
2017-05-03 17:02:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 20:32:18 +02:00
|
|
|
var providersURL = pluginURL{
|
|
|
|
releases: releasesURL,
|
|
|
|
baseName: "terraform-provider",
|
|
|
|
}
|
|
|
|
|
|
|
|
var provisionersURL = pluginURL{
|
|
|
|
releases: releasesURL,
|
|
|
|
baseName: "terraform-provisioners",
|
|
|
|
}
|
2017-05-03 17:02:47 +02:00
|
|
|
|
|
|
|
// GetProvider fetches a provider plugin based on the version constraints, and
|
|
|
|
// copies it to the dst directory.
|
|
|
|
//
|
|
|
|
// TODO: verify checksum and signature
|
|
|
|
func GetProvider(dst, provider string, req Constraints) error {
|
|
|
|
versions, err := listProviderVersions(provider)
|
|
|
|
// TODO: return multiple errors
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
version, err := newestVersion(versions, req)
|
|
|
|
if err != nil {
|
2017-05-03 17:02:47 +02:00
|
|
|
return fmt.Errorf("no version of %q available that fulfills constraints %s", provider, req)
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:32:18 +02:00
|
|
|
url := providersURL.fileURL(provider, version.String())
|
2017-05-03 17:02:47 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] getting provider %q version %q at %s", provider, version, url)
|
|
|
|
return getter.Get(dst, url)
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
var errVersionNotFound = errors.New("version not found")
|
|
|
|
|
2017-05-03 17:02:47 +02:00
|
|
|
// take the list of available versions for a plugin, and the required
|
|
|
|
// Constraints, and return the latest available version that satisfies the
|
|
|
|
// constraints.
|
2017-05-04 20:37:16 +02:00
|
|
|
func newestVersion(available []Version, required Constraints) (Version, error) {
|
|
|
|
var latest Version
|
|
|
|
found := false
|
|
|
|
|
2017-05-03 17:02:47 +02:00
|
|
|
for _, v := range available {
|
2017-05-05 00:53:02 +02:00
|
|
|
if required.Allows(v) {
|
2017-05-04 20:37:16 +02:00
|
|
|
if !found {
|
2017-05-03 17:02:47 +02:00
|
|
|
latest = v
|
2017-05-04 20:37:16 +02:00
|
|
|
found = true
|
2017-05-03 17:02:47 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
if v.NewerThan(latest) {
|
2017-05-03 17:02:47 +02:00
|
|
|
latest = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
if !found {
|
|
|
|
return latest, errVersionNotFound
|
|
|
|
}
|
|
|
|
return latest, nil
|
2017-05-03 17:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// list the version available for the named plugin
|
2017-05-04 20:37:16 +02:00
|
|
|
func listProviderVersions(name string) ([]Version, error) {
|
2017-05-04 20:32:18 +02:00
|
|
|
versions, err := listPluginVersions(providersURL.versionsURL(name))
|
2017-05-03 17:02:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch versions for provider %q: %s", name, err)
|
|
|
|
}
|
|
|
|
return versions, nil
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
func listProvisionerVersions(name string) ([]Version, error) {
|
2017-05-04 20:32:18 +02:00
|
|
|
versions, err := listPluginVersions(provisionersURL.versionsURL(name))
|
2017-05-03 17:02:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch versions for provisioner %q: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a list of the plugin versions at the given URL
|
|
|
|
// TODO: This doesn't yet take into account plugin protocol version.
|
|
|
|
// That may have to be checked via an http header via a separate request
|
|
|
|
// to each plugin file.
|
2017-05-04 20:37:16 +02:00
|
|
|
func listPluginVersions(url string) ([]Version, error) {
|
2017-05-03 17:02:47 +02:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
log.Printf("[ERROR] failed to fetch plugin versions from %s\n%s\n%s", url, resp.Status, body)
|
|
|
|
return nil, errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := html.Parse(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
names := []string{}
|
|
|
|
|
|
|
|
// all we need to do is list links on the directory listing page that look like plugins
|
|
|
|
var f func(*html.Node)
|
|
|
|
f = func(n *html.Node) {
|
|
|
|
if n.Type == html.ElementNode && n.Data == "a" {
|
|
|
|
c := n.FirstChild
|
|
|
|
if c != nil && c.Type == html.TextNode && strings.HasPrefix(c.Data, "terraform-") {
|
|
|
|
names = append(names, c.Data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
f(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(body)
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
var versions []Version
|
2017-05-03 17:02:47 +02:00
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
parts := strings.SplitN(name, "_", 2)
|
|
|
|
if len(parts) == 2 && parts[1] != "" {
|
|
|
|
v, err := VersionStr(parts[1]).Parse()
|
|
|
|
if err != nil {
|
|
|
|
// filter invalid versions scraped from the page
|
|
|
|
log.Printf("[WARN] invalid version found for %q: %s", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-04 20:37:16 +02:00
|
|
|
versions = append(versions, v)
|
2017-05-03 17:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions, nil
|
|
|
|
}
|