apiVersion: apps/v1 kind: DaemonSet metadata: name: sync-daemon namespace: kube-system labels: app: sync-daemon spec: selector: matchLabels: app: sync-daemon template: metadata: labels: app: sync-daemon spec: hostNetwork: true initContainers: - name: fix-perms image: quay.io/platform9/vjailbreak:alpine securityContext: privileged: true runAsUser: 0 command: ["/bin/sh", "-c"] args: - | echo "Fixing permissions on /home/ubuntu/vmware-vix-disklib-distrib..." chown -R 1000:1000 /home/ubuntu/vmware-vix-disklib-distrib chmod 755 /home/ubuntu/vmware-vix-disklib-distrib echo "Fixing permissions on /home/ubuntu/virtio-win..." chown -R 1000:1000 /home/ubuntu/virtio-win chmod 755 /home/ubuntu/virtio-win volumeMounts: - name: vmwarelib mountPath: /home/ubuntu/vmware-vix-disklib-distrib - name: virtio-win mountPath: /home/ubuntu/virtio-win containers: - name: sync-container image: quay.io/platform9/vjailbreak:alpine securityContext: privileged: true runAsUser: 0 runAsGroup: 0 ports: - containerPort: 873 volumeMounts: - name: pf9-env mountPath: /etc/pf9 readOnly: true - name: vmwarelib mountPath: /home/ubuntu/vmware-vix-disklib-distrib - name: logs mountPath: /var/log/pf9 - name: virtio-win mountPath: /home/ubuntu/virtio-win command: ["/bin/sh", "-c"] args: - | IS_MASTER=$(grep -E '^export IS_MASTER=' /etc/pf9/k3s.env | cut -d'=' -f2) MASTER_IP=$(grep -E '^export MASTER_IP=' /etc/pf9/k3s.env | cut -d'=' -f2) # Migration considered idle if quiet for this long; only then is it eligible for cleanup. ACTIVITY_WINDOW_MIN=60 # Delete threshold: older than 24h. RETENTION_MTIME=+1 # Deletes migration--.log + migration--/ as one unit, # skipping the pair if either was written to within ACTIVITY_WINDOW_MIN. cleanup_dir() { local base_dir="$1" [ -d "$base_dir" ] || return 0 local names names=$( { find "$base_dir" -maxdepth 1 -type f -name 'migration-*.log' -printf '%f\n' 2>/dev/null | sed 's/\.log$//' find "$base_dir" -mindepth 1 -maxdepth 1 -type d -name 'migration-*' -printf '%f\n' 2>/dev/null } | sort -u ) echo "$names" | while IFS= read -r name; do [ -n "$name" ] || continue local log_file="$base_dir/$name.log" local dir="$base_dir/$name" if find "$log_file" "$dir" -type f -mmin -"$ACTIVITY_WINDOW_MIN" 2>/dev/null | grep -q .; then echo "[cleanup] Skipping $name (active within last ${ACTIVITY_WINDOW_MIN}m)" continue fi [ -f "$log_file" ] && find "$log_file" -mtime "$RETENTION_MTIME" -delete 2>/dev/null if [ -d "$dir" ]; then find "$dir" -type f -mtime "$RETENTION_MTIME" -delete 2>/dev/null rmdir "$dir" 2>/dev/null || true fi done # Stray non-migration files: clean by age alone. find "$base_dir" -maxdepth 1 -type f ! -name 'migration-*.log' -mtime "$RETENTION_MTIME" -delete 2>/dev/null || true } if [ "$IS_MASTER" = "true" ]; then echo "Master node detected. IP: $MASTER_IP" master_cleanup_loop() { while true; do echo "Cleaning up logs older than 24 hours on master node ($(date))" cleanup_dir /var/log/pf9 sleep 300 done } master_cleanup_loop & echo "Starting rsync daemon..." rsync --daemon --config=/etc/pf9/rsyncd.conf --no-detach if ps aux | grep rsync | grep -v grep; then echo "rsync daemon is running." else echo "Failed to start rsync daemon." exit 1 fi else echo "Worker node detected. Syncing from Master..." agent_cleanup_loop() { while true; do echo "Cleaning up logs older than 24 hours on worker node ($(date))" cleanup_dir /var/log/pf9 sync echo 3 > /proc/sys/vm/drop_caches 2>/dev/null || true sleep 300 done } agent_cleanup_loop & while true; do mkdir -p /home/ubuntu/vmware-vix-disklib-distrib/ echo "Syncing from rsync://${MASTER_IP}" rsync -avz rsync://${MASTER_IP}/vmwarelib/ /home/ubuntu/vmware-vix-disklib-distrib/ rsync -avz /var/log/pf9 rsync://${MASTER_IP}/worker-logs rsync -avz rsync://${MASTER_IP}/virtio-win /home/ubuntu/virtio-win if [ $? -eq 0 ]; then echo "Sync completed successfully." else echo "Sync failed. Retrying in 60 seconds..." fi sleep 20 done fi volumes: - name: pf9-env hostPath: path: /etc/pf9 type: DirectoryOrCreate - name: vmwarelib hostPath: path: /home/ubuntu/vmware-vix-disklib-distrib type: DirectoryOrCreate - name: logs hostPath: type: DirectoryOrCreate path: /var/log/pf9 - name: virtio-win hostPath: type: DirectoryOrCreate path: /home/ubuntu/virtio-win