+hostname setting

This commit is contained in:
m3tam3re 2025-04-11 09:36:53 +02:00
parent 1405f80bfc
commit f48ac1c9ae
4 changed files with 42 additions and 16 deletions

View File

@ -60,6 +60,7 @@ in {
# Networking # Networking
networking = { networking = {
hostName = jsonConfig.hostname;
firewall = { firewall = {
enable = true; enable = true;
# Only allow necessary ports # Only allow necessary ports

View File

@ -42,8 +42,8 @@
inherit self; inherit self;
}; };
}; };
deploy.nodes.server = { deploy.nodes.${jsonConfig.hostname} = {
hostname = "self-host-playbook"; hostname = jsonConfig.hostname;
profiles.system = { profiles.system = {
sshUser = jsonConfig.username; sshUser = jsonConfig.username;
user = "root"; user = "root";

View File

@ -279,10 +279,18 @@ echo ""
echo "📝 Please provide the following information:" echo "📝 Please provide the following information:"
echo "-------------------------------------------" echo "-------------------------------------------"
read -p "1. Enter target server IP address: " IP_ADDRESS read -p "1. Enter target server IP address: " IP_ADDRESS
read -p "2. Enter desired username for server access: " USERNAME read -p "2. Enter hostname for the server: " HOSTNAME
read -s -p "3. Enter desired password: " PASSWORD
# 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
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 Portainer: " PORTAINER_DOMAIN
read -p " - Domain for n8n: " N8N_DOMAIN read -p " - Domain for n8n: " N8N_DOMAIN
read -p " - Domain for Baserow: " BASEROW_DOMAIN read -p " - Domain for Baserow: " BASEROW_DOMAIN
@ -296,10 +304,10 @@ read -p "Enter your choice (1-2): " KEY_CHOICE
case $KEY_CHOICE in case $KEY_CHOICE in
1) 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) 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" echo "❌ Invalid choice"

View File

@ -153,24 +153,25 @@ setup_ssh_config() {
chmod 600 "$ssh_config_file" chmod 600 "$ssh_config_file"
} }
update_config_json() { update_config_value() {
local ip_address=$1 local key=$1
local value=$2
local config_file="config.json" local config_file="config.json"
# Read existing config # Read existing config
local config local config
config=$(cat "$config_file") config=$(cat "$config_file")
# Update or add ipAddress field # Update or add the field
if jq -e '.ipAddress' "$config_file" >/dev/null 2>&1; then if jq -e ".$key" "$config_file" >/dev/null 2>&1; then
config=$(echo "$config" | jq --arg ip "$ip_address" '.ipAddress = $ip') config=$(echo "$config" | jq --arg key "$key" --arg value "$value" '.[$key] = $value')
else else
config=$(echo "$config" | jq --arg ip "$ip_address" '. + {ipAddress: $ip}') config=$(echo "$config" | jq --arg key "$key" --arg value "$value" '. + {($key): $value}')
fi fi
# Write back to file # Write back to file
echo "$config" | jq '.' > "$config_file" echo "$config" | jq '.' > "$config_file"
echo "✅ Updated IP address in config.json" echo "✅ Updated $key in config.json"
} }
install_deploy_rs() { install_deploy_rs() {
@ -204,6 +205,7 @@ fi
USERNAME=$(jq -r '.username' config.json) USERNAME=$(jq -r '.username' config.json)
IP_ADDRESS=$(jq -r '.ipAddress // empty' config.json) IP_ADDRESS=$(jq -r '.ipAddress // empty' config.json)
HOSTNAME=$(jq -r '.hostname // empty' config.json)
if [ -z "$USERNAME" ]; then if [ -z "$USERNAME" ]; then
echo "❌ Error: Could not read username from config.json" echo "❌ Error: Could not read username from config.json"
@ -222,7 +224,22 @@ if [ -z "$IP_ADDRESS" ]; then
fi fi
# Update config.json with the new IP address # Update config.json with the new IP address
update_config_json "$IP_ADDRESS" update_config_value "ipAddress" "$IP_ADDRESS"
fi
# If hostname is not in config.json, prompt for it
if [ -z "$HOSTNAME" ]; then
echo " No hostname found in config.json"
read -p "Enter the hostname for your server: " HOSTNAME
# Validate hostname format (basic validation)
if ! [[ $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])?)*$ ]]; then
echo "❌ Error: Invalid hostname format"
exit 1
fi
# Update config.json with the new hostname
update_config_value "hostname" "$HOSTNAME"
fi fi
VERSION_FILE="version.json" VERSION_FILE="version.json"
@ -267,7 +284,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
echo echo
install_deploy_rs install_deploy_rs
echo "🚀 Applying the update to your system..." echo "🚀 Applying the update to your system..."
deploy .#server deploy .#$HOSTNAME
echo echo
echo "If you encounter any issues, your backup is available in $backup_dir" echo "If you encounter any issues, your backup is available in $backup_dir"
else else