Add strict mode

This commit is contained in:
Juan Carlos Mejías Rodríguez 2018-11-25 12:41:02 -05:00
parent 291ef0c616
commit f2b1a4bd15
3 changed files with 30 additions and 9 deletions

View File

@ -14,7 +14,8 @@ ENV LANG="en_US.UTF-8" \
PORTAINER_ENDPOINT="1"
HTTPIE_VERIFY_SSL="yes" \
VERBOSE_MODE="false" \
DEBUG_MODE="false"
DEBUG_MODE="false" \
STRICT_MODE="false"
RUN apk --update add \
bash \

View File

@ -18,7 +18,7 @@ Script was created for the latest Portainer API, which at the time of writing is
## How to use
The provided `psu` script allows to deploy/update/undeploy Portainer stacks. Settings can be passed through envvars and/or flags. Both envvars and flags can be mixed but flags will always overwrite envvar values. When deploying a stack, if it doesn't exist a new one is created, otherwise it's updated.
The provided `psu` script allows to deploy/update/undeploy Portainer stacks. Settings can be passed through envvars and/or flags. Both envvars and flags can be mixed but flags will always overwrite envvar values. When deploying a stack, if it doesn't exist a new one is created, otherwise it's updated (unless strict mode is active).
### With envvars
@ -35,6 +35,7 @@ This is particularly useful for CI/CD pipelines.
- `HTTPIE_VERIFY_SSL` ("yes" or "no", optional): Whether to verify SSL certificate or not. Defaults to `"yes"`.
- `VERBOSE_MODE` ("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to `"false"`.
- `DEBUG_MODE` ("true" or "false", optional): Whether to activate debug output mode or not. Defaults to `"false"`. See [debug mode warning](#debug-mode) below.
- `STRICT_MODE` ("true" or "false", optional): Whether to activate strict mode or not. Defaults to `"false"`. See [strict mode](#strict-mode) below.
#### Examples
@ -73,7 +74,9 @@ This is more suitable for standalone script usage.
- `-e` (int, optional): Which endpoint to use. Defaults to `1`.
- `-s` ("yes" or "no", optional): Whether to verify SSL certificate or not. Defaults to `"yes"`.
- `-v` ("true" or "false", optional): Whether to activate verbose output mode or not. Defaults to `"false"`.
- `-d` ("true" or "false", optional): Whether to activate debug output mode or not. Defaults to `"false"`. See [debug mode warning](#debug-mode) below.
- `-d` ("true" or "false", optional): Whether to activate debug output mode or not. Defaults to `"false"`. See [debug mode warning](
#debug-mode) below.
- `-t` ("true" or "false", optional): Whether to activate strict mode or not. Defaults to `"false"`. See [strict mode](#strict-mode) below.
#### Examples
@ -91,6 +94,12 @@ This is more suitable for standalone script usage.
Debug mode can be enabled through [DEBUG_MODE envvar](#with-envvars) or [-d flag](with-flags).
### Strict mode
In strict mode the script never updates an existent stack, and instead exits with an error. Following the same principle, it never removes an unexistent stack.
Strict mode can be enabled through [STRICT_MODE envvar](#with-envvars) or [-t flag](with-flags).
## License
Source code contained by this project is licensed under the [GNU General Public License version 3](https://www.gnu.org/licenses/gpl-3.0.en.html). See [LICENSE](LICENSE) file for reference.

23
psu
View File

@ -84,7 +84,7 @@ deploy () {
# If the stack does not exist
if [ -z "$STACK" ]; then
echo_verbose "Stack $PORTAINER_STACK_NAME not found."
echo_verbose "Stack $PORTAINER_STACK_NAME does not exist."
# Get Docker info
echo_verbose "Getting Docker info..."
@ -154,7 +154,11 @@ deploy () {
rm json.tmp
else
echo_verbose "Stack $PORTAINER_STACK_NAME found."
if [ $STRICT_MODE == "true" ]; then
echo "Error: Stack $PORTAINER_STACK_NAME already exists."
exit 1
fi
echo_verbose "Stack $PORTAINER_STACK_NAME exists."
echo_verbose "Preparing stack JSON..."
STACK_ID="$(echo "$STACK" | jq -j ".Id")"
@ -187,10 +191,15 @@ deploy () {
##################
undeploy () {
if [ -z "$STACK" ]; then
echo_verbose "Stack $PORTAINER_STACK_NAME not found."
exit 0
if [ $STRICT_MODE == "true" ]; then
echo "Error: Stack $PORTAINER_STACK_NAME does not exist."
exit 1
else
echo_verbose "Stack $PORTAINER_STACK_NAME does not exist. No need to undeploy it."
exit 0
fi
fi
echo_verbose "Stack $PORTAINER_STACK_NAME found."
echo_verbose "Stack $PORTAINER_STACK_NAME exists."
STACK_ID="$(echo "$STACK" | jq -j ".Id")"
echo_debug "Stack ID -> $STACK_ID"
@ -217,9 +226,10 @@ PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"}
HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"}
VERBOSE_MODE=${VERBOSE_MODE:-"false"}
DEBUG_MODE=${DEBUG_MODE:-"false"}
STRICT_MODE=${STRICT_MODE:-"false"}
# Set arguments through flags (overwrite envvars)
while getopts a:u:p:l:n:c:e:rsvd option; do
while getopts a:u:p:l:n:c:e:rsvdt option; do
case "${option}" in
a) ACTION=${OPTARG} ;;
u) PORTAINER_USER=${OPTARG} ;;
@ -232,6 +242,7 @@ while getopts a:u:p:l:n:c:e:rsvd option; do
s) HTTPIE_VERIFY_SSL="no" ;;
v) VERBOSE_MODE="true" ;;
d) DEBUG_MODE="true" ;;
t) STRICT_MODE="true" ;;
esac
done