Skip to content

Commit 3192719

Browse files
authored
Merge pull request #403 from Paraphraser/20210908-python-master
20210908 Python - master branch - PR 1 of 2
2 parents 97334ad + 0c9e829 commit 3192719

File tree

7 files changed

+567
-41
lines changed

7 files changed

+567
-41
lines changed

.templates/python/Dockerfile

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
11
FROM python:3
22

3-
WORKDIR /usr/src/app
3+
# user+group ID defaults for IOTstack
4+
ENV IOTSTACK_UID=1000
5+
ENV IOTSTACK_GID=1000
46

5-
COPY requirements.txt ./
7+
# the template folder is named
8+
ENV TEMPLATE="app"
69

7-
RUN pip install --no-cache-dir -r requirements.txt
10+
# where IOTstack template files are stored
11+
ENV PYTHON_DEFAULTS="/iotstack_defaults/${TEMPLATE}"
812

9-
CMD [ "python", "./app.py" ]
13+
# ensure the defaults directory exists
14+
RUN mkdir -p ${PYTHON_DEFAULTS}
15+
16+
# copy template files to defaults directory in image
17+
COPY ${TEMPLATE} ${PYTHON_DEFAULTS}
18+
19+
# the requirements file (if it exists) is
20+
ENV REQUIREMENTS="${PYTHON_DEFAULTS}/requirements.txt"
21+
22+
# set up requirements (if the file exists)
23+
RUN if [ -e ${REQUIREMENTS} ] ; then pip3 install --no-cache-dir -r ${REQUIREMENTS} ; fi
24+
25+
# the python working directory is
26+
ENV PYTHON_WORKDIR="/usr/src/${TEMPLATE}"
27+
28+
# ensure the working directory exists
29+
RUN mkdir -p ${PYTHON_WORKDIR}
30+
31+
# add an entry-point script
32+
ENV ENTRY_POINT="docker-entrypoint.sh"
33+
COPY ${ENTRY_POINT} /${ENTRY_POINT}
34+
RUN chmod 755 /${ENTRY_POINT}
35+
36+
# unset variables that are not needed
37+
ENV TEMPLATE=
38+
ENV REQUIREMENTS=
39+
ENV ENTRY_POINT=
40+
41+
# set the working directory
42+
WORKDIR ${PYTHON_WORKDIR}
43+
44+
# away we go
45+
ENTRYPOINT ["/docker-entrypoint.sh"]
46+
CMD ["/usr/local/bin/python", "./app.py"]
47+
48+
# EOF

.templates/python/app/app.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
print("hello world")
1+
import time
2+
3+
print("The world is born. Hello World.", flush=True)
4+
5+
while True:
6+
7+
time.sleep(10)
8+
print("The world is re-born. Hello World.", flush=True)

.templates/python/app/requirements.txt

Whitespace-only changes.

.templates/python/build.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,11 @@ def preBuild():
8989
if not os.path.exists(serviceService):
9090
os.makedirs(serviceService, exist_ok=True)
9191

92-
# Files copy
92+
# copy supporting files
9393
shutil.copy(r'%s/Dockerfile' % serviceTemplate, r'%s/Dockerfile' % serviceService)
94+
shutil.copy(r'%s/docker-entrypoint.sh' % serviceTemplate, r'%s/docker-entrypoint.sh' % serviceService)
95+
shutil.copytree(r'%s/app' % serviceTemplate, r'%s/app' % serviceService)
9496

95-
print("sudo mkdir -p " + serviceVolume + "/app ")
96-
subprocess.call("user=$(whoami) && sudo mkdir -p " + serviceVolume + "/app && sudo chown -R $user:$user " + serviceVolume, shell=True)
97-
print("sudo chown -R $user:$user ./volumes/python")
98-
shutil.copy(r'%s/app/requirements.txt' % serviceTemplate, r'%s/app/requirements.txt' % serviceVolume)
99-
shutil.copy(r'%s/app/app.py' % serviceTemplate, r'%s/app/app.py' % serviceVolume)
10097
return True
10198

10299
# #####################################
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# does the working directory exist (something is badly wrong if it does not)
5+
if [ -d "$PYTHON_WORKDIR" ] ; then
6+
7+
# are self-healing defaults available?
8+
if [ -d "$PYTHON_DEFAULTS" ] ; then
9+
10+
# yes! replace anything that has gone missing
11+
cp -an "$PYTHON_DEFAULTS"/* "$PYTHON_WORKDIR"
12+
13+
fi
14+
15+
# set appropriate ownership throughout
16+
chown -R "$IOTSTACK_UID:$IOTSTACK_GID" "$PYTHON_WORKDIR"
17+
18+
fi
19+
20+
# start python
21+
exec "$@"

.templates/python/service.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ python:
22
container_name: python
33
build: ./services/python/.
44
restart: unless-stopped
5-
network_mode: host
5+
environment:
6+
- TZ=Etc/UTC
7+
- IOTSTACK_UID=1000
8+
- IOTSTACK_GID=1000
9+
# ports:
10+
# - "external:internal"
611
volumes:
712
- ./volumes/python/app:/usr/src/app
13+
networks:
14+
- iotstack_nw

0 commit comments

Comments
 (0)