Skip to content

Instantly share code, notes, and snippets.

@vugluskr86
Created February 13, 2019 14:41
Show Gist options
  • Save vugluskr86/f1461817ba6d23cb70cd5c42fe210eff to your computer and use it in GitHub Desktop.
Save vugluskr86/f1461817ba6d23cb70cd5c42fe210eff to your computer and use it in GitHub Desktop.
Portainer and docker utils (recreate container in stack)
#!/bin/bash -x
#
# Manage docker with portainer api
set -x
STACK_NAME="scriptdev"
SERVICE_NAME="runner"
ENDPOINT_ID=3
PORTAINER_URL=""
PORTAINER_USER="admin"
PORTAINER_PASSWORD=""
#######################################
# Get value from JSON object with string
# Globals:
# None
# Arguments:
# $1 JSON string
# $2 Field name string key
# Returns:
# None
#######################################
function jsonval {
local temp=`echo $1 | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $2`
echo ${temp##*|}
}
#######################################
# Auth with portainer api and get JWT key
# Globals:
# None
# Arguments:
# $1 Portainer api url
# $2 Login username
# $3 Password
# Returns:
# $4 JWT token string
#######################################
function p_get_jwt_auth {
local __resultvar=$4
local json_jwt=`curl -s -H 'Content-Type: application/json' -d '{ "username": "'$2'", "password": "'$3'" }' -X POST $1/api/auth --http1.1`
local ljwt=`jsonval $json_jwt jwt`
eval $__resultvar="$ljwt"
}
#######################################
# Call docker api
# Globals:
# None
# Arguments:
# $1 Portainer api url
# $2 HTTP method
# $3 Login username
# $4 Password
# $5 Endpoint ID
# $6 Command url string
# $7 POST data
# Returns:
# $8 Call command result string
#######################################
function p_call_docker_api {
local __resultvar=$8
p_get_jwt_auth $1 $3 $4 jwt
local http_res=`curl -s -H "Authorization: Bearer $jwt" -H "Content-type:application/json" -H "Accept: application/json" -X $2 $7 $1/api/endpoints/$5/docker/$6 --http1.1`
eval $__resultvar="$http_res"
}
#######################################
# Get docker container id by name
# Globals:
# None
# Arguments:
# $1 Portainer api url
# $2 Login username
# $3 Password
# $4 Endpoint ID
# $5 Stack name
# $6 Service name
# $7 Container index
# Returns:
# $7 Call command result string
#######################################
function p_get_container_id {
local __resultvar=$8
local container_name="$5"_"$6"_"$7"
p_call_docker_api $1 GET $2 $3 $4 containers/$container_name/json "" result
if [[ $result ]]; then
local id_pair=`jsonval $result Id`
eval $__resultvar=${id_pair/#Id:}
fi
}
# p_get_container_id $PORTAINER_URL $PORTAINER_USER $PORTAINER_PASSWORD $ENDPOINT_ID $STACK_NAME $SERVICE_NAME 1 CONTAINER_ID
# echo $CONTAINER_ID
# p_call_docker_api $PORTAINER_URL DELETE $PORTAINER_USER $PORTAINER_PASSWORD $ENDPOINT_ID containers/$CONTAINER_ID?force=true '' result1
# echo $result1
#container_name="$STACK_NAME"_"$SERVICE_NAME"_1
#container_name_encoded=registry.gimaym.com/gimaymscript:develop
#p_call_docker_api $PORTAINER_URL POST $PORTAINER_USER $PORTAINER_PASSWORD $ENDPOINT_ID containers/create?name=$container_name '-d {"Image":"'$container_name_encoded'","Labels":{"com.docker.compose.config-hash":"3ed8ea3534e86ba20da06771030c2815f1e3a2b7","com.docker.compo
#echo $result2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment