Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor update-template-ubuntu.sh and add update-template-debian.sh #2731

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

norio-nomura
Copy link
Contributor

@norio-nomura norio-nomura commented Oct 14, 2024

  • Refactor update-template-ubuntu.sh to separate distribution-specific code.
  • Enable support for other distributions.
  • Add a common update-template.sh.
  • Add update-template-debian.sh to handle Debian.

This PR aims to add Debian support as per #1347.
Also fixes: #2756

@norio-nomura
Copy link
Contributor Author

Finally, I've laid the groundwork to add code for other distributions. Now, I'll start adding code for Debian.

@norio-nomura norio-nomura force-pushed the update-template-debian.sh branch from 2af6bcd to 4b0396f Compare October 22, 2024 07:06
@norio-nomura
Copy link
Contributor Author

Added hack/update-template-debian.sh:

$ hack/update-template-debian.sh
update-template-debian.sh: Update the Debian image location in the specified templates

Usage:
  update-template-debian.sh [--backports[=<bool>]] [--daily[=<bool>]] [--timestamped[=<bool>]] [--version <version>] <template.yaml>...

Description:
  This script updates the Debian 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 version from the image location basename in the template.

  Image location basename format: debian-<version>[-backports]-genericcloud-<arch>[-daily][-<timestamp>].qcow[2](https://github.com/lima-vm/lima/issues/2)

  Published Debian image information is fetched from the following URLs:

    https://cloud.debian.org/images/cloud/<codename>[-backports]/[daily/](latest|<timestamp>)/debian-<version>[-backports]-genericcloud-<arch>[-daily][-<timestamp>].json

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

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

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

  Update the Debian image location to debian-[13](https://github.com/lima-vm/lima/issues/13)-genericcloud-<arch>.qcow[2](https://github.com/lima-vm/lima/issues/2) in ~/.lima/debian/lima.yaml:
  $ update-template-debian.sh --version trixie ~/.lima/debian/lima.yaml

Flags:
  --backports[=<bool>]    Use the backports image
                          The boolean value can be true, false, [1](https://github.com/lima-vm/lima/issues/1), or [0](https://github.com/lima-vm/lima/issues/0)
  --daily[=<bool>]        Use the daily image
  --timestamped[=<bool>]  Use the timestamped image
  --version <version>     Use the specified version
                          The version can be a codename, version number, or alias (testing, stable, oldstable)
  -h, --help              Print this help message

@norio-nomura norio-nomura marked this pull request as ready for review October 22, 2024 07:07
@norio-nomura
Copy link
Contributor Author

The update-template.sh script updates the date-based URLs for all supported distributions.
The update-template-<distribution>.sh scripts allow distribution-specific options to be used.

@AkihiroSuda AkihiroSuda added this to the v1.0 milestone Oct 22, 2024
@AkihiroSuda AkihiroSuda added guest/debian Guest: Debian guest/ubuntu Guest: Ubuntu labels Oct 22, 2024
@AkihiroSuda
Copy link
Member

Thanks, I tried ./hack/update-template.sh templates/*.yaml but it didn't update Debian

$ ./hack/update-template.sh templates/*.yaml
[...]
Processing templates/centos-stream.yaml                                                                                         
Processing templates/debian-11.yaml                                                                                             
Processing templates/debian-12.yaml                                                                                             
Processing templates/debian.yaml                                                                                                
Processing templates/default.yaml                                                                                               
{                                                                                                                               
  "location": "https://cloud-images.ubuntu.com/releases/24.04/release-20241004/ubuntu-24.04-server-cloudimg-amd64.img",         
  "arch": "x86_64",                                             
  "digest": "sha256:fad101d50b06b26590cf30542349f9e9d3041ad7929e3bc3531c81ec27f2c788"                                           
}
...

$ $(basename "${BASH_SOURCE[0]}") ~/.lima/debian/lima.yaml

Update the Debian image location to debian-13-genericcloud-<arch>.qcow2 in ~/.lima/debian/lima.yaml:
$ $(basename "${BASH_SOURCE[0]}") --version trixie ~/.lima/debian/lima.yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs limactl refactor-reset

Copy link
Contributor Author

@norio-nomura norio-nomura Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

  $ limactl factory-reset debian

to help

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda
Copy link
Member

Please consider squashing the commits

…mplate{,-debian}.sh

cache-common-inc.sh: Prevent `download_to_cache` from rechecking URLs within 10 minutes

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

update-template-ubuntu.sh: add `ubuntu_` prefix to some symbols

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

update-template-ubuntu.sh: change some parameters order in some function

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

update-template-ubuntu.sh: avoid reusing parameters for flags

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

update-template-ubuntu.sh: add `ubuntu_{flavor,version}_from_location`

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

update-template-ubuntu.sh: add `ubuntu_location_url_spec`

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

update-template-ubuntu.sh: temporarily remove URL caching for refactoring

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

update-template-ubuntu.sh: add `ubuntu_{arch,path_suffix}_from_location`

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

update-template-ubuntu.sh: change some functions to return json

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

update-template-ubuntu.sh: refactor checking `url_spec` condition

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

hack/cache-common-inc.sh: change `download_to_cache` to use JSON instead of tsv

Since `unpacked/SHA256SUMS` has no `Content-Type`, the output of `curl -w` formatted as TSV causes `IFS=$'\t' read -r` to fail during parsing.

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

update-template-ubuntu.sh: change downloading `unpacked/SHA256SUMS` to use cache

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

update-template-ubuntu.sh: add `ubuntu_image_entry_for_image_kernel_flavor_version`

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

update-template-ubuntu.sh: change to `ubuntu_image_entry_with_kernel_info`

that returns the image entry with the kernel info instead of returning the kernel info.

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

update-template-ubuntu.sh: change to use `json_vars` for creating JSON

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

update-template-ubuntu.sh: support version aliases: latest, lts

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

update-template-ubuntu.sh: change to fully utilize `set -e`

Functions in this script assume error handling with 'set -e'.
To ensure 'set -e' works correctly:
- Use 'set +e' before assignments and '$(set -e; <function>)' to capture output without exiting on errors.
- Avoid calling functions directly in conditions to prevent disabling 'set -e'.
- Use 'shopt -s inherit_errexit' (Bash 4.4+) to avoid repeated 'set -e' in all '$(...)'.

Changes:
- Add `error_exit` and more error messages
- Remove `|| return` after `$(...)`
- Use `jq -e`

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

update-template-ubuntu.sh: simplify script for `limactl edit --set`

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

update-template-ubuntu.sh: add `image_entry` caching

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

update-template-ubuntu.sh: check for `.kernel` in `image_entry` before adding `.kernel.cmdline`

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

update-template-ubuntu.sh: add error message for when `com.ubuntu.cloud:released:download.json` does not contain a matching image

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

update-template-ubuntu.sh: include `_with_kernel` to cache key if kernel location is not null

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

cache-common-inc.sh: move `error_exit` from `update-template-ubuntu.sh`

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

update-template.sh: add `update-template.sh` script to update all distributions

- Move functions common to all distributions here.
- Scripts for specific distributions accept distribution-specific options and perform additional actions.

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

hack/(cache-common-inc|update-template*).sh: fix some comments

- change `return` to `print` in comments
- remove stale comment

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

update-template-ubuntu.sh: rename the functions to indicate they are using location basename

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

update-template-debian.sh: support updating Debian image information in template files

```console
$ hack/update-template-debian.sh
update-template-debian.sh: Update the Debian image location in the specified templates

Usage:
  update-template-debian.sh [--backports[=<bool>]] [--daily[=<bool>]] [--timestamped[=<bool>]] [--version <version>] <template.yaml>...

Description:
  This script updates the Debian 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 version from the image location basename in the template.

  Image location basename format: debian-<version>[-backports]-genericcloud-<arch>[-daily][-<timestamp>].qcow2

  Published Debian image information is fetched from the following URLs:

    https://cloud.debian.org/images/cloud/<codename>[-backports]/[daily/](latest|<timestamp>)/debian-<version>[-backports]-genericcloud-<arch>[-daily][-<timestamp>].json

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

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

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

  Update the Debian image location to debian-13-genericcloud-<arch>.qcow2 in ~/.lima/debian/lima.yaml:
  $ update-template-debian.sh --version trixie ~/.lima/debian/lima.yaml

Flags:
  --backports[=<bool>]    Use the backports image
                          The boolean value can be true, false, 1, or 0
  --daily[=<bool>]        Use the daily image
  --timestamped[=<bool>]  Use the timestamped image
  --version <version>     Use the specified version
                          The version can be a codename, version number, or alias (testing, stable, oldstable)
  -h, --help              Print this help message
```

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

update-template{,-debian}.sh: fix update-template.sh did not sourced `update-template-debian.sh`

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

update-template{,-debian,-ubuntu}.sh: add `limactl factory-reset` to examples in help

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
@norio-nomura norio-nomura force-pushed the update-template-debian.sh branch from 44aaa52 to 7ff9f6b Compare October 22, 2024 08:43
@norio-nomura
Copy link
Contributor Author

Please consider squashing the commits

done.

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda merged commit 5a7fa80 into lima-vm:master Oct 22, 2024
28 of 29 checks passed
@norio-nomura norio-nomura deleted the update-template-debian.sh branch October 22, 2024 09:45
@norio-nomura
Copy link
Contributor Author

Thanks! 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guest/debian Guest: Debian guest/ubuntu Guest: Ubuntu
Projects
None yet
Development

Successfully merging this pull request may close these issues.

update-template-ubuntu.sh should detect the distro version from the template
2 participants