2017-04-14 03:05:58 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-06-15 20:26:12 +02:00
|
|
|
"encoding/json"
|
2017-06-22 20:18:52 +02:00
|
|
|
"errors"
|
2017-04-22 03:06:30 +02:00
|
|
|
"fmt"
|
2017-06-15 20:26:12 +02:00
|
|
|
"io/ioutil"
|
2017-04-14 03:05:58 +02:00
|
|
|
"log"
|
2017-06-15 20:26:12 +02:00
|
|
|
"os"
|
2017-04-14 03:05:58 +02:00
|
|
|
"os/exec"
|
2017-05-04 04:24:51 +02:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2017-04-14 03:05:58 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-08-25 01:13:50 +02:00
|
|
|
"github.com/kardianos/osext"
|
|
|
|
|
2019-12-04 17:30:20 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-08-25 01:13:50 +02:00
|
|
|
terraformProvider "github.com/hashicorp/terraform/builtin/providers/terraform"
|
2017-04-14 03:05:58 +02:00
|
|
|
tfplugin "github.com/hashicorp/terraform/plugin"
|
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
2018-08-25 01:13:50 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/hashicorp/terraform/provisioners"
|
2017-04-14 03:05:58 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-04-22 03:06:30 +02:00
|
|
|
// multiVersionProviderResolver is an implementation of
|
|
|
|
// terraform.ResourceProviderResolver that matches the given version constraints
|
|
|
|
// against a set of versioned provider plugins to find the newest version of
|
|
|
|
// each that satisfies the given constraints.
|
|
|
|
type multiVersionProviderResolver struct {
|
|
|
|
Available discovery.PluginMetaSet
|
2017-11-03 19:36:31 +01:00
|
|
|
|
|
|
|
// Internal is a map that overrides the usual plugin selection process
|
|
|
|
// for internal plugins. These plugins do not support version constraints
|
|
|
|
// (will produce an error if one is set). This should be used only in
|
|
|
|
// exceptional circumstances since it forces the provider's release
|
|
|
|
// schedule to be tied to that of Terraform Core.
|
2019-12-04 17:30:20 +01:00
|
|
|
Internal map[addrs.Provider]providers.Factory
|
2017-04-22 03:06:30 +02:00
|
|
|
}
|
|
|
|
|
2019-12-09 15:47:09 +01:00
|
|
|
func chooseProviders(avail discovery.PluginMetaSet, internal map[addrs.Provider]providers.Factory, reqd discovery.PluginRequirements) map[string]discovery.PluginMeta {
|
2017-05-25 02:35:46 +02:00
|
|
|
candidates := avail.ConstrainVersions(reqd)
|
|
|
|
ret := map[string]discovery.PluginMeta{}
|
|
|
|
for name, metas := range candidates {
|
2017-11-03 19:36:31 +01:00
|
|
|
// If the provider is in our internal map then we ignore any
|
|
|
|
// discovered plugins for it since these are dealt with separately.
|
2019-12-04 17:30:20 +01:00
|
|
|
if _, isInternal := internal[addrs.NewLegacyProvider(name)]; isInternal {
|
2017-11-03 19:36:31 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:35:46 +02:00
|
|
|
if len(metas) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret[name] = metas.Newest()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-04-22 03:06:30 +02:00
|
|
|
func (r *multiVersionProviderResolver) ResolveProviders(
|
|
|
|
reqd discovery.PluginRequirements,
|
2019-12-04 17:30:20 +01:00
|
|
|
) (map[addrs.Provider]providers.Factory, []error) {
|
|
|
|
factories := make(map[addrs.Provider]providers.Factory, len(reqd))
|
2017-04-22 03:06:30 +02:00
|
|
|
var errs []error
|
|
|
|
|
2019-12-09 15:47:09 +01:00
|
|
|
chosen := chooseProviders(r.Available, r.Internal, reqd)
|
2017-06-22 21:28:39 +02:00
|
|
|
for name, req := range reqd {
|
2019-12-04 17:30:20 +01:00
|
|
|
if factory, isInternal := r.Internal[addrs.NewLegacyProvider(name)]; isInternal {
|
2017-11-03 19:36:31 +01:00
|
|
|
if !req.Versions.Unconstrained() {
|
|
|
|
errs = append(errs, fmt.Errorf("provider.%s: this provider is built in to Terraform and so it does not support version constraints", name))
|
|
|
|
continue
|
|
|
|
}
|
2019-12-04 17:30:20 +01:00
|
|
|
factories[addrs.NewLegacyProvider(name)] = factory
|
2017-11-03 19:36:31 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:35:46 +02:00
|
|
|
if newest, available := chosen[name]; available {
|
2017-05-25 00:15:24 +02:00
|
|
|
digest, err := newest.SHA256()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("provider.%s: failed to load plugin to verify its signature: %s", name, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reqd[name].AcceptsSHA256(digest) {
|
2017-06-22 21:28:39 +02:00
|
|
|
errs = append(errs, fmt.Errorf("provider.%s: new or changed plugin executable", name))
|
2017-05-25 00:15:24 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:30:20 +01:00
|
|
|
factories[addrs.NewLegacyProvider(name)] = providerFactory(newest)
|
2017-04-22 03:06:30 +02:00
|
|
|
} else {
|
2017-06-22 20:18:52 +02:00
|
|
|
msg := fmt.Sprintf("provider.%s: no suitable version installed", name)
|
|
|
|
|
2017-06-22 21:28:39 +02:00
|
|
|
required := req.Versions.String()
|
|
|
|
// no version is unconstrained
|
|
|
|
if required == "" {
|
|
|
|
required = "(any version)"
|
|
|
|
}
|
2017-06-22 20:18:52 +02:00
|
|
|
|
2017-06-22 21:28:39 +02:00
|
|
|
foundVersions := []string{}
|
|
|
|
for meta := range r.Available.WithName(name) {
|
|
|
|
foundVersions = append(foundVersions, fmt.Sprintf("%q", meta.Version))
|
|
|
|
}
|
2017-06-22 20:18:52 +02:00
|
|
|
|
2017-06-22 21:28:39 +02:00
|
|
|
found := "none"
|
|
|
|
if len(foundVersions) > 0 {
|
|
|
|
found = strings.Join(foundVersions, ", ")
|
2017-06-22 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 21:28:39 +02:00
|
|
|
msg += fmt.Sprintf("\n version requirements: %q\n versions installed: %s", required, found)
|
|
|
|
|
2017-06-22 20:18:52 +02:00
|
|
|
errs = append(errs, errors.New(msg))
|
2017-04-22 03:06:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return factories, errs
|
|
|
|
}
|
|
|
|
|
2017-06-15 20:26:12 +02:00
|
|
|
// store the user-supplied path for plugin discovery
|
|
|
|
func (m *Meta) storePluginPath(pluginPath []string) error {
|
|
|
|
if len(pluginPath) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-21 17:21:07 +01:00
|
|
|
path := filepath.Join(m.DataDir(), PluginPathFile)
|
|
|
|
|
2018-01-04 22:49:45 +01:00
|
|
|
// remove the plugin dir record if the path was set to an empty string
|
|
|
|
if len(pluginPath) == 1 && (pluginPath[0] == "") {
|
2017-12-21 17:21:07 +01:00
|
|
|
err := os.Remove(path)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-15 20:26:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
js, err := json.MarshalIndent(pluginPath, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-15 21:23:16 +02:00
|
|
|
// if this fails, so will WriteFile
|
|
|
|
os.MkdirAll(m.DataDir(), 0755)
|
|
|
|
|
2017-12-21 17:21:07 +01:00
|
|
|
return ioutil.WriteFile(path, js, 0644)
|
2017-06-15 20:26:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the user-defined plugin search path into Meta.pluginPath if the file
|
|
|
|
// exists.
|
|
|
|
func (m *Meta) loadPluginPath() ([]string, error) {
|
|
|
|
js, err := ioutil.ReadFile(filepath.Join(m.DataDir(), PluginPathFile))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pluginPath []string
|
|
|
|
if err := json.Unmarshal(js, &pluginPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pluginPath, nil
|
|
|
|
}
|
|
|
|
|
2017-05-04 04:24:51 +02: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-13 03:22:47 +02:00
|
|
|
func (m *Meta) pluginDirs(includeAutoInstalled bool) []string {
|
2017-06-15 20:26:12 +02:00
|
|
|
// user defined paths take precedence
|
|
|
|
if len(m.pluginPath) > 0 {
|
|
|
|
return m.pluginPath
|
|
|
|
}
|
2017-04-14 03:05:58 +02: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-04 04:24:51 +02: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 16:12:00 +02:00
|
|
|
// add the user vendor directory
|
|
|
|
dirs = append(dirs, DefaultPluginVendorDir)
|
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
if includeAutoInstalled {
|
|
|
|
dirs = append(dirs, m.pluginDir())
|
|
|
|
}
|
2017-04-14 03:05:58 +02:00
|
|
|
dirs = append(dirs, m.GlobalPluginDirs...)
|
2017-06-15 16:12:00 +02:00
|
|
|
|
2017-05-04 04:24:51 +02:00
|
|
|
return dirs
|
|
|
|
}
|
2017-04-14 03:05:58 +02:00
|
|
|
|
2017-09-02 01:05:05 +02:00
|
|
|
func (m *Meta) pluginCache() discovery.PluginCache {
|
|
|
|
dir := m.PluginCacheDir
|
|
|
|
if dir == "" {
|
|
|
|
return nil // cache disabled
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = filepath.Join(dir, pluginMachineName)
|
|
|
|
|
|
|
|
return discovery.NewLocalPluginCache(dir)
|
|
|
|
}
|
|
|
|
|
2017-05-04 04:24:51 +02:00
|
|
|
// providerPluginSet returns the set of valid providers that were discovered in
|
|
|
|
// the defined search paths.
|
|
|
|
func (m *Meta) providerPluginSet() discovery.PluginMetaSet {
|
2017-06-13 03:22:47 +02:00
|
|
|
plugins := discovery.FindPlugins("provider", m.pluginDirs(true))
|
2017-08-09 17:14:33 +02:00
|
|
|
|
|
|
|
// Add providers defined in the legacy .terraformrc,
|
|
|
|
if m.PluginOverrides != nil {
|
2017-12-08 22:12:23 +01:00
|
|
|
for k, v := range m.PluginOverrides.Providers {
|
|
|
|
log.Printf("[DEBUG] found plugin override in .terraformrc: %q, %q", k, v)
|
|
|
|
}
|
2017-08-09 17:14:33 +02:00
|
|
|
plugins = plugins.OverridePaths(m.PluginOverrides.Providers)
|
|
|
|
}
|
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
plugins, _ = plugins.ValidateVersions()
|
|
|
|
|
|
|
|
for p := range plugins {
|
2017-12-08 22:12:23 +01:00
|
|
|
log.Printf("[DEBUG] found valid plugin: %q, %q, %q", p.Name, p.Version, p.Path)
|
2017-06-13 03:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
// providerPluginAutoInstalledSet returns the set of providers that exist
|
|
|
|
// within the auto-install directory.
|
|
|
|
func (m *Meta) providerPluginAutoInstalledSet() discovery.PluginMetaSet {
|
|
|
|
plugins := discovery.FindPlugins("provider", []string{m.pluginDir()})
|
|
|
|
plugins, _ = plugins.ValidateVersions()
|
|
|
|
|
|
|
|
for p := range plugins {
|
|
|
|
log.Printf("[DEBUG] found valid plugin: %q", p.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
// providerPluginManuallyInstalledSet returns the set of providers that exist
|
|
|
|
// in all locations *except* the auto-install directory.
|
|
|
|
func (m *Meta) providerPluginManuallyInstalledSet() discovery.PluginMetaSet {
|
|
|
|
plugins := discovery.FindPlugins("provider", m.pluginDirs(false))
|
2017-08-09 17:14:33 +02:00
|
|
|
|
|
|
|
// Add providers defined in the legacy .terraformrc,
|
|
|
|
if m.PluginOverrides != nil {
|
2017-12-08 22:12:23 +01:00
|
|
|
for k, v := range m.PluginOverrides.Providers {
|
|
|
|
log.Printf("[DEBUG] found plugin override in .terraformrc: %q, %q", k, v)
|
|
|
|
}
|
|
|
|
|
2017-08-09 17:14:33 +02:00
|
|
|
plugins = plugins.OverridePaths(m.PluginOverrides.Providers)
|
|
|
|
}
|
|
|
|
|
2017-04-14 03:05:58 +02:00
|
|
|
plugins, _ = plugins.ValidateVersions()
|
|
|
|
|
2017-06-09 17:20:27 +02:00
|
|
|
for p := range plugins {
|
2017-12-08 22:12:23 +01:00
|
|
|
log.Printf("[DEBUG] found valid plugin: %q, %q, %q", p.Name, p.Version, p.Path)
|
2017-06-09 17:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:02:47 +02:00
|
|
|
return plugins
|
|
|
|
}
|
|
|
|
|
2018-08-25 01:13:50 +02:00
|
|
|
func (m *Meta) providerResolver() providers.Resolver {
|
2017-04-22 03:06:30 +02:00
|
|
|
return &multiVersionProviderResolver{
|
2017-05-03 17:02:47 +02:00
|
|
|
Available: m.providerPluginSet(),
|
2017-11-03 19:36:31 +01:00
|
|
|
Internal: m.internalProviders(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:30:20 +01:00
|
|
|
func (m *Meta) internalProviders() map[addrs.Provider]providers.Factory {
|
|
|
|
return map[addrs.Provider]providers.Factory{
|
|
|
|
addrs.NewLegacyProvider("terraform"): func() (providers.Interface, error) {
|
2018-08-25 01:13:50 +02:00
|
|
|
return terraformProvider.NewProvider(), nil
|
|
|
|
},
|
2017-04-14 03:05:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:02:47 +02:00
|
|
|
// filter the requirements returning only the providers that we can't resolve
|
2020-03-20 18:59:59 +01:00
|
|
|
func (m *Meta) missingProviders(avail discovery.PluginMetaSet, reqd discovery.PluginRequirements) discovery.PluginRequirements {
|
2017-05-03 17:02:47 +02:00
|
|
|
missing := make(discovery.PluginRequirements)
|
|
|
|
|
2017-05-25 02:35:46 +02:00
|
|
|
candidates := avail.ConstrainVersions(reqd)
|
2018-01-05 17:52:11 +01:00
|
|
|
internal := m.internalProviders()
|
2017-05-03 17:02:47 +02:00
|
|
|
|
|
|
|
for name, versionSet := range reqd {
|
2018-01-05 17:52:11 +01:00
|
|
|
// internal providers can't be missing
|
2019-12-04 17:30:20 +01:00
|
|
|
if _, ok := internal[addrs.NewLegacyProvider(name)]; ok {
|
2018-01-05 17:52:11 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] plugin requirements: %q=%q", name, versionSet.Versions)
|
2017-06-09 17:20:27 +02:00
|
|
|
if metas := candidates[name]; metas.Count() == 0 {
|
2017-05-03 17:02:47 +02:00
|
|
|
missing[name] = versionSet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return missing
|
|
|
|
}
|
|
|
|
|
2018-08-25 01:13:50 +02:00
|
|
|
func (m *Meta) provisionerFactories() map[string]terraform.ProvisionerFactory {
|
2017-06-13 03:22:47 +02:00
|
|
|
dirs := m.pluginDirs(true)
|
2017-04-14 03:05:58 +02: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.
|
|
|
|
|
2018-08-25 01:13:50 +02:00
|
|
|
factories := make(map[string]terraform.ProvisionerFactory)
|
2017-04-14 03:05:58 +02:00
|
|
|
|
|
|
|
// Wire up the internal provisioners first. These might be overridden
|
|
|
|
// by discovered provisioners below.
|
|
|
|
for name := range InternalProvisioners {
|
2018-10-08 18:25:16 +02:00
|
|
|
factories[name] = internalProvisionerFactory(discovery.PluginMeta{Name: name})
|
2017-04-14 03:05:58 +02: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 22:06:12 +02:00
|
|
|
|
|
|
|
factories[name] = provisionerFactory(newest)
|
2017-04-14 03:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return factories
|
|
|
|
}
|
|
|
|
|
|
|
|
func internalPluginClient(kind, name string) (*plugin.Client, error) {
|
|
|
|
cmdLine, err := BuildPluginCommandString(kind, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// See the docstring for BuildPluginCommandString for why we need to do
|
|
|
|
// this split here.
|
|
|
|
cmdArgv := strings.Split(cmdLine, TFSPACE)
|
|
|
|
|
|
|
|
cfg := &plugin.ClientConfig{
|
2018-08-25 01:13:50 +02:00
|
|
|
Cmd: exec.Command(cmdArgv[0], cmdArgv[1:]...),
|
|
|
|
HandshakeConfig: tfplugin.Handshake,
|
|
|
|
Managed: true,
|
|
|
|
VersionedPlugins: tfplugin.VersionedPlugins,
|
|
|
|
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
|
2017-04-14 03:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return plugin.NewClient(cfg), nil
|
|
|
|
}
|
|
|
|
|
2018-10-02 22:06:12 +02:00
|
|
|
func providerFactory(meta discovery.PluginMeta) providers.Factory {
|
2018-08-25 01:13:50 +02:00
|
|
|
return func() (providers.Interface, error) {
|
2018-10-02 22:06:12 +02:00
|
|
|
client := tfplugin.Client(meta)
|
2017-04-14 03:05:58 +02:00
|
|
|
// Request the RPC client so we can get the provider
|
|
|
|
// so we can build the actual RPC-implemented provider.
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := rpcClient.Dispense(tfplugin.ProviderPluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-02 22:06:12 +02:00
|
|
|
// store the client so that the plugin can kill the child process
|
|
|
|
p := raw.(*tfplugin.GRPCProvider)
|
|
|
|
p.PluginClient = client
|
|
|
|
return p, nil
|
2017-04-14 03:05:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 22:06:12 +02:00
|
|
|
func provisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory {
|
2018-08-25 01:13:50 +02:00
|
|
|
return func() (provisioners.Interface, error) {
|
2018-10-02 22:06:12 +02:00
|
|
|
client := tfplugin.Client(meta)
|
|
|
|
return newProvisionerClient(client)
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 03:05:58 +02:00
|
|
|
|
2018-10-08 18:25:16 +02:00
|
|
|
func internalProvisionerFactory(meta discovery.PluginMeta) terraform.ProvisionerFactory {
|
2018-10-02 22:06:12 +02:00
|
|
|
return func() (provisioners.Interface, error) {
|
2018-10-08 18:25:16 +02:00
|
|
|
client, err := internalPluginClient("provisioner", meta.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("[WARN] failed to build command line for internal plugin %q: %s", meta.Name, err)
|
|
|
|
}
|
2018-10-02 22:06:12 +02:00
|
|
|
return newProvisionerClient(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-14 03:05:58 +02:00
|
|
|
|
2018-10-02 22:06:12 +02:00
|
|
|
raw, err := rpcClient.Dispense(tfplugin.ProvisionerPluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-04-14 03:05:58 +02:00
|
|
|
}
|
2018-10-02 22:06:12 +02: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-14 03:05:58 +02:00
|
|
|
}
|