Skip to content

Commit

Permalink
feat: support for multiple sdks and abstract machine making into crea…
Browse files Browse the repository at this point in the history
…te_image script in sdk

This probably requires upping CARTESI_DEFAULT_SDK_VERSION as well.

Signed-off-by: Carsten Munk <[email protected]>
  • Loading branch information
stskeeps committed May 13, 2024
1 parent d8e8329 commit bb054ff
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 15 deletions.
6 changes: 6 additions & 0 deletions apps/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.15.0

### Minor Changes

- Add capability for picking SDK through sdk_name

## 0.14.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cartesi/cli",
"version": "0.14.1",
"version": "0.15.0",
"description": "Cartesi CLI",
"author": "Danilo Tuler <[email protected]>",
"bin": {
Expand Down
28 changes: 15 additions & 13 deletions apps/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ImageInfo = {
env: string[];
ramSize: string;
sdkVersion: string;
sdkName: string;
workdir: string;
};

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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}
`);
Expand Down Expand Up @@ -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}`,
];
}

Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# sdk

## 0.7.0

### Minor Changes

- Add capability for picking SDK through sdk_name

## 0.6.1

### Patch Changes
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
82 changes: 82 additions & 0 deletions packages/sdk/create_machine_snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
set -e

# Define function to display usage
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -r, --ram-length <size> Specify RAM size"
echo " -d, --drive-label <label> Specify drive label, filename for input"
echo " -m, --drive-filename <path> Specify raw drive image filename"
echo " -o, --output <path> Specify output directory"
echo " -w, --workdir <path> Set the working directory"
echo " -e, --entrypoint <cmd> Set the entrypoint command"
echo " -v, --env <var=value> Add an environment variable"
echo " -h, --help Display this help and exit"
}

# Initialize variables with default values
ram_length=""
drive_label=""
drive_filename=""
output_path=""
workdir=""
entrypoint=""
declare -a env_vars

# Parse command line options using getopt
TEMP=$(getopt -o r:d:m:o:w:e:v:h --long ram-length:,drive-label:,drive-filename:,output:,workdir:,entrypoint:,env:,help -- "$@")
eval set -- "$TEMP"

# Extract options and their arguments into variables.
while true ; do
case "$1" in
-r|--ram-length)
ram_length="$2"
shift 2 ;;
-d|--drive-label)
drive_label="$2"
shift 2 ;;
-m|--drive-filename)
drive_filename="$2"
shift 2 ;;
-o|--output)
output_path="$2"
shift 2 ;;
-w|--workdir)
workdir="$2"
shift 2 ;;
-e|--entrypoint)
entrypoint="$2"
shift 2 ;;
-v|--env)
env_vars+=("$2")
shift 2 ;;
--)
shift ; break ;;
-h|--help)
usage
exit 0 ;;
*)
echo "Invalid option: $1" >&2
usage
exit 1 ;;
esac
done

# Construct the command line for cartesi-machine
cmd=("cartesi-machine")
for env_var in "${env_vars[@]}"; do
cmd+=("--append-entrypoint=export $env_var")
done
[[ -n "$ram_length" ]] && cmd+=("--ram-length=$ram_length")
[[ -n "$drive_label" ]] && [[ -n $drive_filename ]] && cmd+=("--flash-drive=label:$drive_label,filename:$drive_filename")
[[ -n "$output_path" ]] && cmd+=("--store=$output_path")
[[ -n "$workdir" ]] && cmd+=("--append-init=WORKDIR=$workdir")
[[ -n "$entrypoint" ]] && cmd+=("--append-entrypoint=$entrypoint")
cmd+=("--append-bootargs=no4lvl")
cmd+=("--assert-rolling-template")
cmd+=("--final-hash")

# Execute the command
"${cmd[@]}"
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cartesi/sdk",
"private": true,
"version": "0.6.1",
"version": "0.7.0",
"scripts": {
"build": "docker buildx bake --load"
}
Expand Down

0 comments on commit bb054ff

Please sign in to comment.