-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpd.sh
More file actions
executable file
·313 lines (267 loc) · 9.44 KB
/
pd.sh
File metadata and controls
executable file
·313 lines (267 loc) · 9.44 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/sh
PURPLE='\033[0;35m'
GREEN='\033[1;32m'
RED='\033[1;31m'
YELLOW='\033[1;33m'
BLUE='\033[1;94m'
NONE='\033[0m'
###########
# Functions
# Remove temporary files and prepare to exit script.
cleanup() {
if [ ! -s ./pd1.sh ];
then
rm ./pd1.sh
fi
if [ ! -s ./pd0.sh ];
then
rm ./pd0.sh
fi
}
###########
# MAIN Program begins...
# Parse command line...
if [ "$#" -ne 2 ] || [ "$1" != "--dev" ] && [ "$1" != "-d" ] && [ "$1" != "--prod" ] && [ "$1" != "-p" ] && [ "$1" != "--exit" ] && [ "$1" != "-x" ];
then
echo
echo "${PURPLE}pd - Python with Docker${NONE}"
echo
echo "${PURPLE}USAGE: pd [OPTION] [PATH]${NONE}"
echo
echo "${PURPLE}Options:${NONE}"
echo "${PURPLE} -d, --dev Setup a new or activate an existing environment & container${NONE}"
echo "${PURPLE} -h, --help This menu${NONE}"
echo "${PURPLE} -p, --prod Build a production container${NONE}"
echo "${PURPLE} -x, --exit Exit / shutdown running development environment & container${NONE}"
echo
exit 1
fi
# Is second parameter a valid directory?
if [ ! -d "$2" ];
then
echo
echo "${RED}ERROR: Path <$2> is not a directory${NONE}"
echo
exit 2
fi
# Lets move into the working directory
echo
cd $2
#########################
# EXIT? If so, cleanup.
# We do this first so we don't have to create all of those files if we are just exiting.
if [ "$1" == "--exit" ] || [ "$1" == "-x" ];
then
echo "${YELLOW}--> Stopping and removing existing docker images...${NONE}"
echo "${BLUE}--> docker stop ${PWD} && docker rm ${PWD}...${NONE}"
docker stop $PWD
docker rm $PWD
cleanup
echo "${GREEN}--> Cleanup complete...script exiting.${NONE}"
# Exit script with no errors.
exit 0
fi
# Does Dockerfile file exist?
if [ ! -s ./Dockerfile ];
then
echo "--> ${YELLOW}Dockerfile does not exist, creating file...${NONE}"
# Filename: Dockerfile
echo "FROM python:3.7" > ./Dockerfile
echo "WORKDIR /usr/src/app" >> ./Dockerfile
echo "LABEL maintainer=\"nsambol@otgexp.com\"" >> ./Dockerfile
echo >> ./Dockerfile
echo "COPY . /usr/src/app" >> ./Dockerfile
echo "RUN pip install --upgrade pip" >> ./Dockerfile
echo "RUN pip install -r /usr/src/app/requirements.txt" >> ./Dockerfile
echo >> ./Dockerfile
echo "# For colored prompt." >> ./Dockerfile
echo 'RUN echo ". /usr/src/app/pd1.sh" >> ~/.bashrc' >> ./Dockerfile
echo >> ./Dockerfile
echo "CMD [\"python3\", \"app.py\"]" >> ./Dockerfile
else
echo "--> ${GREEN}Dockerfile exists proceeding with existing file...${NONE}"
fi
# Does .gitignore file exist?
if [ ! -s ./.gitignore ];
then
echo "--> ${YELLOW}.gitignore does not exist, creating file...${NONE}"
echo "# Byte-compiled / optimized / DLL files" > ./.gitignore
echo "__pycache__/" >> ./.gitignore
echo "*.py[cod]" >> ./.gitignore
echo ".DS_Store" >> ./.gitignore
echo >> ./.gitignore
echo "# C Extensions" >> ./.gitignore
echo "*.so" >> ./.gitignore
echo >> ./.gitignore
echo "# Distribution / packaging" >> ./.gitignore
echo "bin/" >> ./.gitignore
echo "build/" >> ./.gitignore
echo "develop-eggs/" >> ./.gitignore
echo "dist/" >> ./.gitignore
echo "eggs/" >> ./.gitignore
echo "lib/" >> ./.gitignore
echo "lib64/" >> ./.gitignore
echo "parts/" >> ./.gitignore
echo "sdist/" >> ./.gitignore
echo "var/" >> ./.gitignore
echo "*.egg-info/" >> ./.gitignore
echo ".installed.cfg" >> ./.gitignore
echo "*.egg" >> ./.gitignore
echo >> ./.gitignore
echo "# Installer logs" >> ./.gitignore
echo "pip-log.txt" >> ./.gitignore
echo "pip-delete-this-directory.txt" >> ./.gitignore
echo >> ./.gitignore
echo "# venv files" >> ./.gitignore
echo "venv*" >> ./.gitignore
echo "venv/*" >> ./.gitignore
echo "# Unit test / coverage reports" >> ./.gitignore
echo ".tox/" >> ./.gitignore
echo ".coverage" >> ./.gitignore
echo ".cache" >> ./.gitignore
echo "nosetests.xml" >> ./.gitignore
echo "coverage.xml" >> ./.gitignore
echo >> ./.gitignore
echo "# Translations" >> ./.gitignore
echo "*.mo" >> ./.gitignore
echo >> ./.gitignore
echo "# Mr Developer" >> ./.gitignore
echo ".mr.developer.cfg" >> ./.gitignore
echo ".project" >> ./.gitignore
echo ".pydevproject" >> ./.gitignore
echo >> ./.gitignore
echo "# PyCharm" >> ./.gitignore
echo ".idea/*" >> ./.gitignore
echo >> ./.gitignore
echo "# Rope" >> ./.gitignore
echo ".ropeproject" >> ./.gitignore
echo >> ./.gitignore
echo "# Django stuff:" >> ./.gitignore
echo "*.log" >> ./.gitignore
echo "*.pot" >> ./.gitignore
echo >> ./.gitignore
echo "# Sphinx documentation" echo >> ./.gitignore
echo "docs/_build/" >> ./.gitignore
echo "# Environment Variables" >> ./.gitignore
echo ".env*" >> ./.gitignore
else
echo "--> ${GREEN}.gitignore file exists proceeding with existing file...${NONE}"
fi
# Does .dockerignore file exist?
if [ ! -s ./.dockerignore ];
then
echo "--> ${YELLOW}.dockerignore does not exist, creating file...${NONE}"
echo ".git" > ./.dockerignore
echo ".gitignore" >> ./.dockerignore
echo "Dockerfile*" >> ./.dockerignore
echo ".gitignore" >> ./.dockerignore
echo "docker-compose" >> ./.dockerignore
echo "README.md" >> ./.dockerignore
echo "LICENSE" >> ./.dockerignore
echo ".vscode" >> ./.dockerignore
echo ".env" >> ./.dockerignore
echo ".sh" >> ./.dockerignore
echo ".idea" >> ./.dockerignore
echo "__pycache__" >> ./.dockerignore
echo "*.pyc" >> ./.dockerignore
echo "*.pyo" >> ./.dockerignore
echo "*.pyd" >> ./.dockerignore
echo ".Python" >> ./.dockerignore
echo "env" >> ./.dockerignore
else
echo "--> ${GREEN}.dockerignore exists proceeding with existing file...${NONE}"
fi
##########################
# Development Environment?
if [ "$1" == "--dev" ] || [ "$1" == "-d" ];
then
# Does .env_dev file exist?
if [ ! -s ./.env_dev ];
then
echo "--> ${YELLOW}.env_dev file does not exist, creating file...${NONE}"
echo "MYSQL_PASSWORD=" > ./.env_dev
echo "MYSQL_USER=" >> ./.env_dev
echo "TESTING_MODE=" >> ./.env_dev
echo "TESTING_MODE_DB=FALSE" >> ./.env_dev
echo "TESTING_MODE_EMAIL=TRUE" >> ./.env_dev
else
echo "--> ${GREEN}.env_dev file exists proceeding with existing file...${NONE}"
fi
# OK, now we are finally setup for creating the container...
# Lets pull project name ($pn) out of path.
pn=${PWD##*/}
# Remove any old images...TODO SAVE IMAGES / RECYCLE IMAGES?
echo "${GREEN}--> Removing any previous containers for $pn...${NONE}"
echo "${BLUE}docker rm $pn${NONE}"
docker rm $pn
# Create new container...
echo "${GREEN}--> Creating container for $pn...${NONE}"
echo "${BLUE}docker build . --tag $pn:latest --rm --no-cache${NONE}"
docker build . --tag "$pn":latest --rm --no-cache
# Open new terminal for local machine...
echo "${GREEN}--> Opening new terminal for ${BLUE}local machine${GREEN}...${NONE}"
echo "${BLUE}open -a Terminal .${NONE}"
### FOR LOCAL SHELL
# We have to do some funky stuff here to make that happen...
echo "#/bin/bash -i" > ./pd0.sh
echo 'export PS1="\033[1;32mLOCAL SHELL: \033[0m"' >> ./pd0.sh
echo "/bin/bash -i" >> ./pd0.sh
chmod +x ./pd0.sh
open -a Terminal ./pd0.sh
sleep 1
### FOR DOCKER SHELL
#!/bin/bash
echo 'DOCKER_USER=$(whoami)' > ./pd1.sh
echo 'if [ "$DOCKER_USER" == "root" ];' >> ./pd1.sh
echo "then" >> ./pd1.sh
echo 'export PS1="\033[1;31mDOCKER SHELL ($DOCKER_USER)# \033[0m"' >> ./pd1.sh
echo "else" >> ./pd1.sh
echo 'export PS1="\033[1;94mDOCKER SHELL ($DOCKER_USER)$ \033[0m"' >> ./pd1.sh
echo "fi" >> ./pd1.sh
# Run new container and transfer current xterm over to it...
echo "${GREEN}--> Running container for $pn...${NONE}"
echo "${BLUE}docker run -it -v $PWD:/usr/src/app --env-file ./.env_dev --name $pn $pn /bin/bash${NONE}"
# We have to do some funky stuff here to make that happen...
docker run -it -v $PWD:/usr/src/app --env-file ./.env_dev --name $pn $pn /bin/bash
sleep 1
cleanup
# Exit script with no errors.
exit 0
fi
#########################
# Production Environment?
if [ "$1" == "--prod" ] || [ "$1" == "-p" ];
then
# Does .env_prod file exist?
if [ ! -s ./.env_prod ];
then
echo "--> ${YELLOW}.env_prod file does not exist, creating file...${NONE}"
echo "MYSQL_PASSWORD=" > ./.env_prod
echo "MYSQL_USER=" >> ./.env_prod
echo "TESTING_MODE=FALSE" >> ./.env_prod
else
echo "--> ${GREEN}.env_prod file exists proceeding with existing file...${NONE}"
fi
# OK, now we are finally setup for creating the container...
# Lets pull project name ($pn) out of path.
pn=${PWD##*/}
# Remove any old images...TODO SAVE IMAGES / RECYCLE IMAGES?
echo "${GREEN}--> Removing any previous containers for $pn...${NONE}"
echo "${BLUE}docker rm $pn${NONE}"
docker rm $pn
# Create new container...
echo "${RED}--> Creating production container for $pn...${NONE}"
echo "${RED}docker build . --tag $pn:latest --rm --no-cache${NONE}"
docker build . --tag "$pn":latest --rm --no-cache
# Run new container and transfer current xterm over to it...
echo "${RED}--> Running production container for $pn...${NONE}"
echo "${RED}docker run -v $PWD:/usr/src/app --env-file ./.env_prod --name $pn $pn${NONE}"
# We have to do some funky stuff here to make that happen...
docker run -it -v $PWD:/usr/src/app --env-file ./.env_prod --name $pn $pn
echo "${RED}--> There is no interactive shell for a production container $pn...${NONE}"
echo "${RED}--> Script exiting...container running default command in Dockerfile...${NONE}"
sleep 1
cleanup
# Exit script with no errors.
exit 0
fi