justpy-org / justpy

An object oriented high-level Python Web Framework that requires no frontend programming
https://justpy.io
Apache License 2.0
1.22k stars 96 forks source link

Docker support #434

Closed WolfgangFahl closed 2 years ago

WolfgangFahl commented 2 years ago

Providing a dockerized version of justpy would be helpful e.g. for testing

WolfgangFahl commented 2 years ago
scripts/rundocker test
[+] Building 0.6s (11/11) FINISHED                                                      
 => [internal] load build definition from Dockerfile                               0.0s
 => => transferring dockerfile: 37B                                                0.0s
 => [internal] load .dockerignore                                                  0.0s
 => => transferring context: 2B                                                    0.0s
 => [internal] load metadata for docker.io/library/python:3.10-slim                0.5s
 => [1/7] FROM docker.io/library/python:3.10-slim@sha256:59129c9fdea259c6a9b6d9c6  0.0s
 => CACHED [2/7] WORKDIR /usr/src/                                                 0.0s
 => CACHED [3/7] RUN pip install --no-cache-dir justpy                             0.0s
 => CACHED [4/7] RUN apt update                                                    0.0s
 => CACHED [5/7] RUN apt install -y git                                            0.0s
 => CACHED [6/7] RUN git clone https://github.com/elimintz/justpy                  0.0s
 => CACHED [7/7] RUN pip install green                                             0.0s
 => exporting to image                                                             0.0s
 => => exporting layers                                                            0.0s
 => => writing image sha256:b12cd8554c0ece59fb576cefe05a2ea4fc65c42650d5129d67f47  0.0s
 => => naming to docker.io/library/justpy-dev                                      0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
..

Captured stdout for tests.test_demjson.TestDemJson.testDemjson
Starting test testDemjson, debug=False ...
test testDemjson, debug=False took   0.0 s

Captured stdout for tests.test_importability.TestImportability.test_importability
Starting test test_importability, debug=False ...
/mnt/justpy
Module directory: /mnt/justpy, Application directory: /mnt
test test_importability, debug=False took   0.2 s

Ran 2 tests in 0.284s using 8 processes

OK (passes=2)
WolfgangFahl commented 2 years ago
scripts/rundocker     
[+] Building 0.7s (7/7) FINISHED                                                        
 => [internal] load build definition from Dockerfile                               0.0s
 => => transferring dockerfile: 37B                                                0.0s
 => [internal] load .dockerignore                                                  0.0s
 => => transferring context: 2B                                                    0.0s
 => [internal] load metadata for docker.io/library/python:3.10-slim                0.6s
 => [1/3] FROM docker.io/library/python:3.10-slim@sha256:59129c9fdea259c6a9b6d9c6  0.0s
 => CACHED [2/3] WORKDIR /usr/src/                                                 0.0s
 => CACHED [3/3] RUN pip install --no-cache-dir justpy                             0.0s
 => exporting to image                                                             0.0s
 => => exporting layers                                                            0.0s
 => => writing image sha256:5e64e7995717d0f24f37191440b08bf2c3c8f6586fd6878c0bd3c  0.0s
 => => naming to docker.io/library/justpy                                          0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
root@1135e29f9adf:/mnt# ls
LICENSE    docker  examples  pyproject.toml  tests
README.md  docs    justpy    scripts         tutorial
WolfgangFahl commented 2 years ago
scripts/rundocker dev 
[+] Building 1.3s (11/11) FINISHED                                                      
 => [internal] load build definition from Dockerfile                               0.0s
 => => transferring dockerfile: 37B                                                0.0s
 => [internal] load .dockerignore                                                  0.0s
 => => transferring context: 2B                                                    0.0s
 => [internal] load metadata for docker.io/library/python:3.10-slim                1.2s
 => [1/7] FROM docker.io/library/python:3.10-slim@sha256:59129c9fdea259c6a9b6d9c6  0.0s
 => CACHED [2/7] WORKDIR /usr/src/                                                 0.0s
 => CACHED [3/7] RUN pip install --no-cache-dir justpy                             0.0s
 => CACHED [4/7] RUN apt update                                                    0.0s
 => CACHED [5/7] RUN apt install -y git                                            0.0s
 => CACHED [6/7] RUN git clone https://github.com/elimintz/justpy                  0.0s
 => CACHED [7/7] RUN pip install green                                             0.0s
 => exporting to image                                                             0.0s
 => => exporting layers                                                            0.0s
 => => writing image sha256:b12cd8554c0ece59fb576cefe05a2ea4fc65c42650d5129d67f47  0.0s
 => => naming to docker.io/library/justpy-dev                                      0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
root@c4429ef1da39:/mnt# cd /usr/src/justpy/
root@c4429ef1da39:/usr/src/justpy# ls
LICENSE  README.md  docs  examples  justpy  pyproject.toml  scripts  tests  tutorial
rodja commented 2 years ago

@WolfgangFahl should we also provide this image on https://hub.docker.com/?

WolfgangFahl commented 2 years ago

@rodja yes this would be good but i'd prefer to have a solution for showing off all demos in the docker image. on jpdemo.bitplan.com i am currently starting a process per demo and use apache redirects. In a docker image apache is a bit heavy wait and i wonder whether we could do the "routing" with a justpy frontend app instead. In the long run I'd love to supply all some 150 tutorial examples this way.

#!/bin/bash 
# WF 2022-05-15 
# start Justpy demo code

# goto project directory
cd $HOME/source/python/pyJustpyWidgets
# get current version
git pull
# install dependencies
scripts/install
# prepare logging
logroot="/var/log/jpdemo"
if [ ! -d $logroot ]
then
  sudo mkdir -p $logroot
  sudo chgrp $(id -g) $logroot
  sudo chmod g+w $logroot
fi

initIndex() {
  local lIndex="$1"
  sudo touch $lIndex
  sudo chown www-data.www-data $lIndex
  sudo chmod 660 $lIndex
cat << EOF > $lIndex
<html>
  <body>
  <h1>Justpy Demo</h1>
  <h2>wiki.bitplan.com links</h2>
  <ul>
    <li><a href="https://wiki.bitplan.com/index.php/Justpy">Justpy</a>
    <li><a href="https://wiki.bitplan.com/index.php/PyJustpyWidgets">pyJustpyWidgets</a>
  </ul>
  <h2>Demos</h2>
  <table>
    <tr><th>example</th><th>source</th></tr>
EOF
}
addEntryToIndex() {   
  local l_Index="$1"
  local l_module="$2"
  local l_name="$3"
cat << EOF >> $l_Index
  <tr><td><a href="/$l_name">$l_name</a></td><td><a href="https://github.com/WolfgangFahl/pyJustpyWidgets/blob/main/$l_module">$l_module</a></td></tr> 
EOF
}

closeIndex()  {
  local l_Index="$1"
cat << EOF >> $l_Index
     </table>
  </body>
</html>
EOF
}

initRedirectMap() {
  local l_rMap="$1"
  cat << EOF > $l_rMap
#
# this file is the Apache Webserver redirect map for the
# jpdemo.bitplan.com website
#
# WF 2022-05-15
#
# this example is created with the jpdemo script 
#
# see documentation for Apache RewriteMap at
# https://httpd.apache.org/docs/2.4/rewrite/rewritemap.html
#
# if you need redirects with anchors see:
#  https://stackoverflow.com/questions/2686075/mod-rewrite-with-anchor-link
#  https://stackoverflow.com/a/2686157/1497139
#  so the no escape flag is needed [NE]
# 
EOF
   sudo chown www-data.www-data $l_rMap
   sudo chmod g+w $l_rMap
}

addRedirectMapEntry() {
  local l_rMap="$1"
  local l_rLine="$2"
  echo "$2" >> $1
}

export PYTHONPATH=.
port=10000
redirectmap=/var/www/redirects/jpdemo-map.txt
index=/var/www/jpdemo/index.html
initIndex $index
initRedirectMap $redirectmap
host=$(hostname -f)
echo "current directory is $(pwd)"
for module in $(find jpdemo -name "*.py")
do
  moduleName=$(basename $module .py)
  case $moduleName in
    __init__|basedemo) 
      ;; # ignore
    *) 
     echo "starting $module on port $port"
     pgrep -fla "python $module"
     if [ $? -eq 0 ] 
     then
       echo "killing existing demo $moduleName"
       pkill -f "python $module"
     fi 
     logfile="$logroot/$moduleName.log"
     nohup python $module --port $port >$logfile 2>&1&
     echo "log file is at $logfile"
     redirectLine="$moduleName http://$host:$port" 
     addRedirectMapEntry $redirectmap "$redirectLine"
     addEntryToIndex $index $module $moduleName
     port=$((port+1))
     ;;
  esac
done
closeIndex $index
sudo service apache2 restart