Skip to content

Commit

Permalink
Merge branch 'feat/add-upload.sh' of https://github.com/burnsjared041…
Browse files Browse the repository at this point in the history
…5/packer-examples-for-vsphere into feat/add-upload.sh
  • Loading branch information
burnsjared0415 committed Jun 6, 2024
2 parents 7b6c155 + 85f4760 commit 9458062
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 51 deletions.
8 changes: 8 additions & 0 deletions .ansible-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Sets the kind of files to lint.
kinds:
- playbook

# Skips the line-length rule.
skip_list:
- line-length
2 changes: 1 addition & 1 deletion .hadolint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---

# Sets the rules to ignore.
ignored:
- DL3008
- DL3015
28 changes: 24 additions & 4 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
---

# MD013: Line length
# Disables rule MD013. Checks for line length.
MD013: false

# MD026: Trailing Punctuation in Header
# Customizes rule MD026. Checks for trailing punctuation in headers.
MD026:
punctuation: ".,;:"

# MD033" Inline HTML
# Disables rule MD024. Checks for multiple headers with the same content.
MD024: false

# Disables rule MD033. Checks for inline HTML.
MD033: false

# Disables rule MD041. Checks if the first line in file is a top level header.
MD041: false

# Disables rule MD045. Checks if images have alternate text.
MD045: false

# Disables rule MD046. Checks for code block style.
MD046: false

# Disables rule MD053. Checks if link image reference definitions are needed.
MD053: false

# Disables rule MD055. Checks for consistent list style.
MD055: false

# Disables rule MD056. Checks for table column count.
MD056: false
1 change: 0 additions & 1 deletion docs/getting-started/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ machine images.

Replace the example passwords and keys.


```hcl linenums="1" title="config/build.pkrvars.hcl" hl_lines="1"
--8<-- "./builds/build.pkrvars.hcl.example:10:100"
```
Expand Down
1 change: 0 additions & 1 deletion docs/getting-started/downloads.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Download the guest operating system ISOs using the download script (`./download.

The downloads are supported by a JSON configuration file (`project.json`) that includes the details for each guest operating system.


2. Select a guest operating system.

```shell
Expand Down
105 changes: 61 additions & 44 deletions upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Check if the endpoint environment variable is stored

set -e

follow_link() {
FILE="$1"
while [ -h "$FILE" ]; do
Expand Down Expand Up @@ -35,7 +34,12 @@ get_project_info() {

print_message() {
message=$1
echo "$message"
echo -e "$message"
}

get_settings() {
local field=$1
jq -r ".settings.${field}" "$json_path"
}

json_path="project.json"
Expand Down Expand Up @@ -82,16 +86,14 @@ show_help() {


# Set CONFIG_PATH if it's not already set and the environment variables are not set
if [ -z "${PKR_VAR_vsphere_endpoint}" ] && [ -z "${PKR_VAR_vsphere_username}" ] && [ -z "${PKR_VAR_vsphere_password}" ] && [ -z "${PKR_VAR_vsphere_insecure_connection}" ] && [ -z "${PKR_VAR_vsphere_datastore}" ] && [ -z "${PKR_VAR_common_content_library_name}" ] && [ -z "$CONFIG_PATH" ]; then
SCRIPT_PATH=$(realpath "$(dirname "$(follow_link "$0")")")


if [[ "$1" != --* ]]; then
CONFIG_PATH="$(pwd)/$1"
else
CONFIG_PATH="${SCRIPT_PATH}/config"
fi
if [ -z "${PKR_VAR_vsphere_endpoint}" ] && [ -z "${PKR_VAR_vsphere_username}" ] && [ -z "${PKR_VAR_vsphere_password}" ] && [ -z "${PKR_VAR_vsphere_insecure_connection}" ] && [ -z "${PKR_VAR_vsphere_datastore}" ] && [ -z "${PKR_VAR_common_content_library_name}" ] && [ -z "$CONFIG_PATH" ]; then
SCRIPT_PATH=$(realpath "$(dirname "$(follow_link "$0")")")

if [[ -z "$1" || "$1" == --* ]]; then
CONFIG_PATH="${SCRIPT_PATH}/config"
else
CONFIG_PATH="$(pwd)/$1"
fi
fi

check_dependencies() {
Expand Down Expand Up @@ -277,6 +279,28 @@ print_title() {
printf "\033[32m%*s%s\033[0m\n" $padding_subtitle '' "$subtitle"
}

# Set the directory to search for ISO files
local_directory_to_search=$(get_settings "iso_base_path")

# Find all ISO files in the directory and its subfolders
find_files() {
for file in "$1"/*; do
if [[ -d "$file" ]]; then
find_files "$file"
elif [[ -f "$file" && "$file" == *.iso ]]; then
iso_files+="$file "
fi
done
}

find_files "$local_directory_to_search"

# Check if any .iso files were found
if [[ -z "$iso_files" ]]; then
print_message "\033[31mFiles not found in ISO base path. Please check the $local_directory_to_search directory.\033[0m"
exit 1
fi

upload_iso_to_vsphere() {
local target=$1
# Set the necessary environment variables for govc
Expand All @@ -287,20 +311,19 @@ export GOVC_INSECURE="${GOVC_INSECURE}"


# Set the directory to search for ISO files
local_directory_to_search="iso"
#local_directory_to_search=$(get_settings "iso_base_path")

# Find all ISO files in the directory and its subfolders
iso_files=$(find "$local_directory_to_search" -type f -name "*.iso")
#iso_files=$(find "$local_directory_to_search" -type f -name "*.iso")

# Loop over the found ISO files
for iso_file in $iso_files; do
# Extract the file name from the file path
file_name=$(basename "$iso_file")

skip_library_import=false
skip_datastore_import=false

if [ "$target" = "Content Library" ] || [ "$target" = "Both" ]; then
if [ "${target%%(*}" = "Content Library" ] || [ "$target" = "Both" ]; then
# Check if the item exists in the library
file_name_no_ext=${file_name%.iso}
library_file=$(govc library.ls "$library_name/$file_name_no_ext")
Expand All @@ -312,74 +335,68 @@ for iso_file in $iso_files; do
then
# If yes, delete it
if govc library.rm "$library_name/${file_name%.iso}"; then
echo "Item deleted successfully."
print_message "\033[33mItem deleted successfully.\033[0m"
else
echo "Error deleting item. Please check the library and item names."
print_message "\033[31mError deleting item. Please check the library and item names.\033[0m"
exit 1
fi
elif [[ $REPLY =~ ^[Nn]$ ]]
then
# If no, skip this file and continue with the next one
echo "Skipping this library upload because file exist and replace was skipped."
print_message "\033[33mSkipping this library upload because file exist and replace was skipped.\033[0m"
skip_library_import=true
else
echo "Invalid response. Please enter 'Y' for yes or 'N' for no."
print_message "\033[31mInvalid response. Please enter 'Y' for yes or 'N' for no.\033[0m"
exit 1
fi
fi
if [ "$skip_library_import" = false ]; then
# Import the ISO file to the library
GREEN='\033[0;32m'
NC='\033[0m' # No Color
echo -e "${GREEN}Importing $file_name to content library $library_name...${NC}"
print_message "\033[32mUploading:\033[0m \033[34m$file_name\033[0m => \033[34m$library_name\033[0m."
if govc library.import "$library_name" "$iso_file"; then
echo "Item imported successfully."
print_message "\033[32mItem uploaded successfully\033[0m."
else
echo "Error importing item. Please check the library and item names."
print_message "\033[31mError importing item. Please check the library and item names.\033[0m"
exit 1
fi
fi
fi

if [ "$target" = "Datastore" ] || [ "$target" = "Both" ]; then
skip_datastore_import=false
if [ "${target%%(*}" = "Datastore" ] || [ "$target" = "Both" ]; then
datastore_directory=$(dirname "$iso_file" | cut -d'/' -f1-3)
datastore_directory="${datastore_directory//iso\/\//iso/}"
datastore_file=$(govc datastore.ls -ds="$datastore_name" "$datastore_directory/$file_name" 2>/dev/null)
if [ -n "$datastore_file" ]; then
#if govc datastore.ls -ds="$datastore_name" "$datastore_directory/$file_name" >/dev/null; then
datastore_file=$(govc datastore.ls -ds="$datastore_name" "$datastore_directory/$file_name" 2>/dev/null) || true
if [ -n "$datastore_file" ]; then
read -p "The item $file_name exists on $datastore_name. Do you want to delete it? (y/n) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# If yes, delete it
if govc datastore.rm -ds=$datastore_name $datastore_directory/$file_name; then
echo "Item deleted successfully."
print_message "\033[33mItem deleted successfully.\033[0m"
else
echo "Error deleting item. Please check the datastore, directory, and item names."
print_message "\033[31mError deleting item. Please check the datastore, directory, and item names.\033[0m"
print_message "\033[31mTerminating script due to error deleting item.\033[0m"
exit 1
fi
elif [[ $REPLY =~ ^[Nn]$ ]]
elif [[ $REPLY =~ ^[Nn]$ ]]
then
# If no, skip this file and continue with the next one
echo "Skipping this datastore upload because file exist and replace was skipped."
print_message "\033[33mSkipping this datastore upload because file exist and replace was skipped.\033[0m"
skip_datastore_import=true
else
echo "Invalid response. Please enter 'Y' for yes or 'N' for no."
print_message "\033[33mInvalid response. Please enter 'Y' for yes or 'N' for no.\033[0m"
exit 1
fi
fi
fi
if [ "$skip_datastore_import" = false ]; then
# Upload the ISO file to the datastore
#GREEN='\033[0;32m'
#NC='\033[0m' # No Color
#echo -e "${GREEN}Uploading $iso_file to $datastore_name:$datastore_directory/$file_name...${NC}"
printf "\n"
print_message info "\033[32mUploading:\033[0m \033[34m$file_name\033[0m => \033[34m$library_name\033[0m."
print_message "\033[32mUploading:\033[0m \033[34m$file_name\033[0m => \033[34m$datastore_name\033[0m."
printf "\n\n"
if govc datastore.upload -ds="$datastore_name" "$iso_file" "$datastore_directory/$file_name"; then
echo "Item uploaded successfully."
print_message "\033[32mItem uploaded successfully\033[0m."
else
echo "Error uploading item. Please check the datastore, directory, and item names."
print_message "\033[31mError uploading item. Please check the datastore, directory, and item names.\033[0m"
exit 1
fi
fi
Expand All @@ -395,8 +412,8 @@ check_dependencies false
options=("Content Library" "Datastore" "Both")
while true; do
printf "\nDo you want to upload to:\n\n"
printf "1: ${options[0]}\n"
printf "2: ${options[1]}\n"
printf "1: ${options[0]} $library_name\n"
printf "2: ${options[1]} $datastore_name\n"
printf "3: ${options[2]}\n"
printf "\nEnter \033[31mq\033[0m to quit or \033[34mi\033[0m for info.\n\n"
read -r -p "Your choice: " input
Expand Down

0 comments on commit 9458062

Please sign in to comment.