run python script on vagrant up - python

I'm trying to run a vagranfile that will result in a running python flask app. I've tried using this as the last command to achieve this -
config.vm.provision :shell, :path => "python app.py"
But it resulted with the following error -
*pathfor shell provisioner does not exist on the host system: /Users/*****/code/app-vagrant/python app.py
I understand that the script is trying to run this command from the host machine, how do I make Vagrant run the script on the vagrant machine launched?

you have 2 options to run a script using the vagrant shell provisioner you must pass either the inline or path argument:
inline (string) - Specifies a shell command inline to execute on the remote machine.
path (string) - Path to a shell script to upload and execute. It can be a script relative to the project Vagrantfile or a remote script (like a gist).
so when you pass :path => "python app.py" the system tries to find a script named python app.py on your host.
replace using inline argument and it will achieve what you want
config.vm.provision :shell, :inline => "python app.py"
note: provisioner are run as root user by default, if you want to change and run it as vagrant user:
config.vm.provision :shell, :inline => "python app.py", :privileged => false

If you want to start app when the machine start, you can do that:
move your python app code directory to the Vagrantfile's directory
Config the Vagrantfile like that:
config.vm.provision "shell", inline: <<-SHELL
python /vagrant/<your python app code directory>/app.py
SHELL

Related

Running flask command via crontab

I have a simple script that executes a flask command called sendemail (located in the "main" blueprint).
The "task" script, located in /home/ubuntu/tasks:
cd /home/ubuntu/app
source venv/bin/activate
flask main sendemail
deactivate
When I run (from anywhere, including the home directory)
bash /home/ubuntu/tasks/task
The function runs exactly as intended. However, when I add this same script to crontab, it produces an error, emailing me this message:
/home/ubuntu/tasks/task: line 4: flask: command not found
I've made sure that I have the latest flask installed and assume this might have something to do with the PATH variables - how can I fix/debug this?
The activation doesn’t work in the cron because you don’t have the same environment variables. You can use set > /path/to/your.log to diagnose…
You can simplify your scrip by calling Flask directly:
/home/ubuntu/app/venv/bin/flask main sendemail

Problem with flask using Python 3.7.6 with vscode on mac

I have a weird problem with some code I want to run. The code itself should not be the problem since it is downloaded from a Udemy class and not modified:
# coding=utf-8
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route("/")
def hello():
items = ["Apfel", "Birne", "Banane"]
return render_template("start.html", name="Max Mustermann", items=items)
#app.route("/test")
def test():
name = request.args.get("name")
return render_template("test.html", name=name)
I found online that, to start the emulated webserver(?) I have to rund the following temrinal commands before I can see the output:
(base) Christophs-MBP:13-23 chris$ export FLASK_APP=run.py run flask
(base) Christophs-MBP:13-23 chris$ export FLASK_APP=run.py run flask
(base) Christophs-MBP:13-23 chris$ export FLASK_APP=run.py
(base) Christophs-MBP:13-23 chris$ run flask
bash: run: command not found
No reaction to my terminal commands
Basically there is no reaction to the command to start the server(?).It should reply with "Running on 127.0.0.1:5000" as soon as I've run the command once.
If I go to my browser, there is no page when I address http://127.0.0.1:5000. What am I doing wrong? I am pretty new to Python and an absolute rookie regarding the terminal. Not sure if I broke something there, since trying to install pyenv to manage my Python installs better as recommended by a friend does not work either (I cannot update the SDK headers as described on RealPython
What are the export statements?
On Mac, export key=value creates a new (or updates an existing one) environment variable - the tutorial most likely simply asked you to provide one where key is FLASK_APP and value is a path to your app.
To verify it's been saved correctly, you can list the variables by just typing export in the terminal and finding out what's inside each of the environment variables on your system (if you want to only view FLASK_APP you can type export | grep FLASK_APP).
Why do you need FLASK_APP?
When you call flask run in your terminal, you will see the following message:
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py"
module was not found in the current directory.
I presume your file is called run.py, therefore none of the conditions are met. You could rename run.py to app.py and simply type flask run in the terminal, but you can also type export FLASK_APP=<path-to-run.py>. It seems the tutorial author decided to do the latter. Keep in mind that if you rename your file to app.py you will need to run flask run within the directory that file lives in. You can change directory in the terminal using cd command.
Why do you get bash: run: command not found?
bash is a language running inside your terminal, and it only knows of a few commands - it is not aware of any run commands. It does however know about flask command once you have installed it on your machine. Within the command's output there is a part which includes a run command:
Commands:
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.
Therefore, what you want to do is type flask run instead of just run in your terminal.

docker and python using symfony process

I am using Laradock and want to be able to run a python script from my laravel app using Symfony Process. From inside the root on my container I can run "python3 script_name.py arg1" and it runs just fine. pip list shows all modules needed. When I run it from inside Laravel, it tells me:
"import pymysql ImportError: No module named 'pymysql'"
I have used a non-docker Laravel app to do this just fine, using:
$script = storage_path().'/app/script.py';
$process = new Process('python3 '. $script." ".session('division'));
What am I missing?
On *nix make sure that PYTHONPATH is configured correctly for all users or try to set full path to python3.
How to check
At first your php user
php -r "print shell_exec( 'whoami' );" // somebody
When run
su somebody python3 script_name.py arg1

docker: run python CMD startup script alongside ENTRYPOINT

I have an container based on CentOS, running gunicorn, and serving a flask app. I would like a startup script to run when it's first started, although I would like my entrypoint to remain my gunicorn call.
Dockerfile snippet
COPY startup.py /
ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
CMD /usr/bin/python3 /startup.py
I cannot get this to work with the shell style CMD instruction. The logs show CMD complaining that "Error: '/usr/bin/python3 /startup.py' doesn't exist", although both python3 and the startup.py file exist.
The reason I want this script to be run on startup is that I would like it to work against a volume that is being configured on run. I've tried many other configurations, but can't find the magic combo.

execute python script local to docker client - no volumes

I can run a bash script local to my docker client (not local to the docker host or targeted container), without using volumes or copying the script to the container:
docker run debian bash -c "`cat script.sh`"
Q1 How do I do the equivalent on a django container? The following have not worked but my help demonstrate what Im asking for (the bash script printf the python script line with the expaned args):
docker run django shell < `cat script.py`
cat script.py | docker run django shell
Q2 How do I pass arguments to script.py passed to a dockerized managed.py? Again, examples of what does not work (for me):
./script.sh arg1 arg2 | docker run django shell
docker run django shell < echo "$(./script.sh arg1 arg2)"
I think the best way for you is to use custom Dockerfile that uses COPY or ADD command to move whatever scripts you into the container.
As for passing arguments you can either use ENTRYPOINT command in your image, like the example below:
ENTRYPOINT django shell /home/script.sh
Then you can use docker run arg1 arg2 to pass the arguments
This is the link to pass the command line arguments to python: http://www.tutorialspoint.com/python/python_command_line_arguments.htm
eg: python script.py -param1
If the script is already available in the docker you can trigger it using Dockerfile(with passing parameters)
RUN /script.py -param1 <value>
Extra:
Having said that it is always difficult to change the Dockerfile if there are more parameters to be changed frequently.Hence a small shell script can be written as a wrapper to Dockerfile like this:
Dockerwrapper.sh
pass parameters to Dockerfile
dockerbuild --tag <name> .
-
Dockerfile
RUN python script.py -param1 $1
I -------------------------------------------
IF script is not present inside docker
You can copy the script inside and then delete it by using COPY,RUN command...
(Reason: Since docker is an isolated environment, running from outside is not possible (I GUESS..))
Hoped it answered ur question.
All the best

Categories