forked from hughfletcher/dokku-mysql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands
executable file
·160 lines (148 loc) · 5.02 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
set -e;
DOCKER_VOLUMES="/var/lib/docker/vfs/dir"
# Check if name is specified
if [[ $1 == mysql:* ]]; 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
fi
case "$1" in
mysql:create)
DB_IMAGE=mysql/$APP
# Check if DB container is installed
IMAGE=$(docker images | grep "hughfletcher/mysql " | awk '{print $3}')
if [[ -z $IMAGE ]]; then
echo "MySql image not found... Did you run 'dokku plugins-install' ?"
exit 1
fi
# Check if an existing DB volume exists
if [[ -f "$DOKKU_ROOT/.mysql/volume_$APP" ]]; then
VOLUME="`cat $DOKKU_ROOT/.mysql/volume_$APP`:/var/lib/mysql"
echo
echo "-----> Reusing mysql/$APP database"
else
VOLUME="/var/lib/mysql"
# Generate a random password for DB user
DB_PASS=$(< /dev/urandom tr -dc A-Za-z0-9 | head -c 16)
# DB_NAME=$(echo "www.atomic24.com" | sed -e 's/\.//g')
# DB_USER='user'$RANDOM
# Temporarily store it to a file
if [[ ! -d $DOKKU_ROOT/.mysql ]]; then
mkdir -p $DOKKU_ROOT/.mysql
chown -R dokku: $DOKKU_ROOT/.mysql
fi
echo $DB_PASS > "$DOKKU_ROOT/.mysql/pass_$APP"
chown dokku: "$DOKKU_ROOT/.mysql/pass_$APP"
fi
# Fork DB image
ID=$(docker run -d hughfletcher/mysql /bin/bash "exit 0")
docker wait $ID > /dev/null
IMAGE=$(docker commit $ID)
docker tag $IMAGE $DB_IMAGE
# Launch container
ID=$(docker run -v $VOLUME -d $DB_IMAGE /usr/sbin/mysqld)
if [[ ! -f "$DOKKU_ROOT/.mysql/volume_$APP" ]]; then
IP=$(docker inspect $ID | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
sleep 4
docker run $DB_IMAGE mysqladmin -h $IP -u admin -p'mysql-server' password $DB_PASS
docker run $DB_IMAGE mysqladmin -h $IP -u admin -p$DB_PASS create 'db'
# docker run mysql/create mysql -h $IP -u admin -p'newpass' -e "RENAME USER admin@'%' TO $DB_USER@'%';"
# Set volume path reference
VOLUME_PATH=$(docker inspect $ID | grep /var/lib/docker/vfs/dir/ | awk '{print $2}' | sed -e's/"//g')
echo $VOLUME_PATH > "$DOKKU_ROOT/.mysql/volume_$APP"
sleep 1
fi
# Link to a potential existing app
dokku mysql:link $APP $APP
echo
echo "-----> MySql container created: $DB_IMAGE"
sleep 1
dokku mysql:info $APP
;;
mysql:delete)
DB_IMAGE=mysql/$APP
# Stop the container
ID=$(docker ps -a | grep "$DB_IMAGE":latest | awk '{print $1}')
if [[ ! -z $ID ]]; then
docker stop $ID
fi
# Remove image
IMAGE=$(docker images | grep "$DB_IMAGE " | awk '{print $1}')
if [[ ! -z $IMAGE ]]; then
docker rmi $IMAGE
fi
# Remove persistent volume
if [[ -f "$DOKKU_ROOT/.mysql/volume_$APP" ]]; then
rm -rf "`cat "$DOKKU_ROOT/.mysql/volume_$APP"`"
fi
echo
echo "-----> MySql container deleted: $DB_IMAGE"
;;
mysql:info)
DB_IMAGE=mysql/$APP
ID=$(docker ps -a | grep "$DB_IMAGE":latest | awk '{print $1}')
IP=`echo $(docker inspect -format="{{ .NetworkSettings.IPAddress }}" $ID)`
echo "$ID"
# PORT=$(docker port $ID 5432)
echo
echo " Host: $IP"
echo " User: 'admin'"
if [[ -f "$DOKKU_ROOT/.mysql/pass_$APP" ]]; then
echo " Password: '$(cat "$DOKKU_ROOT/.mysql/pass_$APP")'"
fi
echo " Database: 'db'"
# echo " Public port: $PORT"
echo " Internal port: 3306"
echo
;;
mysql:link)
if $APP_EXISTS; then
# Check argument
if [[ -z $3 ]]; then
echo "You must specify a database name"
exit 1
fi
DB_IMAGE="mysql/$3"
# Check temporarily file
if [[ ! -f "$DOKKU_ROOT/.mysql/pass_$APP" ]]; then
echo "Database is already linked"
exit 1
fi
DB_PASSWORD=$(cat "$DOKKU_ROOT/.mysql/pass_$APP")
ID=$(docker ps -a | grep "$DB_IMAGE":latest | awk '{print $1}')
IP=`echo $(docker inspect -format="{{ .NetworkSettings.IPAddress }}" $ID)`
dokku config:set $APP "DATABASE_URL=mysql2://admin:$DB_PASSWORD@$IP/db"
echo
echo "-----> $APP linked to $DB_IMAGE database"
rm "$DOKKU_ROOT/.mysql/pass_$APP"
fi
;;
mysql:logs)
DB_IMAGE=mysql/$APP
ID=$(docker ps -a | grep "$DB_IMAGE" | awk '{print $1}')
docker logs $ID | tail -n 100
;;
mysql-tests)
echo ">>> Running Tests"
/usr/bin/shunit2 /var/lib/dokku/plugins/mysql/tests/commands
;;
help)
cat && cat<<EOF
mysql:create <app> Create a MySql container
mysql:delete <app> Delete specified MySql container
mysql:info <app> Display database informations
mysql:link <app> <db> Link an app to a MySql database
mysql:logs <app> Display last logs from MySql container
EOF
;;
esac