#!/usr/bin/bash
# Copyright (c) 2023 Red Hat, Inc.
#
# 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.

#####################################################################
# Script to validate that Ansible and Ansible Runner are installed.
#
# Usage: check_ansible <PYCMD>
#
# Options:
#     PYCMD - The path to the python executable to use.
#####################################################################

set -x

PYCMD=$1

if [ -z "$PYCMD" ]
then
    echo "Usage: check_ansible <PYCMD>"
    exit 1
fi

if [ ! -x "$PYCMD" ]
then
    echo "$PYCMD is not an executable"
    exit 1
fi

ansible --version

if [ $? -ne 0 ]
then
    cat<<EOF
**********************************************************************
ERROR - Missing Ansible installation

  An Ansible installation cannot be found in the final builder image.

  Ansible must be installed in the final image. If you are using a
  recent enough version of the execution environment file, you may
  use the 'dependencies.ansible_core' configuration option to install
  Ansible for you, or use 'additional_build_steps' to manually do
  this yourself. Alternatively, use a base image with Ansible already
  installed.
**********************************************************************
EOF
    exit 1
fi

ansible-runner --version

if [ $? -ne 0 ]
then
    cat<<EOF
**********************************************************************
ERROR - Missing Ansible Runner installation

  An Ansible Runner installation cannot be found in the final builder
  image.

  Ansible Runner must be installed in the final image. If you are
  using a recent enough version of the execution environment file, you
  may use the 'dependencies.ansible_runner' configuration option to
  install Ansible Runner for you, or use 'additional_build_steps' to
  manually do this yourself. Alternatively, use a base image with
  Ansible Runner already installed.
**********************************************************************
EOF
    exit 1
fi

$PYCMD -c 'import ansible ; import ansible_runner'

if [ $? -ne 0 ]
then
    cat<<EOF
**********************************************************************
ERROR - Missing Ansible or Ansible Runner for selected Python

  An Ansible and/or Ansible Runner installation cannot be found in
  the final builder image using the following Python interpreter:

    $PYCMD

  Ansible and Ansible Runner must be installed in the final image and
  available to the selected Python interpreter. If you are using a
  recent enough version of the execution environment file, you may use
  the 'dependencies.ansible_core' configuration option to install
  Ansible and the 'dependencies.ansible_runner' configuration option
  to install Ansible Runner. You can also use 'additional_build_steps'
  to manually do this yourself. Alternatively, use a base image with
  Ansible and Ansible Runner already installed.
**********************************************************************
EOF
    exit 1
fi
exit 0
