-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
176 lines (144 loc) · 4.24 KB
/
start.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
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
175
176
source <(curl -s https://raw.githubusercontent.com/idawnwon/docker-django/master/bin/shell_functions.sh)
divider
workdir=~/django
project='local_django'
DB_NAME='djangodb'
DB_USR='admin'
DB_PASS='admin1234'
sw_vers=$(which sw_vers)
if [[ -n "$sw_vers" ]]; then
if_is_mac=$(sw_vers -productName | grep Mac)
fi
divider
cecho purple "Here are 5 questions to answer:"
divider
cstr yellow "(1/5) Work directory $(cstr green "[hit ENTER directly to accept default: $(cstr white "\"$workdir\"")$(cstr green "]")"): "
read workdir
if [[ -z "$workdir" ]]; then
workdir=~/django
fi
divider
cstr yellow "(2/5) Project name $(cstr green "[hit ENTER directly to accept default: $(cstr white "\"$project\"")$(cstr green "]")"): "
read project
if [[ -z "$project" ]]; then
project='local_django'
fi
divider
cstr yellow "(3/5) Database name $(cstr green "[hit ENTER directly to accept default: $(cstr white "\"$DB_NAME\"")$(cstr green "]")"): "
read DB_NAME
if [[ -z "$DB_NAME" ]]; then
DB_NAME='djangodb'
fi
divider
cstr yellow "(4/5) Database USER name $(cstr green "[hit ENTER directly to accept default: $(cstr white "\"$DB_USR\"")$(cstr green "]")"): "
read DB_USR
if [[ -z "$DB_USR" ]]; then
DB_USR='admin'
fi
divider
cstr yellow "(5/5) Database PASSWORD $(cstr green "[hit ENTER directly to accept default: $(cstr white "\"$DB_PASS\"")$(cstr green "]")"): "
read DB_PASS
if [[ -z "$DB_PASS" ]]; then
DB_PASS='admin1234'
fi
divider
echo "
Work directory: $(cstr red "$workdir")
Project name: $(cstr purple "$project")
Database name: $(cstr cyan "$DB_NAME")
Database user name: $(cstr green "$DB_USR")
Database password: $(cstr yellow "$DB_PASS")
"
divider
YesNo
divider
docker_engine=$(which docker)
if [[ -z "$docker_engine" ]] && [[ -n "$if_is_mac" ]]; then
cecho whiteRed "
It seems that you didn't have Docker Desktop installed on your mac.
Please install it and start over.
Download a stable version:
https://download.docker.com/mac/stable/Docker.dmg "
exit 1
elif [[ -z "$docker_engine" ]]; then
cstr purple "
It seems that you didn't have Docker and Docker Compose installed.
Do you wanna install them automatically right now?
"
YesNo
source <(curl -s https://raw.githubusercontent.com/idawnwon/docker-django/master/bin/install_docker_and_compose.sh)
fi
divider
if [[ ! -d "$workdir" ]]; then
mkdir -p $workdir
echo "Work directory established: $workdir"
fi
cd $workdir
git clone https://github.com/idawnwon/docker-django.git
mv docker-django/* ./
rm -fR docker-django
sudo chmod -R +x ./
cat << EOF > docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_DB=$DB_NAME
- POSTGRES_USER=$DB_USR
- POSTGRES_PASSWORD=$DB_PASS
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
EOF
if [[ -n "$if_is_mac" ]]; then
docker-compose run web django-admin startproject $project .
else
sudo docker-compose run web django-admin startproject $project .
fi
anchor_line=$(awk '/# Database/{ print NR; exit }' $project/settings.py)
start_line=$((anchor_line+2))
end_line=$((start_line+6))
cat > HereFile << EOF
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '$DB_NAME',
'USER': '$DB_USR',
'PASSWORD': '$DB_PASS',
'HOST': 'db',
'PORT': 5432,
}
}
EOF
if [[ -n "$if_is_mac" ]]; then
echo "Going to change ownership of $workdir: $USER:admin, maybe need ${USER}'s password... "
sudo chown -R ${USER}:admin $workdir
sed -i "" "$start_line,${end_line}d" $project/settings.py
sed -i "" "$start_line r HereFile" $project/settings.py
else
sudo chown -R $USER:$USER .
sed -i "$start_line,${end_line}d" $project/settings.py
sed -i "$start_line r /tmp/HereFile" $project/settings.py
fi
rm HereFile
divider 2
echo "################################################################"
cecho red "
CONGRATULATIONS!
Your local django environment is ready!
Go to 'http://localhost:8000' to view your django welcome page!
"
echo "################################################################"
divider 2
if [[ -n "$if_is_mac" ]]; then
docker-compose up > /dev/null
else
sudo docker-compose up > /dev/null
fi