From bb054ff9dca43f4e52e9e86e542eaf895b140d2c Mon Sep 17 00:00:00 2001 From: Carsten Munk Date: Sat, 11 May 2024 10:42:59 +0200 Subject: [PATCH] feat: support for multiple sdks and abstract machine making into create_image script in sdk This probably requires upping CARTESI_DEFAULT_SDK_VERSION as well. Signed-off-by: Carsten Munk --- apps/cli/CHANGELOG.md | 6 ++ apps/cli/package.json | 2 +- apps/cli/src/commands/build.ts | 28 +++++----- packages/sdk/CHANGELOG.md | 6 ++ packages/sdk/Dockerfile | 1 + packages/sdk/create_machine_snapshot | 82 ++++++++++++++++++++++++++++ packages/sdk/package.json | 2 +- 7 files changed, 112 insertions(+), 15 deletions(-) create mode 100755 packages/sdk/create_machine_snapshot diff --git a/apps/cli/CHANGELOG.md b/apps/cli/CHANGELOG.md index 0cede3c3..1508cbe1 100644 --- a/apps/cli/CHANGELOG.md +++ b/apps/cli/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.15.0 + +### Minor Changes + +- Add capability for picking SDK through sdk_name + ## 0.14.1 ### Patch Changes diff --git a/apps/cli/package.json b/apps/cli/package.json index 1239a1e5..5314cac6 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -1,6 +1,6 @@ { "name": "@cartesi/cli", - "version": "0.14.1", + "version": "0.15.0", "description": "Cartesi CLI", "author": "Danilo Tuler ", "bin": { diff --git a/apps/cli/src/commands/build.ts b/apps/cli/src/commands/build.ts index aa25675c..1e59fe57 100644 --- a/apps/cli/src/commands/build.ts +++ b/apps/cli/src/commands/build.ts @@ -19,6 +19,7 @@ type ImageInfo = { env: string[]; ramSize: string; sdkVersion: string; + sdkName: string; workdir: string; }; @@ -28,7 +29,8 @@ const CARTESI_LABEL_DATA_SIZE = `${CARTESI_LABEL_PREFIX}.data_size`; const CARTESI_DEFAULT_RAM_SIZE = "128Mi"; const CARTESI_LABEL_SDK_VERSION = `${CARTESI_LABEL_PREFIX}.sdk_version`; -const CARTESI_DEFAULT_SDK_VERSION = "0.6.0"; +const CARTESI_LABEL_SDK = `${CARTESI_LABEL_PREFIX}.sdk_name`; +const CARTESI_DEFAULT_SDK_VERSION = "0.7.0"; export default class BuildApplication extends BaseCommand< typeof BuildApplication @@ -99,6 +101,7 @@ export default class BuildApplication extends BaseCommand< entrypoint: imageInfo["Config"]["Entrypoint"] ?? [], env: imageInfo["Config"]["Env"] || [], ramSize: labels[CARTESI_LABEL_RAM_SIZE] ?? CARTESI_DEFAULT_RAM_SIZE, + sdkName: labels[CARTESI_LABEL_SDK] ?? "cartesi/sdk", sdkVersion: labels[CARTESI_LABEL_SDK_VERSION] ?? CARTESI_DEFAULT_SDK_VERSION, @@ -112,7 +115,10 @@ export default class BuildApplication extends BaseCommand< // fail if using unsupported sdk version if (!semver.valid(info.sdkVersion)) { this.warn("sdk version is not a valid semver"); - } else if (semver.lt(info.sdkVersion, CARTESI_DEFAULT_SDK_VERSION)) { + } else if ( + info.sdkName == "cartesi/sdk" && + semver.lt(info.sdkVersion, CARTESI_DEFAULT_SDK_VERSION) + ) { throw new Error(`Unsupported sdk version: ${info.sdkVersion} (used) < ${CARTESI_DEFAULT_SDK_VERSION} (minimum). Update your application Dockerfile using one of the templates at https://github.com/cartesi/application-templates/tree/${DEFAULT_TEMPLATES_BRANCH} `); @@ -232,26 +238,22 @@ Update your application Dockerfile using one of the templates at https://github. const driveLabel = "root"; // XXX: does this need to be customizable? // list of environment variables of docker image - const envs = info.env.map( - (variable) => `--append-entrypoint=export ${variable}`, - ); + const envs = info.env.map((variable) => `--env=${variable}`); // ENTRYPOINT and CMD as a space separated string const entrypoint = [...info.entrypoint, ...info.cmd].join(" "); // command to change working directory if WORKDIR is defined - const cwd = info.workdir ? `--append-init=WORKDIR=${info.workdir}` : ""; + const cwd = info.workdir ? `--workdir=${info.workdir}` : ""; return [ - "cartesi-machine", - "--assert-rolling-template", + "create_machine_snapshot", `--ram-length=${ramSize}`, - `--flash-drive=label:${driveLabel},filename:/tmp/input`, - "--final-hash", - `--store=/tmp/output`, - "--append-bootargs=no4lvl", + `--drive-label=${driveLabel}`, + `--drive-filename=/tmp/input`, + `--output=/tmp/output`, cwd, ...envs, - `--append-entrypoint=${entrypoint}`, + `--entrypoint=${entrypoint}`, ]; } diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index 58d12aa3..0d602a9c 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -1,5 +1,11 @@ # sdk +## 0.7.0 + +### Minor Changes + +- Add capability for picking SDK through sdk_name + ## 0.6.1 ### Patch Changes diff --git a/packages/sdk/Dockerfile b/packages/sdk/Dockerfile index 9811f3c3..802c2493 100644 --- a/packages/sdk/Dockerfile +++ b/packages/sdk/Dockerfile @@ -105,6 +105,7 @@ COPY devnet /usr/local/bin COPY eth_isready /usr/local/bin COPY eth_dump /usr/local/bin COPY eth_load /usr/local/bin +COPY create_machine_snapshot /usr/local/bin COPY entrypoint.sh /usr/local/bin/ COPY --from=su-exec /usr/local/src/su-exec /usr/local/bin/ diff --git a/packages/sdk/create_machine_snapshot b/packages/sdk/create_machine_snapshot new file mode 100755 index 00000000..11e3a7d4 --- /dev/null +++ b/packages/sdk/create_machine_snapshot @@ -0,0 +1,82 @@ +#!/bin/bash +set -e + +# Define function to display usage +usage() { + echo "Usage: $0 [options]" + echo "Options:" + echo " -r, --ram-length Specify RAM size" + echo " -d, --drive-label