-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportainer-service.sh
executable file
·112 lines (95 loc) · 4.28 KB
/
portainer-service.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
_CURRENT_FILE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_CURRENT_RUNNING_DIR="$( cd "$( dirname "." )" && pwd )"
STELLA_APP_PROPERTIES_FILENAME="portainer-service.properties"
. $_CURRENT_FILE_DIR/stella-link.sh include
# SERVICE INFO --------------------------------------
DEFAULT_PORT=20000
DEFAULT_SERVICE_NAME="portainer-service"
# DOCKER IMAGES INFO --------------------------------------
DEFAULT_DOCKER_IMAGE="portainer/portainer"
DEFAULT_DOCKER_IMAGE_VERSION="latest"
# USAGE --------------------------------------
function usage() {
echo "USAGE :"
echo "portainer service as a docker container for managing container"
echo "NOTE : require docker on your system"
echo "----------------"
echo "o-- command :"
echo "L create [--version=<version>] [--port=<port>] [--nopurge] [-- additional docker run options] : create & launch service (must be use once before starting/stopping service)"
echo "L start : start service"
echo "L stop : stop service"
echo "L destroy [--version=<version>] [--nopurge] : stop, delete service and all image files. At next create, everything will be forced to be downloaded."
echo "L status : give service status info"
echo "L shell : launch a shell inside running service"
echo "L logs : show logs"
echo "L purgedata : erase any internal data volume attached to the service and/or folder storing data on host"
echo "o-- options :"
echo "L --port : portainer listening port"
echo "L --version : portainer image version"
echo "L --debug : active some debug trace"
echo "L --nopurge : do not erase any internal data volume attached to service while create/destroy"
}
# COMMAND LINE -----------------------------------------------------------------------------------
PARAMETERS="
ACTION= '' a 'create start stop status shell destroy purgedata logs' '1' Action.
"
OPTIONS="
PORT='$DEFAULT_PORT' '' 'string' s 0 '' Listening port.
VERSION='$DEFAULT_DOCKER_IMAGE_VERSION' 'v' 'string' s 0 '' portainer image version .
DEBUG='' 'd' '' b 0 '1' Active some debug trace.
NOPURGE='' 'n' '' b 0 '1' Do not erase any internal data volume attached to service while create/destroy.
"
$STELLA_API argparse "$0" "$OPTIONS" "$PARAMETERS" "$STELLA_APP_NAME" "$(usage)" "EXTRA_ARG DOCKERARG" "$@"
# ------------- COMPUTE ARGUMENTS AND VALUES -------------------------
DOCKER_IMAGE_VERSION=$VERSION
DOCKER_URI=$DEFAULT_DOCKER_IMAGE
[ ! -z "$DOCKER_IMAGE_VERSION" ] && DOCKER_URI=$DOCKER_URI:$DOCKER_IMAGE_VERSION
SERVICE_NAME=$DEFAULT_SERVICE_NAME
SERVICE_DATA_NAME="$SERVICE_NAME"
# test docker client is installed in this system
$STELLA_API require "docker" "docker" "SYSTEM"
# FUNCTIONS --------------------------------------
__log_run() {
[ "$DEBUG" = "1" ] && echo ">" $@
"$@"
}
# ------------- ACTIONS -------------------------
if [ "$ACTION" = "create" ]; then
# delete previously stored container
__log_run docker stop $SERVICE_NAME 2>/dev/null
__log_run docker rm $SERVICE_NAME 2>/dev/null
[ ! "${NOPURGE}" = "1" ] && __log_run docker volume rm $SERVICE_DATA_NAME 2>/dev/null
__log_run docker run -d \
--name $SERVICE_NAME \
--restart always \
-p $PORT:9000 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $SERVICE_DATA_NAME:/data \
$DOCKERARG $DOCKER_URI
fi
if [ "$ACTION" = "destroy" ]; then
__log_run docker stop $SERVICE_NAME 2>/dev/null
__log_run docker rm $SERVICE_NAME 2>/dev/null
__log_run docker rmi $DOCKER_URI 2>/dev/null
[ ! "${NOPURGE}" = "1" ] && __log_run docker volume rm $SERVICE_DATA_NAME 2>/dev/null
fi
if [ "$ACTION" = "start" ]; then
__log_run docker start $SERVICE_NAME
fi
if [ "$ACTION" = "stop" ]; then
__log_run docker stop $SERVICE_NAME
fi
if [ "$ACTION" = "status" ]; then
__log_run docker ps -a | grep $SERVICE_NAME
__log_run docker stats --no-stream $SERVICE_NAME
fi
if [ "$ACTION" = "shell" ]; then
__log_run docker exec -it $SERVICE_NAME sh
fi
if [ "$ACTION" = "purgedata" ]; then
__log_run docker volume rm $SERVICE_DATA_NAME 2>/dev/null
fi
if [ "$ACTION" = "logs" ]; then
__log_run docker logs $SERVICE_NAME
fi