2022-12-15 14:12:47 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-03 12:39:36 +00:00
|
|
|
"errors"
|
2022-12-15 16:30:33 +00:00
|
|
|
"fmt"
|
2024-06-03 12:39:36 +00:00
|
|
|
"io/fs"
|
2022-12-15 16:30:33 +00:00
|
|
|
"os"
|
2022-12-15 14:12:47 +00:00
|
|
|
"os/exec"
|
2022-12-15 16:30:33 +00:00
|
|
|
"path/filepath"
|
2023-05-23 11:51:15 +00:00
|
|
|
"runtime"
|
2023-03-29 18:46:09 +00:00
|
|
|
"strings"
|
2023-09-07 14:08:16 +00:00
|
|
|
"time"
|
2022-12-15 14:12:47 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2024-04-02 12:56:27 +00:00
|
|
|
"github.com/databricks/cli/bundle/internal/tf/schema"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2022-12-15 16:30:33 +00:00
|
|
|
"github.com/hashicorp/hc-install/product"
|
|
|
|
"github.com/hashicorp/hc-install/releases"
|
2022-12-15 14:12:47 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
2023-03-29 18:46:09 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2022-12-15 14:12:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type initialize struct{}
|
|
|
|
|
|
|
|
func (m *initialize) Name() string {
|
|
|
|
return "terraform.Initialize"
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:30:33 +00:00
|
|
|
func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *config.Terraform) (string, error) {
|
|
|
|
// If set, pass it through [exec.LookPath] to resolve its absolute path.
|
|
|
|
if tf.ExecPath != "" {
|
|
|
|
execPath, err := exec.LookPath(tf.ExecPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
tf.ExecPath = execPath
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "Using Terraform at %s", tf.ExecPath)
|
2022-12-15 16:30:33 +00:00
|
|
|
return tf.ExecPath, nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:56:27 +00:00
|
|
|
// Load exec path from the environment if it matches the currently used version.
|
|
|
|
envExecPath, err := getEnvVarWithMatchingVersion(ctx, TerraformExecPathEnv, TerraformVersionEnv, TerraformVersion.String())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if envExecPath != "" {
|
|
|
|
tf.ExecPath = envExecPath
|
|
|
|
log.Debugf(ctx, "Using Terraform from %s at %s", TerraformExecPathEnv, tf.ExecPath)
|
|
|
|
return tf.ExecPath, nil
|
|
|
|
}
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
binDir, err := b.CacheDir(context.Background(), "bin")
|
2022-12-15 16:30:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the execPath already exists, return it.
|
|
|
|
execPath := filepath.Join(binDir, product.Terraform.BinaryName())
|
|
|
|
_, err = os.Stat(execPath)
|
2024-06-03 12:39:36 +00:00
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2022-12-15 16:30:33 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
tf.ExecPath = execPath
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "Using Terraform at %s", tf.ExecPath)
|
2022-12-15 16:30:33 +00:00
|
|
|
return tf.ExecPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download Terraform to private bin directory.
|
2023-08-30 14:08:37 +00:00
|
|
|
installer := &releases.ExactVersion{
|
|
|
|
Product: product.Terraform,
|
2024-04-02 12:56:27 +00:00
|
|
|
Version: TerraformVersion,
|
2023-08-30 14:08:37 +00:00
|
|
|
InstallDir: binDir,
|
2023-09-07 14:08:16 +00:00
|
|
|
Timeout: 1 * time.Minute,
|
2022-12-15 16:30:33 +00:00
|
|
|
}
|
|
|
|
execPath, err = installer.Install(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error downloading Terraform: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tf.ExecPath = execPath
|
2023-03-17 14:17:31 +00:00
|
|
|
log.Debugf(ctx, "Using Terraform at %s", tf.ExecPath)
|
2022-12-15 16:30:33 +00:00
|
|
|
return tf.ExecPath, nil
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:20:37 +00:00
|
|
|
// This function inherits some environment variables for Terraform CLI.
|
2023-09-11 08:18:43 +00:00
|
|
|
func inheritEnvVars(ctx context.Context, environ map[string]string) error {
|
2023-07-07 11:20:37 +00:00
|
|
|
// Include $HOME in set of environment variables to pass along.
|
2023-09-11 08:18:43 +00:00
|
|
|
home, ok := env.Lookup(ctx, "HOME")
|
2023-07-07 11:20:37 +00:00
|
|
|
if ok {
|
2023-09-11 08:18:43 +00:00
|
|
|
environ["HOME"] = home
|
2023-07-07 11:20:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 09:16:28 +00:00
|
|
|
// Include $USERPROFILE in set of environment variables to pass along.
|
|
|
|
// This variable is used by Azure CLI on Windows to find stored credentials and metadata
|
|
|
|
userProfile, ok := env.Lookup(ctx, "USERPROFILE")
|
|
|
|
if ok {
|
|
|
|
environ["USERPROFILE"] = userProfile
|
|
|
|
}
|
|
|
|
|
2023-09-06 07:54:35 +00:00
|
|
|
// Include $PATH in set of environment variables to pass along.
|
|
|
|
// This is necessary to ensure that our Terraform provider can use the
|
|
|
|
// same auxiliary programs (e.g. `az`, or `gcloud`) as the CLI.
|
2023-09-11 08:18:43 +00:00
|
|
|
path, ok := env.Lookup(ctx, "PATH")
|
2023-09-06 07:54:35 +00:00
|
|
|
if ok {
|
2023-09-11 08:18:43 +00:00
|
|
|
environ["PATH"] = path
|
2023-09-06 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:20:37 +00:00
|
|
|
// Include $TF_CLI_CONFIG_FILE to override terraform provider in development.
|
2024-04-02 12:56:27 +00:00
|
|
|
// See: https://developer.hashicorp.com/terraform/cli/config/config-file#explicit-installation-method-configuration
|
|
|
|
devConfigFile, ok := env.Lookup(ctx, "TF_CLI_CONFIG_FILE")
|
2023-07-07 11:20:37 +00:00
|
|
|
if ok {
|
2024-04-02 12:56:27 +00:00
|
|
|
environ["TF_CLI_CONFIG_FILE"] = devConfigFile
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map $DATABRICKS_TF_CLI_CONFIG_FILE to $TF_CLI_CONFIG_FILE
|
|
|
|
// VSCode extension provides a file with the "provider_installation.filesystem_mirror" configuration.
|
|
|
|
// We only use it if the provider version matches the currently used version,
|
|
|
|
// otherwise terraform will fail to download the right version (even with unrestricted internet access).
|
|
|
|
configFile, err := getEnvVarWithMatchingVersion(ctx, TerraformCliConfigPathEnv, TerraformProviderVersionEnv, schema.ProviderVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if configFile != "" {
|
|
|
|
log.Debugf(ctx, "Using Terraform CLI config from %s at %s", TerraformCliConfigPathEnv, configFile)
|
2023-09-11 08:18:43 +00:00
|
|
|
environ["TF_CLI_CONFIG_FILE"] = configFile
|
2023-07-07 11:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:56:27 +00:00
|
|
|
// Example: this function will return a value of TF_EXEC_PATH only if the path exists and if TF_VERSION matches the TerraformVersion.
|
|
|
|
// This function is used for env vars set by the Databricks VSCode extension. The variables are intended to be used by the CLI
|
|
|
|
// bundled with the Databricks VSCode extension, but users can use different CLI versions in the VSCode terminals, in which case we want to ignore
|
|
|
|
// the variables if that CLI uses different versions of the dependencies.
|
|
|
|
func getEnvVarWithMatchingVersion(ctx context.Context, envVarName string, versionVarName string, currentVersion string) (string, error) {
|
|
|
|
envValue := env.Get(ctx, envVarName)
|
|
|
|
versionValue := env.Get(ctx, versionVarName)
|
2024-04-12 15:22:30 +00:00
|
|
|
|
|
|
|
// return early if the environment variable is not set
|
|
|
|
if envValue == "" {
|
|
|
|
log.Debugf(ctx, "%s is not defined", envVarName)
|
2024-04-02 12:56:27 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2024-04-12 15:22:30 +00:00
|
|
|
|
|
|
|
// If the path does not exist, we return early.
|
2024-04-02 12:56:27 +00:00
|
|
|
_, err := os.Stat(envValue)
|
|
|
|
if err != nil {
|
2024-06-03 12:39:36 +00:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2024-04-12 15:22:30 +00:00
|
|
|
log.Debugf(ctx, "%s at %s does not exist", envVarName, envValue)
|
2024-04-02 12:56:27 +00:00
|
|
|
return "", nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2024-04-12 15:22:30 +00:00
|
|
|
|
|
|
|
// If the version environment variable is not set, we directly return the value of the environment variable.
|
|
|
|
if versionValue == "" {
|
|
|
|
return envValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the version environment variable is set, we check if it matches the current version.
|
|
|
|
// If it does not match, we return an empty string.
|
|
|
|
if versionValue != currentVersion {
|
|
|
|
log.Debugf(ctx, "%s as %s does not match the current version %s, ignoring %s", versionVarName, versionValue, currentVersion, envVarName)
|
|
|
|
return "", nil
|
|
|
|
}
|
2024-04-02 12:56:27 +00:00
|
|
|
return envValue, nil
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:51:15 +00:00
|
|
|
// This function sets temp dir location for terraform to use. If user does not
|
|
|
|
// specify anything here, we fall back to a `tmp` directory in the bundle's cache
|
|
|
|
// directory
|
|
|
|
//
|
|
|
|
// This is necessary to avoid trying to create temporary files in directories
|
|
|
|
// the CLI and its dependencies do not have access to.
|
|
|
|
//
|
|
|
|
// see: os.TempDir for more context
|
2023-09-11 08:18:43 +00:00
|
|
|
func setTempDirEnvVars(ctx context.Context, environ map[string]string, b *bundle.Bundle) error {
|
2023-05-23 11:51:15 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
2023-09-11 08:18:43 +00:00
|
|
|
if v, ok := env.Lookup(ctx, "TMP"); ok {
|
|
|
|
environ["TMP"] = v
|
|
|
|
} else if v, ok := env.Lookup(ctx, "TEMP"); ok {
|
|
|
|
environ["TEMP"] = v
|
2023-05-23 11:51:15 +00:00
|
|
|
} else {
|
2023-09-11 08:18:43 +00:00
|
|
|
tmpDir, err := b.CacheDir(ctx, "tmp")
|
2023-05-23 11:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 08:18:43 +00:00
|
|
|
environ["TMP"] = tmpDir
|
2023-05-23 11:51:15 +00:00
|
|
|
}
|
|
|
|
default:
|
2023-05-26 11:05:30 +00:00
|
|
|
// If TMPDIR is not set, we let the process fall back to its default value.
|
2023-09-11 08:18:43 +00:00
|
|
|
if v, ok := env.Lookup(ctx, "TMPDIR"); ok {
|
|
|
|
environ["TMPDIR"] = v
|
2023-05-23 11:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 19:58:26 +00:00
|
|
|
// This function passes through all proxy related environment variables.
|
2023-09-11 08:18:43 +00:00
|
|
|
func setProxyEnvVars(ctx context.Context, environ map[string]string, b *bundle.Bundle) error {
|
2023-06-14 19:58:26 +00:00
|
|
|
for _, v := range []string{"http_proxy", "https_proxy", "no_proxy"} {
|
|
|
|
// The case (upper or lower) is notoriously inconsistent for tools on Unix systems.
|
|
|
|
// We therefore try to read both the upper and lower case versions of the variable.
|
|
|
|
for _, v := range []string{strings.ToUpper(v), strings.ToLower(v)} {
|
2023-09-11 08:18:43 +00:00
|
|
|
if val, ok := env.Lookup(ctx, v); ok {
|
2023-06-14 19:58:26 +00:00
|
|
|
// Only set uppercase version of the variable.
|
2023-09-11 08:18:43 +00:00
|
|
|
environ[strings.ToUpper(v)] = val
|
2023-06-14 19:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-01 07:46:37 +00:00
|
|
|
func setUserAgentExtraEnvVar(environ map[string]string, b *bundle.Bundle) error {
|
|
|
|
var products []string
|
|
|
|
|
|
|
|
if experimental := b.Config.Experimental; experimental != nil {
|
|
|
|
if experimental.PyDABs.Enabled {
|
|
|
|
products = append(products, "databricks-pydabs/0.0.0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userAgentExtra := strings.Join(products, " ")
|
|
|
|
if userAgentExtra != "" {
|
|
|
|
environ["DATABRICKS_USER_AGENT_EXTRA"] = userAgentExtra
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2022-12-15 16:30:33 +00:00
|
|
|
tfConfig := b.Config.Bundle.Terraform
|
|
|
|
if tfConfig == nil {
|
|
|
|
tfConfig = &config.Terraform{}
|
|
|
|
b.Config.Bundle.Terraform = tfConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
execPath, err := m.findExecPath(ctx, b, tfConfig)
|
2022-12-15 14:12:47 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
workingDir, err := Dir(ctx, b)
|
2022-12-15 14:12:47 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := tfexec.NewTerraform(workingDir, execPath)
|
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
environ, err := b.AuthEnv()
|
2023-03-29 18:46:09 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-03-29 18:46:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 08:18:43 +00:00
|
|
|
err = inheritEnvVars(ctx, environ)
|
2023-07-07 11:20:37 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-04-11 11:11:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 11:51:15 +00:00
|
|
|
// Set the temporary directory environment variables
|
2023-09-11 08:18:43 +00:00
|
|
|
err = setTempDirEnvVars(ctx, environ, b)
|
2023-05-23 11:51:15 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-05-23 11:51:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 19:58:26 +00:00
|
|
|
// Set the proxy related environment variables
|
2023-09-11 08:18:43 +00:00
|
|
|
err = setProxyEnvVars(ctx, environ, b)
|
2023-06-14 19:58:26 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-06-14 19:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 07:46:37 +00:00
|
|
|
err = setUserAgentExtraEnvVar(environ, b)
|
|
|
|
if err != nil {
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:46:09 +00:00
|
|
|
// Configure environment variables for auth for Terraform to use.
|
2023-09-11 08:18:43 +00:00
|
|
|
log.Debugf(ctx, "Environment variables for Terraform: %s", strings.Join(maps.Keys(environ), ", "))
|
|
|
|
err = tf.SetEnv(environ)
|
2023-03-29 18:46:09 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-03-29 18:46:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
b.Terraform = tf
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Initialize() bundle.Mutator {
|
|
|
|
return &initialize{}
|
|
|
|
}
|