Execute a flask run in Jenkins shell without timeout - python

I'm using Jenkins to run a Flask app automatically from a Git branch.
The build works well, and it starts the Flask app on my server, except that when you run flask run, the command line stays active as long as the flask app runs.
Thus, the build never ends, and it ends up as an unstable build.
How can I get the flask app to run and get a Jenkins build success if it got the the * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) message?

If you're running flask run in a bash script, adding & to the end (flask run &) will run the task in the background, allowing the bash script to continue. I think this will let your job finish and Jenkins can scan stdout for the message indicating success.
Edit: Apparently overriding the build number export BUILD_ID=<whatever> is enough to stop Jenkins from killing the background process. I'd be wary of what you choose as <whatever>, if you choose an existing BUILD_ID, there could be side-effects.

Related

When running Python Flask Server, Jenkins is not successful and continues to load

I am building CICD through Jenkins.
But there are problems.
It is planning to upload source code first and turn on flask server through batch file.
I wrote a shell script for Jenkins' Build>Execute Shell.
postCommand=/cygdrive/c/workspace/ContactPortal_Flask/run.bat
sshpass -p ${deployPassword} ssh -o StrictHostKeyChecking=no ${deployUser}#${deployServer} ${postCommand}
Here is run.bat file
set FLASK_ENV=development
set path=%path%;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\develop\instantclient_12_1;C:\develop\Anaconda3;C:\develop\Anaconda3\Library\mingw-w64\bin;C:\develop\Anaconda3\Library\usr\bin;C:\develop\Anaconda3\Library\bin;C:\develop\Anaconda3\Scripts;
set "START=C:\workspace\ContactPortal_Flask\start.bat"
cd C:\workspace\ContactPortal_Flask
python -m flask run
then, The source code upload was successful, and turning on the flask server was also successful, but Jenkins was not marked Success and continued to load.
please help!!
I think the main problem here is that python -m flask run starts the server and will not finish until user hit Ctrl+C.
Since the target system is on Windows, you may want to create custom service and have jenkin start that service at the end instead. For service creation see https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service And by starting this service (e.g. with NET START <service-name>) jenkin can finish, and flask can start running in the background.
Also for a production system, you may want to consider checking this and pick a proper web server instead of using the builtin web server provided by flask.

Authentication error upon running a shell script as sudo inside a flask application being run by uWSGI

I'm using a shell script to restart my flask app upon receiving a webhook request from github, but I'm running into an issue with no-password sudo authentication, but only when uWSGI is runs the restart function.
I have the application deployed as a systemd service and in order to restart the service, I have to run systemctl restart myapp.service as sudo. So I stuck that line into a script, and marked it as runnable as sudo without password authentication in /etc/sudoers by the user that runs the webapp.
The restart function is very simple:
import os
def on_push():
os.setuid(os.geteuid())
os.system('sudo /path/to/script.sh')
And the script itself is a one-liner:
systemctl restart myapp.service
Running the command via os.system('sudo /path/to/script.sh') in a normal python shell restarts the app as expected, without asking for the password. However, when the command is run by a uWSGI worker process, it fails to restart with the message that Interactive authentication required.
Is this an issue that can be solved by changing uWSGI launch parameters?
Turns out that I needed to set uid and gid in the .ini file used to start the uWSGI instance to the user/group used to run the application.

re-run Python script in started in docker-compose with MySQL server running

Using docker compose I've created a container for my MySQL DB and one for my Python script. When I use the command docker compose up, my images are built for my Python app container and my MySQL DB container, and the python script is run. After execution, the shell just hangs since MySQL server is still running and my script has completed execution. How can I either stop the MySQL server after my script runs, or re-run my script while the MySQL server continues to run?
When running docker-compose up, your current shell will display all the logs from all the containers defined in the docker-compose.yaml file. Also if you terminate the command with cmd + c in MacOS, all the containers will stop running.
As a result, this gives you the impression that the shell just hangs while everything is still running as normal.
What you want in this case is to let the containers continue to run in the background (detached mode)
docker-compose up --detach
The MySQL server will now continue to run until you stop it with docker-compose down.

Why Fabric run app terminate?

I have been trying to start app in remote hosts using Fabric.
I was running a daemonized Java app, which run perfectly fine when I logged in and run it manually. The app keep running even after I exit the session.
But when I use Fabric run(), my app terminates once the session ends.
Although run(command, pty=False) solved my problem (It is roughly documented here), I still can not see why these are relevant. I am a python newbie, can anyone explain the difference between:
start daemon with ssh logged in and start manually
start daemon using Fabric with pty=True
start daemon using Fabric with pty=False

Python Twitter Bot w/ Heroku Error: R10 Boot Timeout

I have developed a simple python twitter bot which periodically executes various functions using the following libraries:
TwitterFollowBot==2.0.2
schedule==0.3.2
The application works fine when I execute it on my computer, and I wanted to migrate it to Heroku so it could run independently. Upon executing it on Heroku it works as it should for 60 seconds before timing out:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
After researching this, I found out that Heroku dynamically switches ports and my application must continuously specify which port it should run on. From another thread I read that a possible solution required me to alter my Procfile, so I appended the PORT variable to the end:
Procfile: web: python app.py $PORT
This had no effect so I tried it again with ${PORT},
And I also tried switching web: with bot: (which stopped my application from executing properly)
I found other solutions to this issue which worked for node, or python applications using Django, Flask, etc... However, I was unable to find a solution for just a simple .py application. Is this even possible? Or should I create my app with Flask and attempt one of the other fixes?
If it doesn't provide any web content then you don't need to run a web process - call it something else like bot and then do:
heroku ps:scale web=0
heroku ps:scale bot=1
and you won't get any more R10s.

Categories

Resources