This commit is contained in:
m3tam3re
2025-04-11 11:33:45 +02:00
parent 2d22b15e85
commit 8b975e7d54
5 changed files with 216 additions and 70 deletions

View File

@@ -65,6 +65,14 @@ setup_latest_version() {
local target_version=$1
local dir_name=$2
# Safety check: Ensure we're in the self-host-playbook directory
local current_dir=$(basename "$(pwd)")
if [ "$current_dir" != "self-host-playbook" ]; then
echo "❌ Error: Must be in 'self-host-playbook' directory to setup latest version"
echo "Current directory: $(pwd)"
return 1
fi
echo "⬇️ Downloading version $target_version..."
TEMP_DIR=$(mktemp -d)
@@ -80,6 +88,12 @@ setup_latest_version() {
return 1
fi
# Additional safety check before copying files
if [ ! -f "$CLONE_DIR/flake.nix" ]; then
echo "❌ Error: Downloaded content doesn't appear to be a valid self-host-playbook"
return 1
fi
# Copy files from clone to target directory
cp -r "$CLONE_DIR"/* "$dir_name/"
@@ -88,12 +102,25 @@ setup_latest_version() {
# Function to setup from template
setup_from_template() {
# Ensure we're in the correct directory
local current_dir=$(basename "$(pwd)")
if [ "$current_dir" != "self-host-playbook" ]; then
echo "❌ Error: Must be in 'self-host-playbook' directory"
exit 1
fi
# Create backup if directory is not empty
if [ -n "$(ls -A)" ]; then
local CURRENT_VERSION=$(date +%Y%m%d_%H%M%S)
local backup_dir="backup_${CURRENT_VERSION}_$(date +%Y%m%d_%H%M%S)"
echo "📑 Creating backup in $backup_dir..."
mkdir -p "$backup_dir"
# Add safety check for backup creation
if [ ! -d "$backup_dir" ]; then
echo "❌ Error: Failed to create backup directory"
exit 1
fi
find . -maxdepth 1 ! -name "." ! -name ".." ! -name "$backup_dir" -exec cp -r {} "$backup_dir/" \;
echo "✅ Backup created successfully"
@@ -112,7 +139,7 @@ setup_from_template() {
generate_ssh_key() {
local KEY_NAME="self-host-playbook"
local KEY_PATH="$HOME/.ssh/${KEY_NAME}"
W
if [ ! -f "$KEY_PATH" ]; then
mkdir -p "$HOME/.ssh"
echo "🔑 Generating new SSH key pair..." >&2
@@ -195,6 +222,7 @@ get_device_name() {
setup_ssh_config() {
local username=$1
local ip_address=$2
local hostname=$3 # Add hostname parameter
local ssh_config_dir="$HOME/.ssh"
local ssh_config_file="$ssh_config_dir/config"
local ssh_key_file="$ssh_config_dir/self-host-playbook"
@@ -203,19 +231,20 @@ setup_ssh_config() {
mkdir -p "$ssh_config_dir"
chmod 700 "$ssh_config_dir"
# Create or append to SSH config
local config_entry="Host self-host-playbook
# Create or append to SSH config using hostname as the Host name
local config_entry="Host $hostname
HostName $ip_address
User $username
Port 2222
IdentityFile $ssh_key_file"
# Check if entry already exists
if ! grep -q "Host self-host-playbook" "$ssh_config_file" 2>/dev/null; then
if ! grep -q "Host $hostname" "$ssh_config_file" 2>/dev/null; then
echo -e "\n$config_entry" >> "$ssh_config_file"
echo "✅ Added SSH config entry"
else
# Update existing entry
sed -i.bak "/Host self-host-playbook/,/IdentityFile.*/{
sed -i.bak "/Host $hostname/,/IdentityFile.*/{
s/HostName.*/HostName $ip_address/
s/User.*/User $username/
}" "$ssh_config_file"
@@ -265,11 +294,11 @@ SSH_PUB_KEY=$(cat "${SSH_KEY_PATH}.pub") || {
echo
echo "🔑 Here is your public key:"
echo
cat $SSH_KEY_PATH.pub
cat "$SSH_KEY_PATH.pub"
echo ""
echo "📁 You can also find the keyfile here:"
echo
echo $SSH_KEY_PATH.pub
echo "$SSH_KEY_PATH.pub"
echo
read -p "Press ENTER to continue or CTRL + C to abort..."
@@ -278,15 +307,23 @@ echo ""
echo "📝 Please provide the following information:"
echo "-------------------------------------------"
read -p "1. Enter target server IP address: " IP_ADDRESS
read -p "2. Enter desired username for server access: " USERNAME
read -s -p "3. Enter desired password: " PASSWORD
read -p "2. Enter hostname for the server: " HOSTNAME
# Validate hostname format
while ! [[ $HOSTNAME =~ ^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$ ]]; do
echo "❌ Invalid hostname format. Please use a valid hostname (e.g., my-server.example.com)"
read -p "Enter hostname for the server: " HOSTNAME
done
read -p "3. Enter desired username for server access: " USERNAME
read -s -p "4. Enter desired password: " PASSWORD
echo
echo "4. Enter domain names for services (must point to $IP_ADDRESS):"
echo "5. Enter domain names for services (must point to $IP_ADDRESS):"
read -p " - Domain for Portainer: " PORTAINER_DOMAIN
read -p " - Domain for n8n: " N8N_DOMAIN
read -p " - Domain for Baserow: " BASEROW_DOMAIN
echo
echo "5. How do you authentiate to the target machine?"
echo "6. How do you authenticate to the target machine?"
echo "-------------------------------------------"
echo " 1) Password"
echo " 2) SSH Key"
@@ -295,10 +332,10 @@ read -p "Enter your choice (1-2): " KEY_CHOICE
case $KEY_CHOICE in
1)
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server root@$IP_ADDRESS"
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#$HOSTNAME root@$IP_ADDRESS"
;;
2)
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server -i $SSH_KEY_PATH root@$IP_ADDRESS"
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#$HOSTNAME -i $SSH_KEY_PATH root@$IP_ADDRESS"
;;
*)
echo "❌ Invalid choice"
@@ -309,7 +346,7 @@ esac
setup_from_template
echo
echo "6. Select your cloud provider:"
echo "7. Select your cloud provider:"
echo " 1) AWS (Newer instances with NVMe)"
echo " 2) AWS (Older instances)"
echo " 3) Google Cloud Platform"
@@ -387,7 +424,8 @@ cat > config.json << EOF
"baserow": "$BASEROW_DOMAIN"
},
"rootDevice": "$DEVICE_NAME",
"ipAddress": "$IP_ADDRESS"
"ipAddress": "$IP_ADDRESS",
"hostname": "$HOSTNAME"
}
EOF
@@ -416,7 +454,7 @@ echo "This process might take several minutes..."
# Run nixos-anywhere installation
$INSTALL_COMMAND && {
echo "🔧 Setting up SSH configuration..."
setup_ssh_config "$USERNAME" "$IP_ADDRESS"
setup_ssh_config "$USERNAME" "$IP_ADDRESS" "$HOSTNAME"
echo
echo "🎉 Installation completed successfully!"
echo "=====================================>"
@@ -426,7 +464,7 @@ $INSTALL_COMMAND && {
echo "- Baserow: https://$BASEROW_DOMAIN"
echo
echo "To connect to your server, use:"
echo "ssh self-host-playbook"
echo "ssh $HOSTNAME"
echo
install_deploy_rs
echo