-
Notifications
You must be signed in to change notification settings - Fork 154
Add bundle debug terraform command
#1294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c3ca75d
e8ec958
aff7024
9135426
d99a003
8a3b0b7
c157a5c
f6142ce
91924ed
55c7808
1e549a4
70334a5
20ff8e1
58bbd71
c5df7af
51175c9
7e42269
92e5cef
5f4ddb4
9e84685
e10464f
cd31b66
d38d946
8884a41
d99642f
7413253
138a9e1
d921a82
d157a2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,10 @@ import ( | |
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/internal/tf/schema" | ||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/cli/libs/env" | ||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/hashicorp/go-version" | ||
| "github.com/hashicorp/hc-install/product" | ||
| "github.com/hashicorp/hc-install/releases" | ||
| "github.com/hashicorp/terraform-exec/tfexec" | ||
|
|
@@ -40,6 +40,17 @@ func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *con | |
| return tf.ExecPath, nil | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
|
|
||
| binDir, err := b.CacheDir(context.Background(), "bin") | ||
| if err != nil { | ||
| return "", err | ||
|
|
@@ -60,7 +71,7 @@ func (m *initialize) findExecPath(ctx context.Context, b *bundle.Bundle, tf *con | |
| // Download Terraform to private bin directory. | ||
| installer := &releases.ExactVersion{ | ||
| Product: product.Terraform, | ||
| Version: version.Must(version.NewVersion("1.5.5")), | ||
| Version: TerraformVersion, | ||
| InstallDir: binDir, | ||
| Timeout: 1 * time.Minute, | ||
| } | ||
|
|
@@ -98,14 +109,55 @@ func inheritEnvVars(ctx context.Context, environ map[string]string) error { | |
| } | ||
|
|
||
| // Include $TF_CLI_CONFIG_FILE to override terraform provider in development. | ||
| configFile, ok := env.Lookup(ctx, "TF_CLI_CONFIG_FILE") | ||
| // See: https://developer.hashicorp.com/terraform/cli/config/config-file#explicit-installation-method-configuration | ||
| devConfigFile, ok := env.Lookup(ctx, "TF_CLI_CONFIG_FILE") | ||
| if ok { | ||
| 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) | ||
| environ["TF_CLI_CONFIG_FILE"] = configFile | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // 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) { | ||
ilia-db marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| envValue := env.Get(ctx, envVarName) | ||
| versionValue := env.Get(ctx, versionVarName) | ||
| if envValue == "" || versionValue == "" { | ||
| log.Debugf(ctx, "%s and %s aren't defined", envVarName, versionVarName) | ||
| return "", nil | ||
| } | ||
| if versionValue != currentVersion { | ||
| log.Debugf(ctx, "%s as %s does not match the current version %s, ignoring %s", versionVarName, versionValue, currentVersion, envVarName) | ||
| return "", nil | ||
| } | ||
| _, err := os.Stat(envValue) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| log.Debugf(ctx, "%s at %s does not exist, ignoring %s", envVarName, envValue, versionVarName) | ||
| return "", nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not a hard error? If someone sets these env vars, they intend to use them. Mistakes are ignored.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Environment varialbes in the terminals can have a long timespan, and we can't change them after the initial setup. One edge case is when users uninstall the extension. The variables will stick in the persistent terminal sessions utill users re-create terminals. Since we don't copy the dependencies and point env vars to the internals of the installed extensions, after the uninstall the variables will point to the place that no longer exist. We can copy dependencies to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this tie in with Databricks Terminals? If these are only set in those then we don't have to bother with long lifetimes of env vars in terminals.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For my knowledge, updating the extensions requires reloading the IDE, and then reloading the IDE should refresh the env vars right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reloading VSCode doesn't restart terminals, they are persistent (at least by default), and so the env vars also persist. We've not yet made a decision about "Databricks Terminal". We can change the logic here afterwards though |
||
| } else { | ||
| return "", err | ||
| } | ||
| } | ||
| return envValue, nil | ||
| } | ||
|
|
||
| // 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,34 @@ | ||
| package terraform | ||
|
|
||
| import ( | ||
| "github.com/databricks/cli/bundle/internal/tf/schema" | ||
| "github.com/hashicorp/go-version" | ||
| ) | ||
|
|
||
| const TerraformStateFileName = "terraform.tfstate" | ||
| const TerraformConfigFileName = "bundle.tf.json" | ||
|
|
||
| // Users can provide their own terraform binary and databricks terraform provider by setting the following environment variables. | ||
| // This allows users to use the CLI in an air-gapped environments. See the `debug terraform` command. | ||
| const TerraformExecPathEnv = "DATABRICKS_TF_EXEC_PATH" | ||
| const TerraformVersionEnv = "DATABRICKS_TF_VERSION" | ||
| const TerraformCliConfigPathEnv = "DATABRICKS_TF_CLI_CONFIG_FILE" | ||
| const TerraformProviderVersionEnv = "DATABRICKS_TF_PROVIDER_VERSION" | ||
|
|
||
| var TerraformVersion = version.Must(version.NewVersion("1.5.5")) | ||
|
|
||
| type TerraformMetadata struct { | ||
| Version string `json:"version"` | ||
| ProviderHost string `json:"providerHost"` | ||
| ProviderSource string `json:"providerSource"` | ||
| ProviderVersion string `json:"providerVersion"` | ||
| } | ||
|
|
||
| func NewTerraformMetadata() *TerraformMetadata { | ||
| return &TerraformMetadata{ | ||
| Version: TerraformVersion.String(), | ||
| ProviderHost: schema.ProviderHost, | ||
| ProviderSource: schema.ProviderSource, | ||
| ProviderVersion: schema.ProviderVersion, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package bundle | ||
|
|
||
| import ( | ||
| "github.com/databricks/cli/cmd/bundle/debug" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newDebugCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "debug", | ||
| Short: "Debug information about bundles", | ||
| Long: "Debug information about bundles", | ||
| // This command group is currently intended for the Databricks VSCode extension only | ||
| Hidden: true, | ||
| } | ||
| cmd.AddCommand(debug.NewTerraformCommand()) | ||
| return cmd | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.