Skip to content

Commit 19f49e1

Browse files
committed
fix: attempt to clean image archives if skopeo copy failed
1 parent 68394ec commit 19f49e1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/scanner/images/skopeo.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as fs from 'fs';
12
import * as sleep from 'sleep-promise';
23

4+
import logger = require('../../common/logger');
35
import * as processWrapper from '../../common/process';
46
import * as config from '../../common/config';
57
import * as credentials from './credentials';
@@ -43,10 +45,10 @@ export async function pull(
4345
args.push({body: prefixRespository(image, SkopeoRepositoryType.ImageRegistry), sanitise: false});
4446
args.push({body: prefixRespository(destination, SkopeoRepositoryType.DockerArchive), sanitise: false});
4547

46-
await pullWithRetry(args);
48+
await pullWithRetry(args, destination);
4749
}
4850

49-
async function pullWithRetry(args: Array<processWrapper.IProcessArgument>): Promise<void> {
51+
async function pullWithRetry(args: Array<processWrapper.IProcessArgument>, destination: string): Promise<void> {
5052
const MAX_ATTEMPTS = 10;
5153
const RETRY_INTERVAL_SEC = 0.2;
5254

@@ -55,6 +57,13 @@ async function pullWithRetry(args: Array<processWrapper.IProcessArgument>): Prom
5557
await processWrapper.exec('skopeo', ...args);
5658
return;
5759
} catch (err) {
60+
try {
61+
if (fs.existsSync(destination)) {
62+
fs.unlinkSync(destination);
63+
}
64+
} catch (deleteErr) {
65+
logger.warn({error: deleteErr, destination}, 'could not clean up the Skopeo-copy archive');
66+
}
5867
if (attempt + 1 > MAX_ATTEMPTS) {
5968
throw err;
6069
}

0 commit comments

Comments
 (0)