From a5e61e7ad1d3cf427ff069c25c7f6b1bc2184183 Mon Sep 17 00:00:00 2001 From: Lenny Chen Date: Tue, 21 Nov 2023 11:04:54 -0800 Subject: [PATCH] chore: modify script logic to rule out other errors --- .../clone_vm_template/delete-packer-cache.sh | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/edge/vmware/clone_vm_template/delete-packer-cache.sh b/edge/vmware/clone_vm_template/delete-packer-cache.sh index a7a522f..ae62694 100644 --- a/edge/vmware/clone_vm_template/delete-packer-cache.sh +++ b/edge/vmware/clone_vm_template/delete-packer-cache.sh @@ -2,20 +2,25 @@ source /edge/vmware/clone_vm_template/setenv.sh -# Check if the packer cache exists -CACHE_OUTPUT=$(govc datastore.ls -ds=$vcenter_datastore /packer_cache 2>&1) +# First, list directories to confirm access and credentials +govc datastore.ls -ds=$vcenter_datastore +if [ $? -ne 0 ]; then + echo "Error accessing datastore. Please check credentials and permissions." + exit 1 +fi -if [[ $CACHE_OUTPUT == *"/packer_cache was not found"* ]]; then - echo "Existing packer cache not found. Nothing to delete." +# Now, try to list the packer_cache directory +govc datastore.ls -ds=$vcenter_datastore /packer_cache +if [ $? -ne 0 ]; then + echo "Existing packer cache not found. Nothing to delete." else - # Cache exists, attempt to delete it - govc datastore.rm -ds=$vcenter_datastore /packer_cache - RESULT=$? - if [ $RESULT -eq 0 ]; then - echo "Deleted previous packer cache." - exit 0 - else - echo "Failed to delete packer cache." - exit 1 - fi + # Cache exists, attempt to delete it + govc datastore.rm -ds=$vcenter_datastore /packer_cache + if [ $? -eq 0 ]; then + echo "Deleted previous packer cache." + exit 0 + else + echo "Failed to delete packer cache." + exit 1 + fi fi \ No newline at end of file