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 <carsten@zippie.com>
  • Loading branch information
stskeeps committed May 11, 2024
1 parent d8e8329 commit ee546bd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
26 changes: 14 additions & 12 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;
sdkType: string;
workdir: string;
};

Expand All @@ -28,6 +29,7 @@ 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_LABEL_SDK = `${CARTESI_LABEL_PREFIX}.sdk`;
const CARTESI_DEFAULT_SDK_VERSION = "0.6.0";

export default class BuildApplication extends BaseCommand<
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,
sdkType: 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.sdkType == "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_image",
`--ram-length=${ramSize}`,
`--flash-drive=label:${driveLabel},filename:/tmp/input`,
"--final-hash",
`--store=/tmp/output`,
"--append-bootargs=no4lvl",
`--drive-label=${driveLabel}`,
`--machine-rootfs=/tmp/input`,
`--output=/tmp/output`,
cwd,
...envs,
`--append-entrypoint=${entrypoint}`,
`--entrypoint=${entrypoint}`,
];
}

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_image /usr/local/bin

COPY entrypoint.sh /usr/local/bin/
COPY --from=su-exec /usr/local/src/su-exec /usr/local/bin/
Expand Down
81 changes: 81 additions & 0 deletions packages/sdk/create_image
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

# 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, --machine-rootfs <path> Specify machine root filesystem path"
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=""
input_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:,machine-rootfs:,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|--machine-rootfs)
input_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 $input_filename ]] && cmd+=("--flash-drive=label:$drive_label,filename:$input_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[@]}"

0 comments on commit ee546bd

Please sign in to comment.