#!/usr/bin/env python3

# ------------------------------------------------------------------------
# Copyright 2020 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.
# ------------------------------------------------------------------------

""" Push a container image of a given flavor to the internal cluster registry """

# Global modules

import sys

# Local modules

from modules.args    import (
    getCommonArgsParser,
    addArgFlavor
)
from modules.command import CmdShell
from modules.config  import getConfig
from modules.logger  import setupLogging
from modules.tools   import (
    ocLogin,
    podmanOcpRegistryLogin
)

# Functions


def _getArgs():
    """ Get command line arguments """
    parser = getCommonArgsParser(
        'Push a container image of a given flavor to the internal cluster registry'
    )

    addArgFlavor(parser)

    return parser.parse_args()


def _tagImage(config, flavor):
    names = getattr(config.flavor, flavor).imageNames
    CmdShell().run(f'podman tag {names.local} {names.ocp}')


def _pushImage(config, flavor):
    names = getattr(config.flavor, flavor).imageNames
    cmd = 'podman push'
    cmd += ' --tls-verify=false'
    cmd += f' {names.ocp}'
    CmdShell().run(cmd)

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


def _main():

    args = _getArgs()

    setupLogging(args, args.flavor)

    config = getConfig(args.config_file, True)

    ocLogin(config)
    podmanOcpRegistryLogin(config)

    _tagImage(config, args.flavor)

    _pushImage(config, args.flavor)

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


if __name__ == "__main__":
    _main()
    sys.exit(0)
