diff --git a/command/plugins.go b/command/plugins.go index b36f52929..dfd168dbd 100644 --- a/command/plugins.go +++ b/command/plugins.go @@ -4,12 +4,15 @@ import ( "fmt" "log" "os/exec" + "path/filepath" + "runtime" "strings" plugin "github.com/hashicorp/go-plugin" tfplugin "github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin/discovery" "github.com/hashicorp/terraform/terraform" + "github.com/kardianos/osext" ) // multiVersionProviderResolver is an implementation of @@ -40,20 +43,43 @@ func (r *multiVersionProviderResolver) ResolveProviders( return factories, errs } -// providerPluginSet returns the set of valid providers that were discovered in -// the defined search paths. -func (m *Meta) providerPluginSet() discovery.PluginMetaSet { - var dirs []string +// the default location for automatically installed plugins +func (m *Meta) pluginDir() string { + return filepath.Join(m.DataDir(), "plugins", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)) +} + +// pluginDirs return a list of directories to search for plugins. +// +// Earlier entries in this slice get priority over later when multiple copies +// of the same plugin version are found, but newer versions always override +// older versions where both satisfy the provider version constraints. +func (m *Meta) pluginDirs() []string { // When searching the following directories, earlier entries get precedence // if the same plugin version is found twice, but newer versions will // always get preference below regardless of where they are coming from. // TODO: Add auto-install dir, default vendor dir and optional override // vendor dir(s). - dirs = append(dirs, ".") - dirs = append(dirs, m.GlobalPluginDirs...) + dirs := []string{"."} - plugins := discovery.FindPlugins("provider", dirs) + // Look in the same directory as the Terraform executable. + // If found, this replaces what we found in the config path. + exePath, err := osext.Executable() + if err != nil { + log.Printf("[ERROR] Error discovering exe directory: %s", err) + } else { + dirs = append(dirs, filepath.Dir(exePath)) + } + + dirs = append(dirs, m.pluginDir()) + dirs = append(dirs, m.GlobalPluginDirs...) + return dirs +} + +// providerPluginSet returns the set of valid providers that were discovered in +// the defined search paths. +func (m *Meta) providerPluginSet() discovery.PluginMetaSet { + plugins := discovery.FindPlugins("provider", m.pluginDirs()) plugins, _ = plugins.ValidateVersions() return plugins @@ -81,19 +107,7 @@ func (m *Meta) missingProviders(reqd discovery.PluginRequirements) discovery.Plu } func (m *Meta) provisionerFactories() map[string]terraform.ResourceProvisionerFactory { - var dirs []string - - // When searching the following directories, earlier entries get precedence - // if the same plugin version is found twice, but newer versions will - // always get preference below regardless of where they are coming from. - // - // NOTE: Currently we don't use versioning for provisioners, so the - // version handling here is just the minimum required to be able to use - // the plugin discovery package. All provisioner plugins should always - // be versionless, which we treat as version 0.0.0 here. - dirs = append(dirs, ".") - dirs = append(dirs, m.GlobalPluginDirs...) - + dirs := m.pluginDirs() plugins := discovery.FindPlugins("provisioner", dirs) plugins, _ = plugins.ValidateVersions() diff --git a/plugins.go b/plugins.go index 9717724a0..bf2397806 100644 --- a/plugins.go +++ b/plugins.go @@ -3,8 +3,6 @@ package main import ( "log" "path/filepath" - - "github.com/kardianos/osext" ) // globalPluginDirs returns directories that should be searched for @@ -15,16 +13,6 @@ import ( // older versions where both satisfy the provider version constraints. func globalPluginDirs() []string { var ret []string - - // Look in the same directory as the Terraform executable. - // If found, this replaces what we found in the config path. - exePath, err := osext.Executable() - if err != nil { - log.Printf("[ERROR] Error discovering exe directory: %s", err) - } else { - ret = append(ret, filepath.Dir(exePath)) - } - // Look in ~/.terraform.d/plugins/ , or its equivalent on non-UNIX dir, err := ConfigDir() if err != nil {