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 7, 2024
2 parents 7b6c155 + 85f4760 commit 63eb081
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 50 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
9 changes: 9 additions & 0 deletions project.json
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,15 @@
"url": "https://git-scm.com/"
},
"version_requirement": ">= 2.43.0"
},
{
"name": "govc",
"description": "A vSphere CLI built on top of govmomi.",
"publisher": {
"name": "VMware",
"url": "https://vmware.com"
},
"version_requirement": ">= 0.21.0"
}
]
}
165 changes: 122 additions & 43 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 All @@ -14,6 +13,7 @@ follow_link() {

run_check_dependencies=false
run_show_help=false
run_check_govc=false

# This function prompts the user to press Enter to exit.
press_enter_exit() {
Expand All @@ -35,7 +35,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,17 +87,71 @@ 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 [ -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"
if [[ -z "$1" || "$1" == --* ]]; then
CONFIG_PATH="${SCRIPT_PATH}/config"
else
CONFIG_PATH="$(pwd)/$1"
fi
fi

fi
check_govc() {
cmd="govc"
local deps_version=$(jq -r --arg cmd $cmd '.dependencies[] | select(.name == $cmd ) | .version_requirement' $json_path)
required_version=$(echo "$deps_version" | tr -d '>=' | xargs)
if ! command -v $cmd &>/dev/null; then
echo -e "\033[0;31m[✘]\033[0m $cmd is not installed. $required_version or later is required."
exit 1
else
installed_version=$($cmd version | head -n 1 | awk '{print $2}' | tr -d 'v')
if [ "$(printf '%s\n' "$required_version" "$installed_version" | sort -V | head -n1)" != "$required_version" ]; then
echo -e "\033[0;31m[✘]\033[0m $cmd $installed_version installed. $required_version or later is required."
exit 1
fi
fi
}

# Check if govc is installed.
check_govc

check_command() {
cmd=$1
capitalized_cmd=$(echo "$cmd" | awk '{print toupper(substr($0,1,1))substr($0,2)}')
version_requirement=$(jq -r --arg cmd "$cmd" '.dependencies[] | select(.name == $cmd) | .version_requirement' "$json_path")
operator=$(echo "$version_requirement" | cut -d " " -f 1)
required_version=$(echo "$version_requirement" | cut -d " " -f 2)

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

CHECKMARK="${GREEN}[✔]${NC}"
CROSSMARK="${RED}[✘]${NC}"

if command -v "$cmd" &>/dev/null; then
installed_version=$($cmd version | head -n 1 | awk '{print $NF}' | sed 's/[^0-9.]*//g')
case $operator in
">=")
if ! echo -e "$required_version\n$installed_version" | sort -V -C; then
echo -e "$CROSSMARK $capitalized_cmd: $installed_version is installed. $required_version or later is required."
fi
;;
"==")
if [ "$installed_version" != "$required_version" ]; then
echo -e "$CROSSMARK $capitalized_cmd: $installed_version is installed. $required_version or later is required."
fi
;;
*)
echo -e "$CROSSMARK Unknown operator: $operator"
;;
esac
else
echo -e "$CROSSMARK $capitalized_cmd is not installed. $required_version or later required."
fi
}


check_dependencies() {
local verbose=$1
Expand All @@ -105,7 +164,12 @@ NC='\033[0m' # No Color

CHECKMARK="${GREEN}[✔]${NC}"
CROSSMARK="${RED}[✘]${NC}"
# This function checks if the required dependencies are installed.

# Assuming you're calling check_command in a loop like this:
for cmd in govc; do
check_command $cmd
done

# Check if the PKR_VAR_vsphere_endpoint environment variable is stored
if [[ -z "${PKR_VAR_vsphere_endpoint}" ]]; then
Expand Down Expand Up @@ -277,6 +341,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 +373,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 +397,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 +474,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 63eb081

Please sign in to comment.