#!/usr/bin/env python3

# ------------------------------------------------------------------------
# Copyright 2020, 2021 IBM Corp. 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.
# ------------------------------------------------------------------------

""" Add OpenShift cluster domain entries to '/etc/hosts' """


# Global modules

import shutil
import sys


# Local modules

from modules.args    import getCommonArgs
from modules.context import getContext


# Functions


# ----------------------------------------------------------------------

def _main():

    ctx = getContext(getCommonArgs(
        "Add OpenShift cluster domain entries to '/etc/hosts'"
    ))

    ocpDomain = ctx.cf.ocp.domain
    helperIp  = ctx.cf.ocp.helper.host.ip

    fqdns = (
        f'api.{ocpDomain}',
        f'oauth-openshift.apps.{ocpDomain}',
        f'default-route-openshift-image-registry.apps.{ocpDomain}',
        f'console-openshift-console.apps.{ocpDomain}',
        f'downloads-openshift-console.apps.{ocpDomain}'
    )

    etcHosts = '/etc/hosts'
    hostEntry = f'{helperIp}\t'+' '.join(fqdns)

    found = []
    with(open(etcHosts, 'r')) as fh:  # pylint: disable=invalid-name
        for line in fh.readlines():
            if line.strip().startswith('#'):
                continue
            if ocpDomain in line:
                found.append(line)

    if found:
        print(f"\nThe following line(s) in '{etcHosts}' already"
              f" contain(s) entries for domain '{ocpDomain}':\n")
        for line in found:
            print(f"{line}")
        print("Please check whether the following FQDN's are active and add all missing FQDN's:\n")
        for fqdn in fqdns:
            print(f"   '{fqdn}'")
        print()
    else:
        backup = f'{etcHosts}.orig'
        shutil.copy2(etcHosts, backup)
        with open(etcHosts, 'a') as fh:  # pylint: disable=invalid-name
            print(f'\n# Added by ocp-etc-hosts:\n{hostEntry}', file=fh)
        print(f"\nAdded the following line to file '{etcHosts}'\n\n{hostEntry}\n")
        print(f"Stored backup of original '{etcHosts}' in '{backup}'\n")


# ----------------------------------------------------------------------

if __name__ == '__main__':
    try:
        _main()
    except KeyboardInterrupt:
        print()
    sys.exit(0)
