v0.3.0 - Get & Select the Image of the Virtualmachine

This commit is contained in:
Janic Voser 2021-12-03 10:55:01 +01:00
parent b068b5921a
commit d26165ad7b
2 changed files with 65 additions and 8 deletions

View File

@ -12,11 +12,16 @@ Current State: Development
### Usage: ### Usage:
Just Execute the script, it's a guided Script (chmod +x CreateCloudInitVM.sh && ./ CreateCloudInitVM.sh) Just Execute the script, it's a guided Script (chmod +x CreateCloudInitVM.sh && ./ CreateCloudInitVM.sh)
### Planned Features: ### Planned Features:
The checkbox visualises, if the feature is already working
- [ ] Guided VM Creation - [ ] Guided VM Creation
- [ ] Image Download URL - [X] Image Download URL
- [ ] Instant VM-Start after Creation - [ ] Instant VM-Start after Creation
- [ ] Image Selection - [X] Image Selection
- [X] VM-ID check - [X] VM-ID check
- [ ] Choose Memorysize
- [ ] Choose CPU cores
- [ ] Choose Network
- [ ] Disable Firewall
# Bookstack Link to general Cloud-Init Information # Bookstack Link to general Cloud-Init Information
https://bookstack.voser.cloud/books/virtualisierung/chapter/cloud-init https://bookstack.voser.cloud/books/virtualisierung/chapter/cloud-init

View File

@ -9,11 +9,12 @@
# Date of Last Maintenance: 03.12.2021 # Date of Last Maintenance: 03.12.2021
# #
# Current State: Development # Current State: Development
# Version: 0.2.0 # Version: 0.3.0
# Version History: # Version History:
# v0.0.1 - Basic Script Structure # v0.0.1 - Basic Script Structure
# v0.1.0 - Get the User input # v0.1.0 - Get the User input
# v0.2.0 - Gather Virtual Machine ID # v0.2.0 - Gather Virtual Machine ID
# v0.3.0 - Get & Select the Image of the Virtualmachine
# Planned Features: # Planned Features:
@ -22,27 +23,78 @@
# - [ ] Instant VM-Start after Creation # - [ ] Instant VM-Start after Creation
# - [ ] Image Selection # - [ ] Image Selection
# - [X] VM-ID check # - [X] VM-ID check
# - [ ] Choose Memorysize
# - [ ] Choose CPU cores
# - [ ] Choose Network
# - [ ] Disable Firewall
# Global Conigurable Variables # Global Conigurable Variables
# Global Constants # Global Constants
IMAGE_STORAGE_LOCATION="/var/lib/vz/template/iso/"
# Functions # Functions
## Gather the ID of the Virtual machine
function gather_vmid { function gather_vmid {
echo "Already used IDs" echo "Already used IDs"
qm list | tr -s ' ' | cut -d ' ' -f 2,3 qm list | tr -s ' ' | cut -d ' ' -f 2,3 # Get the already used VMIDs & VM-Name
echo "Enter the new ID of your Virtual Machine (It must be unused)" echo "Enter the new ID of your Virtual Machine (It must be unused)"
read VM_ID read VM_ID # Get the User Input(This VMID will be used for the new created VM)
i=0 i=0
readarray -t vmid_array < <(qm list | tail -n +2 | tr -s ' ' | cut -d ' ' -f 2) readarray -t VMID_ARRAY < <(qm list | tail -n +2 | tr -s ' ' | cut -d ' ' -f 2)
if [[ "${vmid_array[*]}" =~ "$VM_ID" ]]; then if [[ "${VMID_ARRAY[*]}" =~ "$VM_ID" ]]; then
echo "Your selected Virtual Machine ID is already in use, choose an other ID" echo "Your selected Virtual Machine ID is already in use, choose an other ID"
gather_vmid gather_vmid
fi fi
} }
## Download a new Image in to the Image Storage
function get_img {
echo "Do you wish to download an Image? (yes/no)"
read DOWNLOAD_IMG_yn
case "$DOWNLOAD_IMG_yn" in
yes|YES|Yes|y|Y)
echo "Please enter the URL of your Image"
read DOWNLOAD_IMG_URL
wget "$DOWNLOAD_IMG_URL" --directory-prefix="$IMAGE_STORAGE_LOCATION"
if [ $? -eq 0 ]; then
echo "Download was Successful"
else
echo "Download failed"
echo "Retry or Downloadfile manually"
get_img
fi
no|NO|No|n|N)
echo "Carrying on without a new Image"
*)
echo "Invalid Argument: Please type \"yes\" or \"no\""
}
## Select an Image
function sel_img{
echo "The following list represents all Images which exist on the Hoststorage"
ls $IMAGE_STORAGE_LOCATION -l | tr -s ' ' | cut -d' ' -f 6-9
readarray -t OS_IMG_ARRAY < <(ls $IMAGE_STORAGE_LOCATION -l | tr -s ' ' | cut -d' ' -f 9)
echo "Please enter the correct Image Name(shown above)"
read OS_IMG_NAME
if ! [[ "${OS_IMG_ARRAY[*]}" =~ "$OS_IMG_NAME" ]]; then
echo "Your selected Image does not exist"
echo "Maybe it was a Typo, Please check:"
echo " - Upper/Lowercase writing"
echo " - Fileextension(needs to be appended)"
echo "ProTip: Copy & Paste"
sleep 3
sel_img
fi
}
## Get the prepared Cloud-Init Image
function gather_img {
get_img
sel_img
}
## Get all the Variables from the User
function get_user_input { function get_user_input {
gather_vmid gather_vmid
gather_img
} }
# Main Script # Main Script