-
Notifications
You must be signed in to change notification settings - Fork 1
/
drupalizer
executable file
·174 lines (160 loc) · 3.94 KB
/
drupalizer
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
set -eu
export THIS_CWD=$(pwd)
# THIS_NAME should be all lower case
: ${THIS_NAME:='ddd'}
: ${DB_IMAGE:='mysql:5.7'}
: ${DRUPAL_IMAGE:='drupal:9.3-php8.1-fpm-alpine'}
: ${DRUPAL_PORT:=8080}
: ${COMPOSE_FILE:=docker-compose.yml}
: ${DB_NAME:=ddddb}
: ${DB_USER:=ddduser}
: ${DB_PASSWORD:=dddpass}
: ${ROOT_MYSQLPASS:=dddrootpass}
init_db () {
mkdir -p .tmp/db
INIT_TMP=$(mktemp -d)
mkdir $INIT_TMP/${THIS_NAME}
cp desiredresult.gz $INIT_TMP/${THIS_NAME}/
cd $INIT_TMP/${THIS_NAME}
gunzip desiredresult.gz
# Initialize the database
# Break indentation until the EOF
cat << EOF > db_create.sql
CREATE DATABASE $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON $DB_NAME.* TO '$DB_USER'@'%' IDENTIFIED BY '$DB_PASSWORD';
EOF
cat << EOF > $COMPOSE_FILE
version: '3.1'
services:
db:
image: $DB_IMAGE
restart: always
environment:
MYSQL_ROOT_PASSWORD: '$ROOT_MYSQLPASS'
volumes:
- type: bind
source: $THIS_CWD/.tmp/db
target: /var/lib/mysql
volumes:
db:
EOF
# End indentation break
docker pull $DB_IMAGE
docker-compose up -d --remove-orphans
THIS_RESULT=1
echo -n mysql is still starting.
until [[ $THIS_RESULT == 0 ]]
do
set +e
docker exec -it ddd-db-1 mysqladmin ping -p"$ROOT_MYSQLPASS" > $INIT_TMP/${THIS_NAME}/lastresult
# This is a usefule debug step and how I grabbed the desiredresult
#cp $INIT_TMP/${THIS_NAME}/lastresult /tmp/lastresult
diff $INIT_TMP/${THIS_NAME}/desiredresult $INIT_TMP/${THIS_NAME}/lastresult &> /dev/null
THIS_RESULT=$?
set -e
if [[ $THIS_RESULT == 0 ]]; then
echo '.'
break
else
echo -n '.'
sleep 1
fi
done
docker exec -i ddd-db-1 mysql -uroot -p"$ROOT_MYSQLPASS" < db_create.sql
#sleep 1
docker-compose down
cd
#rm -Rf $INIT_TMP/${THIS_NAME}
rm $INIT_TMP/${THIS_NAME}/$COMPOSE_FILE
rm $INIT_TMP/${THIS_NAME}/lastresult
rm $INIT_TMP/${THIS_NAME}/desiredresult
rm $INIT_TMP/${THIS_NAME}/db_create.sql
ls -alh $INIT_TMP/${THIS_NAME}
rmdir $INIT_TMP/${THIS_NAME}
rmdir $INIT_TMP
}
init_dir () {
INITDIR_TMP=$(mktemp -d)
mkdir $INITDIR_TMP/${THIS_NAME}
cd $INITDIR_TMP/${THIS_NAME}
# Break indentation until the EOF
cat << EOF > $COMPOSE_FILE
version: '3.1'
services:
drupal:
image: $DRUPAL_IMAGE
restart: always
ports:
- $DRUPAL_PORT:80
EOF
# End indentation break
docker-compose up -d --remove-orphans
echo here
#copy the drupal web directory out
docker cp ddd-drupal-1:/opt/drupal/web $THIS_CWD/html
sudo chown -R 33:33 $THIS_CWD/html
docker-compose down
rm -Rf $INITDIR_TMP/${THIS_NAME}/$COMPOSE_FILE
rmdir $INITDIR_TMP/${THIS_NAME}
rmdir $INITDIR_TMP
}
main () {
MAIN_TMP=$(mktemp -d)
mkdir $MAIN_TMP/${THIS_NAME}
cd $MAIN_TMP/${THIS_NAME}
# Break indentation until the EOF
cat << EOF > $COMPOSE_FILE
version: '3.1'
services:
nginx:
image: nginx:alpine
restart: always
ports:
- $DRUPAL_PORT:80
volumes:
- type: bind
source: $THIS_CWD/fpm.conf
target: /etc/nginx/conf.d/default.conf
memcached:
image: memcached:1-alpine
restart: always
fpm:
image: $DRUPAL_IMAGE
restart: always
volumes:
- type: bind
source: $THIS_CWD/html
target: /opt/drupal/web
db:
image: $DB_IMAGE
restart: always
volumes:
- type: bind
source: $THIS_CWD/.tmp/db
target: /var/lib/mysql
volumes:
nginx:
fpm:
db:
EOF
# End indentation break
docker-compose up -d --remove-orphans
#10249 docker-compose down
#rm -Rf $MAIN_TMP/$COMPOSE_FILE
rm $MAIN_TMP/${THIS_NAME}/$COMPOSE_FILE
rmdir $MAIN_TMP/${THIS_NAME}
rmdir $MAIN_TMP
}
if [[ -d .tmp/db ]]; then
echo 'db created skipping init_db'
else
init_db
fi
if [[ -d html ]]; then
echo 'html created skipping init_dir'
else
init_dir
fi
main