From 9b6d6430fbf4702b08ca14037ab3865eb9547774 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 12 Sep 2021 07:43:03 -0400 Subject: [PATCH] feat: add the ability to constrain memory on service start/clone Refs dokku/dokku-redis#86 --- README.md | 2 ++ functions | 12 +++++++++++- subcommands/clone | 1 + subcommands/create | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cbf853..2b3ba92 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,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 @@ -425,6 +426,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 7fdf3f8..38e8bc1 100755 --- a/functions +++ b/functions @@ -67,6 +67,10 @@ service_create() { echo "" >"$SERVICE_ROOT/ENV" 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" @@ -79,7 +83,13 @@ service_create_container() { local SERVICE_HOST_ROOT="$PLUGIN_DATA_HOST_ROOT/$SERVICE" local SERVICE_NAME="$(get_service_name "$SERVICE")" - ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_HOST_ROOT/data:/data" -v "$SERVICE_HOST_ROOT/$PLUGIN_CONFIG_SUFFIX:/usr/local/etc/redis" --env-file="$SERVICE_ROOT/ENV" -d --restart always --label dokku=service --label dokku.service=redis "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" redis-server /usr/local/etc/redis/redis.conf --bind 0.0.0.0) + [[ -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" $MEMORY_LIMIT -v "$SERVICE_HOST_ROOT/data:/data" -v "$SERVICE_HOST_ROOT/$PLUGIN_CONFIG_SUFFIX:/usr/local/etc/redis" --env-file="$SERVICE_ROOT/ENV" -d --restart always --label dokku=service --label dokku.service=redis "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" redis-server /usr/local/etc/redis/redis.conf --bind 0.0.0.0) 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"