Add custom stack environment variables

Prior to this pull request `psu` script act like this:
Stack env vars are set in the `deploy()` function. When a new stack is deployed it gets no env vars, and when an existing one is updated its envvars are reused (extracted from its stack definition into the stack_envvars variable and set back again).

For the first case this pull request load the content of the env vars file and transform it into JSON using a `jq` command and set it as the `stack_envvars` value.
For the second case, though, the script update the current stack env vars rather than setting them from scratch, keeping any value not previously set in the env file.

The environment variables file path is customizable with the environment variable `$ENVIRONMENT_VARIABLES_FILE` or the `-g` flag, like this:
```bash
export ACTION="deploy"
export PORTAINER_USER="admin"
export PORTAINER_PASSWORD="password"
export PORTAINER_URL="http://portainer.local"
export PORTAINER_STACK_NAME="mystack"
export DOCKER_COMPOSE_FILE="/path/to/docker-compose.yml"
export ENVIRONMENT_VARIABLES_FILE="/path/to/env_vars_file"

./psu
```

Or with flags:
```bash
./psu -a deploy -u admin -p password -l http://portainer.local -n mystack -c /path/to/docker-compose.yml -g /path/to/env_vars_file
```

close #7
This commit is contained in:
Tortue Torche 2019-05-10 16:33:49 +02:00 committed by GitHub
parent e446fb6b1c
commit 7c5e9e9b57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

120
psu
View File

@ -66,26 +66,27 @@ main() {
exit 1 exit 1
} }
########################## ################################
# Set globals # # Set globals #
# Globals: # # Globals: #
# ACTION # # ACTION #
# PORTAINER_USER # # PORTAINER_USER #
# PORTAINER_PASSWORD # # PORTAINER_PASSWORD #
# PORTAINER_URL # # PORTAINER_URL #
# PORTAINER_STACK_NAME # # PORTAINER_STACK_NAME #
# DOCKER_COMPOSE_FILE # # DOCKER_COMPOSE_FILE #
# PORTAINER_ENDPOINT # # ENVIRONMENT_VARIABLES_FILE #
# PORTAINER_PRUNE # # PORTAINER_ENDPOINT #
# HTTPIE_VERIFY_SSL # # PORTAINER_PRUNE #
# VERBOSE_MODE # # HTTPIE_VERIFY_SSL #
# DEBUG_MODE # # VERBOSE_MODE #
# STRICT_MODE # # DEBUG_MODE #
# Arguments: # # STRICT_MODE #
# None # # Arguments: #
# Returns: # # None #
# None # # Returns: #
########################## # None #
################################
set_globals() { set_globals() {
# Set arguments through envvars # Set arguments through envvars
ACTION=${ACTION} ACTION=${ACTION}
@ -94,6 +95,7 @@ set_globals() {
PORTAINER_URL=${PORTAINER_URL} PORTAINER_URL=${PORTAINER_URL}
PORTAINER_STACK_NAME=${PORTAINER_STACK_NAME} PORTAINER_STACK_NAME=${PORTAINER_STACK_NAME}
DOCKER_COMPOSE_FILE=${DOCKER_COMPOSE_FILE} DOCKER_COMPOSE_FILE=${DOCKER_COMPOSE_FILE}
ENVIRONMENT_VARIABLES_FILE=${ENVIRONMENT_VARIABLES_FILE}
PORTAINER_ENDPOINT=${PORTAINER_ENDPOINT:-"1"} PORTAINER_ENDPOINT=${PORTAINER_ENDPOINT:-"1"}
PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"} PORTAINER_PRUNE=${PORTAINER_PRUNE:-"false"}
HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"} HTTPIE_VERIFY_SSL=${HTTPIE_VERIFY_SSL:-"yes"}
@ -102,7 +104,7 @@ set_globals() {
STRICT_MODE=${STRICT_MODE:-"false"} STRICT_MODE=${STRICT_MODE:-"false"}
# Set arguments through flags (overwrite envvars) # Set arguments through flags (overwrite envvars)
while getopts a:u:p:l:n:c:e:rsvdt option; do while getopts a:u:p:l:n:c:e:g:rsvdt option; do
case "${option}" in case "${option}" in
a) ACTION=${OPTARG} ;; a) ACTION=${OPTARG} ;;
u) PORTAINER_USER=${OPTARG} ;; u) PORTAINER_USER=${OPTARG} ;;
@ -111,6 +113,7 @@ set_globals() {
n) PORTAINER_STACK_NAME=${OPTARG} ;; n) PORTAINER_STACK_NAME=${OPTARG} ;;
c) DOCKER_COMPOSE_FILE=${OPTARG} ;; c) DOCKER_COMPOSE_FILE=${OPTARG} ;;
e) PORTAINER_ENDPOINT=${OPTARG} ;; e) PORTAINER_ENDPOINT=${OPTARG} ;;
g) ENVIRONMENT_VARIABLES_FILE=${OPTARG} ;;
r) PORTAINER_PRUNE="true" ;; r) PORTAINER_PRUNE="true" ;;
s) HTTPIE_VERIFY_SSL="no" ;; s) HTTPIE_VERIFY_SSL="no" ;;
v) VERBOSE_MODE="true" ;; v) VERBOSE_MODE="true" ;;
@ -130,6 +133,7 @@ set_globals() {
echo_debug "PORTAINER_URL -> $PORTAINER_URL" echo_debug "PORTAINER_URL -> $PORTAINER_URL"
echo_debug "PORTAINER_STACK_NAME -> $PORTAINER_STACK_NAME" echo_debug "PORTAINER_STACK_NAME -> $PORTAINER_STACK_NAME"
echo_debug "DOCKER_COMPOSE_FILE -> $DOCKER_COMPOSE_FILE" echo_debug "DOCKER_COMPOSE_FILE -> $DOCKER_COMPOSE_FILE"
echo_debug "ENVIRONMENT_VARIABLES_FILE -> $ENVIRONMENT_VARIABLES_FILE"
echo_debug "PORTAINER_ENDPOINT -> $PORTAINER_ENDPOINT" echo_debug "PORTAINER_ENDPOINT -> $PORTAINER_ENDPOINT"
echo_debug "PORTAINER_PRUNE -> $PORTAINER_PRUNE" echo_debug "PORTAINER_PRUNE -> $PORTAINER_PRUNE"
echo_debug "HTTPIE_VERIFY_SSL -> $HTTPIE_VERIFY_SSL" echo_debug "HTTPIE_VERIFY_SSL -> $HTTPIE_VERIFY_SSL"
@ -145,6 +149,10 @@ set_globals() {
check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_NAME" "n" check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_NAME" "n"
if [ $ACTION == "deploy" ]; then if [ $ACTION == "deploy" ]; then
check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c" check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c"
if [ -n "$ENVIRONMENT_VARIABLES_FILE" ] && [[ ! -f "$ENVIRONMENT_VARIABLES_FILE" ]]; then
echo_error "Error: File path \"$ENVIRONMENT_VARIABLES_FILE\" not found for \"ENVIRONMENT_VARIABLES_FILE\" environment variable or the \"-g\" flag."
exit 1
fi
fi fi
} }
@ -254,21 +262,22 @@ echo_debug() {
fi fi
} }
########################## ################################
# Create/update a stack # # Create/update a stack #
# Globals: # # Globals: #
# STACK # # STACK #
# DOCKER_COMPOSE_FILE # # DOCKER_COMPOSE_FILE #
# PORTAINER_STACK_NAME # # PORTAINER_STACK_NAME #
# PORTAINER_URL # # PORTAINER_URL #
# HTTPIE_VERIFY_SSL # # ENVIRONMENT_VARIABLES_FILE #
# PORTAINER_ENDPOINT # # HTTPIE_VERIFY_SSL #
# AUTH_TOKEN # # PORTAINER_ENDPOINT #
# Arguments: # # AUTH_TOKEN #
# None # # Arguments: #
# Returns: # # None #
# None # # Returns: #
########################## # None #
################################
deploy() { deploy() {
# Read docker-compose file content # Read docker-compose file content
local docker_compose_file_content local docker_compose_file_content
@ -304,14 +313,19 @@ deploy() {
local swarm_id local swarm_id
swarm_id=$(echo $docker_info | jq -r ".Swarm.Cluster.ID // empty") swarm_id=$(echo $docker_info | jq -r ".Swarm.Cluster.ID // empty")
echo_debug "Swarm ID -> $swarm_id" echo_debug "Swarm ID -> $swarm_id"
# If there is no swarm ID # If there is no swarm ID
if [ -z "$swarm_id" ];then if [ -z "$swarm_id" ];then
echo_verbose "Swarm cluster not found." echo_verbose "Swarm cluster not found."
echo_verbose "Preparing stack JSON..." echo_verbose "Preparing stack JSON..."
local stack_envvars
stack_envvars="[]"
if [ -n "$ENVIRONMENT_VARIABLES_FILE" ]; then
stack_envvars=$(env_file_to_json)
fi
local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"StackFileContent\":\"" local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"StackFileContent\":\""
local data_suffix="\"}" local data_suffix="\",\"Env\":"$stack_envvars"}"
echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $(echo $data_prefix$docker_compose_file_content$data_suffix | jq -C .)" echo_debug "Stack JSON -> $(echo $data_prefix$docker_compose_file_content$data_suffix | jq -C .)"
@ -335,8 +349,13 @@ deploy() {
echo_verbose "Swarm cluster found." echo_verbose "Swarm cluster found."
echo_verbose "Preparing stack JSON..." echo_verbose "Preparing stack JSON..."
local stack_envvars
stack_envvars="[]"
if [ -n "$ENVIRONMENT_VARIABLES_FILE" ]; then
stack_envvars=$(env_file_to_json)
fi
local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"SwarmID\":\"$swarm_id\",\"StackFileContent\":\"" local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"SwarmID\":\"$swarm_id\",\"StackFileContent\":\""
local data_suffix="\"}" local data_suffix="\",\"Env\":"$stack_envvars"}"
echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $(echo $data_prefix$docker_compose_file_content$data_suffix | jq -C .)" echo_debug "Stack JSON -> $(echo $data_prefix$docker_compose_file_content$data_suffix | jq -C .)"
@ -370,12 +389,16 @@ deploy() {
local stack_id local stack_id
stack_id="$(echo "$STACK" | jq -j ".Id")" stack_id="$(echo "$STACK" | jq -j ".Id")"
local stack_envvars local stack_envvars
stack_envvars="$(echo -n "$STACK"| jq ".Env" -jc)" stack_envvars="$(echo -n "$STACK" | jq ".Env" -jc)"
if [ -n "$ENVIRONMENT_VARIABLES_FILE" ]; then
new_stack_envvars=$(env_file_to_json)
stack_envvars="$(echo -n "${new_stack_envvars}${stack_envvars}" | jq -sjc 'add | unique_by(.name)')"
fi
local data_prefix="{\"Id\":\"$stack_id\",\"StackFileContent\":\"" local data_prefix="{\"Id\":\"$stack_id\",\"StackFileContent\":\""
local data_suffix="\",\"Env\":"$stack_envvars",\"Prune\":$PORTAINER_PRUNE}" local data_suffix="\",\"Env\":"$stack_envvars",\"Prune\":$PORTAINER_PRUNE}"
echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $(echo $data_prefix$docker_compose_file_content$data_suffix | jq -C .)" echo_debug "Stack JSON -> $(echo $data_prefix$docker_compose_file_content$data_suffix | jq -C .)"
# Update stack # Update stack
echo_verbose "Updating stack $PORTAINER_STACK_NAME..." echo_verbose "Updating stack $PORTAINER_STACK_NAME..."
local update local update
@ -390,7 +413,7 @@ deploy() {
@json.tmp) @json.tmp)
check_for_errors $? "$update" check_for_errors $? "$update"
echo_debug "Update action response -> $(echo $update | jq -C .)" echo_debug "Update action response -> $(echo $update | jq -C .)"
rm json.tmp rm json.tmp
fi fi
} }
@ -436,4 +459,17 @@ undeploy() {
echo_debug "Delete action response -> $(echo $delete | jq -C .)" echo_debug "Delete action response -> $(echo $delete | jq -C .)"
} }
###################################################
# Convert environment variables from file to JSON #
# Globals: #
# ENVIRONMENT_VARIABLES_FILE #
# Arguments: #
# None #
# Returns: #
# JSON string #
###################################################
env_file_to_json() {
echo "$(env -i $(cat $ENVIRONMENT_VARIABLES_FILE) jq -n 'env | to_entries | map({name: .key, value: .value})')"
}
main "$@" main "$@"