Skip to content

Commit 7fb73fa

Browse files
pieternIsaac
andcommitted
Refuse to apply a plan that keeps a modified snapshot under --force
`bundle deploy --plan plan.json --force` reused the zip staged when the plan was produced. If the snapshot was untouched at plan time and the content had not changed, that zip carries the previous snapshot's name and the plan holds a skip, so the forced deploy stayed on the snapshot that was modified in the workspace, contrary to what --force promises. Detect this case and ask the user to rerun `bundle plan --force`, which moves the deployment to a fresh snapshot. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 2534bb0 commit 7fb73fa

4 files changed

Lines changed: 71 additions & 18 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `bundle deploy --plan --force` now fails instead of staying on a snapshot that was modified in the workspace after the plan was created; run `bundle plan --force` again.

‎acceptance/bundle/deploy/immutable/breakglass/output.txt‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ Resources: 1 created, 1 changed, 1 deleted, 0 unchanged
5454
Fourth snapshot path: /Workspace/Users/[UUID]/.snapshots/[SNAPSHOT_HASH]/[SNAPSHOT_HASH]-[UUID]/snapshot/files/src/main.py
5555
OK: the saved plan moved the deployment to a new snapshot
5656

57+
=== Save a plan without changes, then break the glass before deploying it
58+
>>> [CLI] bundle plan -o json
59+
60+
>>> [CLI] api post /api/2.0/snapshots:breakGlass --json {"snapshot_content_path": "/Workspace/Users/[UUID]/.snapshots/[SNAPSHOT_HASH]/[SNAPSHOT_HASH]-[UUID]/snapshot"}
61+
true
62+
63+
=== Deploying the saved plan with --force fails, since it keeps the modified snapshot
64+
>>> musterr [CLI] bundle deploy --plan plan.json --force
65+
Error: the last deployment was modified in the workspace since the plan was created; run 'bundle plan --force' again to move to a new snapshot
66+
67+
Files: 0 uploaded, 0 deleted
68+
69+
=== A plan saved with --force moves to a new snapshot
70+
>>> [CLI] bundle plan --force -o json
71+
72+
>>> [CLI] bundle deploy --plan plan.json --force
73+
Recreated internal_immutable_snapshots.immutable
74+
Updated jobs.my_job
75+
Files: 0 uploaded, 0 deleted
76+
Resources: 1 created, 1 changed, 1 deleted, 0 unchanged
77+
Fifth snapshot path: /Workspace/Users/[UUID]/.snapshots/[SNAPSHOT_HASH]/[SNAPSHOT_HASH]-[UUID]/snapshot/files/src/main.py
78+
OK: the plan saved with --force moved the deployment to a new snapshot
79+
5780
>>> [CLI] bundle destroy --auto-approve
5881
The following resources will be deleted:
5982
delete resources.jobs.my_job

‎acceptance/bundle/deploy/immutable/breakglass/script‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,22 @@ if [ "$SNAPSHOT_PATH_3" = "$SNAPSHOT_PATH_4" ]; then
6060
exit 1
6161
fi
6262
echo "OK: the saved plan moved the deployment to a new snapshot"
63+
64+
title "Save a plan without changes, then break the glass before deploying it"
65+
trace $CLI bundle plan -o json > plan.json
66+
CONTENT_PATH_4="${SNAPSHOT_PATH_4%/files/src/main.py}"
67+
trace $CLI api post /api/2.0/snapshots:breakGlass --json "{\"snapshot_content_path\": \"$CONTENT_PATH_4\"}" | jq .status.dirty
68+
69+
title "Deploying the saved plan with --force fails, since it keeps the modified snapshot"
70+
trace musterr $CLI bundle deploy --plan plan.json --force
71+
72+
title "A plan saved with --force moves to a new snapshot"
73+
trace $CLI bundle plan --force -o json > plan.json
74+
trace $CLI bundle deploy --plan plan.json --force
75+
SNAPSHOT_PATH_5=$($CLI jobs get "$JOB_ID" | jq -r '.settings.tasks[0].spark_python_task.python_file')
76+
echo "Fifth snapshot path: $SNAPSHOT_PATH_5"
77+
if [ "$SNAPSHOT_PATH_4" = "$SNAPSHOT_PATH_5" ]; then
78+
echo "ERROR: snapshot path did not change after deploying the plan saved with --force"
79+
exit 1
80+
fi
81+
echo "OK: the plan saved with --force moved the deployment to a new snapshot"

‎bundle/deploy/snapshot/upload.go‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ func (m *snapshotUpload) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
8282
// Check the previous snapshot before doing any work, so a deploy onto a snapshot that was
8383
// modified outside the bundle fails before building a zip. A saved plan is checked as well:
8484
// the snapshot may have been modified after the plan was produced.
85-
prev, err := checkPrevSnapshot(ctx, b, uploader, remoteRoot)
85+
prev, modified, err := checkPrevSnapshot(ctx, b, uploader, remoteRoot)
8686
if err != nil {
8787
return diag.FromErr(err)
8888
}
8989

9090
var zipPath string
9191
var diags diag.Diagnostics
9292
if m.clean {
93+
// --force moves the deployment off a modified snapshot, so don't let it reuse that name.
94+
if modified {
95+
prev = nil
96+
}
9397
zipPath, diags, err = stageZip(ctx, b, snap, prev)
9498
} else {
9599
zipPath, err = stagedZipPath(ctx, b)
@@ -99,6 +103,11 @@ func (m *snapshotUpload) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
99103
}
100104

101105
snap.ZipPath = filepath.ToSlash(zipPath)
106+
// A plan produced while the snapshot was untouched may keep it in place. Applying it would
107+
// leave the deployment on the modified snapshot, which --force promises to move off.
108+
if !m.clean && modified && snap.RelativePath() == prev.RelativePath {
109+
return diag.Errorf("the last deployment was modified in the workspace since the plan was created; run 'bundle plan --force' again to move to a new snapshot")
110+
}
102111
b.Config.Resources.Snapshots[resources.SnapshotResourceKey] = snap
103112

104113
return diags
@@ -231,60 +240,61 @@ func BuildCanManage(b *bundle.Bundle) []snapshot.ManagePrincipal {
231240
}
232241

233242
// checkPrevSnapshot asks the backend whether the snapshot deployed last time was modified outside
234-
// of the bundle, and returns its state when the deployment may keep using it:
243+
// of the bundle, and returns its state:
235244
// - the previous state while the snapshot is untouched;
236-
// - nil when there is no previous snapshot, or it was modified and --force was given, which
237-
// moves the deployment to a new snapshot;
245+
// - nil when there is no previous snapshot;
246+
// - the previous state and modified=true when it was modified and --force was given; the
247+
// caller must then not keep the deployment on it;
238248
// - an error when it was modified and --force was not given.
239249
//
240250
// The check has to happen here rather than in the resource's DoRead: it decides the snapshot's
241251
// path, and resources referencing ${...full_path} resolve it from the desired state computed
242252
// before any resource is read (see LookupReferencePreDeploy). Erroring unless --force also has no
243253
// equivalent in the resource layer. CheckDashboardsModifiedRemotely is the same shape: read state,
244254
// ask the API, refuse unless forced.
245-
func checkPrevSnapshot(ctx context.Context, b *bundle.Bundle, uploader *snapshot.SnapshotClient, remoteRoot string) (*dresources.SnapshotState, error) {
255+
func checkPrevSnapshot(ctx context.Context, b *bundle.Bundle, uploader *snapshot.SnapshotClient, remoteRoot string) (prev *dresources.SnapshotState, modified bool, err error) {
246256
// The deploy/plan pipeline opens the state DB for read before this runs, but callers that
247257
// exercise PlanUpload in isolation (unit tests) may not. No open state means no previous
248258
// snapshot to check, which is the first-deploy case.
249259
if !b.DeploymentBundle.StateDB.IsOpen() {
250-
return nil, nil
260+
return nil, false, nil
251261
}
252262

253263
entry, ok := b.DeploymentBundle.StateDB.GetResourceEntry(resources.SnapshotKey)
254264
if !ok || len(entry.State) == 0 {
255-
return nil, nil
265+
return nil, false, nil
256266
}
257267

258268
// Decode into the state type the engine persists, so the field names cannot drift apart.
259-
var prev dresources.SnapshotState
260-
if err := json.Unmarshal(entry.State, &prev); err != nil {
261-
return nil, fmt.Errorf("reading previous snapshot state: %w", err)
269+
var state dresources.SnapshotState
270+
if err := json.Unmarshal(entry.State, &state); err != nil {
271+
return nil, false, fmt.Errorf("reading previous snapshot state: %w", err)
262272
}
263-
if prev.RelativePath == "" {
264-
return nil, nil
273+
if state.RelativePath == "" {
274+
return nil, false, nil
265275
}
266276

267277
// Compose the content path from relative_path rather than reading full_path: relative_path
268278
// means the same thing in every CLI version, while full_path from a CLI that predates the
269279
// content subfolder points one level too high.
270-
contentPath := path.Join(remoteRoot, prev.RelativePath, snapshot.ContentSubdir)
280+
contentPath := path.Join(remoteRoot, state.RelativePath, snapshot.ContentSubdir)
271281

272282
status, err := uploader.InspectSnapshot(ctx, contentPath)
273283
// Nothing to protect when no snapshot is there: the deployment predates the content
274284
// subfolder, or the snapshot was deleted out of band, or an earlier deploy never created it.
275285
// Failing here would also be a dead end, since this runs before the --force check and so
276286
// could not be forced past.
277287
if errors.Is(err, apierr.ErrNotFound) {
278-
return nil, nil
288+
return nil, false, nil
279289
}
280290
if err != nil {
281-
return nil, err
291+
return nil, false, err
282292
}
283293
if !status.Dirty {
284-
return &prev, nil
294+
return &state, false, nil
285295
}
286296
if !b.Config.Bundle.Force {
287-
return nil, errors.New("the last deployment was modified in the workspace. Changes might be overwritten by this deployment. To confirm it, use the --force flag")
297+
return nil, false, errors.New("the last deployment was modified in the workspace. Changes might be overwritten by this deployment. To confirm it, use the --force flag")
288298
}
289-
return nil, nil
299+
return &state, true, nil
290300
}

0 commit comments

Comments
 (0)