Skip to content

Commit ef1d86d

Browse files
committed
fix(sim-setup): guard stopped legacy containers' uploads on update
1 parent 1fd914f commit ef1d86d

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎packages/sim-setup/src/lifecycle.test.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,22 @@ describe('uploads an update would discard', () => {
224224
{ Type: 'volume', Name: 'sim_sim_uploads', Destination: '/app/apps/sim/uploads' },
225225
])
226226

227+
const legacyFiles = [
228+
'C /app',
229+
'C /app/apps/sim',
230+
'A /app/apps/sim/uploads',
231+
'A /app/apps/sim/uploads/workspace/report.pdf',
232+
].join('\n')
233+
227234
it.each([
228-
['files with no volume at the uploads path', '[]', 'workspace/\n', true],
229-
['files on the sim_uploads volume', uploadsMount, 'workspace/\n', false],
230-
['an empty uploads directory', '[]', '', false],
231-
['an uploads directory that could not be listed', '[]', null, false],
232-
['mounts that could not be read', null, 'workspace/\n', false],
233-
])('%s', (_, mounts, listing, atRisk) => {
234-
expect(uploadsLostOnRecreate(mounts, listing)).toBe(atRisk)
235+
['files with no volume at the uploads path, running or stopped', '[]', legacyFiles, true],
236+
['files on the sim_uploads volume', uploadsMount, '', false],
237+
['only the empty uploads directory itself', '[]', 'A /app/apps/sim/uploads', false],
238+
['changes elsewhere in the container', '[]', 'A /tmp/scratch', false],
239+
['a container whose changes could not be read, with no volume', '[]', null, true],
240+
['a container whose changes could not be read, with the volume', uploadsMount, null, false],
241+
['mounts that could not be read', null, legacyFiles, false],
242+
])('%s', (_, mounts, changes, atRisk) => {
243+
expect(uploadsLostOnRecreate(mounts, changes)).toBe(atRisk)
235244
})
236245
})

‎packages/sim-setup/src/lifecycle.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,28 +548,35 @@ const UPLOADS_PATH = '/app/apps/sim/uploads'
548548
* Whether recreating the app container would discard uploads: it holds files
549549
* in its own filesystem at the uploads path, with no volume mounted there, as
550550
* on installs that predate the `sim_uploads` volume. `mountsJson` is the
551-
* container's `{{json .Mounts}}` and `uploadsListing` an `ls -A` of the path;
552-
* `null` means the query failed, which is not evidence of files.
551+
* container's `{{json .Mounts}}`, and `filesystemChanges` its `docker diff`,
552+
* which works on stopped containers and never lists files on a mounted volume.
553+
* Unreadable mounts say nothing about the container; an unreadable diff on an
554+
* unmounted path is treated as at risk, since the files cannot be ruled out.
553555
*/
554556
export function uploadsLostOnRecreate(
555557
mountsJson: string | null,
556-
uploadsListing: string | null
558+
filesystemChanges: string | null
557559
): boolean {
558-
if (mountsJson === null || uploadsListing === null || !uploadsListing.trim()) return false
560+
if (mountsJson === null) return false
559561
let mounts: unknown
560562
try {
561563
mounts = JSON.parse(mountsJson)
562564
} catch {
563565
return false
564566
}
565567
if (!Array.isArray(mounts)) return false
566-
return !mounts.some((mount) => toRecordOrNull(mount)?.Destination === UPLOADS_PATH)
568+
if (mounts.some((mount) => toRecordOrNull(mount)?.Destination === UPLOADS_PATH)) return false
569+
if (filesystemChanges === null) return true
570+
return filesystemChanges
571+
.split('\n')
572+
.some((line) => /^[AC] /.test(line) && line.slice(2).startsWith(`${UPLOADS_PATH}/`))
567573
}
568574

569-
/** Inspects the running app container of `install` for uploads an update would discard. */
575+
/** Inspects the app container of `install`, running or stopped, for uploads an update would discard. */
570576
function inContainerUploadsAtRisk(install: ComposeInstall): boolean {
571577
const container = dockerText([
572578
'ps',
579+
'-a',
573580
'-q',
574581
'--filter',
575582
`label=com.docker.compose.project=${install.project}`,
@@ -579,7 +586,7 @@ function inContainerUploadsAtRisk(install: ComposeInstall): boolean {
579586
if (!container) return false
580587
return uploadsLostOnRecreate(
581588
dockerText(['inspect', '--format', '{{json .Mounts}}', container]),
582-
dockerText(['exec', container, 'ls', '-A', UPLOADS_PATH])
589+
dockerText(['diff', container])
583590
)
584591
}
585592

0 commit comments

Comments
 (0)