Unverified Commit 497e86e8 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Seperate subfolder for each OS (#812)

This makes it much easier to switch between different OSs without the need to remove the /storage folder first.
parent 875797b8
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ pipe() {
  local code="99"
  msg="Failed to connect to $1, reason:"

  curl --disable --silent --max-time 10 --fail --location "${1}" || {
  curl --disable --silent --max-time 15 --fail --location "${1}" || {
    code="$?"
  }

@@ -107,7 +107,7 @@ getURL() {
      name="Kubuntu"
      if [[ "$ret" == "url" ]]; then
        body=$(pipe "https://api.launchpad.net/devel/ubuntu/series") || exit 65
        version=$(echo "$body" | jq '.entries | .[] | select(.status=="Current Stable Release").version')
        version=$(echo "$body" | jq -r '.entries | .[] | select(.status=="Current Stable Release").version')
        url="https://cdimage.ubuntu.com/kubuntu/releases/${version}/release/kubuntu-${version}-desktop-amd64.iso"
      fi ;;
    "lmde" )
@@ -175,16 +175,16 @@ getURL() {
      name="Ubuntu Desktop"
      if [[ "$ret" == "url" ]]; then
        body=$(pipe "https://api.launchpad.net/devel/ubuntu/series") || exit 65
        version=$(echo "$body" | jq '.entries | .[] | select(.status=="Current Stable Release").version')
        url="https://releases.ubuntu.com/${version}ubuntu-${version}-desktop-amd64.iso"
        version=$(echo "$body" | jq -r '.entries | .[] | select(.status=="Current Stable Release").version')
        url="https://releases.ubuntu.com/${version}/ubuntu-${version}-desktop-amd64.iso"
        arm="https://cdimage.ubuntu.com/releases/${version}/release/ubuntu-${version}desktop-arm64.iso"
      fi ;;
    "ubuntus" | "ubuntu-server")
      name="Ubuntu Server"
      if [[ "$ret" == "url" ]]; then
        body=$(pipe "https://api.launchpad.net/devel/ubuntu/series") || exit 65
        version=$(echo "$body" | jq '.entries | .[] | select(.status=="Current Stable Release").version')
        url="https://releases.ubuntu.com/${version}ubuntu-${version}-live-server-amd64.iso"
        version=$(echo "$body" | jq -r '.entries | .[] | select(.status=="Current Stable Release").version')
        url="https://releases.ubuntu.com/${version}/ubuntu-${version}-live-server-amd64.iso"
        arm="https://cdimage.ubuntu.com/releases/${version}/release/ubuntu-${version}-live-server-arm64.iso"
      fi ;;
    "windows" )
@@ -194,7 +194,7 @@ getURL() {
      name="Xubuntu"
      if [[ "$ret" == "url" ]]; then
        body=$(pipe "https://api.launchpad.net/devel/ubuntu/series") || exit 65
        version=$(echo "$body" | jq '.entries | .[] | select(.status=="Current Stable Release").version')
        version=$(echo "$body" | jq -r '.entries | .[] | select(.status=="Current Stable Release").version')
        url="https://cdimages.ubuntu.com/xubuntu/releases/${version}/release/xubuntu-${version}-desktop-amd64.iso"
      fi ;;
  esac
+92 −40
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -Eeuo pipefail

getBase() {

  local base="$1"

  base=$(basename "${base%%\?*}")
  : "${base//+/ }"; printf -v base '%b' "${_//%/\\x}"
  base=$(echo "$base" | sed -e 's/[^A-Za-z0-9._-]/_/g')

  echo "$base"

  return 0
}

getFolder() {

  local base=""
  local result="$1"

  if [[ "$result" != *"."* ]]; then

    result="${result,,}"

  else

    base=$(getBase "$result")
    result="${base%.*}"

    case "${base,,}" in

      *".gz" | *".gzip" | *".xz" | *".7z" | *".zip" | *".rar" | *".lzma" | *".bz" | *".bz2" )

        [[ "$result" == *"."* ]] && result="${result%.*}" ;;

    esac

  fi

  [ -z "$result" ] && result="unknown"
  echo "$result"

  return 0
}

moveFile() {

  local file="$1"
@@ -67,7 +110,7 @@ downloadFile() {
  local name="$3"
  local msg rc total size progress

  local dest="$STORAGE/$base.tmp"
  local dest="$STORAGE/$base"
  rm -f "$dest"

  # Check if running with interactive TTY or redirected to docker log
@@ -78,13 +121,13 @@ downloadFile() {
  fi

  if [ -z "$name" ]; then
    name="$base"
    msg="Downloading image"
    info "Downloading $base..."
  else
    msg="Downloading $name"
    info "Downloading $name..."
  fi

  info "Downloading $name..."
  html "$msg..."

  /run/progress.sh "$dest" "0" "$msg ([P])..." &
@@ -100,7 +143,6 @@ downloadFile() {
      error "Invalid image file: is only $size ?" && return 1
    fi
    html "Download finished successfully..."
    mv -f "$dest" "$STORAGE/$base"
    return 0
  fi

@@ -214,7 +256,8 @@ findFile() {

  if [ -d "$dir" ]; then
    if hasDisk; then
      BOOT="$dir" && return 0
      BOOT="none"
      return 0
    fi
    error "The bind $dir maps to a file that does not exist!" && exit 37
  fi
@@ -233,62 +276,71 @@ findFile "boot" "raw" && return 0
findFile "boot" "qcow2" && return 0
findFile "custom" "iso" && return 0

if [ -z "$BOOT" ] || [[ "$BOOT" == *"example.com/image.iso" ]]; then
  BOOT="alpine"
  hasDisk && return 0
  warn "no value specified for the BOOT variable, defaulting to \"alpine\"."
if hasDisk; then
  BOOT="none"
  return 0
fi

url=$(getURL "$BOOT" "url") || exit 34
name=$(getURL "$BOOT" "name") || exit 34

[ -n "$url" ] && BOOT="$url"

if [[ "$BOOT" != *"."* ]]; then
  error "Invalid BOOT value specified, shortcut \"$BOOT\" is not recognized!" && exit 64
if [[ "${BOOT}" == \"*\" || "${BOOT}" == \'*\' ]]; then
  VERSION="${BOOT:1:-1}"
fi

if [[ "${BOOT,,}" != "http"* ]]; then
  error "Invalid BOOT value specified, \"$BOOT\" is not a valid URL!" && exit 64
fi
BOOT=$(expr "$BOOT" : "^\ *\(.*[^ ]\)\ *$")

base=$(basename "${BOOT%%\?*}")
: "${base//+/ }"; printf -v base '%b' "${_//%/\\x}"
base=$(echo "$base" | sed -e 's/[^A-Za-z0-9._-]/_/g')
if [ -z "$BOOT" ] || [[ "$BOOT" == *"example.com/"* ]]; then

case "${base,,}" in
  BOOT="alpine"
  warn "no value specified for the BOOT variable, defaulting to \"${BOOT}\"."

  *".iso" | *".img" | *".raw" | *".qcow2" )
fi

    detectType "$STORAGE/$base" && return 0 ;;
folder=$(getFolder "$BOOT")
STORAGE="$STORAGE/$folder"

  *".vdi" | *".vmdk" | *".vhd" | *".vhdx" )
if [ -d "$STORAGE" ]; then

    detectType "$STORAGE/${base%.*}.img" && return 0
    detectType "$STORAGE/${base%.*}.qcow2" && return 0 ;;
  findFile "boot" "iso" && return 0
  findFile "boot" "img" && return 0
  findFile "boot" "raw" && return 0
  findFile "boot" "qcow2" && return 0
  findFile "custom" "iso" && return 0

  *".gz" | *".gzip" | *".xz" | *".7z" | *".zip" | *".rar" | *".lzma" | *".bz" | *".bz2" )
  if hasDisk; then
    BOOT="none"
    return 0
  fi

    case "${base%.*}" in
fi

      *".iso" | *".img" | *".raw" | *".qcow2" )
name=$(getURL "$BOOT" "name") || exit 34

        detectType "$STORAGE/${base%.*}" && return 0 ;;
if [ -n "$name" ]; then

      *".vdi" | *".vmdk" | *".vhd" | *".vhdx" )
  info "Retrieving latest $name version..."
  url=$(getURL "$BOOT" "url") || exit 34

        find="${base%.*}"
  [ -n "$url" ] && BOOT="$url"

        detectType "$STORAGE/${find%.*}.img" && return 0
        detectType "$STORAGE/${find%.*}.qcow2" && return 0 ;;
fi

    esac ;;
if [[ "$BOOT" != *"."* ]]; then
  if [ -z "$BOOT" ]; then
    error "No BOOT value specified!"
  else
    error "Invalid BOOT value specified, option \"$BOOT\" is not recognized!"
  fi
  exit 64
fi

  * ) error "Unknown file extension, type \".${base/*./}\" is not recognized!" && exit 33 ;;
esac
if [[ "${BOOT,,}" != "http"* ]]; then
  error "Invalid BOOT value specified, \"$BOOT\" is not a valid URL!" && exit 64
fi

mkdir -p "$STORAGE"
base=$(getBase "$BOOT")

if ! downloadFile "$BOOT" "$base" "$name"; then
  rm -f "$STORAGE/$base.tmp" && exit 60
  rm -f "$STORAGE/$base" && exit 60
fi

case "${base,,}" in