setup-instance/init-instance.sh
2025-06-06 08:34:18 -06:00

62 lines
1.8 KiB
Bash

#!/usr/bin/env bash
#
# setup-student.sh
#
# Usage:
# curl -sL URL_TO_THIS_SCRIPT | bash -s -- GITHUB_USERNAME
#
# This script must be run as the 'rocky' user (with passwordless sudo).
# It takes one argument: the GitHub username of the student.
# It will:
# 1. Retrieve the student's public SSH keys from GitHub and install them
# so they can SSH in as 'rocky'.
# 2. Install cowsay, screen, and tmux.
# 3. Update /etc/motd to welcome the student via cowsay and point them
# at the tutorial repo.
set -euo pipefail
if [ "$#" -ne 1 ]; then
echo "Error: Missing GitHub username."
echo "Usage: $0 GITHUB_USERNAME"
exit 1
fi
GH_USER="$1"
SSH_DIR="/home/rocky/.ssh"
AUTHORIZED_KEYS="$SSH_DIR/authorized_keys"
MOTD_FILE="/etc/motd"
TUTORIAL_URL="https://github.com/OpenCHAMI/tutorial-2025"
echo "→ Installing SSH keys for GitHub user '$GH_USER'..."
# Create .ssh directory if needed
sudo mkdir -p "$SSH_DIR"
sudo chmod 700 "$SSH_DIR"
# Fetch and install public keys
KEYS=$(curl -fsSL "https://github.com/${GH_USER}.keys")
if [ ! -z "${KEYS}" ]; then
echo "${KEYS}" | sudo tee "$AUTHORIZED_KEYS" >/dev/null
echo " ✓ Retrieved keys from https://github.com/${GH_USER}.keys"
else
echo " ✗ Could not find keys for user '$GH_USER'; please check the username and that authorization SSH keys are present."
exit 2
fi
sudo chmod 600 "$AUTHORIZED_KEYS"
sudo chown -R rocky:rocky "$SSH_DIR"
echo "→ Installing packages: cowsay, screen, tmux..."
sudo dnf install -y cowsay screen tmux
echo "→ Updating MOTD in $MOTD_FILE..."
# Generate motd with cowsay greeting and tutorial link
sudo tee "$MOTD_FILE" >/dev/null <<EOF
$(/usr/bin/cowsay "Welcome, ${GH_USER}!")
To get started, see the tutorial at:
${TUTORIAL_URL}
EOF
echo "✔ Setup complete. Student '$GH_USER' can now SSH in and will be greeted on login."