forked from jezdez/dokku-redis-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands
executable file
·128 lines (112 loc) · 3.72 KB
/
commands
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
set -e;
# Check if name is specified
if [[ $1 == redis:* ]]; then
if [[ -z $2 ]]; then
echo "You must specify an app name"
exit 1
else
APP="$2"
# Check if app exists with the same name
if [[ -d "$DOKKU_ROOT/$APP" ]]; then
APP_EXISTS=true
else
APP_EXISTS=false
fi
fi
if [[ ! -d "$PLUGIN_PATH/link" ]]; then
echo "Link plugin not found... Did you install it from https://github.com/rlaneve/dokku-link?"
exit 1
fi
PLUGIN_NAME="redis"
CONTAINER_NAME="${PLUGIN_NAME}_$APP"
HOST_DIR="$DOKKU_ROOT/$APP/$PLUGIN_NAME"
ENVVAR_NAME="REDIS_URL"
fi
case "$1" in
redis:create)
# Check if Redis container is installed
IMAGE=$(docker images | grep "jezdez/redis" | awk '{print $3}')
if [[ -z $IMAGE ]]; then
echo "Redis image not found... Did you run 'dokku plugins-install' ?"
exit 1
fi
# Stop existing container with the same persistent Redis
ID=$(docker ps | grep "$CONTAINER_NAME" | awk '{print $1}')
if [[ ! -z "$ID" ]]; then
docker stop $ID > /dev/null
fi
# Check if an existing DB volume exists
if [[ -d $HOST_DIR ]]; then
echo "-----> Reusing $CONTAINER_NAME data"
else
mkdir -p $HOST_DIR
chown -R dokku:dokku $HOST_DIR
fi
VOLUME="$HOST_DIR:/opt/redis"
# Launch container
docker run -v $VOLUME -name=$CONTAINER_NAME -d jezdez/redis \
/usr/bin/redis-server /etc/redis/redis.conf
# Link to a potential existing app
dokku redis:link $APP $APP
echo "-----> Redis container created: $CONTAINER_NAME"
sleep 1
dokku redis:info $APP
;;
redis:delete)
# Stop the container
ID=$(docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}')
if [[ ! -z $ID ]]; then
docker stop $ID > /dev/null
docker rm $ID > /dev/null
fi
# Remove persistent volume
if [[ -d $HOST_DIR ]]; then
rm -rf $HOST_DIR
fi
if $APP_EXISTS; then
# unlink this container as "redis"
dokku link:delete "$APP" "$CONTAINER_NAME" "$PLUGIN_NAME"
dokku config:unset "$APP" $ENVVAR_NAME
fi
echo "-----> Redis container deleted: $CONTAINER_NAME"
;;
redis:info)
ID=$(docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}')
IP=$(docker inspect $ID | grep IPAddress | cut -d '"' -f 4)
echo
echo " Host: ${IP}"
echo " Private port: 6379"
echo
;;
redis:link)
if $APP_EXISTS; then
# Check argument
if [[ -z $3 ]]; then
echo "You must specify a container name"
exit 1
fi
CONTAINER_NAME="${PLUGIN_NAME}_$3"
# link this container as "redis"
dokku link:create "$APP" "$CONTAINER_NAME" "$PLUGIN_NAME"
# figure out IP to set env var
ID=$(docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}')
IP=$(docker inspect $ID | grep IPAddress | cut -d '"' -f 4)
dokku config:set "$APP" $ENVVAR_NAME="redis://\$REDIS_PORT_6379_TCP_ADDR:\$REDIS_PORT_6379_TCP_PORT"
echo "-----> $APP linked to $CONTAINER_NAME container"
fi
;;
redis:logs)
ID=$(docker ps -a | grep "$CONTAINER_NAME" | awk '{print $1}')
docker logs $ID | tail -n 100
;;
help)
cat && cat<<EOF
redis:create <app> Create a Redis container
redis:delete <app> Delete specified Redis container
redis:info <app> Display container informations
redis:link <app> <container> Link an app to a Redis container
redis:logs <app> Display last logs from Redis container
EOF
;;
esac