From 9ddad3c2909e04964a805147f20005faf2434194 Mon Sep 17 00:00:00 2001 From: Vague Rabbit Date: Thu, 4 Feb 2021 18:11:19 -0800 Subject: [PATCH] ^ --- deploy-dashboard.sh | 88 ++++++++---- deploy-saleor.sh | 136 ++++++++++++++---- resources/saleor-dashboard/dashboard-location | 5 + resources/saleor-dashboard/server_block | 15 ++ resources/saleor-dashboard/template.env | 6 +- resources/saleor/server_block | 22 +-- resources/saleor/template.env | 3 +- resources/saleor/template.service | 2 +- resources/saleor/template.uwsgi | 32 +++++ 9 files changed, 239 insertions(+), 70 deletions(-) create mode 100644 resources/saleor-dashboard/dashboard-location create mode 100644 resources/saleor-dashboard/server_block create mode 100644 resources/saleor/template.uwsgi diff --git a/deploy-dashboard.sh b/deploy-dashboard.sh index 63e1c63..8afb533 100644 --- a/deploy-dashboard.sh +++ b/deploy-dashboard.sh @@ -1,4 +1,3 @@ - ######################################################################################### echo "" echo "Creating production deployment packages for Saleor Dashboard..." @@ -10,21 +9,23 @@ echo "" ######################################################################################### # Collect input from the user to assign required installation parameters ######################################################################################### -echo "Please provide details for your Saleor API instillation..." +echo "Please provide details for your Saleor Dashboard installation..." echo "" # Get the Dashboard & GraphQL host domain -while [ "$HOST" = "" ] +while [ "$SAME_HOST" = "" ] do - echo -n "Enter the Dashboard & GraphQL host domain:" - read HOST + echo -n "Are you hosting the Dashboard on the same host domain as the API (yes|no)?" + read SAME_HOST done # Get the API host IP or domain -while [ "$API_HOST" = "" ] -do - echo "" - echo -n "Enter the API host IP or domain:" - read API_HOST -done +if [ "SAME_HOST" = "no" ]; then + while [ "$APP_HOST" = "" ] + do + echo "" + echo -n "Enter the Dashboard host domain:" + read APP_HOST + done +fi # Get the APP Mount (Dashboard) URI while [ "$APP_MOUNT_URI" = "" ] do @@ -32,14 +33,6 @@ do echo -n "Enter the APP Mount (Dashboard) URI:" read APP_MOUNT_URI done -# Get an optional custom API port -echo -n "Enter the API port (optional):" -read API_PORT -# -if [[ "$API_PORT" = "" ]]; then - API_PORT="8000" -fi -# ######################################################################################### @@ -48,12 +41,19 @@ fi # Setup the environment variables for Saleor API ######################################################################################### # Build the API URL -APIURL="http://$API_HOST:$API_PORT/$APIURI/" +API_URL="http://$API_HOST:$API_PORT/$APIURI/" # Write the production .env file from template.env -sudo sed "s|{apiuri}|$APIURL| - s|{mounturi}|$APP_MOUNT_URI| - s|{url}|$HOST|" $HD/Deploy_Saleor/resources/saleor-dashboard/template.env > $HD/saleor-dashboard/.env -wait +if [ "SAME_HOST" = "no" ]; then + sudo sed "s|{api_url}|$API_URL| + s|{app_mount_uri}|$APP_MOUNT_URI| + s|{app_host}|$APP_HOST|" $HD/Deploy_Saleor/resources/saleor-dashboard/template.env > $HD/saleor-dashboard/.env + wait +else + sudo sed "s|{api_url}|$API_URL| + s|{app_mount_uri}|$APP_MOUNT_URI| + s|{app_host}|$HOST|" $HD/Deploy_Saleor/resources/saleor-dashboard/template.env > $HD/saleor-dashboard/.env + wait +fi ######################################################################################### @@ -83,11 +83,41 @@ wait ######################################################################################### - ######################################################################################### -# Tell the user what's happening +# Setup the nginx block and move the static build files ######################################################################################### -echo "I think we're done here." -echo "Test the installation." -echo "Run python3 manage.py createsuperuser from $HD/saleor" +echo "Moving static files for the Dashboard..." +echo "" +if [ "$SAME_HOST" = "no" ]; then + # Move static files for the Dashboard + sudo mv $HD/saleor-dashboard/build/* /var/www/$APP_HOST/ + # Make an empry variable + DASHBOARD_LOCATION="" + # Clean the saleor server block + sudo sed -i "s|{dashboard-location}|$DASHBOARD_LOCATION|" /etc/nginx/sites-available/saleor + # Create the saleor-dashboard server block + sudo sed "s|{hd}|$HD|g + s/{app_mount_uri}/$APP_MOUNT_URI/g + s/{host}/$APP_HOST/g" $HD/Deploy_Saleor/resources/saleor-dashboard/server_block > /etc/nginx/sites-available/saleor-dashboard + wait + echo "Enabling server block and Restarting nginx..." + sudo ln -s /etc/nginx/sites-available/saleor-dashboard /etc/nginx/sites-enabled/ + sudo systemctl restart nginx +else + # Move static files for the Dashboard + sudo mv $HD/saleor-dashboard/build/* /var/www/$HOST/ + # Populate the DASHBOARD_LOCATION variable + DASHBOARD_LOCATION=$(<$HD/Deploy_Saleor/resources/saleor-dashboard/dashboard-location) + # Modify the new server block + sudo sed -i "s|{dashboard-location}|$DASHBOARD_LOCATION|" /etc/nginx/sites-available/saleor + wait + # Modify the new server block again + sudo sed -i "s|{hd}|$HD|g + s/{app_mount_uri}/$APP_MOUNT_URI/g + s/{host}/$HOST/g" $HD/Deploy_Saleor/resources/saleor-dashboard/server + wait + echo "Enabling server block and Restarting nginx..." + sudo ln -s /etc/nginx/sites-available/saleor /etc/nginx/sites-enabled/ + sudo systemctl restart nginx +fi ######################################################################################### \ No newline at end of file diff --git a/deploy-saleor.sh b/deploy-saleor.sh index 6d5df9b..f60acf5 100644 --- a/deploy-saleor.sh +++ b/deploy-saleor.sh @@ -41,20 +41,45 @@ while [ -n "$1" ]; do # while loop starts ;; -host) + HOST="$2" + shift + ;; + + -api-host) API_HOST="$2" shift ;; - -uri) + -api-port) + API_PORT="$2" + shift + ;; + + -dashboard-uri) APP_MOUNT_URI="$2" shift ;; - -url) + -static-url) STATIC_URL="$2" shift ;; + -media-url) + MEDIA_URL="$2" + shift + ;; + + -admin-email) + ADMIN_EMAIL="$2" + shift + ;; + + -admin-pw) + ADMIN_PASS="$2" + shift + ;; + -dbhost) PGDBHOST="$2" shift @@ -65,6 +90,31 @@ while [ -n "$1" ]; do # while loop starts shift ;; + -graphql-port) + GQL_PORT="$2" + shift + ;; + + -graphql-uri) + APIURI="$2" + shift + ;; + + -email) + EMAIL="$2" + shift + ;; + + -email-pw) + EMAIL_PW="$2" + shift + ;; + + -email-host) + EMAIL_HOST="$2" + shift + ;; + -repo) REPO="$2" shift @@ -247,16 +297,22 @@ done # Get an optional custom API port echo -n "Enter the API port (optional):" read API_PORT -# Get the APP Mount (Dashboard) URI -while [ "$APP_MOUNT_URI" = "" ] -do - echo "" - echo -n "Enter the Dashboard URI:" - read APP_MOUNT_URI -done # Get an optional custom Static URL -echo -n "Enter a custom Static Files URI (optional):" -read STATIC_URL +if [ $STATIC_URL = "" ]; then + echo -n "Enter a custom Static Files URI (optional):" + read STATIC_URL + STATIC_URL="/$STATIC_URL/" +else + STATIC_URL="/$STATIC_URL/" +fi +# Get an optional custom media URL +if [ $MEDIA_URL = "" ]; then + echo -n "Enter a custom Media Files URI (optional):" + read MEDIA_URL + MEDIA_URL="/$MEDIA_URL/" +else + MEDIA_URL="/$MEDIA_URL/" +fi # Get the Admin's email address while [ "$ADMIN_EMAIL" = "" ] do @@ -400,7 +456,7 @@ fi ###### This following section is for future use and will be modified to allow an alternative repo clone ###### # Was the -v (version) option used or Mirumee repo specified? if [ "vOPT" = "true" ] || [ "$REPO" = "mirumee" ]; then - # Create the new service file + # Create the saleor service file sudo sed "s/{un}/$UN/ s|{hd}|$HD|" $HD/Deploy_Saleor/resources/saleor/template.service > /etc/systemd/system/saleor.service wait @@ -409,11 +465,13 @@ if [ "vOPT" = "true" ] || [ "$REPO" = "mirumee" ]; then # Remove the old service file sudo rm /etc/nginx/sites-available/saleor fi - # Create the new server block + # Create the saleor server block sudo sed "s|{hd}|$HD|g s/{api_host}/$API_HOST/ s/{host}/$HOST/g - s/{apiport}/$API_PORT/" $HD/Deploy_Saleor/resources/saleor/server_block > /etc/nginx/sites-available/saleor + s|{static}|$STATIC_URL|g + s|{media}|$MEDIA_URL|g + s/{api_port}/$API_PORT/" $HD/Deploy_Saleor/resources/saleor/server_block > /etc/nginx/sites-available/saleor wait else # Create the new service file @@ -429,9 +487,17 @@ else sudo sed "s|{hd}|$HD|g s/{api_host}/$API_HOST/ s/{host}/$HOST/g - s/{apiport}/$API_PORT/" $HD/Deploy_Saleor/resources/saleor/server_block > /etc/nginx/sites-available/saleor + s|{static}|$STATIC_URL|g + s|{media}|$MEDIA_URL|g + s/{api_port}/$API_PORT/" $HD/Deploy_Saleor/resources/saleor/server_block > /etc/nginx/sites-available/saleor wait fi +# Create the host directory in /var/www/ +sudo mkdir /var/www/$HOST +wait +# Create the media directory +sudo mkdir /var/www/${HOST}${MEDIA_URL} +# Static directory will be moved into /var/www/$HOST/ after collectstatic is performed ######################################################################################### @@ -459,8 +525,9 @@ sudo sed "s|{dburl}|$DB_URL| s|{emailurl}|$EMAIL_URL| s/{chosts}/$C_HOSTS/ s/{ahosts}/$A_HOSTS/ - s|{static}|$STATIC_URL| - s|{media}|$MEDIA_URL| + s/{host}/$HOST/g + s|{static}|$STATIC_URL|g + s|{media}|$MEDIA_URL|g s/{adminemail}/$ADMIN_EMAIL/ s/{gqlorigins}/$QL_ORIGINS/" $HD/Deploy_Saleor/resources/saleor/template.env > $HD/saleor/.env wait @@ -545,8 +612,19 @@ wait # Build the emails npm run build-emails wait +# Run the uwsgi socket and create it for the first time +uwsgi --ini $HD/saleor/saleor/wsgi/uwsgi.ini --pidfile $HD/saleortemp.pid +wait +sleep 5 +# Stop the uwsgi processes +uwsgi --stop $HD/saleortemp.pid # Exit the virtual environment here? _#_ deactivate +# Move static files to /var/www/$HOST +sudo mv $HD/saleor/saleor$STATIC_URL /var/www/${HOST}${STATIC_URL} +sudo chown -R www-data:www-data /var/www/$HOST +sudo chmod -R 776 /var/www/$HOST +sudo chown -R $UN:www-data $HD/saleor ######################################################################################### @@ -554,16 +632,12 @@ deactivate ######################################################################################### # Enable the Saleor service ######################################################################################### -# Update with defaults +# Enable sudo systemctl enable saleor.service -######################################################################################### +# Reload the daemon sudo systemctl daemon-reload -systemctl start emperor.uwsgi.service - -######################################################################################### -echo "Enabling server block and Restarting nginx..." -sudo ln -s /etc/nginx/sites-available/saleor /etc/nginx/sites-enabled/ -sudo systemctl restart nginx +# Start the service +sudo systemctl start saleor.service ######################################################################################### @@ -577,8 +651,18 @@ echo "" ######################################################################################### + ######################################################################################### # Call the dashboard deployment script ######################################################################################### -source ./deploy-dashboard.sh +source $HD/Deploy_Saleor/deploy-dashboard.sh +######################################################################################### + + + +######################################################################################### +# Tell the user what's happening +######################################################################################### +echo "I think we're done here." +echo "Test the installation." ######################################################################################### \ No newline at end of file diff --git a/resources/saleor-dashboard/dashboard-location b/resources/saleor-dashboard/dashboard-location new file mode 100644 index 0000000..e54cd5e --- /dev/null +++ b/resources/saleor-dashboard/dashboard-location @@ -0,0 +1,5 @@ + location ^~ /{app_mount_uri} { + alias /var/www/{host}/{app_mount_uri}; + index index.html; + try_files $uri $uri/ /{app_mount_uri}/index.html; + } \ No newline at end of file diff --git a/resources/saleor-dashboard/server_block b/resources/saleor-dashboard/server_block new file mode 100644 index 0000000..c5ed4cc --- /dev/null +++ b/resources/saleor-dashboard/server_block @@ -0,0 +1,15 @@ +# configuration of the server +server { + listen 80; + server_name {host} www.{host}; + charset utf-8; + root /var/www/{host} + # max upload size + client_max_body_size 75M; + + location ^~ /{app_mount_uri} { + alias /var/www/{host}/{app_mount_uri}; + index index.html; + try_files $uri $uri/ /{app_mount_uri}/index.html; + } +} \ No newline at end of file diff --git a/resources/saleor-dashboard/template.env b/resources/saleor-dashboard/template.env index 78abcb5..6612ed1 100644 --- a/resources/saleor-dashboard/template.env +++ b/resources/saleor-dashboard/template.env @@ -1,3 +1,3 @@ -API_URI="{apiuri}" -APP_MOUNT_URI="/{dashuri}/" -STATIC_URL="{url}" \ No newline at end of file +API_URI="{api_url}" +APP_MOUNT_URI="/{app_mount_uri}/" +STATIC_URL="http://{app_host}/" \ No newline at end of file diff --git a/resources/saleor/server_block b/resources/saleor/server_block index fd43180..1a88784 100644 --- a/resources/saleor/server_block +++ b/resources/saleor/server_block @@ -2,20 +2,25 @@ upstream django { server unix://{hd}/saleor/saleor.sock; } + # configuration of the server server { listen 80; server_name {host} www.{host}; charset utf-8; + # max upload size client_max_body_size 75M; + # Django media and static files - location /media { - alias {hd}}/saleor/media; + location {media} { + alias /var/www/{host}{media}; } - location /static { - alias {hd}/saleor/static; + + location {static} { + alias /var/www/{host}{static}; } + # Send all non-media requests to the Django server. location / { uwsgi_pass django; @@ -23,12 +28,9 @@ server { } location /graphql/ { - proxy_pass https://{apihost}:{apiport}; + proxy_pass https://{api_host}:{api_port}; } - location /dashboard/ { - path {hd}/saleor-dashboard/build/; - index index.html; - try_files $uri $uri/ /dashboard/index.html; - } +{dashboard-location} + } \ No newline at end of file diff --git a/resources/saleor/template.env b/resources/saleor/template.env index 4af76bd..f8121df 100644 --- a/resources/saleor/template.env +++ b/resources/saleor/template.env @@ -14,8 +14,9 @@ EMAIL_URL="{emailurl}" #SENDGRID_USERNAME="{sguser}" #SENDGRID_PASSWORD="{sgpass}" #DEFAULT_FROM_EMAIL="{dfemail}" - +STATIC_ROOT="/var/www/{host}{static}" STATIC_URL="{static}" +MEDIA_ROOT="/var/www/{host}{media}" MEDIA_URL="{media}" #MAX_CHECKOUT_LINE_QUANTITY="{mclq}" diff --git a/resources/saleor/template.service b/resources/saleor/template.service index d167cf5..44689de 100644 --- a/resources/saleor/template.service +++ b/resources/saleor/template.service @@ -4,6 +4,6 @@ After=network.target [Service] User={un} Restart=always -ExecStart={hd}/env/saleor/bin/uwsgi --emperor {hd}/env/saleor/vassals --uid www-data --gid www-data +ExecStart={hd}/env/saleor/bin/uwsgi --master --emperor {hd}/env/saleor/vassals --uid www-data --gid www-data [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/resources/saleor/template.uwsgi b/resources/saleor/template.uwsgi new file mode 100644 index 0000000..0f2ccaf --- /dev/null +++ b/resources/saleor/template.uwsgi @@ -0,0 +1,32 @@ +[uwsgi] +die-on-term = true +enable-threads = true +http = :$(PORT) + +# full path to Django project's root directory +chdir = {hd}/saleor/ +# Django's wsgi file +module = saleor.wsgi:application +# full path to python virtual env +home = {hd}/env/saleor +# enable uwsgi master process +master = true +# maximum number of worker processes +processes = 4 +# the socket (use the full path to be safe +socket = {hd}/saleor/saleor.sock +# socket permissions +chmod-socket = 666 +# clear environment on exit +vacuum = true +# daemonize uwsgi and write messages into given log +daemonize = {hd}/uwsgi-emperor.log + +log-format = UWSGI uwsgi "%(method) %(uri) %(proto)" %(status) %(size) %(msecs)ms [PID:%(pid):Worker-%(wid)] [RSS:%(rssM)MB] +max-requests = 100 +memory-report = true +static-map = /static=/app/static +mimefile = /etc/mime.types +ignore-sigpipe = true +ignore-write-errors = true +disable-write-exception = true \ No newline at end of file