EasyDeployer steps
Source code is available at this url:
https://github.com/behrad-kzm/EasyDeployer
Steps
- Pulling git latest commits.
#!/bin/bashecho $'\n-----Updating git local branch-----\n'
cd ../../git reset --hard
echo $'pulling from git...'
git pullecho $'\n-----Done-----\n'
cd Runner/scripts
- Updating
apt-get
and install dependencies.
#!/bin/bashcd ../../echo $'\n-----Updating local packages-----\n'
sudo apt-get updateecho $'\n-----Installing dependencies-----\n'
sudo apt-get install python3-pip python3-dev nginxecho $'\n-----Done-----\n'
cd Runner/scripts
- Creating
Virtualenv
and activate.
#!/usr/bin/env bashset -eecho $'\n-----Initiating Venv-----\n'
cd ../../if [ -d venv/ ];
then
echo $'\n-----Removing existing venv directory-----\n';
sudo rm -Rf ../../venv;
fiecho $'\n-----Creating venv-----\n'
python3 -m venv venvsource venv/bin/activate
echo $'\n-----Activating venv-----\n'echo $'\n-----Done-----\n'
cd Runner/scripts
- Installing
flask
,gunicorn
project requirements fromrequirements.txt
.
#!/bin/bashecho $'\n-----Installing Gunicorn-----\n'
cd ../../pip3 install gunicorn flaskecho $'\n-----Installing requirements.txt-----\n'
pip3 install -r requirements.txtecho $'\n-----Done-----\n'
cd Runner/scripts
- Closing all processes on port 5000 and binding
gunicorn
anddeactivate
venv.
#!/usr/bin/env bashset -e
echo $'\n-----Binding local to gunicorn-----\n'
cd ../../sudo fuser -k 5000/tcp
gunicorn --bind 0.0.0.0:5000 wsgi:app &echo $'\n-----Deactivating venv-----\n'deactivateecho $'\n-----Done-----\n'
cd Runner/scripts
- Setting up
app.service
and creatingapp.sock
into your project.
#!/bin/bashecho $'\n-----Creating systemd unit file-----\n'
cd ../../if [ /etc/systemd/system/app.service ];
then
echo $'\n-----Removing existing app.service-----\n';
sudo rm /etc/systemd/system/app.service;
fiecho $'\n-----Creating app.service-----\n'var_username=$USERecho "Created gunicorn with $1 worker processes to start"
var_workers=$1DIR=$PWDsystemd_path="/etc/systemd/system/app.service"
systemd_content=$'[Unit]\n# specifies metadata and dependencies\nDescription=Gunicorn instance to serve project\nAfter=network.target\n# tells the init system to only start this after the networking target has been reached\n# We will give our regular user account ownership of the process since it owns all of the relevant files\n[Service]\n# Service specify the user and group under which our process will run.\nUser='$var_username$'\n# give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.\nGroup=www-data\n# Well then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for the process are located (within our virtual environment).\nWorkingDirectory='$DIR$'\nEnvironment="PATH='$DIR$'/venv/bin"\n# Well then specify the commanded to start the service\nExecStart='$DIR$'/venv/bin/gunicorn --workers '$var_workers$' --bind unix:app.sock -m 007 wsgi:app\n# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-user system is up and running:\n[Install]\nWantedBy=multi-user.target'
echo "$systemd_content" > $systemd_pathsystemctl daemon-reload
echo $'\n-----Starting app-----\n'
sudo systemctl start app
sudo systemctl enable appecho $'\n-----Done-----\n'
cd Runner/scripts
- Creating
Nginx
files withserver_ip
and config thesites-available
withsites-enabled
and linking them togather.
#!/bin/bashcd ../../if [ /etc/nginx/sites-available/app ];
then
echo $'\n-----Removing existing nginx app-----\n';
sudo rm /etc/nginx/sites-available/app;
sudo rm /etc/nginx/sites-enabled/app;
fisystemctl daemon-reload
echo $'\n-----Creating nginx configuration-----\n';echo "setting $1 as server domain"
var_server_ip=$1DIR=$PWD
config=$'server {\n\tlisten 80;\n\tserver_name '$var_server_ip$';\n\nlocation / {\n\tinclude proxy_params;\n\tproxy_pass http://unix:'$DIR$'/app.sock;\n\t}\n}\n'
nginx_path="/etc/nginx/sites-available/app"echo $'\n-----Creating nginx sites-available-----\n';
echo "$config" > $nginx_pathecho $'\n-----Linking nginx server-----\n';
sudo ln -s $nginx_path /etc/nginx/sites-enabledecho $'\n-----Testing nginx server-----\n';
sudo nginx -techo $'\n-----Restarting nginx server-----\n';
sudo systemctl restart nginxecho $'\n-----Adjusting firewall to allow access to nginx server-----\n';
sudo ufw allow 'Nginx Full'echo $'\n-----Done-----\n';
cd Runner/scripts
- Creating a bash to pass arguments to the specific bash file.
#!/bin/bashecho $'\n-----Welcome to the EasyDeployer-----\n'cd scripts
source git_reset_and_pull.sh
source install_apt_tools.sh
source init_venv.sh
source install_requirements.sh
source setup_gunicorn.sh
source setup_systemd.sh $2
source config_nginx.sh $1echo $'\n-----Deployment Completed-----\n'
Bashes directory:
Runner
|____execute.sh
|____scripts/
|____git_reset_and_pull.sh
|____install_apt_tools.sh
|____init_venv.sh
|____install_requirements.sh
|____setup_gunicorn.sh
|____setup_systemd.sh
|____config_nginx.sh
Execute:
$ cd Runner/
$ source execute.sh example.com 3