Prepare v0.1.0
This commit is contained in:
155
install.sh
155
install.sh
@@ -13,14 +13,23 @@ fi
|
||||
if [ -z "${INSIDE_NIX_SHELL+x}" ]; then
|
||||
export NIX_CONFIG="experimental-features = nix-command flakes"
|
||||
export INSIDE_NIX_SHELL=1
|
||||
exec nix shell nixpkgs#git nixpkgs#mkpasswd --command bash "$0"
|
||||
exec nix shell nixpkgs#git nixpkgs#mkpasswd nixpkgs#jq --command bash "$0"
|
||||
fi
|
||||
|
||||
# Function to setup from template
|
||||
setup_from_template() {
|
||||
local TEMPLATE=starter
|
||||
local DIR_NAME="self-host-playbook"
|
||||
# Check directory situation and handle navigation
|
||||
DIR_NAME="self-host-playbook"
|
||||
CURRENT_DIR=$(basename "$(pwd)")
|
||||
|
||||
if [ "$CURRENT_DIR" = "$DIR_NAME" ]; then
|
||||
echo "📂 Already in $DIR_NAME directory"
|
||||
echo "⚠️ Warning: Proceeding will overwrite the current version!"
|
||||
read -p "Do you want to continue? (y/N) " -n 1 -r
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo "❌ Operation cancelled"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
if [ -d "$DIR_NAME" ]; then
|
||||
echo "📂 Directory '$DIR_NAME' already exists"
|
||||
read -p "Do you want to proceed in the existing directory? (y/N) " -n 1 -r
|
||||
@@ -32,17 +41,78 @@ setup_from_template() {
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "🔄 Creating new self-host-playbook configuration from template..."
|
||||
nix flake new --template "git+https://code.m3tam3re.com/m3tam3re/self-host-playbook#${TEMPLATE}" "$DIR_NAME"
|
||||
echo "🔄 Creating new self-host-playbook configuration..."
|
||||
mkdir -p "$DIR_NAME"
|
||||
cd "$DIR_NAME"
|
||||
fi
|
||||
fi
|
||||
|
||||
get_latest_version() {
|
||||
local LATEST_VERSION
|
||||
|
||||
latest_version=$(curl -s "https://code.m3tam3re.com/api/v1/repos/m3tam3re/self-host-playbook/tags" | jq -r '.[] | select(.name | startswith("v")) | .name' | sort -V | tail -n1)
|
||||
|
||||
if [ -z "$latest_version" ]; then
|
||||
echo "❌ Error: Could not fetch latest version from repository"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Remove 'v' prefix if present and return
|
||||
echo "${latest_version#v}"
|
||||
}
|
||||
|
||||
setup_latest_version() {
|
||||
local target_version=$1
|
||||
local dir_name=$2
|
||||
|
||||
echo "⬇️ Downloading version $target_version..."
|
||||
|
||||
TEMP_DIR=$(mktemp -d)
|
||||
trap 'rm -rf "$TEMP_DIR"' EXIT
|
||||
|
||||
# Create a subdirectory for the clone
|
||||
CLONE_DIR="${TEMP_DIR}/clone"
|
||||
mkdir -p "$CLONE_DIR"
|
||||
|
||||
# Clone to temporary directory with --quiet flag
|
||||
if ! nix flake clone --quiet "git+https://code.m3tam3re.com/m3tam3re/self-host-playbook?ref=v${target_version}" --dest "$CLONE_DIR" 2>/dev/null; then
|
||||
echo "❌ Failed to clone repository"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Copy files from clone to target directory
|
||||
cp -r "$CLONE_DIR"/* "$dir_name/"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to setup from template
|
||||
setup_from_template() {
|
||||
# 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"
|
||||
find . -maxdepth 1 ! -name "." ! -name ".." ! -name "$backup_dir" -exec cp -r {} "$backup_dir/" \;
|
||||
echo "✅ Backup created successfully"
|
||||
|
||||
# Clean current directory except backup
|
||||
echo "🗑️ Cleaning current directory..."
|
||||
find . -maxdepth 1 ! -name "." ! -name ".." ! -name "$backup_dir" -exec rm -rf {} \;
|
||||
fi
|
||||
|
||||
# Get and setup latest version
|
||||
local LATEST_VERSION=$(get_latest_version)
|
||||
echo "⬇️ Setting up version $LATEST_VERSION..."
|
||||
setup_latest_version "$LATEST_VERSION" "."
|
||||
}
|
||||
|
||||
# Function to generate SSH key
|
||||
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
|
||||
@@ -122,6 +192,58 @@ get_device_name() {
|
||||
esac
|
||||
}
|
||||
|
||||
setup_ssh_config() {
|
||||
local username=$1
|
||||
local ip_address=$2
|
||||
local ssh_config_dir="$HOME/.ssh"
|
||||
local ssh_config_file="$ssh_config_dir/config"
|
||||
local ssh_key_file="$ssh_config_dir/self-host-playbook"
|
||||
|
||||
# Create .ssh directory if it doesn't exist
|
||||
mkdir -p "$ssh_config_dir"
|
||||
chmod 700 "$ssh_config_dir"
|
||||
|
||||
# Create or append to SSH config
|
||||
local config_entry="Host self-host-playbook
|
||||
HostName $ip_address
|
||||
User $username
|
||||
IdentityFile $ssh_key_file"
|
||||
|
||||
# Check if entry already exists
|
||||
if ! grep -q "Host self-host-playbook" "$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.*/{
|
||||
s/HostName.*/HostName $ip_address/
|
||||
s/User.*/User $username/
|
||||
}" "$ssh_config_file"
|
||||
echo "✅ Updated existing SSH config entry"
|
||||
fi
|
||||
|
||||
# Set appropriate permissions
|
||||
chmod 600 "$ssh_config_file"
|
||||
}
|
||||
|
||||
install_deploy_rs() {
|
||||
echo "🔧 Installing deploy-rs to user environment..."
|
||||
|
||||
# Check if deploy is already installed
|
||||
if command -v deploy >/dev/null 2>&1; then
|
||||
echo "ℹ️ deploy-rs is already installed"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Install deploy-rs using nix profile
|
||||
if nix profile install 'github:serokell/deploy-rs'; then
|
||||
echo "✅ deploy-rs installed successfully"
|
||||
else
|
||||
echo "❌ Failed to install deploy-rs"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
echo "🚀 Welcome to the Self-Host Playbook!"
|
||||
echo "================================================"
|
||||
echo "This script will help you manage your NixOS server with:"
|
||||
@@ -155,11 +277,11 @@ read -p "Press ENTER to continue or CTRL + C to abort..."
|
||||
echo ""
|
||||
echo "📝 Please provide the following information:"
|
||||
echo "-------------------------------------------"
|
||||
read -p "1. Enter target server IP address: " SERVER_IP
|
||||
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
|
||||
echo
|
||||
echo "4. Enter domain names for services (must point to $SERVER_IP):"
|
||||
echo "4. 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
|
||||
@@ -173,10 +295,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@$SERVER_IP"
|
||||
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server root@$IP_ADDRESS"
|
||||
;;
|
||||
2)
|
||||
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server -i $SSH_KEY_PATH root@$SERVER_IP"
|
||||
INSTALL_COMMAND="nix run github:nix-community/nixos-anywhere -- --flake .#server -i $SSH_KEY_PATH root@$IP_ADDRESS"
|
||||
;;
|
||||
*)
|
||||
echo "❌ Invalid choice"
|
||||
@@ -264,7 +386,8 @@ cat > config.json << EOF
|
||||
"n8n": "$N8N_DOMAIN",
|
||||
"baserow": "$BASEROW_DOMAIN"
|
||||
},
|
||||
"rootDevice": "$DEVICE_NAME"
|
||||
"rootDevice": "$DEVICE_NAME",
|
||||
"ipAddress": "$IP_ADDRESS"
|
||||
}
|
||||
EOF
|
||||
|
||||
@@ -292,6 +415,8 @@ echo "This process might take several minutes..."
|
||||
|
||||
# Run nixos-anywhere installation
|
||||
$INSTALL_COMMAND && {
|
||||
echo "🔧 Setting up SSH configuration..."
|
||||
setup_ssh_config "$USERNAME" "$IP_ADDRESS"
|
||||
echo
|
||||
echo "🎉 Installation completed successfully!"
|
||||
echo "=====================================>"
|
||||
@@ -301,7 +426,9 @@ $INSTALL_COMMAND && {
|
||||
echo "- Baserow: https://$BASEROW_DOMAIN"
|
||||
echo
|
||||
echo "To connect to your server, use:"
|
||||
echo "ssh -i $SSH_KEY_PATH -p 2222 $USERNAME@$SERVER_IP"
|
||||
echo "ssh self-host-playbook"
|
||||
echo
|
||||
install_deploy_rs
|
||||
echo
|
||||
echo "⚠️ Important: Please save your SSH key path: $SSH_KEY_PATH"
|
||||
echo "=====================================>"
|
||||
|
||||
Reference in New Issue
Block a user