#!/usr/bin/python
# Copyright 2022 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import argparse
import jinja2
import logging
import os
import sys
import yaml

# STANDALONE_VARS is a dict with keys of ansible var names, and values of a
# jinja2 template. The jinja2 template can use jinja2 variables as defined from
# TEMPLATE_VARS below.
STANDALONE_VARS = {
    'tripleo_nova_compute_DEFAULT_transport_url': {
        'template':
            'rabbit://'
            '{% for node in oslo_messaging_rpc_node_names %}'
            '{% if not loop.first %},{% endif %}'
            '{{oslo_messaging_rpc_user_name}}:{{oslo_messaging_rpc_password}}'
            '@{{node}}:5672'
            '{% endfor %}'
            '/?ssl={% if oslo_messaging_rpc_use_ssl=="True" %}1'
            '{% else %}0{% endif %}',
        'section': 'DEFAULT',
        'option': 'transport_url'
    },
    'tripleo_nova_compute_oslo_messaging_notifications_transport_url': {
        'template':
            'rabbit://'
            '{% for node in oslo_messaging_rpc_node_names %}'
            '{% if not loop.first %},{% endif %}'
            '{{oslo_messaging_rpc_user_name}}:{{oslo_messaging_rpc_password}}'
            '@{{node}}:5672'
            '{% endfor %}'
            '/?ssl={% if oslo_messaging_rpc_use_ssl=="True" %}1'
            '{% else %}0{% endif %}',
        'section': 'DEFAULT',
        'option': 'oslo_messaging_notifications_transport_url'
    },
    'tripleo_nova_compute_cache_memcache_servers': {
        'template':
            '{% for node in memcached_node_names %}'
            '{% if not loop.first %},{% endif %}'
            '{{ node }}:{{ memcached_port }}'
            '{% endfor %}',
        'section': 'cache',
        'option': 'memcache_servers'
    },
    'tripleo_nova_compute_cinder_auth_url': {
        'template': '{{ nova_cinder_auth_url }}',
        'section': 'cinder',
        'option': 'auth_url'
    },
    'tripleo_nova_compute_cinder_password': {
        'template': '{{ nova_cinder_password }}',
        'section': 'cinder',
        'option': 'password'
    },
    'tripleo_nova_compute_neutron_auth_url': {
        'template': '{{ nova_neutron_auth_url }}',
        'section': 'neutron',
        'option': 'auth_url'
    },
    'tripleo_nova_compute_neutron_password': {
        'template': '{{ nova_neutron_password }}',
        'section': 'neutron',
        'option': 'password'
    },
    'ctlplane_dns_nameservers': {
        'template': '{{ ctlplane_dns_nameservers }}',
    },
    'dns_search_domains': {
        'template': '{{ dns_search_domains }}',
    },
    'tripleo_nova_compute_vnc_novncproxy_base_url': {
        'template':
            '{{ vncproxy_protocol }}://{{ vncproxy_host }}:{{ vncproxy_port }}',
        'section': 'vnc',
        'option': 'novncproxy_base_url'
    },
    'tripleo_nova_compute_service_user_username': {
        'template': 'nova',
        'section': 'service_user',
        'option': 'username'
    },
    'tripleo_nova_compute_service_user_password': {
        'template': '{{ service_user_password }}',
        'section': 'service_user',
        'option': 'password'
    },
    'tripleo_nova_compute_service_user_auth_url': {
        'template': '{{ service_user_auth_url }}',
        'section': 'service_user',
        'option': 'auth_url'
    },
    'tripleo_nova_compute_service_user_auth_type': {
        'template': 'password',
        'section': 'service_user',
        'option': 'auth_type'
    },
    'tripleo_nova_compute_service_user_project_name': {
        'template': '{{ service_user_project_name }}',
        'section': 'service_user',
        'option': 'project_name'
    },
    'tripleo_nova_compute_service_user_project_domain_name': {
        'template': 'Default',
        'section': 'service_user',
        'option': 'project_domain_name'
    },
    'tripleo_nova_compute_service_user_user_domain_name': {
        'template': 'Default',
        'section': 'service_user',
        'option': 'user_domain_name'
    },
    'tripleo_nova_compute_service_user_region_name': {
        'template': '{{ service_user_region_name }}',
        'section': 'service_user',
        'option': 'region_name'
    },
    'tripleo_nova_compute_service_user_send_service_user_token': {
        'template': '{{ service_user_send_service_user_token }}',
        'section': 'service_user',
        'option': 'send_service_user_token'
    },

    'tripleo_nova_compute_placement_username': {
        'template': 'placement',
        'section': 'placement',
        'option': 'username'
    },
    'tripleo_nova_compute_placement_password': {
        'template': '{{ placement_password }}',
        'section': 'placement',
        'option': 'password'
    },
    'tripleo_nova_compute_placement_auth_url': {
        'template': '{{ placement_auth_url }}',
        'section': 'placement',
        'option': 'auth_url'
    },
    'tripleo_nova_compute_placement_auth_type': {
        'template': 'password',
        'section': 'placement',
        'option': 'auth_type'
    },
    'tripleo_nova_compute_placement_project_name': {
        'template': '{{ placement_project_name }}',
        'section': 'placement',
        'option': 'project_name'
    },
    'tripleo_nova_compute_placement_project_domain_name': {
        'template': 'Default',
        'section': 'placement',
        'option': 'project_domain_name'
    },
    'tripleo_nova_compute_placement_user_domain_name': {
        'template': 'Default',
        'section': 'placement',
        'option': 'user_domain_name'
    },
    'tripleo_nova_compute_placement_region_name': {
        'template': '{{ placement_region_name }}',
        'section': 'placement',
        'option': 'region_name'
    },
    'tripleo_nova_compute_placement_valid_interfaces': {
        'template': '{{ placement_valid_interfaces }}',
        'section': 'placement',
        'option': 'valid_interfaces'
    },
    'tripleo_nova_compute_neutron_auth_type': {
        'template': 'v3password',
        'section': 'neutron',
        'option': 'auth_type'
    },
    'tripleo_nova_compute_neutron_project_name': {
        'template': 'service',
        'section': 'neutron',
        'option': 'project_name'
    },
    'tripleo_nova_compute_neutron_user_domain_name': {
        'template': 'Default',
        'section': 'neutron',
        'option': 'user_domain_name'
    },
    'tripleo_nova_compute_neutron_project_domain_name': {
        'template': 'Default',
        'section': 'neutron',
        'option': 'project_domain_name'
    },
    'tripleo_nova_compute_neutron_region_name': {
        'template': 'regionOne',
        'section': 'neutron',
        'option': 'region_name'
    },
    'tripleo_nova_compute_neutron_username': {
        'template': 'neutron',
        'section': 'neutron',
        'option': 'username'
    },
    'tripleo_ovn_dbs': {
        'template': '{{ ovn_dbs }}',
    },
}

# TEMPLATE_VARS is a dict with keys jinja2 variables names that will be passed
# when the templates from STANDALONE_VARS are rendered. The values are a dict
# of a file name and YAML key to look up from the config-download directory.
# The file names use string formatting with replacement fields (role).
TEMPLATE_VARS = {
    'oslo_messaging_rpc_user_name': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.oslo_messaging_rpc_user_name'
    },
    'oslo_messaging_rpc_password': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.oslo_messaging_rpc_password'
    },
    'oslo_messaging_rpc_use_ssl': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.oslo_messaging_rpc_use_ssl'
    },
    'oslo_messaging_rpc_node_names': {
        'file': 'group_vars/overcloud.json',
        'key': 'oslo_messaging_rpc_node_names'
    },
    'memcached_port': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.memcached_port'
    },
    'memcached_node_names': {
        'file': 'group_vars/overcloud.json',
        'key': 'memcached_node_names'
    },
    'nova_cinder_auth_url': {
        'file': 'group_vars/{role}',
        # nova-compute which sets nova::cinder::auth_url may not be deployed
        # if the deployment is a standalone ctlplane, but the auth_url for
        # neutron is the same value.
        'key': 'service_configs.nova::network::neutron::auth_url'
    },
    'nova_cinder_password': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.cinder::db::mysql::password'
    },
    'nova_neutron_auth_url': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::network::neutron::auth_url'
    },
    'nova_neutron_password': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::network::neutron::password'
    },
    'ctlplane_dns_nameservers': {
        'file': 'group_vars/{role}',
        'key': 'ctlplane_dns_nameservers'
    },
    'dns_search_domains': {
        'file': 'group_vars/{role}',
        'key': 'dns_search_domains'
    },
    'vncproxy_protocol': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::vncproxy::common::vncproxy_protocol'
    },
    'vncproxy_host': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::vncproxy::common::vncproxy_host'
    },
    'vncproxy_port': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::vncproxy::common::vncproxy_port'
    },

    'service_user_password': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::keystone::service_user::password'
    },
    'service_user_auth_url': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::keystone::service_user::auth_url'
    },
    'service_user_project_name': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::keystone::service_user::project_name'
    },
    'service_user_region_name': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::keystone::service_user::region_name'
    },
    'service_user_send_service_user_token': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::keystone::service_user::send_service_user_token'
    },

    'placement_password': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::placement::password'
    },
    'placement_auth_url': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::placement::auth_url'
    },
    'placement_project_name': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::placement::project_name'
    },
    'placement_region_name': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::placement::region_name'
    },
    'placement_valid_interfaces': {
        'file': 'group_vars/{role}',
        'key': 'service_configs.nova::placement::valid_interfaces'
    },
    'ovn_dbs': {
        'file': 'group_vars/overcloud.json',
        'key': 'ovn_dbs_node_ips'
    },
}


def parse_args():
    parser = argparse.ArgumentParser(
        description=("tripleo-standalone-vars"),
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '--config-download-dir', '-c',
        action='store',
        default='~/overcloud-deploy/overcloud/config-download/overcloud',
        help=('The config-download directory for the deployment used as '
              'the source of the generated ansible variables.'))
    parser.add_argument('--output-file', '-o',
                        default='99-standalone-vars',
                        help=("Output file containing the generated ansible "
                              "vars."))
    parser.add_argument('--role', '-r',
                        default='Controller',
                        help="Primary role name from the source deployment.")
    parser.add_argument('--force', '-f',
                        action='store_true',
                        help="Force overwriting the output file if it exists.")

    args = parser.parse_args(sys.argv[1:])
    return args


def main():

    logging.basicConfig()
    log = logging.getLogger()
    log.setLevel(logging.INFO)

    args = parse_args()

    if os.path.exists(args.output_file) and not args.force:
        print("Output file {} exists. Won't continue without --force, or "
              "delete the file first.".format(args.output_file))
        sys.exit(1)

    tmpl_vars = {}
    standalone_vars = {}
    nova_config = {}
    standalone_vars['tripleo_nova_compute_config_overrides'] = nova_config
    file_cache = {}
    format_dict = dict(role=args.role)

    for t_var, t_dict in TEMPLATE_VARS.items():
        # Load the file if not already in file_cache
        log.info('Looking up {}'.format(t_var))
        file_path = os.path.join(
            args.config_download_dir,
            t_dict['file']).format(**format_dict)
        if file_path not in file_cache:
            with open(file_path) as f:
                log.info('Caching {}'.format(file_path))
                file_cache[file_path] = yaml.safe_load(f.read())

        keys = t_dict['key'].split('.')
        val = file_cache[file_path][keys[0]]
        for k in keys[1:]:
            val = val[k]
        tmpl_vars[t_var] = val

    for s_var, s_tmpl in STANDALONE_VARS.items():
        log.info("Loading template for {}".format(s_var))
        jinja_tmpl = jinja2.Environment().from_string(s_tmpl['template'])
        log.info("Rendering {}".format(s_var))
        s_val = jinja_tmpl.render(**tmpl_vars)
        try:
            s_val_yaml = yaml.safe_load(s_val)
        except yaml.YAMLError as ye:
            s_val_yaml = s_val
        standalone_vars[s_var] = s_val_yaml
        if 'section' in s_tmpl:
            section_config = nova_config.setdefault(s_tmpl['section'], {})
            section_config[s_tmpl['option']] = s_val

    config_dict = {'Compute': {'vars': standalone_vars}}
    with open(args.output_file, 'w') as f:
        f.write(yaml.safe_dump(config_dict, default_flow_style=False, width=10000))


if __name__ == '__main__':
    main()
