127 lines
3.8 KiB
Bash
Executable File
127 lines
3.8 KiB
Bash
Executable File
#! /bin/bash
|
|
|
|
# JConf, a JupyterHub user manager. Manages system users and links them with jupyterhub accounts.
|
|
# Version 0.1 - 24th Nov, 2025
|
|
# MIT Licensed, by The Grammer Society
|
|
|
|
|
|
# required options:
|
|
# setup: must be run after downloaded. Will create a new venv in /opt, installs jupyterhub, lab and notebook. Checks if npm is installed, then installs configurable http server, installs itself to /usr/bin. Installs service file, enables and starts it. Adds root to admins.
|
|
# adduser: creates a system user, asks for password, calls jupyterhub api to create a new user, adds penv to user, su user, penv init and penv creates a default environment. Also copy some instructions in readme file.
|
|
# deluser: deletes jupyterhub user, deletes user home, deletes user profile from system.
|
|
# start: starts the server, systemd call
|
|
# stop: stops the server, systemd call
|
|
# reference: https://medium.com/@prateekaverma/install-jupyterhub-and-jupyterlab-on-ubuntu-server-d390570b5bb6
|
|
# detect if jconf is already installed, print who the admin is if so.
|
|
|
|
title() {
|
|
echo "JConf: JupyterHub Configuration Manager."
|
|
}
|
|
|
|
usage() {
|
|
echo "Must run as root."
|
|
echo "Usage: jconf <command> <args>"
|
|
echo ""
|
|
echo "User Management Commands:"
|
|
echo " - adduser <name>"
|
|
echo " - deluser <name>"
|
|
echo " - listusers"
|
|
echo ""
|
|
echo "Server Management Commands:"
|
|
echo " - start"
|
|
echo " - stop"
|
|
echo " - install"
|
|
echo " - uninstall"
|
|
}
|
|
|
|
|
|
if [ "$#" -lt 1 ]; then
|
|
title
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$EUID" -ne 0 ]]; then
|
|
echo "Run the script as root"
|
|
exit 2
|
|
fi
|
|
|
|
|
|
cmd=$1
|
|
case "$cmd" in
|
|
adduser|mku)
|
|
USER=$(echo "$2" | tr "[:upper:]" "[:lower:]")
|
|
exist=$(awk -F: -v username="$USER" '{if ($1 == username && $3 >= 1000 && $1 != "nobody") {print 1; found=1; exit}} END {if (!found) print 0}' /etc/passwd)
|
|
if [ "$exist" -eq 1 ]; then
|
|
echo "$USER user already exists"
|
|
else
|
|
read -rp "Do you want to create an user account $USER [y/N]:" yn
|
|
case $yn in
|
|
[Yy])
|
|
useradd "$USER"
|
|
echo "Enter the password for $USER"
|
|
passwd "$USER"
|
|
echo "$USER user account created successfully"
|
|
;;
|
|
[Nn])
|
|
exit 1
|
|
;;
|
|
* )
|
|
echo "Please answer Y or N."
|
|
;;
|
|
esac
|
|
fi
|
|
exit 1
|
|
;;
|
|
deluser|rmu)
|
|
USER=$(echo "$2" | tr "[:upper:]" "[:lower:]")
|
|
exist=$(awk -F: -v username="$USER" '{if ($1 == username && $3 >= 1000 && $1 != "nobody") {print 1; found=1; exit}} END {if (!found) print 0}' /etc/passwd)
|
|
if [ "$exist" -eq 0 ]; then
|
|
echo "$USER user doesn't exist"
|
|
exit 1
|
|
else
|
|
read -rp "Do you want to delete $USER user account? [y/N]:" yn
|
|
case $yn in
|
|
[Yy])
|
|
;;
|
|
*)
|
|
exit 1
|
|
;;
|
|
esac
|
|
read -rp "This DELETES $USER's data, are you sure? [y/N]:" yn
|
|
case $yn in
|
|
[Yy])
|
|
userdel -rRZ "$USER";
|
|
echo "$USER user account deleted successfully"
|
|
;;
|
|
[Nn])
|
|
exit 1
|
|
;;
|
|
* )
|
|
echo "Please answer Y or N."
|
|
;;
|
|
esac
|
|
fi
|
|
exit 1
|
|
;;
|
|
listusers|ls)
|
|
husers=$(awk -F: '{ if ($3 >= 1000 && $1 != "nobody" ) {print $1}}' /etc/passwd)
|
|
echo "$husers"
|
|
;;
|
|
start)
|
|
echo
|
|
;;
|
|
stop)
|
|
echo
|
|
;;
|
|
install)
|
|
echo
|
|
;;
|
|
uninstall)
|
|
echo
|
|
;;
|
|
*)
|
|
echo
|
|
;;
|
|
esac
|