2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-09-22 12:56:50 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
command: Start of propagating OpenTelemetry context
Several times over the years we've considered adding tracing
instrumentation to Terraform, since even when running in isolation as a
CLI program it has a "distributed system-like" structure, with lots of
concurrent internal work and also some work delegated to provider plugins
that are essentially temporarily-running microservices.
However, it's always felt a bit overwhelming to do it because much of
Terraform predates the Go context.Context idiom and so it's tough to get
a clean chain of context.Context values all the way down the stack without
disturbing a lot of existing APIs.
This commit aims to just get that process started by establishing how a
context can propagate from "package main" into the command package,
focusing initially on "terraform init" and some other commands that share
some underlying functions with that command.
OpenTelemetry has emerged as a de-facto industry standard and so this uses
its API directly, without any attempt to hide it behind an abstraction.
The OpenTelemetry API is itself already an adapter layer, so we should be
able to swap in any backend that uses comparable concepts. For now we just
discard the tracing reports by default, and allow users to opt in to
delivering traces over OTLP by setting an environment variable when
running Terraform (the environment variable was established in an earlier
commit, so this commit builds on that.)
When tracing collection is enabled, every Terraform CLI run will generate
at least one overall span representing the command that was run. Some
commands might also create child spans, but most currently do not.
2023-07-10 13:29:57 -05:00
|
|
|
"context"
|
2019-08-16 07:31:21 -05:00
|
|
|
"fmt"
|
2014-09-22 12:56:50 -05:00
|
|
|
"strings"
|
2014-09-22 13:15:27 -05:00
|
|
|
|
2021-05-17 12:11:06 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2014-09-22 12:56:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetCommand is a Command implementation that takes a Terraform
|
|
|
|
// configuration and downloads all the modules.
|
|
|
|
type GetCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Run(args []string) int {
|
2014-09-22 13:18:49 -05:00
|
|
|
var update bool
|
2023-07-19 03:07:46 -05:00
|
|
|
var testsDirectory string
|
2014-09-22 13:18:49 -05:00
|
|
|
|
2020-04-01 14:01:08 -05:00
|
|
|
args = c.Meta.process(args)
|
2018-11-21 08:35:27 -06:00
|
|
|
cmdFlags := c.Meta.defaultFlagSet("get")
|
2014-09-22 13:18:49 -05:00
|
|
|
cmdFlags.BoolVar(&update, "update", false, "update")
|
2023-07-19 03:07:46 -05:00
|
|
|
cmdFlags.StringVar(&testsDirectory, "test-directory", "tests", "test-directory")
|
2014-09-22 12:56:50 -05:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
2019-08-16 07:31:21 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
|
2014-09-22 12:56:50 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
command: Start of propagating OpenTelemetry context
Several times over the years we've considered adding tracing
instrumentation to Terraform, since even when running in isolation as a
CLI program it has a "distributed system-like" structure, with lots of
concurrent internal work and also some work delegated to provider plugins
that are essentially temporarily-running microservices.
However, it's always felt a bit overwhelming to do it because much of
Terraform predates the Go context.Context idiom and so it's tough to get
a clean chain of context.Context values all the way down the stack without
disturbing a lot of existing APIs.
This commit aims to just get that process started by establishing how a
context can propagate from "package main" into the command package,
focusing initially on "terraform init" and some other commands that share
some underlying functions with that command.
OpenTelemetry has emerged as a de-facto industry standard and so this uses
its API directly, without any attempt to hide it behind an abstraction.
The OpenTelemetry API is itself already an adapter layer, so we should be
able to swap in any backend that uses comparable concepts. For now we just
discard the tracing reports by default, and allow users to opt in to
delivering traces over OTLP by setting an environment variable when
running Terraform (the environment variable was established in an earlier
commit, so this commit builds on that.)
When tracing collection is enabled, every Terraform CLI run will generate
at least one overall span representing the command that was run. Some
commands might also create child spans, but most currently do not.
2023-07-10 13:29:57 -05:00
|
|
|
// Initialization can be aborted by interruption signals
|
|
|
|
ctx, done := c.InterruptibleContext(c.CommandContext())
|
|
|
|
defer done()
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
path, err := ModulePath(cmdFlags.Args())
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2014-09-22 12:56:50 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-01-17 17:56:53 -06:00
|
|
|
path = c.normalizePath(path)
|
2014-09-22 13:18:49 -05:00
|
|
|
|
2023-07-19 03:07:46 -05:00
|
|
|
abort, diags := getModules(ctx, &c.Meta, path, testsDirectory, update)
|
2019-01-17 17:56:53 -06:00
|
|
|
c.showDiagnostics(diags)
|
2021-11-01 15:09:16 -05:00
|
|
|
if abort || diags.HasErrors() {
|
2014-09-22 12:56:50 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Help() string {
|
|
|
|
helpText := `
|
2023-06-26 15:42:22 -05:00
|
|
|
Usage: terraform [global options] get [options]
|
2014-09-22 12:56:50 -05:00
|
|
|
|
2023-06-26 15:42:22 -05:00
|
|
|
Downloads and installs modules needed for the configuration in the
|
|
|
|
current working directory.
|
2014-09-22 12:56:50 -05:00
|
|
|
|
|
|
|
This recursively downloads all modules needed, such as modules
|
|
|
|
imported by modules imported by the root and so on. If a module is
|
|
|
|
already downloaded, it will not be redownloaded or checked for updates
|
|
|
|
unless the -update flag is specified.
|
|
|
|
|
2020-10-23 18:55:32 -05:00
|
|
|
Module installation also happens automatically by default as part of
|
|
|
|
the "terraform init" command, so you should rarely need to run this
|
|
|
|
command separately.
|
|
|
|
|
2014-09-22 12:56:50 -05:00
|
|
|
Options:
|
|
|
|
|
2023-07-19 03:07:46 -05:00
|
|
|
-update Check already-downloaded modules for available updates
|
|
|
|
and install the newest versions available.
|
2014-09-22 12:56:50 -05:00
|
|
|
|
2023-07-19 03:07:46 -05:00
|
|
|
-no-color Disable text coloring in the output.
|
|
|
|
|
|
|
|
-test-directory=path Set the Terraform test directory, defaults to "tests".
|
2015-06-22 07:14:01 -05:00
|
|
|
|
2014-09-22 12:56:50 -05:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GetCommand) Synopsis() string {
|
2020-10-23 18:55:32 -05:00
|
|
|
return "Install or upgrade remote Terraform modules"
|
2014-09-22 12:56:50 -05:00
|
|
|
}
|
2017-01-18 22:50:45 -06:00
|
|
|
|
2023-07-19 03:07:46 -05:00
|
|
|
func getModules(ctx context.Context, m *Meta, path string, testsDir string, upgrade bool) (abort bool, diags tfdiags.Diagnostics) {
|
2019-01-17 17:56:53 -06:00
|
|
|
hooks := uiModuleInstallHooks{
|
|
|
|
Ui: m.Ui,
|
|
|
|
ShowLocalPaths: true,
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|
2023-07-19 03:07:46 -05:00
|
|
|
return m.installModules(ctx, path, testsDir, upgrade, hooks)
|
2017-01-18 22:50:45 -06:00
|
|
|
}
|