## template: jinja
%define use_systemd (0%{?fedora} && 0%{?fedora} >= 18) || (0%{?rhel} && 0%{?rhel} >= 7)

%if %{use_systemd}
%define init_system systemd
%else
%define init_system sysvinit
%endif

# 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:           cloud-init
Version:        {{rpm_upstream_version}}
Release:        1{{subrelease}}%{?dist}
Summary:        Cloud instance init scripts

Group:          System Environment/Base
License:        Dual-licesed GPLv3 or Apache 2.0
URL:            http://launchpad.net/cloud-init

Source0:        {{archive_name}}
BuildArch:      noarch
BuildRoot:      %{_tmppath}

%if "%{?el6}" == "1"
BuildRequires:  python-argparse
%endif
%if %{use_systemd}
Requires:           systemd
BuildRequires:      systemd
Requires:           systemd-units
BuildRequires:      systemd-units
%else
Requires:           initscripts >= 8.36
Requires(postun):   initscripts
Requires(post):     chkconfig
Requires(preun):    chkconfig
%endif

{% for r in buildrequires %}
BuildRequires:  {{r}}
{% endfor %}

# System util packages needed
%ifarch %{?ix86} x86_64 ia64
Requires:       dmidecode
%endif

# python2.6 needs argparse
%if "%{?el6}" == "1"
Requires:  python-argparse
%endif


# Install 'dynamic' runtime reqs from *requirements.txt and pkg-deps.json.
# Install them as BuildRequires too as they're used for testing.
{% for r in requires %}
BuildRequires:  {{r}}
Requires:       {{r}}
{% endfor %}

# Custom patches
{% for p in patches %}
Patch{{loop.index0}}: {{p}}
{% endfor %}

%if "%{init_system}" == "systemd"
Requires(post):       systemd
Requires(preun):      systemd
Requires(postun):     systemd
%else
Requires(post):       chkconfig
Requires(postun):     initscripts
Requires(preun):      chkconfig
Requires(preun):      initscripts
%endif

%description
Cloud-init is a set of init scripts for cloud instances.  Cloud instances
need special scripts to run during initialization to retrieve and install
ssh keys and to let the user run various scripts.

%prep
%setup -q -n {{source_name}}

# Custom patches activation
{% for p in patches %}
%patch{{loop.index0}} -p1
{% endfor %}

%build
%{__python3} setup.py build

%install

%{__python3} setup.py install -O1 \
            --skip-build --root $RPM_BUILD_ROOT \
            --init-system=%{init_system}

# Note that /etc/rsyslog.d didn't exist by default until F15.
# el6 request: https://bugzilla.redhat.com/show_bug.cgi?id=740420
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/rsyslog.d
cp -p tools/21-cloudinit.conf \
      $RPM_BUILD_ROOT/%{_sysconfdir}/rsyslog.d/21-cloudinit.conf

# Remove the tests
rm -rf $RPM_BUILD_ROOT%{python3_sitelib}/tests

# Required dirs...
mkdir -p $RPM_BUILD_ROOT/%{_sharedstatedir}/cloud
mkdir -p $RPM_BUILD_ROOT/%{_libexecdir}/%{name}

# patch in the full version to version.py
version_pys=$(cd "$RPM_BUILD_ROOT" && find . -name version.py -type f)
[ -n "$version_pys" ] ||
   { echo "failed to find 'version.py' to patch with version." 1>&2; exit 1; }
( cd "$RPM_BUILD_ROOT" &&
  sed -i "s,@@PACKAGED_VERSION@@,%{version}-%{release}," $version_pys )

%clean
rm -rf $RPM_BUILD_ROOT

%post

%if "%{init_system}" == "systemd"
if [ $1 -eq 1 ]
then
    /bin/systemctl enable cloud-config.service     >/dev/null 2>&1 || :
    /bin/systemctl enable cloud-final.service      >/dev/null 2>&1 || :
    /bin/systemctl enable cloud-init.service       >/dev/null 2>&1 || :
    /bin/systemctl enable cloud-init-local.service >/dev/null 2>&1 || :
fi
%else
/sbin/chkconfig --add %{_initrddir}/cloud-init-local
/sbin/chkconfig --add %{_initrddir}/cloud-init
/sbin/chkconfig --add %{_initrddir}/cloud-config
/sbin/chkconfig --add %{_initrddir}/cloud-final
%endif

%preun

%if "%{init_system}" == "systemd"
if [ $1 -eq 0 ]
then
    /bin/systemctl --no-reload disable cloud-config.service >/dev/null 2>&1 || :
    /bin/systemctl --no-reload disable cloud-final.service  >/dev/null 2>&1 || :
    /bin/systemctl --no-reload disable cloud-init.service   >/dev/null 2>&1 || :
    /bin/systemctl --no-reload disable cloud-init-local.service >/dev/null 2>&1 || :
fi
%else
if [ $1 -eq 0 ]
then
    /sbin/service cloud-init stop >/dev/null 2>&1 || :
    /sbin/chkconfig --del cloud-init || :
    /sbin/service cloud-init-local stop >/dev/null 2>&1 || :
    /sbin/chkconfig --del cloud-init-local || :
    /sbin/service cloud-config stop >/dev/null 2>&1 || :
    /sbin/chkconfig --del cloud-config || :
    /sbin/service cloud-final stop >/dev/null 2>&1 || :
    /sbin/chkconfig --del cloud-final || :
fi
%endif

%postun

%if "%{init_system}" == "systemd"
/bin/systemctl daemon-reload >/dev/null 2>&1 || :
%endif

%files

/lib/udev/rules.d/66-azure-ephemeral.rules

%if "%{init_system}" == "systemd"
/usr/lib/systemd/system-generators/cloud-init-generator
%{_sysconfdir}/systemd/system/sshd-keygen@.service.d/disable-sshd-keygen-if-cloud-init-active.conf
%{_unitdir}/cloud-*
%else
%attr(0755, root, root) %{_initddir}/cloud-config
%attr(0755, root, root) %{_initddir}/cloud-final
%attr(0755, root, root) %{_initddir}/cloud-init-local
%attr(0755, root, root) %{_initddir}/cloud-init
%endif

%{_sysconfdir}/NetworkManager/dispatcher.d/hook-network-manager
%{_sysconfdir}/dhcp/dhclient-exit-hooks.d/hook-dhclient

# Program binaries
%{_bindir}/cloud-init*
%{_bindir}/cloud-id*

# Docs
%doc LICENSE ChangeLog TODO.rst requirements.txt
%doc %{_defaultdocdir}/cloud-init/*

# Configs
%config(noreplace)      %{_sysconfdir}/cloud/cloud.cfg
%dir                    %{_sysconfdir}/cloud/cloud.cfg.d
%config(noreplace)      %{_sysconfdir}/cloud/cloud.cfg.d/*.cfg
%config(noreplace)      %{_sysconfdir}/cloud/cloud.cfg.d/README
%dir                    %{_sysconfdir}/cloud/templates
%config(noreplace)      %{_sysconfdir}/cloud/templates/*
%config(noreplace) %{_sysconfdir}/rsyslog.d/21-cloudinit.conf

# Bash completion script
%{_datadir}/bash-completion/completions/cloud-init

%{_libexecdir}/%{name}
%dir %{_sharedstatedir}/cloud

# Python code is here...
%{python3_sitelib}/*
