Skip to content

Commit

Permalink
Add update-template-ubuntu.sh
Browse files Browse the repository at this point in the history
```console
$ hack/update-template-ubuntu.sh
update-template-ubuntu.sh: Update the Ubuntu image location in the specified templates

Usage:
  update-template-ubuntu.sh [--flavor <flavor>|--minimal|--server] [--version <version>] <template.yaml>...

Description:
  This script updates the Ubuntu image location in the specified templates.
  If the image location in the template contains a release date in the URL, the script replaces it with the latest available date.
  If no flags are specified, the script uses the flavor and version from the image location basename in the template.

  Image location basename format: ubuntu-<version>-<flavor>-cloudimg-<arch>.img

  Released Ubuntu image information is fetched from the following URLs:

    Server: https://cloud-images.ubuntu.com/releases/stream/v1/com.ubuntu.cloud:released:download.json
    Minimal: https://cloud-images.ubuntu.com/minimal/releases/stream/v1/com.ubuntu.cloud:released:download.json

  The downloaded JSON file will be cached in the Lima cache directory.

Examples:
  Update the Ubuntu image location in templates/**.yaml:
  $ update-template-ubuntu.sh templates/**.yaml

  Update the Ubuntu image location in ~/.lima/ubuntu/lima.yaml:
  $ update-template-ubuntu.sh ~/.lima/ubuntu/lima.yaml

  Update the Ubuntu image location to ubuntu-24.04-minimal-cloudimg-<arch>.img in ~/.lima/docker/lima.yaml:
  $ update-template-ubuntu.sh --minimal --version 24.04 ~/.lima/docker/lima.yaml

Flags:
  --flavor <flavor>    Use the specified flavor image
  --server             Shortcut for --flavor server
  --minimal            Shortcut for --flavor minimal
  --version <version>  Use the specified version
  -h, --help           Print this help message
```

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>

hack/update-ubuntu-image.sh: add `--flavor <flavor>` flag

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>

rename to `hack/update-template-ubuntu.sh`

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
  • Loading branch information
norio-nomura committed Oct 9, 2024
1 parent 4a052fc commit 7bf550a
Show file tree
Hide file tree
Showing 2 changed files with 401 additions and 1 deletion.
67 changes: 66 additions & 1 deletion hack/cache-common-inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@ function location_to_sha256() {
)
}

# e.g.
# ```console
# $ cache_download_dir
# .download # on GitHub Actions
# /home/user/.cache/lima/download # on Linux
# /Users/user/Library/Caches/lima/download # on macOS
# /home/user/.cache/lima/download # on others
# ```
function cache_download_dir() {
if [[ ${GITHUB_ACTIONS:-false} == true ]]; then
echo ".download"
else
case "$(uname -s)" in
Linux) echo "${XDG_CACHE_HOME:-${HOME}/.cache}/lima/download" ;;
Darwin) echo "${HOME}/Library/Caches/lima/download" ;;
*) echo "${HOME}/.cache/lima/download" ;;
esac
fi
}

# e.g.
# ```console
# $ location_to_cache_path "https://cloud-images.ubuntu.com/releases/24.04/release-20240809/ubuntu-24.04-server-cloudimg-arm64.img"
Expand All @@ -224,7 +244,7 @@ function location_to_sha256() {
function location_to_cache_path() {
local location=$1
[[ ${location} != "null" ]] || return
sha256=$(location_to_sha256 "${location}") && echo ".download/by-url-sha256/${sha256}"
sha256=$(location_to_sha256 "${location}") && download_dir=$(cache_download_dir) && echo "${download_dir}/by-url-sha256/${sha256}"
}

# e.g.
Expand Down Expand Up @@ -321,3 +341,48 @@ function hash_file() {
echo "${hash}" | xxd -r -p | sha256sum | cut -d' ' -f1
)
}

# Download the file to the cache directory and print the path.
# e.g.
# ```console
# $ download_to_cache "https://cloud-images.ubuntu.com/releases/24.04/release-20240821/ubuntu-24.04-server-cloudimg-arm64.img"
# .download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on GitHub Actions
# /home/user/.cache/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on Linux
# /Users/user/Library/Caches/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on macOS
# /home/user/.cache/lima/download/by-url-sha256/346ee1ff9e381b78ba08e2a29445960b5cd31c51f896fc346b82e26e345a5b9a/data # on others
function download_to_cache() {
local code_time_type_url
code_time_type_url=$(
curl -sSLI -w "%{http_code}\t%header{Last-Modified}\t%header{Content-Type}\t%{url_effective}" "$1" -o /dev/null
)

local code time type url
IFS=$'\t' read -r code time type url filename <<<"${code_time_type_url}"
[[ ${code} == 200 ]] || exit 1

local cache_path
cache_path=$(location_to_cache_path "${url}")
[[ -d ${cache_path} ]] || mkdir -p "${cache_path}"

local needs_download=0
[[ -f ${cache_path}/data ]] || needs_download=1
[[ -f ${cache_path}/time && "$(<"${cache_path}/time")" == "${time}" ]] || needs_download=1
[[ -f ${cache_path}/type && "$(<"${cache_path}/type")" == "${type}" ]] || needs_download=1
if [[ ${needs_download} -eq 1 ]]; then
local code_time_type_url_filename
code_time_type_url_filename=$(
echo "downloading ${url}" >&2
curl -SL -w "%{http_code}\t%header{Last-Modified}\t%header{Content-Type}\t%{url_effective}\t%{filename_effective}" --no-clobber -o "${cache_path}/data" "${url}"
)
local filename
IFS=$'\t' read -r code time type url filename <<<"${code_time_type_url_filename}"
[[ ${code} == 200 ]] || exit 1
[[ "${cache_path}/data" == "${filename}" ]] || mv "${filename}" "${cache_path}/data"
# sha256.digest seems existing if expected digest is available. so, not creating it here.
# sha256sum "${cache_path}/data" | awk '{print "sha256:"$1}' >"${cache_path}/sha256.digest"
echo -n "${time}" >"${cache_path}/time"
fi
[[ -f ${cache_path}/type ]] || echo -n "${type}" >"${cache_path}/type"
[[ -f ${cache_path}/url ]] || echo -n "${url}" >"${cache_path}/url"
echo "${cache_path}/data"
}
Loading

0 comments on commit 7bf550a

Please # to comment.