Skip to content

Commit

Permalink
fix: added packer plugin section & improved error handling logic (#26)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
lennessyy and lennessyy authored Nov 21, 2023
1 parent d525fc6 commit d857c12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
36 changes: 21 additions & 15 deletions edge/vmware/clone_vm_template/delete-packer-cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions edge/vmware/packer/build.pkr.hcl
Original file line number Diff line number Diff line change
@@ -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."
Expand Down

0 comments on commit d857c12

Please sign in to comment.