1
1
import { execSync } from "child_process" ;
2
- import { mkdir , rm , writeFile , readFile , readdir , stat } from "fs/promises" ;
3
- import { join , dirname } from "path" ;
4
- import { createReadStream , createWriteStream } from "fs" ;
5
- import { pipeline } from "stream/promises" ;
2
+ import { join } from "path" ;
3
+ import { mkdir , readFile , rm , writeFile } from "fs/promises" ;
6
4
import * as tar from "tar" ;
7
5
8
6
export interface OCIConversionResult {
@@ -12,14 +10,14 @@ export interface OCIConversionResult {
12
10
13
11
export async function convertDockerTarToOCIBundle (
14
12
dockerTarPath : string ,
15
- tempDir : string = "/tmp/oci-conversion"
13
+ tempDir = "/tmp/oci-conversion" ,
16
14
) : Promise < OCIConversionResult > {
17
15
const conversionId = Math . random ( ) . toString ( 36 ) . substring ( 7 ) ;
18
16
const workDir = join ( tempDir , conversionId ) ;
19
-
17
+
20
18
try {
21
19
await mkdir ( workDir , { recursive : true } ) ;
22
-
20
+
23
21
const dockerImagePath = join ( workDir , "docker-image.tar" ) ;
24
22
const ociImagePath = join ( workDir , "oci-image" ) ;
25
23
const ociBundlePath = join ( workDir , "oci-bundle" ) ;
@@ -30,54 +28,71 @@ export async function convertDockerTarToOCIBundle(
30
28
await writeFile ( dockerImagePath , dockerTarData ) ;
31
29
32
30
// Convert Docker image to OCI image using skopeo
33
- console . log ( `Converting Docker image to OCI image: ${ dockerImagePath } -> ${ ociImagePath } ` ) ;
34
- execSync ( `skopeo copy docker-archive:${ dockerImagePath } oci:${ ociImagePath } :default` , {
35
- stdio : "pipe"
36
- } ) ;
31
+ console . log (
32
+ `Converting Docker image to OCI image: ${ dockerImagePath } -> ${ ociImagePath } ` ,
33
+ ) ;
34
+ execSync (
35
+ `skopeo copy docker-archive:${ dockerImagePath } oci:${ ociImagePath } :default` ,
36
+ {
37
+ stdio : "pipe" ,
38
+ } ,
39
+ ) ;
37
40
38
41
// Convert OCI image to OCI bundle using umoci
39
- console . log ( `Converting OCI image to OCI bundle: ${ ociImagePath } -> ${ ociBundlePath } ` ) ;
40
- execSync ( `umoci unpack --rootless --image ${ ociImagePath } :default ${ ociBundlePath } ` , {
41
- stdio : "pipe"
42
- } ) ;
42
+ console . log (
43
+ `Converting OCI image to OCI bundle: ${ ociImagePath } -> ${ ociBundlePath } ` ,
44
+ ) ;
45
+ execSync (
46
+ `umoci unpack --rootless --image ${ ociImagePath } :default ${ ociBundlePath } ` ,
47
+ {
48
+ stdio : "pipe" ,
49
+ } ,
50
+ ) ;
43
51
44
52
// Create tar from OCI bundle
45
- console . log ( `Creating tar from OCI bundle: ${ ociBundlePath } -> ${ bundleTarPath } ` ) ;
53
+ console . log (
54
+ `Creating tar from OCI bundle: ${ ociBundlePath } -> ${ bundleTarPath } ` ,
55
+ ) ;
46
56
await tar . create (
47
57
{
48
58
file : bundleTarPath ,
49
59
cwd : ociBundlePath ,
50
60
} ,
51
- [ "." ]
61
+ [ "." ] ,
52
62
) ;
53
63
54
64
// Clean up intermediate files
55
65
await Promise . all ( [
56
66
rm ( dockerImagePath , { force : true } ) ,
57
67
rm ( ociImagePath , { recursive : true , force : true } ) ,
58
- rm ( ociBundlePath , { recursive : true , force : true } )
68
+ rm ( ociBundlePath , { recursive : true , force : true } ) ,
59
69
] ) ;
60
70
61
71
const cleanup = async ( ) => {
62
72
try {
63
73
await rm ( workDir , { recursive : true , force : true } ) ;
64
74
} catch ( error ) {
65
- console . warn ( `Failed to cleanup OCI conversion directory ${ workDir } :` , error ) ;
75
+ console . warn (
76
+ `Failed to cleanup OCI conversion directory ${ workDir } :` ,
77
+ error ,
78
+ ) ;
66
79
}
67
80
} ;
68
81
69
82
return {
70
83
bundleTarPath,
71
- cleanup
84
+ cleanup,
72
85
} ;
73
86
} catch ( error ) {
74
87
// Cleanup on error
75
88
try {
76
89
await rm ( workDir , { recursive : true , force : true } ) ;
77
90
} catch ( cleanupError ) {
78
- console . warn ( `Failed to cleanup after error in ${ workDir } :` , cleanupError ) ;
91
+ console . warn (
92
+ `Failed to cleanup after error in ${ workDir } :` ,
93
+ cleanupError ,
94
+ ) ;
79
95
}
80
96
throw new Error ( `OCI conversion failed: ${ error } ` ) ;
81
97
}
82
98
}
83
-
0 commit comments