Fixed #15, added error handling, refactored (#17)

* Update install.sh
* Update README.md
This commit is contained in:
whiskerz007 2019-03-15 01:18:01 +01:00 committed by GitHub
parent 311fab559a
commit 04f2ce1765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 43 deletions

View File

@ -3,7 +3,7 @@
This script will create a new Proxmox VM with the latest version of HassOS. To create a new VM in the `local-lvm` storage, run the following in a SSH session or the console from Proxmox interface This script will create a new Proxmox VM with the latest version of HassOS. To create a new VM in the `local-lvm` storage, run the following in a SSH session or the console from Proxmox interface
``` ```
TMP=`mktemp -d`;pushd $TMP > /dev/null;wget -qO - https://raw.githubusercontent.com/whiskerz007/proxmox_hassos_install/master/install.sh | bash -s local-lvm;popd > /dev/null;rm -rf $TMP;unset TMP wget -qO - https://raw.githubusercontent.com/whiskerz007/proxmox_hassos_install/master/install.sh | bash -s local-lvm
``` ```
After script completes, click on the new VM (_the script will tell you the ID_), click on the `Hardware` tab for the VM and change the `Memory` and `Processors` settings to what you desire. The `Hard Disk` can be expanded by clicking on it, then click on the `Resize disk` button above (_Note: additional steps must be taken for storage to take effect in the VM after the first boot_). The network MAC address can be changed by selecting `Network Device` and clicking `Edit` above. Once all changes have been made, click `Start` above. After script completes, click on the new VM (_the script will tell you the ID_), click on the `Hardware` tab for the VM and change the `Memory` and `Processors` settings to what you desire. The `Hard Disk` can be expanded by clicking on it, then click on the `Resize disk` button above (_Note: additional steps must be taken for storage to take effect in the VM after the first boot_). The network MAC address can be changed by selecting `Network Device` and clicking `Edit` above. Once all changes have been made, click `Start` above.

View File

@ -1,63 +1,96 @@
#!/bin/bash #!/bin/bash
set -o pipefail
shopt -s expand_aliases
alias die='EXIT=$? LINE=$LINENO error_exit'
trap die ERR
function error_exit() {
REASON=$1
MSG="\e[91mERROR: \e[93m$EXIT@"
if [ -z "$REASON" ]; then
MSG="$MSG$LINE:"
REASON="Unknown failure occured."
else
MSG="$MSG`echo $(( $LINE - 1 ))`:"
fi
echo -e "$MSG \e[97m$REASON\e[39m\e[49m"
exit $EXIT
}
function cleanup() {
popd >/dev/null
rm -rf $TMP
}
trap cleanup EXIT
TMP=`mktemp -d`
pushd $TMP >/dev/null
VM_STORAGE=${1:-local-lvm} && \ STORAGE=${1:-local-lvm}
pvesm list $VM_STORAGE >& /dev/null || { \ pvesm list $STORAGE >& /dev/null ||
echo -e "\n\n\nERROR: '$VM_STORAGE' is not a valid storage ID.\n\n\n" && \ die "'$STORAGE' is not a valid storage ID."
exit 1 pvesm status -content images -storage $STORAGE >&/dev/null ||
} && \ die "'$STORAGE' does not allow 'Disk image' to be stored."
STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'`
VMID=$(cat<<EOF | python3 VMID=$(cat<<EOF | python3
import json import json
with open('/etc/pve/.vmlist') as vmlist: with open('/etc/pve/.vmlist') as vmlist:
vmids = json.load(vmlist) vmids = json.load(vmlist)
if 'ids' not in vmids: if 'ids' not in vmids:
print(100) print(100)
else: else:
last_vm = sorted(vmids['ids'].keys())[-1:][0] last_vm = sorted(vmids['ids'].keys())[-1:][0]
print(int(last_vm)+1) print(int(last_vm)+1)
EOF EOF
) && \ )
echo -e "\n\n\n" \ echo -e "\n\n\n" \
"********************************\n" \ "********************************\n" \
"* Getting latest HassOS Info *\n" \ "* Getting latest HassOS Info *\n" \
"********************************\n" && \ "********************************\n"
URL=$(cat<<EOF | python3 URL=$(cat<<EOF | python3
import requests import requests
url = 'https://api.github.com/repos/home-assistant/hassos/releases/latest' url = 'https://api.github.com/repos/home-assistant/hassos/releases/latest'
r = requests.get(url).json() r = requests.get(url).json()
if 'message' in r: if 'message' in r:
exit() exit()
for asset in r['assets']: for asset in r['assets']:
if asset['name'].endswith('vdi.gz'): if asset['name'].endswith('vdi.gz'):
print(asset['browser_download_url']) print(asset['browser_download_url'])
EOF EOF
) && \ )
if [ -z "$URL" ]; then if [ -z "$URL" ]; then
echo "Github has returned an error. A rate limit may have been applied to your connection. Please wait a while, then try again." die "Github has returned an error. A rate limit may have been applied to your connection."
exit 1 fi
fi && \
echo -e "\n\n\n" \ echo -e "\n\n\n" \
"********************************\n" \ "********************************\n" \
"* Downloading HassOS *\n" \ "* Downloading HassOS *\n" \
"********************************\n" && \ "********************************\n"
wget -q $URL && \ wget -q --show-progress $URL
FILE=$(basename $URL) && \ FILE=$(basename $URL)
echo -e "\n\n\n" \ echo -e "\n\n\n" \
"********************************\n" \ "********************************\n" \
"* Extracting HassOS *\n" \ "* Extracting HassOS *\n" \
"********************************\n" && \ "********************************\n"
gunzip -f $FILE && \ gunzip -f $FILE
echo -e "\n\n\n" \ echo -e "\n\n\n" \
"********************************\n" \ "********************************\n" \
"* Creating new VM *\n" \ "* Creating new VM *\n" \
"********************************\n" && \ "********************************\n"
qm create $VMID -bios ovmf -bootdisk sata0 -efidisk0 ${VM_STORAGE}:vm-${VMID}-disk-0,size=128K \ if [ "$STORAGE_TYPE" = "dir" ]; then
-name $(sed -e "s/\_//g" -e "s/.vdi.gz//" <<< $FILE) -net0 virtio,bridge=vmbr0 \ DISK_EXT=".qcow2"
-onboot 1 -ostype l26 -sata0 ${VM_STORAGE}:vm-${VMID}-disk-1,size=6G \ DISK_REF="$VMID/"
-scsihw virtio-scsi-pci && \ IMPORT_OPT="-format qcow2"
pvesm alloc $VM_STORAGE $VMID vm-${VMID}-disk-0 128 1>&/dev/null && \ fi
qm importdisk $VMID ${FILE%".gz"} $VM_STORAGE 1>&/dev/null && \ for i in {0,1}; do
disk="DISK$i"
eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT}
eval DISK${i}_REF=${STORAGE}:$DISK_REF${!disk}
done
qm create $VMID -bios ovmf -name $(sed -e "s/\_//g" -e "s/.vdi.gz//" <<< $FILE) \
-net0 virtio,bridge=vmbr0 -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null
qm importdisk $VMID ${FILE%".gz"} $STORAGE $IMPORT_OPT 1>&/dev/null
qm set $VMID -bootdisk sata0 -efidisk0 ${DISK0_REF},size=128K \
-sata0 ${DISK1_REF},size=6G > /dev/null
echo -e "\n\n\n" \ echo -e "\n\n\n" \
"********************************\n" \ "********************************\n" \
"* Completed Successfully *\n" \ "* Completed Successfully *\n" \
"* New VM ID is $VMID *\n" \ "* New VM ID is \e[1m$VMID\e[0m *\n" \
"********************************\n" "********************************\n"