2016-04-04 21:11:11 -05:00
|
|
|
//go:generate go run ./scripts/generate-plugins.go
|
2014-05-25 19:39:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-08-19 11:56:50 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-08-28 19:27:15 -05:00
|
|
|
"log"
|
2014-08-27 22:46:50 -05:00
|
|
|
"os"
|
2014-06-09 23:44:13 -05:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2014-08-27 22:46:50 -05:00
|
|
|
"strings"
|
2014-06-09 23:44:13 -05:00
|
|
|
|
2016-01-24 20:10:52 -06:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2014-08-19 11:56:50 -05:00
|
|
|
"github.com/hashicorp/hcl"
|
2016-04-04 21:11:11 -05:00
|
|
|
"github.com/hashicorp/terraform/command"
|
2016-01-24 20:10:52 -06:00
|
|
|
tfplugin "github.com/hashicorp/terraform/plugin"
|
2014-06-09 13:53:41 -05:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-10-17 19:33:45 -05:00
|
|
|
"github.com/kardianos/osext"
|
2016-04-26 12:25:20 -05:00
|
|
|
"github.com/mitchellh/cli"
|
2014-05-25 19:39:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the structure of the configuration for the Terraform CLI.
|
|
|
|
//
|
|
|
|
// This is not the configuration for Terraform itself. That is in the
|
|
|
|
// "config" package.
|
|
|
|
type Config struct {
|
2014-07-09 16:47:37 -05:00
|
|
|
Providers map[string]string
|
|
|
|
Provisioners map[string]string
|
2014-10-13 16:05:29 -05:00
|
|
|
|
|
|
|
DisableCheckpoint bool `hcl:"disable_checkpoint"`
|
|
|
|
DisableCheckpointSignature bool `hcl:"disable_checkpoint_signature"`
|
2014-05-25 19:39:44 -05:00
|
|
|
}
|
|
|
|
|
2014-06-09 23:44:13 -05:00
|
|
|
// BuiltinConfig is the built-in defaults for the configuration. These
|
|
|
|
// can be overridden by user configurations.
|
|
|
|
var BuiltinConfig Config
|
|
|
|
|
2014-07-03 14:01:20 -05:00
|
|
|
// ContextOpts are the global ContextOpts we use to initialize the CLI.
|
|
|
|
var ContextOpts terraform.ContextOpts
|
|
|
|
|
2014-08-19 12:58:23 -05:00
|
|
|
// ConfigFile returns the default path to the configuration file.
|
|
|
|
//
|
|
|
|
// On Unix-like systems this is the ".terraformrc" file in the home directory.
|
|
|
|
// On Windows, this is the "terraform.rc" file in the application data
|
|
|
|
// directory.
|
|
|
|
func ConfigFile() (string, error) {
|
2014-08-21 00:24:35 -05:00
|
|
|
return configFile()
|
2014-08-19 12:58:23 -05:00
|
|
|
}
|
|
|
|
|
2014-09-27 14:36:13 -05:00
|
|
|
// ConfigDir returns the configuration directory for Terraform.
|
|
|
|
func ConfigDir() (string, error) {
|
|
|
|
return configDir()
|
|
|
|
}
|
|
|
|
|
2014-05-25 19:39:44 -05:00
|
|
|
// LoadConfig loads the CLI configuration from ".terraformrc" files.
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
2014-08-19 11:56:50 -05:00
|
|
|
// Read the HCL file and prepare for parsing
|
|
|
|
d, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading %s: %s", path, err)
|
2014-05-25 19:39:44 -05:00
|
|
|
}
|
|
|
|
|
2014-08-19 11:56:50 -05:00
|
|
|
// Parse it
|
|
|
|
obj, err := hcl.Parse(string(d))
|
2014-05-25 19:39:44 -05:00
|
|
|
if err != nil {
|
2014-08-19 11:56:50 -05:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error parsing %s: %s", path, err)
|
2014-05-25 19:39:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the result
|
|
|
|
var result Config
|
2014-08-19 11:56:50 -05:00
|
|
|
if err := hcl.DecodeObject(&result, obj); err != nil {
|
2014-05-25 19:39:44 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:52:51 -06:00
|
|
|
// Replace all env vars
|
|
|
|
for k, v := range result.Providers {
|
|
|
|
result.Providers[k] = os.ExpandEnv(v)
|
|
|
|
}
|
|
|
|
for k, v := range result.Provisioners {
|
|
|
|
result.Provisioners[k] = os.ExpandEnv(v)
|
|
|
|
}
|
|
|
|
|
2014-05-25 19:39:44 -05:00
|
|
|
return &result, nil
|
|
|
|
}
|
2014-06-09 23:44:13 -05:00
|
|
|
|
2016-04-04 21:11:11 -05:00
|
|
|
// Discover plugins located on disk, and fall back on plugins baked into the
|
|
|
|
// Terraform binary.
|
2014-08-28 19:27:15 -05:00
|
|
|
//
|
2016-04-04 21:11:11 -05:00
|
|
|
// We look in the following places for plugins:
|
|
|
|
//
|
|
|
|
// 1. Terraform configuration path
|
|
|
|
// 2. Path where Terraform is installed
|
|
|
|
// 3. Path where Terraform is invoked
|
|
|
|
//
|
|
|
|
// Whichever file is discoverd LAST wins.
|
|
|
|
//
|
|
|
|
// Finally, we look at the list of plugins compiled into Terraform. If any of
|
|
|
|
// them has not been found on disk we use the internal version. This allows
|
|
|
|
// users to add / replace plugins without recompiling the main binary.
|
2016-04-26 12:25:20 -05:00
|
|
|
func (c *Config) Discover(ui cli.Ui) error {
|
2016-04-04 21:11:11 -05:00
|
|
|
// Look in ~/.terraform.d/plugins/
|
2014-09-27 14:36:13 -05:00
|
|
|
dir, err := ConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Error loading config directory: %s", err)
|
|
|
|
} else {
|
|
|
|
if err := c.discover(filepath.Join(dir, "plugins")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:11:11 -05:00
|
|
|
// Next, look in the same directory as the Terraform executable, usually
|
|
|
|
// /usr/local/bin. If found, this replaces what we found in the config path.
|
2014-08-28 19:27:15 -05:00
|
|
|
exePath, err := osext.Executable()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Error loading exe directory: %s", err)
|
|
|
|
} else {
|
|
|
|
if err := c.discover(filepath.Dir(exePath)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:11:11 -05:00
|
|
|
// Finally look in the cwd (where we are invoke Terraform). If found, this
|
|
|
|
// replaces anything we found in the config / install paths.
|
|
|
|
if err := c.discover("."); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, if we have a plugin compiled into Terraform and we didn't find
|
2016-05-27 12:51:23 -05:00
|
|
|
// a replacement on disk, we'll just use the internal version. Only do this
|
2016-05-27 13:49:09 -05:00
|
|
|
// from the main process, or the log output will break the plugin handshake.
|
2016-05-27 12:51:23 -05:00
|
|
|
if os.Getenv("TF_PLUGIN_MAGIC_COOKIE") == "" {
|
|
|
|
for name, _ := range command.InternalProviders {
|
|
|
|
if path, found := c.Providers[name]; found {
|
2016-05-27 13:49:09 -05:00
|
|
|
// Allow these warnings to be suppressed via TF_PLUGIN_DEV=1 or similar
|
|
|
|
if os.Getenv("TF_PLUGIN_DEV") == "" {
|
|
|
|
ui.Warn(fmt.Sprintf("[WARN] %s overrides an internal plugin for %s-provider.\n"+
|
|
|
|
" If you did not expect to see this message you will need to remove the old plugin.\n"+
|
|
|
|
" See https://www.terraform.io/docs/internals/internal-plugins.html", path, name))
|
|
|
|
}
|
2016-05-27 12:51:23 -05:00
|
|
|
} else {
|
|
|
|
cmd, err := command.BuildPluginCommandString("provider", name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Providers[name] = cmd
|
2016-04-04 21:11:11 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-27 12:51:23 -05:00
|
|
|
for name, _ := range command.InternalProvisioners {
|
|
|
|
if path, found := c.Provisioners[name]; found {
|
2016-05-27 13:49:09 -05:00
|
|
|
if os.Getenv("TF_PLUGIN_DEV") == "" {
|
|
|
|
ui.Warn(fmt.Sprintf("[WARN] %s overrides an internal plugin for %s-provisioner.\n"+
|
|
|
|
" If you did not expect to see this message you will need to remove the old plugin.\n"+
|
|
|
|
" See https://www.terraform.io/docs/internals/internal-plugins.html", path, name))
|
|
|
|
}
|
2016-05-27 12:51:23 -05:00
|
|
|
} else {
|
|
|
|
cmd, err := command.BuildPluginCommandString("provisioner", name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Provisioners[name] = cmd
|
2016-04-04 21:11:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 19:27:15 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-09 23:57:37 -05:00
|
|
|
// Merge merges two configurations and returns a third entirely
|
|
|
|
// new configuration with the two merged.
|
|
|
|
func (c1 *Config) Merge(c2 *Config) *Config {
|
|
|
|
var result Config
|
|
|
|
result.Providers = make(map[string]string)
|
2014-07-09 16:47:37 -05:00
|
|
|
result.Provisioners = make(map[string]string)
|
2014-06-09 23:57:37 -05:00
|
|
|
for k, v := range c1.Providers {
|
|
|
|
result.Providers[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range c2.Providers {
|
2016-09-13 07:05:37 -05:00
|
|
|
if v1, ok := c1.Providers[k]; ok {
|
2016-09-14 03:19:03 -05:00
|
|
|
log.Printf("[INFO] Local %s provider configuration '%s' overrides '%s'", k, v, v1)
|
2016-09-13 07:05:37 -05:00
|
|
|
}
|
2014-06-09 23:57:37 -05:00
|
|
|
result.Providers[k] = v
|
|
|
|
}
|
2014-07-09 16:47:37 -05:00
|
|
|
for k, v := range c1.Provisioners {
|
|
|
|
result.Provisioners[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range c2.Provisioners {
|
2016-09-13 07:05:37 -05:00
|
|
|
if v1, ok := c1.Provisioners[k]; ok {
|
2016-09-14 03:19:03 -05:00
|
|
|
log.Printf("[INFO] Local %s provisioner configuration '%s' overrides '%s'", k, v, v1)
|
2016-09-13 07:05:37 -05:00
|
|
|
}
|
2014-07-09 16:47:37 -05:00
|
|
|
result.Provisioners[k] = v
|
|
|
|
}
|
2016-11-17 17:51:41 -06:00
|
|
|
result.DisableCheckpoint = c1.DisableCheckpoint || c2.DisableCheckpoint
|
|
|
|
result.DisableCheckpointSignature = c1.DisableCheckpointSignature || c2.DisableCheckpointSignature
|
2014-06-09 23:57:37 -05:00
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2014-08-28 19:27:15 -05:00
|
|
|
func (c *Config) discover(path string) error {
|
|
|
|
var err error
|
2014-09-26 00:39:31 -05:00
|
|
|
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path, err = filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 19:27:15 -05:00
|
|
|
err = c.discoverSingle(
|
|
|
|
filepath.Join(path, "terraform-provider-*"), &c.Providers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.discoverSingle(
|
|
|
|
filepath.Join(path, "terraform-provisioner-*"), &c.Provisioners)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) discoverSingle(glob string, m *map[string]string) error {
|
|
|
|
matches, err := filepath.Glob(glob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if *m == nil {
|
|
|
|
*m = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
file := filepath.Base(match)
|
|
|
|
|
|
|
|
// If the filename has a ".", trim up to there
|
|
|
|
if idx := strings.Index(file, "."); idx >= 0 {
|
|
|
|
file = file[:idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for foo-bar-baz. The plugin name is "baz"
|
|
|
|
parts := strings.SplitN(file, "-", 3)
|
|
|
|
if len(parts) != 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-02-17 10:28:33 -06:00
|
|
|
log.Printf("[DEBUG] Discovered plugin: %s = %s", parts[2], match)
|
2014-08-28 19:27:15 -05:00
|
|
|
(*m)[parts[2]] = match
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-09 23:44:13 -05:00
|
|
|
// ProviderFactories returns the mapping of prefixes to
|
|
|
|
// ResourceProviderFactory that can be used to instantiate a
|
|
|
|
// binary-based plugin.
|
|
|
|
func (c *Config) ProviderFactories() map[string]terraform.ResourceProviderFactory {
|
|
|
|
result := make(map[string]terraform.ResourceProviderFactory)
|
|
|
|
for k, v := range c.Providers {
|
|
|
|
result[k] = c.providerFactory(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) providerFactory(path string) terraform.ResourceProviderFactory {
|
2014-09-28 13:40:15 -05:00
|
|
|
// Build the plugin client configuration and init the plugin
|
|
|
|
var config plugin.ClientConfig
|
|
|
|
config.Cmd = pluginCmd(path)
|
2016-01-24 20:10:52 -06:00
|
|
|
config.HandshakeConfig = tfplugin.Handshake
|
2014-09-28 13:40:15 -05:00
|
|
|
config.Managed = true
|
2016-01-24 20:10:52 -06:00
|
|
|
config.Plugins = tfplugin.PluginMap
|
2014-09-28 13:40:15 -05:00
|
|
|
client := plugin.NewClient(&config)
|
2014-06-09 23:44:13 -05:00
|
|
|
|
2014-09-28 13:40:15 -05:00
|
|
|
return func() (terraform.ResourceProvider, error) {
|
|
|
|
// Request the RPC client so we can get the provider
|
2014-06-09 23:44:13 -05:00
|
|
|
// so we can build the actual RPC-implemented provider.
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-24 20:10:52 -06:00
|
|
|
raw, err := rpcClient.Dispense(tfplugin.ProviderPluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw.(terraform.ResourceProvider), nil
|
2014-06-09 23:44:13 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-09 16:47:37 -05:00
|
|
|
|
|
|
|
// ProvisionerFactories returns the mapping of prefixes to
|
|
|
|
// ResourceProvisionerFactory that can be used to instantiate a
|
|
|
|
// binary-based plugin.
|
|
|
|
func (c *Config) ProvisionerFactories() map[string]terraform.ResourceProvisionerFactory {
|
|
|
|
result := make(map[string]terraform.ResourceProvisionerFactory)
|
|
|
|
for k, v := range c.Provisioners {
|
|
|
|
result[k] = c.provisionerFactory(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) provisionerFactory(path string) terraform.ResourceProvisionerFactory {
|
2014-09-28 13:40:15 -05:00
|
|
|
// Build the plugin client configuration and init the plugin
|
|
|
|
var config plugin.ClientConfig
|
2016-01-24 20:10:52 -06:00
|
|
|
config.HandshakeConfig = tfplugin.Handshake
|
2014-09-28 13:40:15 -05:00
|
|
|
config.Cmd = pluginCmd(path)
|
|
|
|
config.Managed = true
|
2016-01-24 20:10:52 -06:00
|
|
|
config.Plugins = tfplugin.PluginMap
|
2014-09-28 13:40:15 -05:00
|
|
|
client := plugin.NewClient(&config)
|
2014-07-09 16:47:37 -05:00
|
|
|
|
2014-09-28 13:40:15 -05:00
|
|
|
return func() (terraform.ResourceProvisioner, error) {
|
2014-07-09 16:47:37 -05:00
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-24 20:10:52 -06:00
|
|
|
raw, err := rpcClient.Dispense(tfplugin.ProvisionerPluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw.(terraform.ResourceProvisioner), nil
|
2014-07-09 16:47:37 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 09:29:53 -05:00
|
|
|
|
|
|
|
func pluginCmd(path string) *exec.Cmd {
|
2014-08-27 22:46:50 -05:00
|
|
|
cmdPath := ""
|
2014-07-24 09:29:53 -05:00
|
|
|
|
2014-08-27 22:46:50 -05:00
|
|
|
// If the path doesn't contain a separator, look in the same
|
|
|
|
// directory as the Terraform executable first.
|
|
|
|
if !strings.ContainsRune(path, os.PathSeparator) {
|
2014-07-24 09:29:53 -05:00
|
|
|
exePath, err := osext.Executable()
|
|
|
|
if err == nil {
|
2014-08-27 22:46:50 -05:00
|
|
|
temp := filepath.Join(
|
2014-07-24 09:29:53 -05:00
|
|
|
filepath.Dir(exePath),
|
2014-08-27 22:46:50 -05:00
|
|
|
filepath.Base(path))
|
|
|
|
|
|
|
|
if _, err := os.Stat(temp); err == nil {
|
|
|
|
cmdPath = temp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still haven't found the executable, look for it
|
|
|
|
// in the PATH.
|
|
|
|
if v, err := exec.LookPath(path); err == nil {
|
|
|
|
cmdPath = v
|
2014-07-24 09:29:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:11:11 -05:00
|
|
|
// No plugin binary found, so try to use an internal plugin.
|
|
|
|
if strings.Contains(path, command.TFSPACE) {
|
|
|
|
parts := strings.Split(path, command.TFSPACE)
|
|
|
|
return exec.Command(parts[0], parts[1:]...)
|
|
|
|
}
|
|
|
|
|
2014-08-27 22:46:50 -05:00
|
|
|
// If we still don't have a path, then just set it to the original
|
|
|
|
// given path.
|
|
|
|
if cmdPath == "" {
|
|
|
|
cmdPath = path
|
2014-07-24 09:29:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the command to execute the plugin
|
2014-08-27 22:46:50 -05:00
|
|
|
return exec.Command(cmdPath)
|
2014-07-24 09:29:53 -05:00
|
|
|
}
|