From d857c12ba858f2358c184bfb411cc46520addb6b Mon Sep 17 00:00:00 2001 From: Lenny Chen <55669665+lennessyy@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:46:40 -0800 Subject: [PATCH] fix: added packer plugin section & improved error handling logic (#26) * fix: added packer plugin section & improved error handling logic * chore: modify script logic to rule out other errors * chore: make error message less specific as we do not know the actual error * chore: add logic for error handling * chore: redirect successul standard output of first command to null --------- Co-authored-by: Lenny Chen --- .../clone_vm_template/delete-packer-cache.sh | 36 +++++++++++-------- edge/vmware/packer/build.pkr.hcl | 9 +++++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/edge/vmware/clone_vm_template/delete-packer-cache.sh b/edge/vmware/clone_vm_template/delete-packer-cache.sh index 725627d..8c491f8 100644 --- a/edge/vmware/clone_vm_template/delete-packer-cache.sh +++ b/edge/vmware/clone_vm_template/delete-packer-cache.sh @@ -2,21 +2,27 @@ source /edge/vmware/clone_vm_template/setenv.sh -# Check if the packer cache exists -CACHE_EXISTS=$(govc datastore.ls -ds=$vcenter_datastore /packer_cache 2>&1) - -if [ -z "$CACHE_EXISTS" ]; 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." +# First, list directories to confirm access and credentials +govc datastore.ls -ds=$vcenter_datastore > /dev/null +if [ $? -ne 0 ]; then + echo "Error accessing datastore." exit 1 - fi fi +# Now, try to list the packer_cache directory +PACKER_CACHE_OUTPUT=$(govc datastore.ls -ds=$vcenter_datastore /packer_cache 2>&1) +PACKER_CACHE_STATUS=$? + +if [ $PACKER_CACHE_STATUS -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 + 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 diff --git a/edge/vmware/packer/build.pkr.hcl b/edge/vmware/packer/build.pkr.hcl index 09c91bc..e9f86cc 100644 --- a/edge/vmware/packer/build.pkr.hcl +++ b/edge/vmware/packer/build.pkr.hcl @@ -1,3 +1,12 @@ +packer { + required_plugins { + vsphere = { + source = "github.com/hashicorp/vsphere" + version = "~> 1" + } + } +} + variable "vcenter_username" { type = string description = "The username for authenticating to vCenter."