%define __python /usr/bin/python3

%{!?python_sitelib: %global python_sitelib %(%{__python} -c "from sysconfig import get_path; print(get_path('platlib'))")}

%define pkg_version 0.0.5

# See: http://www.zarb.org/~jasonc/macros.php
# Or: http://fedoraproject.org/wiki/Packaging:ScriptletSnippets
# Or: http://www.rpm.org/max-rpm/ch-rpm-inside.html

Name:           scaleway-ecosystem
Version:        %pkg_version
Release:        3
Summary:        Scaleway Cloud Ecosystem

Group:          System Environment/Base
License:        Dual-licesed GPLv3 or Apache 2.0
URL:            https://www.scaleway.com

Source0:        scaleway-ecosystem-%pkg_version.tgz

BuildArch:      noarch
BuildRoot:      %{_tmppath}

Requires:           systemd
BuildRequires:      systemd python3

Patch0:		scaleway-ecosystem-vpc-force-scw-vpc-ifup-to-run-after-cloud-init-to-us.patch
Patch1:		scaleway-ecosystem-vpc-Use-add-move-udev-hook-for-all-distros.patch
Patch2:		scaleway-ecosystem-vpc-Let-netplan-complete-before-setting-route.patch
Patch3:		scaleway-ecosystem-vpc-Drop-the-use-of-scw-metadata-in-scripts.patch
Patch4:		scaleway-ecosystem-run_vpc_iface_service_after_NM.patch
Patch5:		scaleway-ecosystem-vpc-remove-bindto-in-systemd-unit.patch

%description
Scaleway Ecosystem is a set of scripts and systemd units designed to
customize the Cloud image to fit Scaleway's environment.

%prep
%setup -q -n scaleway-ecosystem-%pkg_version
%patch0 -p2
%patch1 -p2
%patch2 -p2
%patch3 -p2
%patch4 -p2
%patch5 -p2

%install
cp -pr etc $RPM_BUILD_ROOT
cp -pr usr $RPM_BUILD_ROOT
cp -pr lib $RPM_BUILD_ROOT
mv $RPM_BUILD_ROOT/usr/bin/scw-vpc-ifup.sysconfig $RPM_BUILD_ROOT/usr/bin/scw-vpc-ifup
mv $RPM_BUILD_ROOT/usr/bin/scw-vpc-ifdown.sysconfig $RPM_BUILD_ROOT/usr/bin/scw-vpc-ifdown
rm $RPM_BUILD_ROOT/usr/bin/scw-vpc-ifup.*
rm $RPM_BUILD_ROOT/usr/bin/scw-vpc-ifdown.*

%clean
rm -rf $RPM_BUILD_ROOT

%post
%systemd_post scw-set-hostname.service
%systemd_post scw-set-hostname.service
%systemd_post scw-signal-booted.service
%systemd_post scw-generate-ssh-keys.service
%systemd_post scw-fetch-ssh-keys.service
%systemd_post scw-sync-kernel-modules.service
%systemd_post vpc-iface@.service
/usr/bin/chmod 0755 /usr
#
#  Make sure that the gateway to api-metadata is present
#
gw_route=$(ip r | grep "169.254.42.42" || true)
PRIVATE_IP=$(grep private_ip /run/cloud-init/instance-data.json | cut -d":" -f2 | sed 's/[", ]//g')
PREFIX=${PRIVATE_IP%.*}
# On some Fedora distro the gateway is wrongly set on the VPC nic
priv_nic=$(ip a | grep inet | grep $PREFIX | awk '{print $NF}' || true)
vpc_nic=$(ip l | grep "ether 02:00" || true)
eth0_in_route=$(echo $gw_route | grep $priv_nic || true)
if [ "$eth0_in_route" = "" ] && [ "$vpc_nic" != "" ];then
	SUFFIX=${PRIVATE_IP#${PREFIX}.}
	last=`expr $SUFFIX - 1`
	gateway4="$PREFIX.$last"
	ip route del 169.254.42.42 || true
	ip route add 169.254.42.42/32 via ${gateway4} dev $priv_nic || true
fi


%preun
%systemd_preun scw-set-hostname.service
%systemd_preun scw-set-hostname.service
%systemd_preun scw-signal-booted.service
%systemd_preun scw-generate-ssh-keys.service
%systemd_preun scw-fetch-ssh-keys.service
%systemd_preun scw-sync-kernel-modules.service

%files
%attr(0755, root, root) /usr/bin/scw*
/usr/bin/oc*
%attr(0755, root, root) /usr/sbin/scw*
/usr/sbin/oc*
%attr(0644, root, root) /etc/sysctl.d/99-scaleway.conf
%attr(0644, root, root) /etc/systemd/system-preset/00-scaleway-ecosystem.preset
%attr(0644, root, root) /etc/scw-kernel-check.conf
%attr(0644, root, root) /etc/hosts.default
%attr(0755, root, root) /etc/NetworkManager/dispatcher.d/scw-metadata
%attr(0644, root, root) /lib/systemd/system/scw-fetch-ssh-keys.service
%attr(0644, root, root) /lib/systemd/system/scw-generate-ssh-keys.service
%attr(0644, root, root) /lib/systemd/system/scw-set-hostname.service
%attr(0644, root, root) /lib/systemd/system/scw-signal-booted.service
%attr(0644, root, root) /lib/systemd/system/scw-sync-kernel-modules.service
%attr(0644, root, root) /lib/systemd/system/scw-vpc-iface@.service
%attr(0644, root, root) /lib/udev/rules.d/72-scw-vpc-iface.rules

%changelog

*Thu Jan 20 2022 Louis Bouchard <lbouchard@scaleway.com> - 0.0.5-3
- Remove BindTo statement from scw-vpc-iface@.service :
  The NIC does not come up on Ubuntu Focal otherwise. Tested
  OK on other distributions.

*Tue Sep 28 2021 Louis Bouchard <lbouchard@scaleway.com> - 0.0.5-1
- No change release to stay in sync with debian packaging

*Fri Aug 27 2021 Mathieu Poussin <mpoussin@scaleway.com> - 0.0.4-2
- Remove dependencies on systemd-units (fix broken dependencies for openSUSE)

*Thu Apr 15 2021 Louis Bouchard <lbouchard@scaleway.com> - 0.0.4
- Implement support for VPC GW
  - Add vpc-gateway config scripts
  - include alternative udev rules in install
  - Parametrize file name for centos udev rules
  - Use only ifup for vpc iface scripts, detect ifupdown file extension
    to be set or not
  - Make vpc udev rules run before any ifupdown rules
  - Disabling cloud-init's udev rules for ifupdown as it conflicts with
    our boot process when a vpc iface is plugged
- Remove root password generation

* Fri Nov 20 2020 Louis Bouchard <lbouchard@scaleway.com> - 0.0.3
- No change release to raise the version number to stay in sync
  with the DEV version

* Tue Oct 20 2020 Louis Bouchard <lbouchard@scaleway.com> - 0.0.2
- Move files out of /usr/local
- Move systemd units for proper installation
- Handle cleanup to prior version if pkg removed

* Mon Sep 21 2020 Louis Bouchard <lbouchard@scaleway.com> - 0.0.1
- Set ownership on Scaleway specific files
- Force 0755 on /usr