Skip to content

Commit

Permalink
Merge pull request #34632 from hashicorp/use-plugin-cache-for-provide…
Browse files Browse the repository at this point in the history
…r-lock

improve provider lock speed by using cache
  • Loading branch information
DanielMSchmidt authored Feb 9, 2024
2 parents 4c0ebff + 91ea23a commit 9fbe6c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
77 changes: 45 additions & 32 deletions internal/command/providers_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ func (c *ProvidersLockCommand) Run(args []string) int {
var optPlatforms FlagStringSlice
var fsMirrorDir string
var netMirrorURL string

cmdFlags.Var(&optPlatforms, "platform", "target platform")
cmdFlags.StringVar(&fsMirrorDir, "fs-mirror", "", "filesystem mirror directory")
cmdFlags.StringVar(&netMirrorURL, "net-mirror", "", "network mirror base URL")
pluginCache := cmdFlags.Bool("enable-plugin-cache", false, "")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
Expand Down Expand Up @@ -246,6 +248,13 @@ func (c *ProvidersLockCommand) Run(args []string) int {
dir := providercache.NewDirWithPlatform(tempDir, platform)
installer := providercache.NewInstaller(dir, source)

// Use global plugin cache for extra speed if present and flag is set
globalCacheDir := c.providerGlobalCacheDir()
if *pluginCache && globalCacheDir != nil {
installer.SetGlobalCacheDir(globalCacheDir.WithPlatform(platform))
installer.SetGlobalCacheDirMayBreakDependencyLockFile(c.PluginCacheMayBreakDependencyLockFile)
}

newLocks, err := installer.EnsureProviderVersions(ctx, oldLocks, reqs, providercache.InstallNewProvidersForce)
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
Expand Down Expand Up @@ -370,38 +379,42 @@ Usage: terraform [global options] providers lock [options] [providers...]
Options:
-fs-mirror=dir Consult the given filesystem mirror directory instead
of the origin registry for each of the given providers.
This would be necessary to generate lock file entries for
a provider that is available only via a mirror, and not
published in an upstream registry. In this case, the set
of valid checksums will be limited only to what Terraform
can learn from the data in the mirror directory.
-net-mirror=url Consult the given network mirror (given as a base URL)
instead of the origin registry for each of the given
providers.
This would be necessary to generate lock file entries for
a provider that is available only via a mirror, and not
published in an upstream registry. In this case, the set
of valid checksums will be limited only to what Terraform
can learn from the data in the mirror indices.
-platform=os_arch Choose a target platform to request package checksums
for.
By default Terraform will request package checksums
suitable only for the platform where you run this
command. Use this option multiple times to include
checksums for multiple target systems.
Target names consist of an operating system and a CPU
architecture. For example, "linux_amd64" selects the
Linux operating system running on an AMD64 or x86_64
CPU. Each provider is available only for a limited
set of target platforms.
-fs-mirror=dir Consult the given filesystem mirror directory instead
of the origin registry for each of the given providers.
This would be necessary to generate lock file entries for
a provider that is available only via a mirror, and not
published in an upstream registry. In this case, the set
of valid checksums will be limited only to what Terraform
can learn from the data in the mirror directory.
-net-mirror=url Consult the given network mirror (given as a base URL)
instead of the origin registry for each of the given
providers.
This would be necessary to generate lock file entries for
a provider that is available only via a mirror, and not
published in an upstream registry. In this case, the set
of valid checksums will be limited only to what Terraform
can learn from the data in the mirror indices.
-platform=os_arch Choose a target platform to request package checksums
for.
By default Terraform will request package checksums
suitable only for the platform where you run this
command. Use this option multiple times to include
checksums for multiple target systems.
Target names consist of an operating system and a CPU
architecture. For example, "linux_amd64" selects the
Linux operating system running on an AMD64 or x86_64
CPU. Each provider is available only for a limited
set of target platforms.
-enable-plugin-cache Enable the usage of the globally configured plugin cache.
This will speed up the locking process, but the providers
wont be loaded from an authoritative source.
`
}

Expand Down
6 changes: 6 additions & 0 deletions internal/providercache/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (d *Dir) BasePath() string {
return filepath.Clean(d.baseDir)
}

// WithPlatform creates a new dir with the provided platform based
// on this dir
func (d *Dir) WithPlatform(platform getproviders.Platform) *Dir {
return NewDirWithPlatform(d.baseDir, platform)
}

// AllAvailablePackages returns a description of all of the packages already
// present in the directory. The cache entries are grouped by the provider
// they relate to and then sorted by version precedence, with highest
Expand Down

0 comments on commit 9fbe6c6

Please sign in to comment.