nanovms / ops-examples

A repository of basic and advanced examples using Ops
114 stars 28 forks source link

Running flask application #76

Closed iamshreeram closed 2 years ago

iamshreeram commented 3 years ago

I would want to run a flask application. I was following the doc specified in here. But, Seeing the below exception -

~/r/p/u/p/s ❯❯❯ ops pkg load python_3.8.6 -c config.json
booting /Users/rams/.ops/images/python3 ...
en1: assigned 10.0.2.15
Traceback (most recent call last):
  File "/Applications/anaconda3/envs/simpleflaskenv/bin/flask", line 5, in <module>
    from flask.cli import main
ModuleNotFoundError: No module named 'flask'
exit status 3

I have the flask.cli in the given path. When I try to import the same in python repl, it works good.

Below is the config.json

{
  "Env": { "FLASK_APP": "hi.py" },
  "MapDirs": {"/Users/rams/.local/*": "/Users/rams/.local" },
  "Args": ["/Applications/anaconda3/envs/simpleflaskenv/bin/flask", "run", "--port=8080", "--host=0.0.0.0"],
  "Files": ["hi.py"]
}

cc: @dpfeif , @eyberg

eyberg commented 3 years ago

it might make sense to turn this into a package if other people might want the same initial setup

two things though:

1) I'd adjust the the first args to use the same flask path as your .local that you are loading in 'MapDirs' (eg: .local/bin/flask vs /Applications/anaconda3/envs) so as to ensure it's the same modules being loaded

2) I replicated this on linux using the instructions but ran into a missing libz for running on mac, works after i included it

iamshreeram commented 3 years ago

As Flask is very common, I would also vote to make this as package . Also, Can you enable discussion for this repository as we can get a lot of suggestion from other developers as well.

I have modified the config according to your suggestion.

{
    "Env": { "FLASK_APP": "hi.py" },
    "MapDirs": { "/Applications/anaconda3/envs/simpleflaskenv/*": "/Users/rams/.local/myops/simpleflaskenv" },
    "Args": ["/Users/rams/.local/myops/simpleflaskenv/bin/flask", "run", "--port=8080", "--host=0.0.0.0"],
    "Files": ["hi.py"]
}

I'm still seeing same error.

~/r/p/u/p/s ❯❯❯ ops pkg load python_3.8.6 -c config.json
booting /Users/rams/.ops/images/python3 ...
en1: assigned 10.0.2.15
Traceback (most recent call last):
  File "/Users/rams/.local/myops/simpleflaskenv/bin/flask", line 5, in <module>
    from flask.cli import main
ModuleNotFoundError: No module named 'flask'

I'm facing "missing libz" only on 1181. The stack trace in this issue seems to be different. Suggest pls. I'm follwing this doc. I think, the doc needs some modification to make the process very generic (w.r.t OS, filesystem, etc.).

@eyberg , @dpfeif

iamshreeram commented 2 years ago

Hi @eyberg, Could you please suggest a fix for above error.

Also, Im trying to create a image for flask. But running into below error - ops build -e PORT=8000 -c config.json python_3.8.6 -i pyhttpserver

error: python_3.8.6: stat /Users/rams/.ops/python_3.8.6: no such file or directory

Like the error mentioned, the path doesnt exists. python_3.8.6 is inside /Users/rams/.ops/packages/python_3.8.6.

eyberg commented 2 years ago

you'll need to use ops pkg load python_3.8.6 -c config.json syntax - when you use this command it auto-builds an image on demand for you and places it in ~/.ops/images/pyhttpserver (or whatever the package name is if you leave off the '-i')

iamshreeram commented 2 years ago

Thank you @eyberg . Could you please take a look at this issue. I now have libz included in my python. I'm still facing same issue.

Python is able to locate my flask location. But, for some reason, The flask located in /Users/rams/.local/myops/simpleflaskenv/bin/flask says, module not found. This is copied from my anaconda's virtual environment.

eyberg commented 2 years ago

anaconda is a env manager so you'll need to make sure all your envs match (eg: python version, etc.) - on a mac with the 3.8.6 package I was able to reproduce what you are seeing and this is how I went about fixing it:

conda create --name 3.8.6 python=3.8
conda activate 3.8.6
conda install flask
conda install requests

(note: this didn't actually install 3.8.6 - it was 3.8.12 or something)

my config ->

{
    "Env": {
        "FLASK_APP": "hi.py",
        "PYTHONPATH": "/Users/eyberg/opt/anaconda3/envs/3.8.6/lib/python3.8/site-packages"
    },
    "MapDirs": {
        "/Users/eyberg/opt/anaconda3/envs/3.8.6/*": "/Users/eyberg/opt/anaconda3/envs/3.8.6"
    },
    "Args": [
        "/Users/eyberg/opt/anaconda3/envs/3.8.6/bin/flask",
        "run",
        "--port=8080",
        "--host=0.0.0.0"
    ],
    "Files": [
        "hi.py"
    ]
}
(3.8.6) ➜  pytest ops pkg load python_3.8.6 -c config.json -p 8080
booting /Users/eyberg/.ops/images/python3 ...
en1: assigned 10.0.2.15
 * Serving Flask app 'hi.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
en1: assigned FE80::2C63:10FF:FE52:6740
10.0.2.2 - - [19/Nov/2021 22:11:24] "GET / HTTP/1.1" 200 -
10.0.2.2 - - [19/Nov/2021 22:11:24] "GET /favicon.ico HTTP/1.1" 404 -
iamshreeram commented 2 years ago

Thanks. This works. While adding files from my directory, Is there any way to add based on wild card? Instead of using below,

{
   ...
    "Files": ["hi.py", "app.py", "auth.py", "comments.py", "username.txt", "password.txt"]
}

Is there any way to add like ,

{
   ...
    "Files": ["*.py", "*.txt"]
}

This will eliminate a lot of manual work from adding each file names from current working directory.

eyberg commented 2 years ago

we don't have support for that yet but could look at doing that; what we usually do though in cases like this is just add the directory - so whatever directory those files in you could do:

{
    "Dirs": ["myapp"]
}