2017-04-12 19:39:04 +02:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2017-06-09 17:20:09 +02:00
|
|
|
"log"
|
2017-09-02 01:00:15 +02:00
|
|
|
"os"
|
2017-04-12 19:39:04 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindPlugins looks in the given directories for files whose filenames
|
|
|
|
// suggest that they are plugins of the given kind (e.g. "provider") and
|
|
|
|
// returns a PluginMetaSet representing the discovered potential-plugins.
|
|
|
|
//
|
|
|
|
// Currently this supports two different naming schemes. The current
|
|
|
|
// standard naming scheme is a subdirectory called $GOOS-$GOARCH containing
|
|
|
|
// files named terraform-$KIND-$NAME-V$VERSION. The legacy naming scheme is
|
|
|
|
// files directly in the given directory whose names are like
|
|
|
|
// terraform-$KIND-$NAME.
|
|
|
|
//
|
|
|
|
// Only one plugin will be returned for each unique plugin (name, version)
|
|
|
|
// pair, with preference given to files found in earlier directories.
|
|
|
|
//
|
|
|
|
// This is a convenience wrapper around FindPluginPaths and ResolvePluginsPaths.
|
|
|
|
func FindPlugins(kind string, dirs []string) PluginMetaSet {
|
|
|
|
return ResolvePluginPaths(FindPluginPaths(kind, dirs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPluginPaths looks in the given directories for files whose filenames
|
|
|
|
// suggest that they are plugins of the given kind (e.g. "provider").
|
|
|
|
//
|
|
|
|
// The return value is a list of absolute paths that appear to refer to
|
|
|
|
// plugins in the given directories, based only on what can be inferred
|
|
|
|
// from the naming scheme. The paths returned are ordered such that files
|
|
|
|
// in later dirs appear after files in earlier dirs in the given directory
|
|
|
|
// list. Within the same directory plugins are returned in a consistent but
|
|
|
|
// undefined order.
|
|
|
|
func FindPluginPaths(kind string, dirs []string) []string {
|
|
|
|
// This is just a thin wrapper around findPluginPaths so that we can
|
|
|
|
// use the latter in tests with a fake machineName so we can use our
|
|
|
|
// test fixtures.
|
2017-06-16 19:58:40 +02:00
|
|
|
return findPluginPaths(kind, dirs)
|
2017-04-12 19:39:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 19:58:40 +02:00
|
|
|
func findPluginPaths(kind string, dirs []string) []string {
|
2017-04-12 19:39:04 +02:00
|
|
|
prefix := "terraform-" + kind + "-"
|
|
|
|
|
|
|
|
ret := make([]string, 0, len(dirs))
|
|
|
|
|
2017-06-16 19:58:40 +02:00
|
|
|
for _, dir := range dirs {
|
|
|
|
items, err := ioutil.ReadDir(dir)
|
2017-04-12 19:39:04 +02:00
|
|
|
if err != nil {
|
|
|
|
// Ignore missing dirs, non-dirs, etc
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:09:54 +02:00
|
|
|
log.Printf("[DEBUG] checking for %s in %q", kind, dir)
|
2017-06-16 19:58:40 +02:00
|
|
|
|
|
|
|
for _, item := range items {
|
2017-04-12 19:39:04 +02:00
|
|
|
fullName := item.Name()
|
|
|
|
|
2017-06-16 19:58:40 +02:00
|
|
|
if !strings.HasPrefix(fullName, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// New-style paths must have a version segment in filename
|
|
|
|
if strings.Contains(strings.ToLower(fullName), "_v") {
|
|
|
|
absPath, err := filepath.Abs(filepath.Join(dir, fullName))
|
2017-04-12 19:39:04 +02:00
|
|
|
if err != nil {
|
2017-06-16 19:58:40 +02:00
|
|
|
log.Printf("[ERROR] plugin filepath error: %s", err)
|
2017-04-12 19:39:04 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-02 01:00:15 +02:00
|
|
|
// Check that the file we found is usable
|
|
|
|
if !pathIsFile(absPath) {
|
|
|
|
log.Printf("[ERROR] ignoring non-file %s", absPath)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:09:54 +02:00
|
|
|
log.Printf("[DEBUG] found %s %q", kind, fullName)
|
2017-06-16 19:58:40 +02:00
|
|
|
ret = append(ret, filepath.Clean(absPath))
|
2017-04-12 19:39:04 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:28:48 +02:00
|
|
|
// Legacy style with files directly in the base directory
|
|
|
|
absPath, err := filepath.Abs(filepath.Join(dir, fullName))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] plugin filepath error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2017-04-12 19:39:04 +02:00
|
|
|
|
2017-09-02 01:00:15 +02:00
|
|
|
// Check that the file we found is usable
|
|
|
|
if !pathIsFile(absPath) {
|
|
|
|
log.Printf("[ERROR] ignoring non-file %s", absPath)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-17 03:05:26 +01:00
|
|
|
log.Printf("[WARN] found legacy %s %q", kind, fullName)
|
2017-06-09 17:20:09 +02:00
|
|
|
|
2017-06-16 21:28:48 +02:00
|
|
|
ret = append(ret, filepath.Clean(absPath))
|
2017-04-12 19:39:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-09-02 01:00:15 +02:00
|
|
|
// Returns true if and only if the given path refers to a file or a symlink
|
|
|
|
// to a file.
|
|
|
|
func pathIsFile(path string) bool {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !info.IsDir()
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:39:04 +02:00
|
|
|
// ResolvePluginPaths takes a list of paths to plugin executables (as returned
|
|
|
|
// by e.g. FindPluginPaths) and produces a PluginMetaSet describing the
|
|
|
|
// referenced plugins.
|
|
|
|
//
|
|
|
|
// If the same combination of plugin name and version appears multiple times,
|
|
|
|
// the earlier reference will be preferred. Several different versions of
|
|
|
|
// the same plugin name may be returned, in which case the methods of
|
|
|
|
// PluginMetaSet can be used to filter down.
|
|
|
|
func ResolvePluginPaths(paths []string) PluginMetaSet {
|
|
|
|
s := make(PluginMetaSet)
|
|
|
|
|
|
|
|
type nameVersion struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
found := make(map[nameVersion]struct{})
|
|
|
|
|
|
|
|
for _, path := range paths {
|
2017-06-09 22:05:15 +02:00
|
|
|
baseName := strings.ToLower(filepath.Base(path))
|
2017-04-12 19:39:04 +02:00
|
|
|
if !strings.HasPrefix(baseName, "terraform-") {
|
|
|
|
// Should never happen with reasonable input
|
|
|
|
continue
|
|
|
|
}
|
2017-06-09 22:05:15 +02:00
|
|
|
|
2017-04-12 19:39:04 +02:00
|
|
|
baseName = baseName[10:]
|
|
|
|
firstDash := strings.Index(baseName, "-")
|
|
|
|
if firstDash == -1 {
|
|
|
|
// Should never happen with reasonable input
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
baseName = baseName[firstDash+1:]
|
|
|
|
if baseName == "" {
|
|
|
|
// Should never happen with reasonable input
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:52:45 +02:00
|
|
|
// Trim the .exe suffix used on Windows before we start wrangling
|
|
|
|
// the remainder of the path.
|
|
|
|
if strings.HasSuffix(baseName, ".exe") {
|
|
|
|
baseName = baseName[:len(baseName)-4]
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:05:15 +02:00
|
|
|
parts := strings.SplitN(baseName, "_v", 2)
|
2017-04-12 19:39:04 +02:00
|
|
|
name := parts[0]
|
2017-06-16 22:19:13 +02:00
|
|
|
version := VersionZero
|
2017-04-12 19:39:04 +02:00
|
|
|
if len(parts) == 2 {
|
|
|
|
version = parts[1]
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:29:10 +02:00
|
|
|
// Auto-installed plugins contain an extra name portion representing
|
|
|
|
// the expected plugin version, which we must trim off.
|
2017-06-09 22:05:15 +02:00
|
|
|
if underX := strings.Index(version, "_x"); underX != -1 {
|
|
|
|
version = version[:underX]
|
2017-05-25 02:29:10 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 19:39:04 +02:00
|
|
|
if _, ok := found[nameVersion{name, version}]; ok {
|
|
|
|
// Skip duplicate versions of the same plugin
|
|
|
|
// (We do this during this step because after this we will be
|
|
|
|
// dealing with sets and thus lose our ordering with which to
|
|
|
|
// decide preference.)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Add(PluginMeta{
|
|
|
|
Name: name,
|
2017-04-20 02:04:09 +02:00
|
|
|
Version: VersionStr(version),
|
2017-04-12 19:39:04 +02:00
|
|
|
Path: path,
|
|
|
|
})
|
|
|
|
found[nameVersion{name, version}] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|