Skip to content

Commit

Permalink
make image loading process more robust if the image exists, try to us…
Browse files Browse the repository at this point in the history
…e the Docker cache
  • Loading branch information
lastmjs committed Feb 9, 2024
1 parent bb7205a commit dd6465a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ jobs:
node-version: 18
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
run: DFX_VERSION=0.16.1 sh -ci "$(curl -fsSL https://sdk.dfinity.org/install.sh)"
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
uses: actions/cache@v3
with:
path: ~/.config/azle
key: config-azle-${{ hashFiles('src/compiler/Dockerfile') }}
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
run: sudo apt-get install -y podman
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && runner.os == 'macOS' }}
Expand Down
104 changes: 79 additions & 25 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { generateWorkspaceCargoToml } from './generate_cargo_toml_files';
import { generateCandidAndCanisterMethods } from './generate_candid_and_canister_methods';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs';
import { copySync } from 'fs-extra';
import { execSync } from 'child_process';
import { execSync, IOType } from 'child_process';
import { GLOBAL_AZLE_CONFIG_DIR } from './utils/global_paths';

azle();
Expand Down Expand Up @@ -87,20 +87,42 @@ async function azle() {
mkdirSync(GLOBAL_AZLE_CONFIG_DIR, { recursive: true });
mkdirSync('.azle', { recursive: true });

const imageHasBeenLoaded = hasImageBeenLoaded(stdioType);

if (process.env.AZLE_USE_DOCKERFILE === 'true') {
try {
execSync(`podman image inspect azle_${azleVersion}_image`, {
stdio: stdioType
});

if (
!existsSync(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar`
)
) {
throw new Error(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar does not exist`
);
if (!imageHasBeenLoaded) {
if (
existsSync(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar`
)
) {
console.info(yellow(`\nLoading image...\n`));

execSync(
`podman load -i ${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar`,
{
stdio: 'inherit'
}
);
} else if (
existsSync(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar.gz`
)
) {
console.info(yellow(`\nLoading image...\n`));

execSync(
`podman load -i ${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar.gz`,
{
stdio: 'inherit'
}
);
} else {
throw new Error(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar or ${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar.gz does not exist`
);
}
}
} catch (error) {
console.info(yellow(`\nBuilding image...\n`));
Expand All @@ -125,18 +147,38 @@ async function azle() {
}
} else {
try {
execSync(`podman image inspect azle_${azleVersion}_image`, {
stdio: stdioType
});

if (
!existsSync(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar`
)
) {
throw new Error(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar does not exist`
);
if (!imageHasBeenLoaded) {
if (
existsSync(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar`
)
) {
console.info(yellow(`\nLoading image...\n`));

execSync(
`podman load -i ${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar`,
{
stdio: 'inherit'
}
);
} else if (
existsSync(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar.gz`
)
) {
console.info(yellow(`\nLoading image...\n`));

execSync(
`podman load -i ${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar.gz`,
{
stdio: 'inherit'
}
);
} else {
throw new Error(
`${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar or ${GLOBAL_AZLE_CONFIG_DIR}/azle_${azleVersion}_image.tar.gz does not exist`
);
}
}
} catch (error) {
console.info(yellow(`\nDownloading image...\n`));
Expand Down Expand Up @@ -350,3 +392,15 @@ function getEnvVars(canisterConfig: JSCanisterConfig): [string, string][] {
return [envVarName, process.env[envVarName] ?? ''];
});
}

function hasImageBeenLoaded(stdioType: IOType): boolean {
try {
execSync(`podman image inspect azle_${azleVersion}_image`, {
stdio: stdioType
});

return true;
} catch (error) {
return false;
}
}

0 comments on commit dd6465a

Please sign in to comment.