#!/usr/bin/make -f

include /usr/share/openstack-pkg-tools/pkgos.make

NOVA_CONF=$(CURDIR)/debian/nova-common/usr/share/nova-common/nova.conf

%:
	dh $@ --buildsystem=pybuild --with python3,sphinxdoc

override_dh_auto_clean:
	python3 setup.py clean
	rm -rf doc/build/ doc/source/api doc/.autogenerated
	rm -rf tests.sqlite clean.sqlite run_tests.log
	rm -rf CA
	rm -rf nova.egg-info
	rm -f po/nova.pot
	rm -rf .autogenerated
	rm -f debian/nova-common.config debian/nova-common.postinst \
		debian/nova-api.config debian/nova-api.postinst debian/nova-common.postrm
	rm -f debian/*.init debian/*.service debian/*.upstart
	rm -rf build etc/nova/nova.conf.sample
	rm -rf .testrepository subunit.log instances keys
	rm -rf debian/bla doc/source/_static/*.sample
	rm -f debian/nova-api.templates debian/nova-common.templates
	rm -rf debian/po
	find .  -type d -name __pycache__ -exec rm -r {} \+

override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func nova-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func nova-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func nova-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func nova-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_postrm nova-common.postrm
	cp -axuf debian/po.in debian/po
	pkgos-merge-templates nova-api nova endpoint
	pkgos-merge-templates nova-common nova db rabbit ksat

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	set -e ; set -x ; if ! PYTHONPATH=. PYTHON=python3 python3 -m sphinx -b html doc/source $(CURDIR)/debian/nova-doc/usr/share/doc/nova-doc/html ; then \
		cat /tmp/sphinx-err-* ; \
		exit 1 ; \
	fi
	dh_sphinxdoc
endif

override_dh_installman:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. python3 -m sphinx -b man doc/source doc/build/man
	dh_installman
endif

override_dh_auto_install:
	echo "Please do nothing"

override_dh_install:
	for i in $(PYTHON3S) ; do \
		python3 setup.py install -f --install-layout=deb --root=$(CURDIR)/debian/tmp ; \
	done

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# Fails on buildd:
	# db.main.test_api.ArchiveTestCase.test_archive_deleted_rows_task_log
	# Non-deterministic (see: https://bugs.launchpad.net/nova/+bug/2121125):
	# nova.tests.unit.compute.test_compute.ComputeTestCase.test_add_remove_fixed_ip_updates_instance_updated_at
	# Non-deterministic (See: https://bugs.launchpad.net/nova/+bug/2130703)
	# nova.tests.unit.policies.test_simple_tenant_usage..*
	pkgos-dh_auto_test --no-py2 'nova\.tests\.unit\.(?!(.*virt.libvirt\.test_driver\.LibvirtConnTestCase\.test_spawn_with_config_drive.*|.*test_wsgi\.TestWSGIServerWithSSL.*|.*test_hacking\.HackingTestCase.*|.*CreateInstanceTypeTest\.test_name_with_non_printable_characters.*|.*PatternPropertiesTestCase\.test_validate_patternProperties_fails.*|.*virt\.libvirt\.test_driver\.LibvirtDriverTestCase\.test_get_disk_xml.*|.*virt\.libvirt\.test_driver\.LibvirtConnTestCase\.test_detach_volume_with_vir_domain_affect_live_flag.*|.*virt\.libvirt\.test_driver\.LibvirtConnTestCase\.test_update_volume_xml.*|.*console\.test_websocketproxy\.NovaProxyRequestHandlerTestCase\.test_tcp_rst_no_compute_rpcapi.*|.*virt\.libvirt\.test_blockinfo\.LibvirtBlockInfoTest\.test_get_disk_mapping_rescue_with_config.*|.*virt\.libvirt\.test_blockinfo\.LibvirtBlockInfoTest\.test_get_disk_mapping_stable_rescue_ide_cdrom.*|.*virt\.libvirt\.volume\.test_nvme\.LibvirtNVMEVolumeDriverTestCase\.test_libvirt_nvme_driver_connect.*|.*virt\.libvirt\.volume\.test_nvme\.LibvirtNVMEVolumeDriverTestCase\.test_libvirt_nvme_driver_disconnect.*|.*virt\.libvirt\.volume\.test_nvme\.LibvirtNVMEVolumeDriverTestCase\.test_libvirt_nvme_driver_get_config.*|.*virt\.libvirt\.volume\.test_scaleio\.LibvirtScaleIOVolumeDriverTestCase.*|.*virt\.libvirt\.test_driver\.LibvirtDriverTestCase\.test_cross_cell_move_rbd_flatten_fetch_image_cache.*|.*virt\.libvirt\.test_driver\.LibvirtConnTestCase\.test_check_discard_for_attach_volume_blk_controller_no_unmap.*|.*virt\.libvirt\.test_driver\.LibvirtConnTestCase\.test_check_discard_for_attach_volume_no_unmap.*|.*virt\.libvirt\.test_driver\.LibvirtConnTestCase\.test_check_discard_for_attach_volume_valid_controller.*|.*virt\.libvirt\.test_driver\.LibvirtDriverTestCase\.test_rbd_image_flatten_during_fetch_image_cache.*|.*test_utils\.GenericUtilsTestCase\.test_temporary_chown.*|console\.test_websocketproxy\.NovaProxyRequestHandlerTestCase\.test_reject_open_redirect|console\.test_websocketproxy\.NovaProxyRequestHandlerTestCase\.test_reject_open_redirect_3_slashes|privsep\.test_utils\.SupportDirectIOTestCase\.test_supports_direct_io_with_exception_in_open|privsep\.test_utils\.SupportDirectIOTestCase\.test_supports_direct_io_with_exception_in_write|notifications\.objects\.test_objects\.TestObjectVersions\.test_versions|objects\.test_objects\.TestObjectVersions\.test_versions|notifications\.objects\.test_notification\.TestNotificationObjectVersions\.test_versions|db\.main\.test_api\.ArchiveTestCase\.test_archive_deleted_rows_task_log|db\.main\.test_api\.UnsupportedDbRegexpTestCase\.test_instance_get_all_by_filters_sort_keys|nova\.tests\.unit\.compute\.test_compute\.ComputeTestCase\.test_add_remove_fixed_ip_updates_instance_updated_at))'
endif

	rm -rf $(CURDIR)/debian/tmp/usr/etc

	mkdir -p $(CURDIR)/debian/nova-common/usr/share/nova-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file=$(CURDIR)/debian/nova-common/usr/share/nova-common/policy.json.example \
		--format json \
		--namespace nova

	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file=$(CURDIR)/debian/nova-common/usr/share/nova-common/policy.yaml.example \
		--format yaml \
		--namespace nova

	mkdir -p $(CURDIR)/debian/nova-common/usr/share/nova-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file=$(CURDIR)/debian/nova-common/usr/share/nova-common/nova.conf \
		--wrap-width 140 \
		--namespace nova.conf \
		--namespace oslo.limit \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.policy \
		--namespace oslo.privsep \
		--namespace oslo.service.periodic_task \
		--namespace oslo.service.service \
		--namespace oslo.middleware \
		--namespace oslo.concurrency \
		--namespace oslo.reports \
		--namespace oslo.versionedobjects \
		--namespace keystonemiddleware.auth_token \
		--namespace osprofiler \
		--namespace os_vif \
		--namespace os_brick

	pkgos-readd-keystone-authtoken-missing-options $(NOVA_CONF) keystone_authtoken nova

	mkdir -p $(CURDIR)/debian/nova-common/etc/nova/policy.d
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file=$(CURDIR)/debian/nova-common/etc/nova/policy.d/00_default_policy.yaml \
		--format yaml \
		--namespace nova

	# Comment out deprecated policies, otherwise, Nova doesn't work
	sed -i 's/^"/#"/' $(CURDIR)/debian/nova-common/etc/nova/policy.d/00_default_policy.yaml

	# Use the policy.d folder
	pkgos-fix-config-default $(CURDIR)/debian/nova-common/usr/share/nova-common/nova.conf oslo_policy policy_dirs /etc/nova/policy.d


	# Otherwise, this is set to the build dir
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT pybasedir /usr/lib/python3/dist-packages
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT bindir /usr/bin

	# General needed fixes
	pkgos-fix-config-default $(NOVA_CONF) oslo_concurrency lock_path /var/lock/nova
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT state_path /var/lib/nova

	# Let's use Neutron by default, and not stupid nova-network
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT security_group_api neutron
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT network_api_class nova.network.neutronv2.api.API
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT use_neutron True
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT linuxnet_interface_driver nova.network.linux_net.LinuxOVSInterfaceDriver
	pkgos-fix-config-default $(NOVA_CONF) DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver
	pkgos-fix-config-default $(NOVA_CONF) neutron default_floating_pool ext-net

	# Fixup a few defaults for the [neutron] auth
	pkgos-fix-config-default $(NOVA_CONF) neutron auth_type password
	pkgos-fix-config-default $(NOVA_CONF) neutron username admin
	pkgos-fix-config-default $(NOVA_CONF) neutron user_domain_name default
	pkgos-fix-config-default $(NOVA_CONF) neutron project_name admin
	pkgos-fix-config-default $(NOVA_CONF) neutron project_domain_name default
	pkgos-fix-config-default $(NOVA_CONF) neutron service_name neutron
	pkgos-fix-config-default $(NOVA_CONF) neutron auth_url http://127.0.0.1:5000

	pkgos-fix-config-default $(NOVA_CONF) neutron service_metadata_proxy true

	# Convenient default for the SQLite default db
	pkgos-fix-config-default $(NOVA_CONF) database connection sqlite:////var/lib/nova/novadb

	# Let's use SPICE byd default
	pkgos-fix-config-default $(NOVA_CONF) VNC enabled false
	pkgos-fix-config-default $(NOVA_CONF) VNC vncserver_listen 0.0.0.0
	pkgos-fix-config-default $(NOVA_CONF) spice vncserver_listen 0.0.0.0
	pkgos-fix-config-default $(NOVA_CONF) spice server_listen 0.0.0.0
	pkgos-fix-config-default $(NOVA_CONF) spice server_proxyclient_address \$$my_ip
	pkgos-fix-config-default $(NOVA_CONF) spice enabled true

	# Works by default
	pkgos-fix-config-default $(NOVA_CONF) keystone_authtoken auth_protocol http

	# Cinder os_region_name set to regionOne as default value
	pkgos-fix-config-default $(NOVA_CONF) cinder os_region_name regionOne

	# Fixup a few default for the [placement] auth
	pkgos-fix-config-default $(NOVA_CONF) placement region_name regionOne
	pkgos-fix-config-default $(NOVA_CONF) placement project_domain_name default
	pkgos-fix-config-default $(NOVA_CONF) placement project_name service
	pkgos-fix-config-default $(NOVA_CONF) placement auth_type password
	pkgos-fix-config-default $(NOVA_CONF) placement user_domain_name default
	pkgos-fix-config-default $(NOVA_CONF) placement auth_url http://localhost:5000/v3
	pkgos-fix-config-default $(NOVA_CONF) placement username = placement

	dh_install
	dh_missing --fail-missing -Xbin/nova-all -Xbin/nova-network

	install -D -m 0440 $(CURDIR)/debian/nova-common.sudoers $(CURDIR)/debian/nova-common/etc/sudoers.d/nova-common
	install -D -m 0644 $(CURDIR)/etc/nova/logging_sample.conf $(CURDIR)/debian/nova-common/usr/share/nova-common/logging.conf
	set -e ; for hypervisor in qemu kvm lxc ironic; do \
		install -D -m 0600 $(CURDIR)/debian/nova-compute-$${hypervisor}.conf $(CURDIR)/debian/nova-compute-$${hypervisor}/etc/nova/nova-compute.conf; \
	done
	find $(CURDIR)/debian -name .gitignore -delete

	echo "Generating bash completion..."
	mkdir -p $(CURDIR)/debian/nova-common/usr/share/bash-completion/completions
	mkdir -p $(CURDIR)/debian/nova-common/usr/share/zsh/vendor-completions
	set -e ; set -x ; for i in manage status ; do \
		echo "nova-$$i" ; \
		PYTHONPATH=$(CURDIR)/debian/python3-nova/usr/lib/python3/dist-packages $(CURDIR)/debian/nova-common/usr/bin/nova-$$i --shell_completion bash >$(CURDIR)/debian/nova-common/usr/share/bash-completion/completions/nova-$$i ; \
		PYTHONPATH=$(CURDIR)/debian/python3-nova/usr/lib/python3/dist-packages $(CURDIR)/debian/nova-common/usr/bin/nova-$$i --shell_completion zsh >$(CURDIR)/debian/nova-common/usr/share/zsh/vendor-completions/_nova-$$i ; \
	done

override_dh_fixperms:
	dh_fixperms -Xnova_sudoers
	dh_fixperms -Xnova_tgt

override_dh_auto_test:
	echo "Do nothing..."

# We use override_dh_installmenu because it's done in the sequence with dh,
# and we already override dh_installinit in pkgos.make.
# Obviously, we will never use a Desktop menu in such a package, so that's
# not a problem.
override_dh_installmenu:
	dh_installinit --name=nova-novncproxy
	dh_installinit --name=nova-spicehtml5proxy
	dh_installinit --name=nova-serialproxy
	dh_installinit --name=nova-api
	dh_installinit --name=nova-api-metadata
	install -D -m 0644 $(CURDIR)/debian/nova-consoleproxy.mydefault $(CURDIR)/debian/nova-consoleproxy/usr/share/nova-consoleproxy/default

override_dh_installsystemd:
	dh_installsystemd -pnova-compute
	dh_installsystemd -pnova-conductor
	dh_installsystemd -pnova-scheduler
	dh_installsystemd -pnova-api --name=nova-api
	dh_installsystemd -pnova-api --name=nova-api-metadata
	dh_installsystemd -pnova-consoleproxy --name=nova-novncproxy
	dh_installsystemd -pnova-consoleproxy --name=nova-serialproxy
	dh_installsystemd -pnova-consoleproxy --name=nova-spicehtml5proxy

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
