2023-03-30 10:01:09 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-03-30 10:01:09 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/apierr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statePull struct{}
|
|
|
|
|
|
|
|
func (l *statePull) Name() string {
|
|
|
|
return "terraform:state-pull"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *statePull) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
2023-04-12 14:54:36 +00:00
|
|
|
f, err := filer.NewWorkspaceFilesClient(b.WorkspaceClient(), b.Config.Workspace.StatePath)
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := Dir(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download state file from filer to local cache directory.
|
|
|
|
log.Infof(ctx, "Opening remote state file")
|
|
|
|
remote, err := f.Read(ctx, TerraformStateFileName)
|
|
|
|
if err != nil {
|
|
|
|
// On first deploy this state file doesn't yet exist.
|
|
|
|
if apierr.IsMissing(err) {
|
|
|
|
log.Infof(ctx, "Remote state file does not exist")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect the state file to live under dir.
|
2023-05-16 15:02:33 +00:00
|
|
|
local, err := os.OpenFile(filepath.Join(dir, TerraformStateFileName), os.O_CREATE|os.O_RDWR, 0600)
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-16 15:02:33 +00:00
|
|
|
defer local.Close()
|
|
|
|
|
|
|
|
if !IsLocalStateStale(local, remote) {
|
|
|
|
log.Infof(ctx, "Local state is the same or newer, ignoring remote state")
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncating the file before writing
|
|
|
|
local.Truncate(0)
|
|
|
|
local.Seek(0, 0)
|
2023-03-30 10:01:09 +00:00
|
|
|
|
|
|
|
// Write file to disk.
|
|
|
|
log.Infof(ctx, "Writing remote state file to local cache directory")
|
|
|
|
_, err = io.Copy(local, remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatePull() bundle.Mutator {
|
|
|
|
return &statePull{}
|
|
|
|
}
|