2023-03-30 10:01:09 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-18 09:47:59 +00:00
|
|
|
"encoding/json"
|
2023-05-31 18:47:00 +00:00
|
|
|
"errors"
|
2023-03-30 10:01:09 +00:00
|
|
|
"io"
|
2023-05-31 18:47:00 +00:00
|
|
|
"io/fs"
|
2023-03-30 10:01:09 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/databricks/cli/bundle/deploy"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-03-30 10:01:09 +00:00
|
|
|
)
|
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
type tfState struct {
|
|
|
|
Serial int64 `json:"serial"`
|
|
|
|
Lineage string `json:"lineage"`
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:15:46 +00:00
|
|
|
type statePull struct {
|
2024-03-18 14:41:58 +00:00
|
|
|
filerFactory deploy.FilerFactory
|
2023-11-24 11:15:46 +00:00
|
|
|
}
|
2023-03-30 10:01:09 +00:00
|
|
|
|
|
|
|
func (l *statePull) Name() string {
|
|
|
|
return "terraform:state-pull"
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
func (l *statePull) remoteState(ctx context.Context, b *bundle.Bundle) (*tfState, []byte, error) {
|
|
|
|
f, err := l.filerFactory(b)
|
2023-11-24 11:15:46 +00:00
|
|
|
if err != nil {
|
2024-07-18 09:47:59 +00:00
|
|
|
return nil, nil, err
|
2023-11-24 11:15:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
r, err := f.Read(ctx, TerraformStateFileName)
|
2023-11-24 11:15:46 +00:00
|
|
|
if err != nil {
|
2024-07-18 09:47:59 +00:00
|
|
|
return nil, nil, err
|
2023-11-24 11:15:46 +00:00
|
|
|
}
|
2024-07-18 09:47:59 +00:00
|
|
|
defer r.Close()
|
2023-11-24 11:15:46 +00:00
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
content, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-11-24 11:15:46 +00:00
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
state := &tfState{}
|
|
|
|
err = json.Unmarshal(content, state)
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
2024-07-18 09:47:59 +00:00
|
|
|
return nil, nil, err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
return state, content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *statePull) localState(ctx context.Context, b *bundle.Bundle) (*tfState, error) {
|
2023-09-11 08:18:43 +00:00
|
|
|
dir, err := Dir(ctx, b)
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
2024-07-18 09:47:59 +00:00
|
|
|
return nil, err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
content, err := os.ReadFile(filepath.Join(dir, TerraformStateFileName))
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
2024-07-18 09:47:59 +00:00
|
|
|
return nil, err
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
2024-07-18 09:47:59 +00:00
|
|
|
|
|
|
|
state := &tfState{}
|
|
|
|
err = json.Unmarshal(content, state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-24 11:15:46 +00:00
|
|
|
}
|
2023-03-30 10:01:09 +00:00
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *statePull) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
|
|
|
dir, err := Dir(ctx, b)
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
2023-05-16 15:02:33 +00:00
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
localStatePath := filepath.Join(dir, TerraformStateFileName)
|
|
|
|
|
|
|
|
// Case: Remote state file does not exist. In this case we fallback to using the
|
|
|
|
// local Terraform state. This allows users to change the "root_path" their bundle is
|
|
|
|
// configured with.
|
|
|
|
remoteState, remoteContent, err := l.remoteState(ctx, b)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
log.Infof(ctx, "Remote state file does not exist. Using local Terraform state.")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-05-16 15:02:33 +00:00
|
|
|
}
|
2024-07-18 09:47:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return diag.Errorf("failed to read remote state file: %v", err)
|
|
|
|
}
|
2023-05-16 15:02:33 +00:00
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
// Expected invariant: remote state file should have a lineage UUID. Error
|
|
|
|
// if that's not the case.
|
|
|
|
if remoteState.Lineage == "" {
|
|
|
|
return diag.Errorf("remote state file does not have a lineage")
|
|
|
|
}
|
2023-03-30 10:01:09 +00:00
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
// Case: Local state file does not exist. In this case we should rely on the remote state file.
|
|
|
|
localState, err := l.localState(ctx, b)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
log.Infof(ctx, "Local state file does not exist. Using remote Terraform state.")
|
|
|
|
err := os.WriteFile(localStatePath, remoteContent, 0600)
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
2023-03-30 10:01:09 +00:00
|
|
|
if err != nil {
|
2024-07-18 09:47:59 +00:00
|
|
|
return diag.Errorf("failed to read local state file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the lineage does not match, the Terraform state files do not correspond to the same deployment.
|
|
|
|
if localState.Lineage != remoteState.Lineage {
|
|
|
|
log.Infof(ctx, "Remote and local state lineages do not match. Using remote Terraform state. Invalidating local Terraform state.")
|
|
|
|
err := os.WriteFile(localStatePath, remoteContent, 0600)
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the remote state is newer than the local state, we should use the remote state.
|
|
|
|
if remoteState.Serial > localState.Serial {
|
|
|
|
log.Infof(ctx, "Remote state is newer than local state. Using remote Terraform state.")
|
|
|
|
err := os.WriteFile(localStatePath, remoteContent, 0600)
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 09:47:59 +00:00
|
|
|
// default: local state is newer or equal to remote state in terms of serial sequence.
|
|
|
|
// It is also of the same lineage. Keep using the local state.
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StatePull() bundle.Mutator {
|
2024-03-18 14:41:58 +00:00
|
|
|
return &statePull{deploy.StateFiler}
|
2023-03-30 10:01:09 +00:00
|
|
|
}
|