Change some global variables to local

This commit is contained in:
Juan Carlos Mejías Rodríguez 2018-11-25 21:58:57 -05:00
parent 45c6f1101d
commit e224207e31

78
psu
View File

@ -221,16 +221,16 @@ echo_debug() {
########################## ##########################
deploy() { deploy() {
# Read docker-compose file content # Read docker-compose file content
STACK_YAML_CONTENT=$(cat "$DOCKER_COMPOSE_FILE") local docker_compose_file_content=$(cat "$DOCKER_COMPOSE_FILE")
# Remove carriage returns # Remove carriage returns
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\r'/''}" docker_compose_file_content="${docker_compose_file_content//$'\r'/''}"
# Escape double quotes # Escape double quotes
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'"'/'\"'}" docker_compose_file_content="${docker_compose_file_content//$'"'/'\"'}"
# Escape newlines # Escape newlines
STACK_YAML_CONTENT="${STACK_YAML_CONTENT//$'\n'/'\n'}" docker_compose_file_content="${docker_compose_file_content//$'\n'/'\n'}"
# If the stack does not exist # If the stack does not exist
if [ -z "$STACK" ]; then if [ -z "$STACK" ]; then
@ -238,33 +238,33 @@ deploy() {
# Get Docker info # Get Docker info
echo_verbose "Getting Docker info..." echo_verbose "Getting Docker info..."
DOCKER_INFO=$(http \ local docker_info=$(http \
--check-status \ --check-status \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
"$PORTAINER_URL/api/endpoints/$PORTAINER_ENDPOINT/docker/info" \ "$PORTAINER_URL/api/endpoints/$PORTAINER_ENDPOINT/docker/info" \
"Authorization: Bearer $AUTH_TOKEN") "Authorization: Bearer $AUTH_TOKEN")
check_for_errors $? "$DOCKER_INFO" check_for_errors $? "$docker_info"
echo_debug "Docker info -> $DOCKER_INFO" echo_debug "Docker info -> $docker_info"
# Get Docker swarm ID # Get Docker swarm ID
echo_verbose "Getting swarm cluster (if any)..." echo_verbose "Getting swarm cluster (if any)..."
SWARM_ID=$(echo $DOCKER_INFO | jq -r ".Swarm.Cluster.ID // empty") local 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..."
DATA_PREFIX="{\"Name\":\"$PORTAINER_STACK_NAME\",\"StackFileContent\":\"" local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"StackFileContent\":\""
DATA_SUFFIX="\"}" local data_suffix="\"}"
echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" echo_debug "Stack JSON -> $data_prefix$docker_compose_file_content$data_suffix"
# Create stack for single Docker instance # Create stack for single Docker instance
echo_verbose "Creating stack $PORTAINER_STACK_NAME..." echo_verbose "Creating stack $PORTAINER_STACK_NAME..."
CREATE=$(http \ local create=$(http \
--check-status \ --check-status \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
@ -275,19 +275,19 @@ deploy() {
method==string \ method==string \
endpointId==$PORTAINER_ENDPOINT \ endpointId==$PORTAINER_ENDPOINT \
@json.tmp) @json.tmp)
echo_debug "Create action response -> $CREATE" echo_debug "Create action response -> $create"
else else
echo_verbose "Swarm cluster found." echo_verbose "Swarm cluster found."
echo_verbose "Preparing stack JSON..." echo_verbose "Preparing stack JSON..."
DATA_PREFIX="{\"Name\":\"$PORTAINER_STACK_NAME\",\"SwarmID\":\"$SWARM_ID\",\"StackFileContent\":\"" local data_prefix="{\"Name\":\"$PORTAINER_STACK_NAME\",\"SwarmID\":\"$swarm_id\",\"StackFileContent\":\""
DATA_SUFFIX="\"}" local data_suffix="\"}"
echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" echo_debug "Stack JSON -> $data_prefix$docker_compose_file_content$data_suffix"
# Create stack for Docker swarm # Create stack for Docker swarm
echo_verbose "Creating stack $PORTAINER_STACK_NAME..." echo_verbose "Creating stack $PORTAINER_STACK_NAME..."
CREATE=$(http \ local create=$(http \
--check-status \ --check-status \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
@ -298,9 +298,9 @@ deploy() {
method==string \ method==string \
endpointId==$PORTAINER_ENDPOINT \ endpointId==$PORTAINER_ENDPOINT \
@json.tmp) @json.tmp)
echo_debug "Create action response -> $CREATE" echo_debug "Create action response -> $create"
fi fi
check_for_errors $? "$CREATE" check_for_errors $? "$create"
rm json.tmp rm json.tmp
else else
@ -311,26 +311,26 @@ deploy() {
echo_verbose "Stack $PORTAINER_STACK_NAME exists." echo_verbose "Stack $PORTAINER_STACK_NAME exists."
echo_verbose "Preparing stack JSON..." echo_verbose "Preparing stack JSON..."
STACK_ID="$(echo "$STACK" | jq -j ".Id")" local stack_id="$(echo "$STACK" | jq -j ".Id")"
STACK_ENV_VARS="$(echo -n "$STACK"| jq ".Env" -jc)" local stack_envvars="$(echo -n "$STACK"| jq ".Env" -jc)"
DATA_PREFIX="{\"Id\":\"$STACK_ID\",\"StackFileContent\":\"" local data_prefix="{\"Id\":\"$stack_id\",\"StackFileContent\":\""
DATA_SUFFIX="\",\"Env\":"$STACK_ENV_VARS",\"Prune\":$PORTAINER_PRUNE}" local data_suffix="\",\"Env\":"$stack_envvars",\"Prune\":$PORTAINER_PRUNE}"
echo "$DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" > json.tmp echo "$data_prefix$docker_compose_file_content$data_suffix" > json.tmp
echo_debug "Stack JSON -> $DATA_PREFIX$STACK_YAML_CONTENT$DATA_SUFFIX" echo_debug "Stack JSON -> $data_prefix$docker_compose_file_content$data_suffix"
# Update stack # Update stack
echo_verbose "Updating stack $PORTAINER_STACK_NAME..." echo_verbose "Updating stack $PORTAINER_STACK_NAME..."
UPDATE=$(http \ local update=$(http \
--check-status \ --check-status \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
--timeout=300 \ --timeout=300 \
PUT "$PORTAINER_URL/api/stacks/$STACK_ID" \ PUT "$PORTAINER_URL/api/stacks/$stack_id" \
"Authorization: Bearer $AUTH_TOKEN" \ "Authorization: Bearer $AUTH_TOKEN" \
endpointId==$PORTAINER_ENDPOINT \ endpointId==$PORTAINER_ENDPOINT \
@json.tmp) @json.tmp)
echo_debug "Update action response -> $UPDATE" echo_debug "Update action response -> $update"
check_for_errors $? "$UPDATE" check_for_errors $? "$update"
rm json.tmp rm json.tmp
fi fi
@ -360,17 +360,17 @@ undeploy() {
fi fi
echo_verbose "Stack $PORTAINER_STACK_NAME exists." echo_verbose "Stack $PORTAINER_STACK_NAME exists."
STACK_ID="$(echo "$STACK" | jq -j ".Id")" local stack_id="$(echo "$STACK" | jq -j ".Id")"
echo_debug "Stack ID -> $STACK_ID" echo_debug "Stack ID -> $stack_id"
echo_verbose "Deleting stack $PORTAINER_STACK_NAME..." echo_verbose "Deleting stack $PORTAINER_STACK_NAME..."
DELETE=$(http \ local delete=$(http \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
DELETE "$PORTAINER_URL/api/stacks/$STACK_ID" \ DELETE "$PORTAINER_URL/api/stacks/$stack_id" \
"Authorization: Bearer $AUTH_TOKEN") "Authorization: Bearer $AUTH_TOKEN")
echo_debug "Delete action response -> $UPDATE" echo_debug "Delete action response -> $delete"
check_for_errors $? "$DELETE" check_for_errors $? "$delete"
} }
main "$@" main "$@"