2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-04-13 20:05:58 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-04-21 20:06:30 -05:00
|
|
|
"fmt"
|
2017-04-13 20:05:58 -05:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
2017-05-03 21:24:51 -05:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2017-04-13 20:05:58 -05:00
|
|
|
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-08-24 18:13:50 -05:00
|
|
|
"github.com/kardianos/osext"
|
|
|
|
|
2021-05-17 10:55:57 -05:00
|
|
|
fileprovisioner "github.com/hashicorp/terraform/internal/builtin/provisioners/file"
|
|
|
|
localexec "github.com/hashicorp/terraform/internal/builtin/provisioners/local-exec"
|
|
|
|
remoteexec "github.com/hashicorp/terraform/internal/builtin/provisioners/remote-exec"
|
2020-10-17 08:57:03 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/logging"
|
2021-05-17 14:35:22 -05:00
|
|
|
tfplugin "github.com/hashicorp/terraform/internal/plugin"
|
|
|
|
"github.com/hashicorp/terraform/internal/plugin/discovery"
|
2021-05-17 12:51:48 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/provisioners"
|
2017-04-13 20:05:58 -05:00
|
|
|
)
|
|
|
|
|
2020-03-30 17:30:56 -05:00
|
|
|
// NOTE WELL: The logic in this file is primarily about plugin types OTHER THAN
|
|
|
|
// providers, which use an older set of approaches implemented here.
|
|
|
|
//
|
|
|
|
// The provider-related functions live primarily in meta_providers.go, and
|
|
|
|
// lean on some different underlying mechanisms in order to support automatic
|
2020-07-23 08:09:22 -05:00
|
|
|
// installation and a hierarchical addressing namespace, neither of which
|
2020-03-30 17:30:56 -05:00
|
|
|
// are supported for other plugin types.
|
2017-04-21 20:06:30 -05:00
|
|
|
|
2017-06-15 13:26:12 -05:00
|
|
|
// store the user-supplied path for plugin discovery
|
|
|
|
func (m *Meta) storePluginPath(pluginPath []string) error {
|
|
|
|
if len(pluginPath) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 19:01:44 -05:00
|
|
|
m.fixupMissingWorkingDir()
|
2017-12-21 10:21:07 -06:00
|
|
|
|
2018-01-04 15:49:45 -06:00
|
|
|
// remove the plugin dir record if the path was set to an empty string
|
|
|
|
if len(pluginPath) == 1 && (pluginPath[0] == "") {
|
2021-09-01 19:01:44 -05:00
|
|
|
return m.WorkingDir.SetForcedPluginDirs(nil)
|
2017-06-15 13:26:12 -05:00
|
|
|
}
|
|
|
|
|
2021-09-01 19:01:44 -05:00
|
|
|
return m.WorkingDir.SetForcedPluginDirs(pluginPath)
|
2017-06-15 13:26:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the user-defined plugin search path into Meta.pluginPath if the file
|
|
|
|
// exists.
|
|
|
|
func (m *Meta) loadPluginPath() ([]string, error) {
|
2021-09-01 19:01:44 -05:00
|
|
|
m.fixupMissingWorkingDir()
|
|
|
|
return m.WorkingDir.ForcedPluginDirs()
|
2017-06-15 13:26:12 -05:00
|
|
|
}
|
|
|
|
|
2017-05-03 21:24:51 -05:00
|
|
|
// 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.
|
2017-06-12 20:22:47 -05:00
|
|
|
func (m *Meta) pluginDirs(includeAutoInstalled bool) []string {
|
2017-06-15 13:26:12 -05:00
|
|
|
// user defined paths take precedence
|
|
|
|
if len(m.pluginPath) > 0 {
|
|
|
|
return m.pluginPath
|
|
|
|
}
|
2017-04-13 20:05:58 -05:00
|
|
|
|
|
|
|
// 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).
|
2017-05-03 21:24:51 -05:00
|
|
|
dirs := []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 {
|
|
|
|
dirs = append(dirs, filepath.Dir(exePath))
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:12:00 -05:00
|
|
|
// add the user vendor directory
|
|
|
|
dirs = append(dirs, DefaultPluginVendorDir)
|
|
|
|
|
2017-06-12 20:22:47 -05:00
|
|
|
if includeAutoInstalled {
|
|
|
|
dirs = append(dirs, m.pluginDir())
|
|
|
|
}
|
2017-04-13 20:05:58 -05:00
|
|
|
dirs = append(dirs, m.GlobalPluginDirs...)
|
2017-06-15 09:12:00 -05:00
|
|
|
|
2017-05-03 21:24:51 -05:00
|
|
|
return dirs
|
|
|
|
}
|
2017-04-13 20:05:58 -05:00
|
|
|
|
2020-11-18 12:41:33 -06:00
|
|
|
func (m *Meta) provisionerFactories() map[string]provisioners.Factory {
|
2017-06-12 20:22:47 -05:00
|
|
|
dirs := m.pluginDirs(true)
|
2017-04-13 20:05:58 -05:00
|
|
|
plugins := discovery.FindPlugins("provisioner", dirs)
|
|
|
|
plugins, _ = plugins.ValidateVersions()
|
|
|
|
|
|
|
|
// For now our goal is to just find the latest version of each plugin
|
|
|
|
// we have on the system. All provisioners should be at version 0.0.0
|
|
|
|
// currently, so there should actually only be one instance of each plugin
|
|
|
|
// name here, even though the discovery interface forces us to pretend
|
|
|
|
// that might not be true.
|
|
|
|
|
2020-11-18 12:41:33 -06:00
|
|
|
factories := make(map[string]provisioners.Factory)
|
2017-04-13 20:05:58 -05:00
|
|
|
|
|
|
|
// Wire up the internal provisioners first. These might be overridden
|
|
|
|
// by discovered provisioners below.
|
2020-11-25 16:22:40 -06:00
|
|
|
for name, factory := range internalProvisionerFactories() {
|
|
|
|
factories[name] = factory
|
2017-04-13 20:05:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
byName := plugins.ByName()
|
|
|
|
for name, metas := range byName {
|
|
|
|
// Since we validated versions above and we partitioned the sets
|
|
|
|
// by name, we're guaranteed that the metas in our set all have
|
|
|
|
// valid versions and that there's at least one meta.
|
|
|
|
newest := metas.Newest()
|
2018-10-02 15:06:12 -05:00
|
|
|
|
|
|
|
factories[name] = provisionerFactory(newest)
|
2017-04-13 20:05:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return factories
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:41:33 -06:00
|
|
|
func provisionerFactory(meta discovery.PluginMeta) provisioners.Factory {
|
2018-08-24 18:13:50 -05:00
|
|
|
return func() (provisioners.Interface, error) {
|
2020-10-17 08:57:03 -05:00
|
|
|
cfg := &plugin.ClientConfig{
|
|
|
|
Cmd: exec.Command(meta.Path),
|
|
|
|
HandshakeConfig: tfplugin.Handshake,
|
|
|
|
VersionedPlugins: tfplugin.VersionedPlugins,
|
|
|
|
Managed: true,
|
2020-10-22 14:32:13 -05:00
|
|
|
Logger: logging.NewLogger("provisioner"),
|
2020-10-17 08:57:03 -05:00
|
|
|
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
|
|
|
|
AutoMTLS: enableProviderAutoMTLS,
|
2021-05-13 14:59:51 -05:00
|
|
|
SyncStdout: logging.PluginOutputMonitor(fmt.Sprintf("%s:stdout", meta.Name)),
|
|
|
|
SyncStderr: logging.PluginOutputMonitor(fmt.Sprintf("%s:stderr", meta.Name)),
|
2020-10-17 08:57:03 -05:00
|
|
|
}
|
|
|
|
client := plugin.NewClient(cfg)
|
2018-10-02 15:06:12 -05:00
|
|
|
return newProvisionerClient(client)
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 20:05:58 -05:00
|
|
|
|
2020-11-25 16:22:40 -06:00
|
|
|
func internalProvisionerFactories() map[string]provisioners.Factory {
|
|
|
|
return map[string]provisioners.Factory{
|
|
|
|
"file": provisioners.FactoryFixed(fileprovisioner.New()),
|
|
|
|
"local-exec": provisioners.FactoryFixed(localexec.New()),
|
|
|
|
"remote-exec": provisioners.FactoryFixed(remoteexec.New()),
|
2018-10-02 15:06:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProvisionerClient(client *plugin.Client) (provisioners.Interface, error) {
|
|
|
|
// Request the RPC client so we can get the provisioner
|
|
|
|
// so we can build the actual RPC-implemented provisioner.
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-13 20:05:58 -05:00
|
|
|
|
2018-10-02 15:06:12 -05:00
|
|
|
raw, err := rpcClient.Dispense(tfplugin.ProvisionerPluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-04-13 20:05:58 -05:00
|
|
|
}
|
2018-10-02 15:06:12 -05:00
|
|
|
|
|
|
|
// store the client so that the plugin can kill the child process
|
|
|
|
p := raw.(*tfplugin.GRPCProvisioner)
|
|
|
|
p.PluginClient = client
|
|
|
|
return p, nil
|
2017-04-13 20:05:58 -05:00
|
|
|
}
|