feat: Make script idempotent - check before creating
This commit is contained in:
parent
5fa67c6ed3
commit
1878ff9c77
|
|
@ -1,6 +1,7 @@
|
|||
#!/bin/bash
|
||||
# TrueNAS Share Setup Script for DevMatrix
|
||||
# Run this on TrueNAS (192.168.5.195) as root
|
||||
# TrueNAS Share Setup Script for DevMatrix (Idempotent)
|
||||
# Run this on TrueNAS as root
|
||||
# This script checks if resources exist before creating them
|
||||
|
||||
set -e
|
||||
|
||||
|
|
@ -15,141 +16,141 @@ echo " Network: $NETWORK_ALLOW"
|
|||
echo ""
|
||||
|
||||
# ============================================
|
||||
# CREATE DATASET STRUCTURE
|
||||
# CREATE DATASET STRUCTURE (Skip if exists)
|
||||
# ============================================
|
||||
echo "📁 Creating dataset structure..."
|
||||
echo "📁 Checking datasets..."
|
||||
|
||||
# Main dataset (if not exists)
|
||||
if ! zfs list "$DATASET_NAME/$SHARE_PREFIX" >/dev/null 2>&1; then
|
||||
zfs create "$DATASET_NAME/$SHARE_PREFIX"
|
||||
echo "✓ Created: $DATASET_NAME/$SHARE_PREFIX"
|
||||
# Main dataset
|
||||
main_dataset="$DATASET_NAME/$SHARE_PREFIX"
|
||||
if zfs list "$main_dataset" >/dev/null 2>&1; then
|
||||
echo "✓ Dataset exists: $main_dataset"
|
||||
else
|
||||
echo "✓ Exists: $DATASET_NAME/$SHARE_PREFIX"
|
||||
zfs create "$main_dataset"
|
||||
echo "✓ Created: $main_dataset"
|
||||
fi
|
||||
|
||||
# Sub-datasets
|
||||
for subdir in projects backups iso-archive shared; do
|
||||
dataset_path="$DATASET_NAME/$SHARE_PREFIX/$subdir"
|
||||
if ! zfs list "$dataset_path" >/dev/null 2>&1; then
|
||||
if zfs list "$dataset_path" >/dev/null 2>&1; then
|
||||
echo "✓ Dataset exists: $dataset_path"
|
||||
else
|
||||
zfs create "$dataset_path"
|
||||
echo "✓ Created: $dataset_path"
|
||||
else
|
||||
echo "✓ Exists: $dataset_path"
|
||||
fi
|
||||
done
|
||||
|
||||
# ============================================
|
||||
# SET PERMISSIONS
|
||||
# SET PERMISSIONS (Always ensure correct)
|
||||
# ============================================
|
||||
echo ""
|
||||
echo "🔐 Setting permissions..."
|
||||
|
||||
# Set ownership (nobody:nogroup for NFS)
|
||||
mountpoint_base=$(zfs get -H -o value mountpoint "$DATASET_NAME")
|
||||
for subdir in projects backups iso-archive shared; do
|
||||
mountpoint=$(zfs get -H -o value mountpoint "$DATASET_NAME/$SHARE_PREFIX/$subdir")
|
||||
chown -R nobody:nogroup "$mountpoint"
|
||||
chmod -R 777 "$mountpoint"
|
||||
echo "✓ Permissions set for: $subdir"
|
||||
mountpath="$mountpoint_base/$SHARE_PREFIX/$subdir"
|
||||
chown -R nobody:nogroup "$mountpath"
|
||||
chmod -R 777 "$mountpath"
|
||||
echo "✓ Permissions set: $subdir"
|
||||
done
|
||||
|
||||
# ============================================
|
||||
# CREATE NFS SHARES
|
||||
# ENABLE NFS SERVICE (Skip if running)
|
||||
# ============================================
|
||||
echo ""
|
||||
echo "🔗 Creating NFS shares..."
|
||||
echo "🚀 Checking NFS service..."
|
||||
|
||||
# Function to create NFS share
|
||||
create_nfs_share() {
|
||||
local name=$1
|
||||
local path=$2
|
||||
local options=$3
|
||||
nfs_status=$(midclt call service.query '[["service","=","nfs"]]' 2>/dev/null | grep -o '"state": "[^"]*"' | cut -d'"' -f4)
|
||||
|
||||
# Check if share already exists
|
||||
if midclt call sharing.nfs.query "[[\"path\",\"=\",\"$path\"]]" | grep -q "$path"; then
|
||||
echo "✓ NFS share exists: $name"
|
||||
return
|
||||
fi
|
||||
|
||||
# Create share using midclt (TrueNAS API)
|
||||
midclt call sharing.nfs.create "{
|
||||
\"path\": \"$path\",
|
||||
\"comment\": \"DevMatrix $name\",
|
||||
\"hosts\": [\"$NETWORK_ALLOW\"],
|
||||
\"ro\": $options,
|
||||
\"maproot_user\": \"root\",
|
||||
\"maproot_group\": \"root\",
|
||||
\"mapall_user\": \"\",
|
||||
\"mapall_group\": \"\",
|
||||
\"security\": ["SYS"]
|
||||
}" > /dev/null
|
||||
|
||||
echo "✓ Created NFS share: $name ($path)"
|
||||
}
|
||||
|
||||
# Create shares
|
||||
mountpoint_base=$(zfs get -H -o value mountpoint "$DATASET_NAME")
|
||||
|
||||
create_nfs_share "projects" "$mountpoint_base/$SHARE_PREFIX/projects" "false"
|
||||
create_nfs_share "backups" "$mountpoint_base/$SHARE_PREFIX/backups" "false"
|
||||
create_nfs_share "iso-archive" "$mountpoint_base/$SHARE_PREFIX/iso-archive" "false"
|
||||
create_nfs_share "shared" "$mountpoint_base/$SHARE_PREFIX/shared" "false"
|
||||
|
||||
# ============================================
|
||||
# ENABLE NFS SERVICE
|
||||
# ============================================
|
||||
echo ""
|
||||
echo "🚀 Enabling NFS service..."
|
||||
|
||||
# Check if NFS is running
|
||||
if ! midclt call service.query "[[\"service\",\"=\",\"nfs\"]]" | grep -q '"state": "RUNNING"'; then
|
||||
midclt call service.start "nfs" > /dev/null
|
||||
midclt call service.update "nfs" '{"enable": true}' > /dev/null
|
||||
echo "✓ NFS service started and enabled"
|
||||
else
|
||||
if [ "$nfs_status" = "RUNNING" ]; then
|
||||
echo "✓ NFS service already running"
|
||||
else
|
||||
midclt call service.start "nfs" >/dev/null 2>&1 || true
|
||||
midclt call service.update "nfs" '{"enable": true}' >/dev/null 2>&1 || true
|
||||
echo "✓ NFS service started and enabled"
|
||||
fi
|
||||
|
||||
# ============================================
|
||||
# CREATE SMB SHARES (Optional - for Windows access)
|
||||
# CREATE NFS SHARES (Skip if exists)
|
||||
# ============================================
|
||||
echo ""
|
||||
echo "🖥️ Creating SMB shares (for Windows access)..."
|
||||
echo "🔗 Checking NFS shares..."
|
||||
|
||||
# Enable SMB if not already
|
||||
if ! midclt call service.query "[[\"service\",\"=\",\"cifs\"]]" | grep -q '"state": "RUNNING"'; then
|
||||
midclt call service.start "cifs" > /dev/null
|
||||
midclt call service.update "cifs" '{"enable": true}' > /dev/null
|
||||
# Function to check if NFS share exists
|
||||
check_nfs_share() {
|
||||
local path=$1
|
||||
midclt call sharing.nfs.query "[[\"path\",\"=\",\"$path\"]]" 2>/dev/null | grep -q "$path"
|
||||
}
|
||||
|
||||
# Create NFS shares
|
||||
for share in projects backups iso-archive shared; do
|
||||
path="$mountpoint_base/$SHARE_PREFIX/$share"
|
||||
|
||||
if check_nfs_share "$path"; then
|
||||
echo "✓ NFS share exists: $share"
|
||||
else
|
||||
echo " Creating NFS share: $share..."
|
||||
midclt call sharing.nfs.create "{
|
||||
\"path\": \"$path\",
|
||||
\"comment\": \"DevMatrix $share\",
|
||||
\"hosts\": [\"$NETWORK_ALLOW\"],
|
||||
\"ro\": false,
|
||||
\"maproot_user\": \"root\",
|
||||
\"maproot_group\": \"root\",
|
||||
\"security\": [\"SYS\"]
|
||||
}" >/dev/null 2>&1
|
||||
echo "✓ Created NFS share: $share"
|
||||
fi
|
||||
done
|
||||
|
||||
# ============================================
|
||||
# ENABLE SMB SERVICE (Skip if running)
|
||||
# ============================================
|
||||
echo ""
|
||||
echo "🖥️ Checking SMB service..."
|
||||
|
||||
smb_status=$(midclt call service.query '[["service","=","cifs"]]' 2>/dev/null | grep -o '"state": "[^"]*"' | cut -d'"' -f4)
|
||||
|
||||
if [ "$smb_status" = "RUNNING" ]; then
|
||||
echo "✓ SMB service already running"
|
||||
else
|
||||
midclt call service.start "cifs" >/dev/null 2>&1 || true
|
||||
midclt call service.update "cifs" '{"enable": true}' >/dev/null 2>&1 || true
|
||||
echo "✓ SMB service started and enabled"
|
||||
fi
|
||||
|
||||
# Function to create SMB share
|
||||
create_smb_share() {
|
||||
local name=$1
|
||||
local path=$2
|
||||
# ============================================
|
||||
# CREATE SMB SHARES (Skip if exists)
|
||||
# ============================================
|
||||
echo ""
|
||||
echo "🔗 Checking SMB shares..."
|
||||
|
||||
# Check if exists
|
||||
if midclt call sharing.smb.query "[[\"path\",\"=\",\"$path\"]]" | grep -q "$path"; then
|
||||
echo "✓ SMB share exists: $name"
|
||||
return
|
||||
fi
|
||||
|
||||
midclt call sharing.smb.create "{
|
||||
\"path\": \"$path\",
|
||||
\"name\": \"devmatrix-$name\",
|
||||
\"comment\": \"DevMatrix $name\",
|
||||
\"browseable\": true,
|
||||
\"readonly\": false,
|
||||
\"guestok\": true,
|
||||
\"afp\": false
|
||||
}" > /dev/null
|
||||
|
||||
echo "✓ Created SMB share: devmatrix-$name"
|
||||
# Function to check if SMB share exists
|
||||
check_smb_share() {
|
||||
local path=$1
|
||||
midclt call sharing.smb.query "[[\"path\",\"=\",\"$path\"]]" 2>/dev/null | grep -q "$path"
|
||||
}
|
||||
|
||||
# Create SMB shares
|
||||
create_smb_share "projects" "$mountpoint_base/$SHARE_PREFIX/projects"
|
||||
create_smb_share "backups" "$mountpoint_base/$SHARE_PREFIX/backups"
|
||||
create_smb_share "shared" "$mountpoint_base/$SHARE_PREFIX/shared"
|
||||
# Create SMB shares (no iso-archive for SMB)
|
||||
for share in projects backups shared; do
|
||||
path="$mountpoint_base/$SHARE_PREFIX/$share"
|
||||
share_name="devmatrix-$share"
|
||||
|
||||
if check_smb_share "$path"; then
|
||||
echo "✓ SMB share exists: $share_name"
|
||||
else
|
||||
echo " Creating SMB share: $share_name..."
|
||||
midclt call sharing.smb.create "{
|
||||
\"path\": \"$path\",
|
||||
\"name\": \"$share_name\",
|
||||
\"comment\": \"DevMatrix $share\",
|
||||
\"browseable\": true,
|
||||
\"readonly\": false,
|
||||
\"guestok\": true
|
||||
}" >/dev/null 2>&1
|
||||
echo "✓ Created SMB share: $share_name"
|
||||
fi
|
||||
done
|
||||
|
||||
# ============================================
|
||||
# SUMMARY
|
||||
|
|
@ -159,19 +160,26 @@ echo "╔═══════════════════════
|
|||
echo "║ TRUENAS SETUP COMPLETE ║"
|
||||
echo "╠════════════════════════════════════════════════════════╣"
|
||||
echo "║ ║"
|
||||
echo "║ NFS Shares (for Linux VMs): ║"
|
||||
echo "║ $mountpoint_base/$SHARE_PREFIX/projects ║"
|
||||
echo "║ $mountpoint_base/$SHARE_PREFIX/backups ║"
|
||||
echo "║ $mountpoint_base/$SHARE_PREFIX/iso-archive ║"
|
||||
echo "║ $mountpoint_base/$SHARE_PREFIX/shared ║"
|
||||
echo "║ Datasets: ║"
|
||||
echo "║ ✓ $DATASET_NAME/$SHARE_PREFIX ║"
|
||||
echo "║ ✓ .../projects ║"
|
||||
echo "║ ✓ .../backups ║"
|
||||
echo "║ ✓ .../iso-archive ║"
|
||||
echo "║ ✓ .../shared ║"
|
||||
echo "║ ║"
|
||||
echo "║ SMB Shares (for Windows): ║"
|
||||
echo "║ \\$(hostname)\\devmatrix-projects ║"
|
||||
echo "║ \\$(hostname)\\devmatrix-backups ║"
|
||||
echo "║ \\$(hostname)\\devmatrix-shared ║"
|
||||
echo "║ NFS Shares (Linux VMs): ║"
|
||||
echo "║ ✓ /mnt/$DATASET_NAME/$SHARE_PREFIX/projects ║"
|
||||
echo "║ ✓ /mnt/$DATASET_NAME/$SHARE_PREFIX/backups ║"
|
||||
echo "║ ✓ /mnt/$DATASET_NAME/$SHARE_PREFIX/iso-archive ║"
|
||||
echo "║ ✓ /mnt/$DATASET_NAME/$SHARE_PREFIX/shared ║"
|
||||
echo "║ ║"
|
||||
echo "║ Access from: $NETWORK_ALLOW ║"
|
||||
echo "║ SMB Shares (Windows): ║"
|
||||
echo "║ ✓ \\truenas\devmatrix-projects ║"
|
||||
echo "║ ✓ \\truenas\devmatrix-backups ║"
|
||||
echo "║ ✓ \\truenas\devmatrix-shared ║"
|
||||
echo "║ ║"
|
||||
echo "║ Access: $NETWORK_ALLOW ║"
|
||||
echo "║ ║"
|
||||
echo "╚════════════════════════════════════════════════════════╝"
|
||||
echo ""
|
||||
echo "Next: Run 'setup_truenas.sh' inside VM 300 to mount these shares"
|
||||
echo "Run this script again anytime to add missing resources!"
|
||||
|
|
|
|||
Loading…
Reference in New Issue