diff --git a/.gitignore b/.gitignore index 137ebe9..97e9f72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ images/ .env bitcoin-22.0/ +nmap.txt diff --git a/Makefile b/Makefile index 3aa3af6..d174a15 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,10 @@ prosperity: free: @echo "This will install freespace, once it exists" + +flask: + @chmod +x recipes/flask.sh + @recipes/flask.sh manifest: @chmod +x recipes/wordpress.sh diff --git a/ingredients/copper b/ingredients/copper index d4a517d..e7bfdf2 100644 --- a/ingredients/copper +++ b/ingredients/copper @@ -281,4 +281,6 @@ check_ports() { fi } +# TODO Expose ports via firewalld + # using yggdrasil could maybe fit in here diff --git a/ingredients/iron b/ingredients/iron index e4b329a..9813796 100644 --- a/ingredients/iron +++ b/ingredients/iron @@ -59,9 +59,7 @@ build_service_from_template() { read reset case $reset in "Y" | "y") - cat /etc/systemd/system/ sudo rm $SERVICE_FILE - cat /etc/systemd/system/ ;; "N" | "n") echo "Okay, we'll leave it as is." diff --git a/ingredients/lead b/ingredients/lead index 336b6ca..7ec1159 100644 --- a/ingredients/lead +++ b/ingredients/lead @@ -121,6 +121,14 @@ source_env check_for() { 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!) install_if_needed() { @@ -194,12 +202,12 @@ remember() { say "Keys must consist only of capital letters and underscores" fi - if [[ ! $VALUE =~ ^[A-Za-z0-9/_.:]+$ ]]; then - say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\" + if [[ ! $VALUE =~ ^[A-Za-z0-9/_.:-]+$ ]]; then + say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\" fi # 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}) # If something already exists and we're trying to set it to something new @@ -230,3 +238,5 @@ remember() { } LEAD=1 + +# Confirm? Something to automate the y/n dialog diff --git a/recipes/flask.sh b/recipes/flask.sh new file mode 100755 index 0000000..cf35357 --- /dev/null +++ b/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 diff --git a/recipes/init.sh b/recipes/init.sh index 101caf4..d233ba8 100755 --- a/recipes/init.sh +++ b/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 # ~ Zen, 2022 -if [ -f "/etc/debian_version" ]; then - 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 -} +source ingredients/lead echo "Updating the repositories..." case $DISTRO in @@ -66,13 +25,13 @@ echo "" echo "Making sure we've got the basics..." case $DISTRO in "debian") - install_if_needed make vim tmux zsh git silversearcher-ag + install_if_needed vim tmux zsh git silversearcher-ag ;; "arch") - install_if_needed make vim tmux zsh git the_silver_searcher + install_if_needed vim tmux zsh git the_silver_searcher ;; "mac") - install_if_needed make vim tmux zsh git the_silver_searcher + install_if_needed vim tmux zsh git the_silver_searcher ;; esac 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 echo "" -# TODO is this neededd? can we install p10k on base zsh? -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)" +# TODO is this needed? can we install p10k on base zsh? +#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)" cat resources/zshrc-extras >> $HOME/.zshrc 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 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 diff --git a/resources/flask/app.py b/resources/flask/app.py new file mode 100644 index 0000000..dbe31f5 --- /dev/null +++ b/resources/flask/app.py @@ -0,0 +1,10 @@ +from flask import Flask, render_template +import os + +app = Flask(__name__) + +@app.route("/") +@app.route("/") +def demo(name=None): + wd = os.popen('pwd').readline() + return render_template('demo.html', name=name, wd=wd) diff --git a/resources/flask/demo.css b/resources/flask/demo.css new file mode 100644 index 0000000..d570aa5 --- /dev/null +++ b/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; +} diff --git a/resources/flask/demo.html b/resources/flask/demo.html new file mode 100644 index 0000000..7422744 --- /dev/null +++ b/resources/flask/demo.html @@ -0,0 +1,32 @@ + + + + + Flask Boilerplate + + + +
This server is running via the app.py + file in {{ wd }}
+
+ {% if name %} +

Hello, {{ name }}!

+

You provided me a name through the URL of this webpage.

+

Flask parsed it and passed it as a parameter to the + render_template function in flask_demo.py, + which is located in the directory where you installed Flask

+

It also imports the css as a file in the static/ + directory. This is the standard used for images etc.

+
+

There's plenty to learn about Flask! Hopefully this gives you + some of the information you need to dive deeper

+ {% else %} +

Hello!

+

This is a page served up by Flask. Try adding a / followed by + your first name in the URL, or click here + for a demonstration of URL parsing.

+ {% endif %} +
+ + + diff --git a/resources/flask/requirements.txt b/resources/flask/requirements.txt new file mode 100644 index 0000000..6fa9321 --- /dev/null +++ b/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 diff --git a/resources/nginx/flask.nginx.conf b/resources/nginx/flask.nginx.conf new file mode 100644 index 0000000..c1b43a1 --- /dev/null +++ b/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; + } +} diff --git a/resources/service-templates/flask b/resources/service-templates/flask new file mode 100644 index 0000000..58a9a99 --- /dev/null +++ b/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