From dd8a5a41f081a602528b0fdd1a838502f78e0af5 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 12 Sep 2021 07:43:01 -0400 Subject: [PATCH] feat: add the ability to constrain memory on service start/clone Refs dokku/dokku-redis#86 --- README.md | 2 ++ functions | 11 ++++++++++- subcommands/clone | 1 + subcommands/create | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 628d774..15b4b9a 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ flags: - `-C|--custom-env "USER=alpha;HOST=beta"`: semi-colon delimited environment variables to start the service with - `-i|--image IMAGE`: the image name to start the service with - `-I|--image-version IMAGE_VERSION`: the image version to start the service with +- `-m|--memory MEMORY`: container memory limit (default: unlimited) - `-p|--password PASSWORD`: override the user-level service password - `-r|--root-password PASSWORD`: override the root-level service password @@ -426,6 +427,7 @@ flags: - `-C|--custom-env "USER=alpha;HOST=beta"`: semi-colon delimited environment variables to start the service with - `-i|--image IMAGE`: the image name to start the service with - `-I|--image-version IMAGE_VERSION`: the image version to start the service with +- `-m|--memory MEMORY`: container memory limit (default: unlimited) - `-p|--password PASSWORD`: override the user-level service password - `-r|--root-password PASSWORD`: override the root-level service password diff --git a/functions b/functions index b09525a..75f5514 100755 --- a/functions +++ b/functions @@ -75,6 +75,10 @@ service_create() { echo "" >"$SERVICE_ROOT/MONGO_CONFIG_OPTIONS" fi + if [[ -n "$SERVICE_MEMORY" ]]; then + echo "$SERVICE_MEMORY" >"$SERVICE_ROOT/SERVICE_MEMORY" + fi + write_database_name "$SERVICE" plugn trigger service-action post-create "$SERVICE" service_create_container "$SERVICE" @@ -94,8 +98,13 @@ service_create_container() { export MONGO_CONFIG_OPTIONS="$(cat "$SERVICE_ROOT/MONGO_CONFIG_OPTIONS")" fi + [[ -f "$SERVICE_ROOT/SERVICE_MEMORY" ]] && SERVICE_MEMORY="$(cat "$SERVICE_ROOT/SERVICE_MEMORY")" + if [[ -n "$SERVICE_MEMORY" ]]; then + MEMORY_LIMIT="--memory=${SERVICE_MEMORY}m" + fi + # shellcheck disable=SC2086 - ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_HOST_ROOT/data:/data/db" --env-file="$SERVICE_ROOT/ENV" --env "MONGO_INITDB_DATABASE=$DATABASE_NAME" -d --restart always --label dokku=service --label dokku.service=mongo "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" mongod $MONGO_CONFIG_OPTIONS) + ID=$(docker run --name "$SERVICE_NAME" $MEMORY_LIMIT -v "$SERVICE_HOST_ROOT/data:/data/db" --env-file="$SERVICE_ROOT/ENV" --env "MONGO_INITDB_DATABASE=$DATABASE_NAME" -d --restart always --label dokku=service --label dokku.service=mongo "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" mongod $MONGO_CONFIG_OPTIONS) echo "$ID" >"$SERVICE_ROOT/ID" dokku_log_verbose_quiet "Waiting for container to be ready" diff --git a/subcommands/clone b/subcommands/clone index 744e28a..647aa19 100755 --- a/subcommands/clone +++ b/subcommands/clone @@ -13,6 +13,7 @@ service-clone-cmd() { #F -C|--custom-env "USER=alpha;HOST=beta", semi-colon delimited environment variables to start the service with #F -i|--image IMAGE, the image name to start the service with #F -I|--image-version IMAGE_VERSION, the image version to start the service with + #F -m|--memory MEMORY, container memory limit (default: unlimited) #F -p|--password PASSWORD, override the user-level service password #F -r|--root-password PASSWORD, override the root-level service password declare desc="create container then copy data from into " diff --git a/subcommands/create b/subcommands/create index 6b7edac..62a4df9 100755 --- a/subcommands/create +++ b/subcommands/create @@ -21,6 +21,7 @@ service-create-cmd() { #F -C|--custom-env "USER=alpha;HOST=beta", semi-colon delimited environment variables to start the service with #F -i|--image IMAGE, the image name to start the service with #F -I|--image-version IMAGE_VERSION, the image version to start the service with + #F -m|--memory MEMORY, container memory limit (default: unlimited) #F -p|--password PASSWORD, override the user-level service password #F -r|--root-password PASSWORD, override the root-level service password declare desc="create a $PLUGIN_SERVICE service"