Files
2026-01-21 09:56:46 +01:00

82 lines
2.3 KiB
Bash

#!/bin/bash
# Read me: prima di esecuzione fare chmod +x import_pool.sh
# Per l'utilizzo: ./import_pool.sh pool_name
if [ $# -ne 1 ]; then
echo "Usage: $0 <pool_name>"
exit 1
fi
pool_name=$1
# Function to check if a pool is already imported
check_pool_imported() {
local name=$1
response=$(midclt call pool.query)
echo "$response" | grep -q "\"name\": \"${name}\""
}
# Check if the pool is already imported
if check_pool_imported "$pool_name"; then
echo "Pool '${pool_name}' is already imported and available."
exit 0
fi
# Start the import_find job and get the job ID
job_id=$(midclt call pool.import_find)
#echo "Started import_find job with ID: $job_id"
# Poll until the job is complete
while true; do
echo "Checking import job status..."
job_status=$(midclt call core.get_jobs '[["id", "=", '"$job_id"']]' | jq -r '.[0]')
state=$(echo $job_status | jq -r '.state')
echo "Import job state: $state"
if [ "$state" == "SUCCESS" ]; then
echo "Import job succeeded."
break
elif [ "$state" == "FAILED" ]; then
echo "Import job failed"
exit 1
fi
sleep 1
done
# Extract the job result once it's complete
pools=$(echo $job_status | jq -r '.result')
# Find and import the pool with the specified name
found_pool=""
imported_pool=""
# Process each pool result using jq and a while loop
echo "$pools" | jq -c '.[]' | while read -r pool; do
name=$(echo "$pool" | jq -r '.name')
guid=$(echo "$pool" | jq -r '.guid')
if [ "$name" == "$pool_name" ]; then
found_pool=$name
import_job_id=$(midclt call pool.import_pool "{\"guid\": \"$guid\"}")
# Poll until the import job is complete
while true; do
import_job_status=$(midclt call core.get_jobs '[["id", "=", '"$import_job_id"']]' | jq -r '.[0]')
import_state=$(echo $import_job_status | jq -r '.state')
if [ "$import_state" == "SUCCESS" ]; then
break
elif [ "$import_state" == "FAILED" ]; then
exit 1
fi
sleep 1
done
fi
done
# Final check if the pool is imported
if check_pool_imported "$pool_name"; then
echo "Successfully imported pool: $pool_name"
exit 0
else
echo "Pool with name $pool_name not found or not imported"
exit 1
fi