# Packager = Tuxcore <dev@tuxcore.org> 
pkgname=cloud-init
pkgver=24.3.1
pkgrel=2
pkgdesc="Cloud instance initialization"
arch=(any)
url="https://cloud-init.io"
_url="https://github.com/canonical/cloud-init"
license=('GPL-3.0-only OR Apache-2.0')
depends=(
  bash
  dhclient
  openbsd-netcat
  python
  python-configobj
  python-jinja
  python-jsonpatch
  python-jsonschema
  python-netifaces
  python-oauthlib
  python-pyserial
  python-pyyaml
  python-requests
  python-typing_extensions
  sudo
  systemd
)
makedepends=(
  netplan
  python-build
  python-installer
  python-setuptools
  python-wheel
)
checkdepends=(
  procps-ng
  python-httpretty
  python-passlib
  python-pytest
  python-pytest-mock
  python-responses
)
optdepends=(
  'cloud-guest-utils: for growpart'
  'netplan: for configuring network using netplan'
  'python-passlib: for Azure and BSD support'
  'python-urllib3: for LXD and Scaleway data sources'
)
backup=(
  etc/cloud/cloud.cfg
  etc/cloud/cloud.cfg.d/05_logging.cfg
)
source=(
  $_url/archive/$pkgver/$pkgname-$pkgver.tar.gz
  $pkgname-24.3.1-systemd-ordering.patch
)
sha512sums=('01b798d67328ecd66229568233fb674f45c055ac469adb31a55a909b6b2c8fd1901a833accb66423923b8945210aa4dc6a0d61945787aabe414c01b501b1416d'
            '83985119e95d7f377353d207b7485b496ba60a724a909899071ee0b09a52ecec22c0353d0e107631336db0018bf86db12d4b951b4f2dcbef208f5a0e6cbd2f78')
b2sums=('7e4cb8bd65d34d08b4b4e5ea2370ac952e05b3a210b91a9b29d8e4b633246a9520c2d9259aedfe8edded0d7d761808b86b6b19d98309633c981b2eb0e7cf1f93'
        '5069819bf18f156b35768d3607dba1f3fcd79ef6f0571886734f6472cc2f3fa3db4fc330f32b176501ea7fde34d1ccf15d747a17b4b80ec050a996d106a4afe8')

prepare() {
  # backport of fix for systemd unit ordering issue:
  # https://github.com/canonical/cloud-init/issues/5755
  # also applies https://github.com/canonical/cloud-init/commit/9cc458c7d88467914a29f2608d0b371e36b563b9
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-24.3.1-systemd-ordering.patch
}

build() {
  cd $pkgname-$pkgver
  python -m build --wheel --no-isolation -C--distro=arch -C--prefix=/usr
}

check() {
  local pytest_options=(
   -vv
    # we don't ship /etc/ca-certificates.conf
    --deselect tests/unittests/config/test_cc_ca_certs.py::TestRemoveDefaultCaCerts::test_commands
    --deselect tests/unittests/test_ds_identify.py::TestWSL::test_empty_cloudinitdir
    --deselect tests/unittests/test_ds_identify.py::TestWSL::test_found_via_userdata
    --deselect tests/unittests/config/test_schema.py::TestNetplanValidateNetworkSchema::test_network_config_schema_validation_false_when_skipped
    --deselect 'tests/unittests/config/test_schema.py::TestNetworkSchema::test_network_schema[net_v2_complex_example]'
    --deselect 'tests/unittests/config/test_schema.py::TestNetworkSchema::test_network_schema[net_v2_invalid_config]'
    --deselect 'tests/unittests/config/test_schema.py::TestNetworkSchema::test_network_schema[net_v2_skipped]'
  )
  cd $pkgname-$pkgver
  pytest "${pytest_options[@]}"
}

package() {
  local _file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  # NOTE: due to limitations with PEP517, files are installed to site-packages,
  # not to the correct global locations (e.g. /etc and /usr), so we remove them
  # and install them manually below.
  rm -frv "$pkgdir/$site_packages/"{usr,etc}

  # configuration and hooks
  install -vDm 644 config/cloud.cfg.d/* -t "$pkgdir/etc/cloud/cloud.cfg.d/"
  ./tools/render-template --variant arch ./config/cloud.cfg.tmpl "$pkgdir/etc/cloud/cloud.cfg"
  install -vDm 644 templates/*.tmpl -t "$pkgdir/etc/cloud/templates/"
  install -vDm 755 tools/{ds-identify,hook-hotplug,uncloud-init,write-ssh-key-fingerprints} -t "$pkgdir/usr/lib/$pkgname/"
  # documentation, man pages and shell completion
  install -vDm 644 doc/*.txt -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 doc/examples/*.txt -t "$pkgdir/usr/share/doc/$pkgname/examples/"
  install -vDm 644 doc/examples/seed/* -t "$pkgdir/usr/share/doc/$pkgname/examples/seed/"
  install -vDm 644 doc/man/*.1 -t "$pkgdir/usr/share/man/man1/"
  install -vDm 655 bash_completion/$pkgname -t "$pkgdir/usr/share/bash-completion/completions/"
  # udev rules
  install -vDm 644 udev/*.rules -t "$pkgdir/usr/lib/udev/rules.d/"
  # systemd integration
  install -vdm 755 "$pkgdir/usr/lib/systemd/system"{,-generators}
  for _file in cloud-{config,final,init-{local,main,network}}.service; do
    ./tools/render-template --variant arch ./systemd/$_file.tmpl "$pkgdir/usr/lib/systemd/system/$_file"
  done
  install -vDm 644 systemd/*.{service,socket,target} "$pkgdir/usr/lib/systemd/system/"
  ./tools/render-template --variant arch ./systemd/cloud-init-generator.tmpl "$pkgdir/usr/lib/systemd/system-generators/cloud-init-generator"
  chmod 755 "$pkgdir/usr/lib/systemd/system-generators/cloud-init-generator"
  install -vDm 644 systemd/disable-sshd-keygen-if-cloud-init-active.conf -t "$pkgdir/usr/lib/systemd/system/sshd-keygen@.service.d/"
}
