Skip to content

Commit b9d1cd7

Browse files
committed
fix: use init container to setup permissions before running user image
1 parent c2a3c05 commit b9d1cd7

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/components/c2d/compute_engine_docker.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,52 @@ export class C2DEngineDocker extends C2DEngine {
15191519
}
15201520
}
15211521

1522+
private async ensureImage(image: string): Promise<void> {
1523+
try {
1524+
await this.docker.getImage(image).inspect()
1525+
} catch {
1526+
CORE_LOGGER.info(`Image ${image} not found locally, pulling...`)
1527+
const pullStream = await this.docker.pull(image)
1528+
await new Promise<void>((resolve, reject) => {
1529+
this.docker.modem.followProgress(pullStream, (err: any) => {
1530+
if (err) reject(err)
1531+
else resolve()
1532+
})
1533+
})
1534+
}
1535+
}
1536+
1537+
private async initializeVolumePermissions(volumeName: string): Promise<boolean> {
1538+
let initContainer: Dockerode.Container | null = null
1539+
try {
1540+
await this.ensureImage('busybox')
1541+
initContainer = await this.docker.createContainer({
1542+
Image: 'busybox',
1543+
Cmd: [
1544+
'sh',
1545+
'-c',
1546+
'mkdir -p /data/inputs /data/outputs /data/transformations /data/ddos /data/logs && chmod 777 /data /data/inputs /data/outputs /data/transformations /data/ddos /data/logs'
1547+
],
1548+
HostConfig: {
1549+
NetworkMode: 'none',
1550+
Mounts: [{ Type: 'volume', Source: volumeName, Target: '/data' }]
1551+
}
1552+
})
1553+
await initContainer.start()
1554+
await initContainer.wait()
1555+
return true
1556+
} catch (e) {
1557+
CORE_LOGGER.error(`Failed to initialize volume permissions: ${e.message}`)
1558+
return false
1559+
} finally {
1560+
if (initContainer) {
1561+
try {
1562+
await initContainer.remove()
1563+
} catch {}
1564+
}
1565+
}
1566+
}
1567+
15221568
private async createDockerVolume(
15231569
volume: VolumeCreateOptions,
15241570
retry: boolean = false
@@ -1635,6 +1681,15 @@ export class C2DEngineDocker extends C2DEngine {
16351681
await this.cleanupJob(job)
16361682
return
16371683
}
1684+
if (!(await this.initializeVolumePermissions(volume.Name))) {
1685+
job.status = C2DStatusNumber.VolumeCreationFailed
1686+
job.statusText = C2DStatusText.VolumeCreationFailed
1687+
job.isRunning = false
1688+
job.dateFinished = String(Date.now() / 1000)
1689+
await this.db.updateJob(job)
1690+
await this.cleanupJob(job)
1691+
return
1692+
}
16381693

16391694
// create the container
16401695
const mountVols: any = { '/data': {} }

0 commit comments

Comments
 (0)