Skip to content

Commit

Permalink
refactor: download-qcow2-image.sh to use /tmp and name the image/disk…
Browse files Browse the repository at this point in the history
… as the VM name (#58)
  • Loading branch information
venkatamutyala authored Nov 26, 2024
1 parent a62c304 commit a1e19db
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions tools/developer-setup/download-qcow2-image.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
#!/bin/bash


if [ -z "$TAG" ]; then
echo ""
echo "TAG environment variable is required"
echo "e.g. export TAG=v1.0.0"
if [ -z "$VM_NAME" ] || [ -z "$TAG" ]; then
echo ""
[ -z "$VM_NAME" ] && echo "VM_NAME environment variable is required" && echo "e.g. export VM_NAME=dinosaur-cat" && echo ""
[ -z "$TAG" ] && echo "TAG environment variable is required" && echo "e.g. export TAG=v1.0.0" && echo ""
exit 1
fi

# Replace these variables with your repository details
owner="GlueOps"
repo="codespaces"

if [ -f "$TAG.qcow2" ]; then
echo "File $TAG.qcow2.tar already exists. Skipping download."
echo "Cleaning up any files"
rm -rf $TAG.qcow2.tar*
else
# Capture the start time
start_time=$(date +%s)
# Capture the start time
start_time=$(date +%s)

# Get the release ID for the specified tag
release_id=$(curl -s "https://api.github.com/repos/$owner/$repo/releases/tags/$TAG" | jq -r '.id')
# Get the release ID for the specified tag
release_id=$(curl -s "https://api.github.com/repos/$owner/$repo/releases/tags/$TAG" | jq -r '.id')

# Get the asset download URLs
asset_urls=$(curl -s "https://api.github.com/repos/$owner/$repo/releases/$release_id/assets" | jq -r '.[].browser_download_url')
# Get the asset download URLs
asset_urls=$(curl -s "https://api.github.com/repos/$owner/$repo/releases/$release_id/assets" | jq -r '.[].browser_download_url')

# Download each asset in parallel
for url in $asset_urls; do
echo "Downloading $url"
curl -L -O "$url" &
done
# Download each asset in parallel
for url in $asset_urls; do
filename=$(basename "$url")
echo "Downloading $url"
mkdir -p "/tmp/$VM_NAME"
curl -L -o "/tmp/$VM_NAME/$filename" "$url" &
done

# Wait for all background jobs to complete
wait
# Wait for all background jobs to complete
wait

# Calculate and display elapsed time
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "Total time taken: $elapsed_time seconds"
# Calculate and display elapsed time
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "Total time taken: $elapsed_time seconds"


cat /tmp/$VM_NAME/*.qcow2.tar.part_* > /tmp/$VM_NAME/$VM_NAME.qcow2.tar
tar -xvf /tmp/$VM_NAME/$VM_NAME.qcow2.tar -C /tmp/$VM_NAME
mv /tmp/$VM_NAME/*.qcow2 /var/lib/libvirt/images/$VM_NAME.qcow2
echo "Deleting /tmp/$VM_NAME/"
rm -rf /tmp/$VM_NAME/
echo "resizing /var/lib/libvirt/images/$VM_NAME.qcow2"
qemu-img resize /var/lib/libvirt/images/$VM_NAME.qcow2 120G


cat $TAG.qcow2.tar.part_* > $TAG.qcow2.tar
tar -xvf $TAG.qcow2.tar
echo "Deleting downloaded .tar files"
rm -rf $TAG.qcow2.tar*
fi

0 comments on commit a1e19db

Please sign in to comment.