#!/bin/bash

set -eux

### --start_docs
## Deploying the undercloud
## ========================

## .. note:: In progress documentation is available at https://github.com/dprince/undercloud_containers/blob/master/doit.sh
## ::

## * Export the required environment variables.
## ::

{% for var in (undercloud_env_vars|default([])) %}
export {{ var }}
{% endfor %}

## * Set the local ip used for the undercloud install
## ::

undercloud_local_ip={{ hostvars['undercloud'].undercloud_ip }}

## * Deploy the undercloud
## ::

{% if release in ['pike', 'queens'] -%}
{%- set env_files_path='services-docker' -%}
{%- else -%}
{%- set env_files_path='services' -%}
{%- endif %}

sudo openstack undercloud deploy \
    --heat-native \
    --templates={{ undercloud_templates_path }} \
    --local-ip=$undercloud_local_ip \
{% if undercloud_cloud_domain is defined %}
    --local-domain={{ undercloud_cloud_domain }} \
{% endif %}
{% if containerized_undercloud|bool %}
{% if undercloud_enable_mistral|bool %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/mistral.yaml \
{% endif %}
{% if undercloud_enable_tempest|bool %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/tempest.yaml \
{% endif %}
{% if undercloud_enable_ironic|bool %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/ironic.yaml \
{% endif %}
{% if undercloud_enable_ironic_inspector|bool %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/ironic-inspector.yaml \
{% endif %}
{% if undercloud_enable_zaqar|bool %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/zaqar.yaml \
{% endif %}
{% if undercloud_generate_service_certificate is defined %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/undercloud-haproxy.yaml \
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/undercloud-keepalived.yaml \
{% endif %}
{% if undercloud_enable_telemetry|bool %}
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/undercloud-gnocchi.yaml \
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/undercloud-aodh.yaml \
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/undercloud-panko.yaml \
    -e {{ undercloud_templates_path }}/environments/{{ env_files_path }}/undercloud-ceilometer.yaml \
{% endif %}
{% if release in ['pike', 'queens', 'rocky'] %}
    -e {{ undercloud_templates_path }}/environments/docker.yaml \
{% endif %}
    -e {{ working_dir }}/undercloud-containers-default-parameters.yaml \
    -e {{ working_dir }}/undercloud-parameter-defaults.yaml \
{% endif %}
{% if undercloud_custom_env_files is defined %}
{% for f in undercloud_custom_env_files.split() %}
    -e {{ f }} \
{% endfor %}
{% endif %}
{% if release not in ['pike', 'queens'] and undercloud_roles_data is defined -%}
    -r {{undercloud_roles_data}} \
{% endif %}
{% if undercloud_extra_args %}
    {{ undercloud_extra_args }} \
{% endif %}
    -e {{ working_dir }}/hieradata-overrides-t-h-t-undercloud.yaml

### --stop_docs
