command: Make provider installation interruptible

In earlier commits we started to make the installation codepath
context-aware so that it could be canceled in the event of a SIGINT, but
we didn't complete wiring that through the API of the getproviders
package.

Here we make the getproviders.Source interface methods, along with some
other functions that can make network requests, take a context.Context
argument and act appropriately if that context is cancelled.

The main providercache.Installer.EnsureProviderVersions method now also
has some context-awareness so that it can abort its work early if its
context reports any sort of error. That avoids waiting for the process
to wind through all of the remaining iterations of the various loops,
logging each request failure separately, and instead returns just
a single aggregate "canceled" error.

We can then set things up in the "terraform init" and
"terraform providers mirror" commands so that the context will be
cancelled if we get an interrupt signal, allowing provider installation
to abort early while still atomically completing any local-side effects
that may have started.
This commit is contained in:
Martin Atkins
2020-09-28 17:13:32 -07:00
parent f0ccee854c
commit 0b734a2803
23 changed files with 258 additions and 93 deletions

View File

@@ -1,6 +1,7 @@
package getproviders
import (
"context"
"fmt"
svchost "github.com/hashicorp/terraform-svchost"
@@ -32,13 +33,13 @@ func NewRegistrySource(services *disco.Disco) *RegistrySource {
// ErrHostNoProviders, ErrHostUnreachable, ErrUnauthenticated,
// ErrProviderNotKnown, or ErrQueryFailed. Callers must be defensive and
// expect errors of other types too, to allow for future expansion.
func (s *RegistrySource) AvailableVersions(provider addrs.Provider) (VersionList, Warnings, error) {
func (s *RegistrySource) AvailableVersions(ctx context.Context, provider addrs.Provider) (VersionList, Warnings, error) {
client, err := s.registryClient(provider.Hostname)
if err != nil {
return nil, nil, err
}
versionsResponse, warnings, err := client.ProviderVersions(provider)
versionsResponse, warnings, err := client.ProviderVersions(ctx, provider)
if err != nil {
return nil, nil, err
}
@@ -94,13 +95,13 @@ func (s *RegistrySource) AvailableVersions(provider addrs.Provider) (VersionList
// ErrHostNoProviders, ErrHostUnreachable, ErrUnauthenticated,
// ErrPlatformNotSupported, or ErrQueryFailed. Callers must be defensive and
// expect errors of other types too, to allow for future expansion.
func (s *RegistrySource) PackageMeta(provider addrs.Provider, version Version, target Platform) (PackageMeta, error) {
func (s *RegistrySource) PackageMeta(ctx context.Context, provider addrs.Provider, version Version, target Platform) (PackageMeta, error) {
client, err := s.registryClient(provider.Hostname)
if err != nil {
return PackageMeta{}, err
}
return client.PackageMeta(provider, version, target)
return client.PackageMeta(ctx, provider, version, target)
}
// LookupLegacyProviderNamespace is a special method available only on
@@ -118,12 +119,12 @@ func (s *RegistrySource) PackageMeta(provider addrs.Provider, version Version, t
// in older configurations. New configurations should be written so as not to
// depend on it, and this fallback mechanism will likely be removed altogether
// in a future Terraform version.
func (s *RegistrySource) LookupLegacyProviderNamespace(hostname svchost.Hostname, typeName string) (string, string, error) {
func (s *RegistrySource) LookupLegacyProviderNamespace(ctx context.Context, hostname svchost.Hostname, typeName string) (string, string, error) {
client, err := s.registryClient(hostname)
if err != nil {
return "", "", err
}
return client.LegacyProviderDefaultNamespace(typeName)
return client.LegacyProviderDefaultNamespace(ctx, typeName)
}
func (s *RegistrySource) registryClient(hostname svchost.Hostname) (*registryClient, error) {