Skip to content

Commit f6dd5b0

Browse files
committed
feat: download image
1 parent 36333b9 commit f6dd5b0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/extension.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import { resolve } from 'node:path';
2222
import * as macadamJSPackage from '@crc-org/macadam.js';
2323
import * as extensionApi from '@podman-desktop/api';
2424

25+
import { initAuthentication } from './authentication';
26+
import { getImageSha } from './images';
2527
import { LoggerDelegator } from './logger';
2628
import { ProviderConnectionShellAccessImpl } from './macadam-machine-stream';
27-
import { getErrorMessage, verifyContainerProivder } from './utils';
29+
import { getErrorMessage, pullImageFromRedHatRegistry, verifyContainerProivder } from './utils';
2830
import { isHyperVEnabled, isWSLEnabled } from './win/utils';
2931

3032
const MACADAM_CLI_NAME = 'macadam';
@@ -476,6 +478,15 @@ async function createVM(
476478
if (existsSync(cachedImagePath)) {
477479
imagePath = cachedImagePath;
478480
telemetryRecords.imagePath = 'cached';
481+
} else {
482+
const client = await initAuthentication();
483+
if (!client) {
484+
throw new Error('unable to authenticate');
485+
}
486+
const imageSha = getImageSha(provider);
487+
await pullImageFromRedHatRegistry(client, imageSha, cachedImagePath);
488+
imagePath = cachedImagePath;
489+
telemetryRecords.imagePath = 'downloaded';
479490
}
480491
}
481492

src/images.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**********************************************************************
2+
* Copyright (C) 2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
const IMAGES: { [provider: string]: string } = {
20+
'applehv': '24f35ffb80911f2687f0bcd4237f62e46c19a6f0445aacb951b77d1974130187',
21+
'wsl': '1351d19fddb169ed01dc8815e9318027d27d7fe8c80e1844559ccd9c041ad9ca',
22+
};
23+
24+
export function getImageSha(provider?: string): string {
25+
if (!provider) {
26+
throw new Error('undefined provider is not supported');
27+
}
28+
if (provider in IMAGES) {
29+
return IMAGES[provider];
30+
}
31+
throw new Error(`provider ${provider} is not supported`);
32+
}

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ export async function pullImageFromRedHatRegistry(
5050
output.write(chunk);
5151
},
5252
});
53-
await redirectToImage?.data?.pipeTo(stream);
53+
await redirectToImage.data?.pipeTo(stream);
5454
}

0 commit comments

Comments
 (0)