Browse Source

Merge branch 'flask'

main
Zen 3 years ago
parent
commit
664f610f7a
  1. 1
      .gitignore
  2. 4
      Makefile
  3. 2
      ingredients/copper
  4. 2
      ingredients/iron
  5. 16
      ingredients/lead
  6. 140
      recipes/flask.sh
  7. 57
      recipes/init.sh
  8. 10
      resources/flask/app.py
  9. 12
      resources/flask/demo.css
  10. 32
      resources/flask/demo.html
  11. 7
      resources/flask/requirements.txt
  12. 17
      resources/nginx/flask.nginx.conf
  13. 12
      resources/service-templates/flask

1
.gitignore vendored

@ -1,3 +1,4 @@
images/ images/
.env .env
bitcoin-22.0/ bitcoin-22.0/
nmap.txt

4
Makefile

@ -31,6 +31,10 @@ prosperity:
free: free:
@echo "This will install freespace, once it exists" @echo "This will install freespace, once it exists"
flask:
@chmod +x recipes/flask.sh
@recipes/flask.sh
manifest: manifest:
@chmod +x recipes/wordpress.sh @chmod +x recipes/wordpress.sh

2
ingredients/copper

@ -281,4 +281,6 @@ check_ports() {
fi fi
} }
# TODO Expose ports via firewalld
# using yggdrasil could maybe fit in here # using yggdrasil could maybe fit in here

2
ingredients/iron

@ -59,9 +59,7 @@ build_service_from_template() {
read reset read reset
case $reset in case $reset in
"Y" | "y") "Y" | "y")
cat /etc/systemd/system/
sudo rm $SERVICE_FILE sudo rm $SERVICE_FILE
cat /etc/systemd/system/
;; ;;
"N" | "n") "N" | "n")
echo "Okay, we'll leave it as is." echo "Okay, we'll leave it as is."

16
ingredients/lead

@ -121,6 +121,14 @@ source_env
check_for() { check_for() {
command -v "$1" >/dev/null command -v "$1" >/dev/null
} }
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}Woah there!${RESET} Seems you're running this script as a superuser."
echo ""
echo "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions"
echo ""
exit 1
fi
# This one installs utilities to your OS (If you need them!) # This one installs utilities to your OS (If you need them!)
install_if_needed() { install_if_needed() {
@ -194,12 +202,12 @@ remember() {
say "Keys must consist only of capital letters and underscores" say "Keys must consist only of capital letters and underscores"
fi fi
if [[ ! $VALUE =~ ^[A-Za-z0-9/_.:]+$ ]]; then if [[ ! $VALUE =~ ^[A-Za-z0-9/_.:-]+$ ]]; then
say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\" say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\"
fi fi
# If we're setting a valid key/value pair # If we're setting a valid key/value pair
if [[ ${1} =~ ^[A-Z_]+\=[A-Za-z0-9/._:]*$ ]]; then if [[ ${1} =~ ^[A-Z_]+\=[A-Za-z0-9/._:-]*$ ]]; then
DOTENV_ENTRY=$(cat .env | grep ${KEY}) DOTENV_ENTRY=$(cat .env | grep ${KEY})
# If something already exists and we're trying to set it to something new # If something already exists and we're trying to set it to something new
@ -230,3 +238,5 @@ remember() {
} }
LEAD=1 LEAD=1
# Confirm? Something to automate the y/n dialog

140
recipes/flask.sh

@ -0,0 +1,140 @@
#!/bin/sh
# Script for installing a simple Flask server and deploying it
# Bare Metal Alchemist, 2022
source ingredients/lead
source ingredients/iron
source ingredients/copper
clear
say "${BLUE}"
say " ${AAAAAAAAAAAA} &&&&&&&&&&& "
say " ${AAAAAAAAAAAA} &@ && "
say " ${AAAAAAAAAAAA} &@ && "
say " ${AAAAAAAAAAAA} &@ && "
say "${RESET} 8888888888 888 888 ${BLUE} /&& @& "
say "${RESET} 8888888888 888 888 ${BLUE} @&# &&&&&, "
say "${RESET} 888 888 888 ${BLUE} #&. /&/ "
say "${RESET} 888 888 888 ${BLUE} && *&@ "
say "${RESET} 8888888 888 8888b. .d8888b 888 888${BLUE} && &&&&&@ "
say "${RESET} 888 888 '88b 88K 888 .88P${BLUE} && && "
say "${RESET} 888 888 .d888888 'Y8888b. 888888K ${BLUE} &@ && "
say "${RESET} 888 888 888 888 X88 888 '88b${BLUE} && .&@ "
say "${RESET} 888 888 'Y888888 88888P' 888 888${BLUE} (&&&&&&&&&&&&&&&&&&&&&. "
say "${RESET}"
# ------------------- Step 1 - Baseline Setup -------------------
say "${BOLD}Hi again!${RESET} Looks like you want to get ${BLUE}Flask${RESET} up and running."
say "Well, as an alchemy-themed toolkit, I must say: ${GREEN}good decision :)${RESET}"
say ""
# Make sure this script isn't being run with sudo in front
if [ "$EUID" -eq 0 ]; then
say "${RED}${BOLD}Woah there!${RESET} Seems you're running this script as a superuser."
say ""
say "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions"
say ""
exit 1
fi
say "${ULINE}Making sure we've got the basics...${RESET}"
echo -e "(you'll probably need to input ${BLUE}your 'sudo' password${RESET} here)"
case $DISTRO in
"debian")
say "HEY I HAVEN'T TESTED THIS BY THE WAY"
install_if_needed python python-pip
pip install --upgrade pip
;;
"arch")
install_if_needed python python-pip
python -m pip install --upgrade pip
;;
"mac")
say "HEY I HAVEN'T TESTED THIS BY THE WAY"
install_if_needed python python-pip
pip install --upgrade pip
;;
"fedora")
say "HEY I HAVEN'T TESTED THIS BY THE WAY"
install_if_needed python python-pip
pip install --upgrade pip
;;
esac
echo ""
while [ ! -d "$FLASK_DIR" ]; do
ask_for FLASK_DIR "Please enter the path you would like to install \
Flask to (or enter nothing for ${BLUE}~/flask${RESET}): "
if [ -z "$FLASK_DIR" ]; then
remember "FLASK_DIR=$HOME/flask"
fi
say ""
ask_for CONFIRM "Okay, should we install to \
${BLUE}${FLASK_DIR}${RESET}? ${BLUE}(y/n)${RESET} "
case $CONFIRM in
"Y" | "y")
mkdir -p $FLASK_DIR
;;
esac
done
# ------------------- Step 2 - Create venv -------------------
if [ -d "$FLASK_VENV" ]; then
say "We already have a virtualenv folder for Flask here: ${BLUE}$FLASK_VENV${RESET}"
else
say "Creating virtual environment for Flask"
python -m venv $FLASK_DIR/venv
remember FLASK_VENV=$FLASK_DIR/venv
fi
if [ "$VIRTUAL_ENV" != "$FLASK_VENV" ]; then
say "Sourcing the virtual environment"
source ${FLASK_VENV}/bin/activate
fi
say "Making sure we've got all the python packages we need!"
pip install -r resources/flask/requirements.txt
say ""
# ------------------- Step 3 - Build Flask -------------------
say "${BOLD}We've got everything!${RESET} I'm going to set you up with \
a basic Flask page now\n"
mkdir -p ${FLASK_DIR}/{templates,static}
cp resources/flask/app.py ${FLASK_DIR}
cp resources/flask/demo.css ${FLASK_DIR}/static
cp resources/flask/demo.html ${FLASK_DIR}/templates
say "Flask directory initialized, setting up reverse proxy\n"
# ------------------- Step 4 - NGINX Setup -------------------
initialize_nginx
make_site flask "FILE_ROOT=${FLASK_DIR}"
say ""
configure_domain_for_site flask
enable_ssl
say "Excellent! We've configured this computer to serve this Flask\
server from ${BLUE}${ACCESS_POINT}:5000${RESET}"
# ------------------- Step 5 - Service Configuration -------------------
build_service_from_template flask "GUNICORN=`which gunicorn`" \
"FLASK_DIR=${FLASK_DIR}" "PORT=5000"
say ""
activate_service flask
say "${BOLD}\nAaaand, we're done!${RESET}\nAs long as everything \
worked properly, you should be able to visit your flask server at \
${BLUE}${ACCESS_POINT}:5000${RESET}"
say "\nThe main file is located in ${BLUE}${FLASK_DIR}${RESET}, other recipes may rely \
on making further modifications to this application. Take a look, and \
don't forget to experiment!"
exit 0

57
recipes/init.sh

@ -3,48 +3,7 @@
# This is a script to be run on a fresh installation of Raspbian in order to make it suitable (to me) for CLI development # This is a script to be run on a fresh installation of Raspbian in order to make it suitable (to me) for CLI development
# ~ Zen, 2022 # ~ Zen, 2022
if [ -f "/etc/debian_version" ]; then source ingredients/lead
DISTRO="debian"
echo "Debian, Ubuntu, or Raspbian OS detected."
elif [ -f "/etc/arch-release" ]; then
DISTRO="arch"
echo "Arch- or Manjaro-based OS detected."
elif [ $(uname | grep -c "Darwin") -eq 1 ]; then
DISTRO="mac"
echo "MacOS detected."
else
echo "I don't know what OS you're running! Cancelling this operation."
exit 1
fi
echo ""
# This makes sure that ctrl+C exits the entire script
trap "exit" INT
install_if_needed() {
for package in "$@"
do
if [ -z $(which $package) ]; then
echo "installing" $package
case $DISTRO in
"debian")
sudo apt install -y $package
;;
"arch")
sudo pacman -S $package --noconfirm
;;
"mac")
brew install $package
;;
esac
else
echo $package 'already installed!'
fi
done
}
echo "Updating the repositories..." echo "Updating the repositories..."
case $DISTRO in case $DISTRO in
@ -66,13 +25,13 @@ echo ""
echo "Making sure we've got the basics..." echo "Making sure we've got the basics..."
case $DISTRO in case $DISTRO in
"debian") "debian")
install_if_needed make vim tmux zsh git silversearcher-ag install_if_needed vim tmux zsh git silversearcher-ag
;; ;;
"arch") "arch")
install_if_needed make vim tmux zsh git the_silver_searcher install_if_needed vim tmux zsh git the_silver_searcher
;; ;;
"mac") "mac")
install_if_needed make vim tmux zsh git the_silver_searcher install_if_needed vim tmux zsh git the_silver_searcher
;; ;;
esac esac
echo "" echo ""
@ -90,9 +49,9 @@ cp resources/tmux.conf $HOME/.tmux.conf
cp resources/tmux-powerline-theme.sh $HOME/.tmux/tmux-powerline/themes/default.sh cp resources/tmux-powerline-theme.sh $HOME/.tmux/tmux-powerline/themes/default.sh
echo "" echo ""
# TODO is this neededd? can we install p10k on base zsh? # TODO is this needed? can we install p10k on base zsh?
echo "Installing Oh My Zsh for theming - this could take a moment" #echo "Installing Oh My Zsh for theming - this could take a moment"
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" #sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
cat resources/zshrc-extras >> $HOME/.zshrc cat resources/zshrc-extras >> $HOME/.zshrc
echo "" echo ""
@ -101,4 +60,4 @@ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$
sed -i 's/^ZSH_THEME.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/' $HOME/.zshrc sed -i 's/^ZSH_THEME.*/ZSH_THEME="powerlevel10k\/powerlevel10k"/' $HOME/.zshrc
echo "" echo ""
echo "...and we're back! Now that you've installed everything you need, try closing your connection to the terminal and re-opening." $SHELL

10
resources/flask/app.py

@ -0,0 +1,10 @@
from flask import Flask, render_template
import os
app = Flask(__name__)
@app.route("/")
@app.route("/<name>")
def demo(name=None):
wd = os.popen('pwd').readline()
return render_template('demo.html', name=name, wd=wd)

12
resources/flask/demo.css

@ -0,0 +1,12 @@
html {
font-family: sans-serif;
}
body {
margin: 40px auto;
max-width: 650px;
line-height: 1.6;
font-size: 18px;
color: #444;
padding: 0 10px;
}

32
resources/flask/demo.html

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>Flask Boilerplate</title>
<link rel="stylesheet" href="{{ url_for('static', filename='demo.css') }}">
</head>
<body>
<header>This server is running via the <tt>app.py</tt>
file in <tt>{{ wd }}</tt></header>
<main>
{% if name %}
<h1>Hello, {{ name }}!</h1>
<p>You provided me a name through the URL of this webpage.</p>
<p>Flask parsed it and passed it as a parameter to the
<tt>render_template</tt> function in <tt>flask_demo.py</tt>,
which is located in the directory where you installed Flask</p>
<p>It also imports the css as a file in the <tt>static/</tt>
directory. This is the standard used for images etc.</p>
<hr/>
<p>There's plenty to learn about Flask! Hopefully this gives you
some of the information you need to dive deeper</p>
{% else %}
<h1>Hello!</h1>
<p>This is a page served up by Flask. Try adding a / followed by
your first name in the URL, or click <a href="/beautiful%20human">here</a>
for a demonstration of URL parsing.</p>
{% endif %}
</main>
<footer><small>courtesy of Bare Metal Alchemist, March 2022</small></footer>
</body>
</html>

7
resources/flask/requirements.txt

@ -0,0 +1,7 @@
click==8.0.4
Flask==2.0.3
gunicorn==20.1.0
itsdangerous==2.1.0
Jinja2==3.0.3
MarkupSafe==2.1.0
Werkzeug==2.0.3

17
resources/nginx/flask.nginx.conf

@ -0,0 +1,17 @@
server {
listen 5000 default_server;
listen [::]:5000 default_server;
server_name _;
root FILE_ROOT;
index index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location / {
proxy_pass http://localhost:5000;
}
}

12
resources/service-templates/flask

@ -0,0 +1,12 @@
[Unit]
Description=Service File for a Flask application or API
After=network.target
[Service]
User=USER
WorkingDirectory=FLASK_DIR
ExecStart=GUNICORN -b localhost:PORT app:app
Restart=always
[Install]
WantedBy=multi-user.target
Loading…
Cancel
Save