|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2025 Cloudera, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# |
| 18 | +# Sets up working Ansible controller on a RHEL9 system and readies an Ansible project |
| 19 | +# |
| 20 | +# Run via the following command: |
| 21 | +# ./rhel9-init.sh |
| 22 | +# |
| 23 | +# Or supply a Github project URl to download and use for the requirements.yml |
| 24 | +# ./rhel9-init.sh https://github.com/some-repo/some-project.git [<some/branch>] |
| 25 | +# |
| 26 | + |
| 27 | +# Check for execution mode (source only) |
| 28 | +# [[ "${BASH_SOURCE[0]}" == "${0}" ]] && echo "Please source '$(basename -- ${0})'. Do not execute directly." && exit 1 |
| 29 | + |
| 30 | +# Exit on errors |
| 31 | +set -e |
| 32 | + |
| 33 | +# Set the destination directory, using RUNNER_PROJECT environment variable if it exists. |
| 34 | +# Otherwise, default to /opt/cldr-runner. |
| 35 | +DEST_DIR="${RUNNER_PROJECT:-/opt/cldr-runner}" |
| 36 | + |
| 37 | +# Define the workspace group |
| 38 | +WORKSPACE_GROUP="${RUNNER_GROUP:-cdp-navigator}" |
| 39 | + |
| 40 | +clone_repo() { |
| 41 | + local repo_url="$1" |
| 42 | + local branch_name="$2" |
| 43 | + local dest_dir="$3" |
| 44 | + local group="$4" |
| 45 | + |
| 46 | + # Remove destination directory if it exists |
| 47 | + if [ -d "$dest_dir" ]; then |
| 48 | + echo "Removing existing directory: $dest_dir" |
| 49 | + rm -rf "$dest_dir" |
| 50 | + fi |
| 51 | + |
| 52 | + echo "Cloning repository: $repo_url" |
| 53 | + if [ -n "$branch_name" ]; then |
| 54 | + git clone --depth 1 --branch "$branch_name" "$repo_url" "$dest_dir" |
| 55 | + else |
| 56 | + git clone --depth 1 "$repo_url" "$dest_dir" |
| 57 | + fi |
| 58 | + |
| 59 | + echo "Setting permissions" |
| 60 | + chgrp -R "$group" "$dest_dir" |
| 61 | + chmod 2775 "$dest_dir" |
| 62 | +} |
| 63 | + |
| 64 | +echo -e "===== Prepare base system =====\n" |
| 65 | +yum update -y |
| 66 | +yum install -y yum-utils gcc python3-devel |
| 67 | + |
| 68 | +# Install git |
| 69 | +yum -y install git |
| 70 | + |
| 71 | +echo -e "\n===== Provision Terraform =====\n" |
| 72 | +yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo |
| 73 | +yum -y install terraform |
| 74 | + |
| 75 | +# Use existing Python3.9 and pip |
| 76 | +OS_RELEASE=$(cat /etc/os-release | grep REDHAT_SUPPORT_PRODUCT_VERSION | awk -F= '{ print $2 }') |
| 77 | + |
| 78 | +echo -e "\n===== Provision Python virtual environment =====\n" |
| 79 | +python3 -m venv /opt/cdp-navigator |
| 80 | + |
| 81 | +# Set the permissions on the shared environment |
| 82 | +if getent group "${WORKSPACE_GROUP}" > /dev/null; then |
| 83 | + echo "Group '${WORKSPACE_GROUP}' exists." |
| 84 | +else |
| 85 | + groupadd "${WORKSPACE_GROUP}" |
| 86 | +fi |
| 87 | +chgrp -R "${WORKSPACE_GROUP}" /opt/cdp-navigator |
| 88 | +chmod -R 2774 /opt/cdp-navigator |
| 89 | + |
| 90 | +# Add the calling user to the group if appropriate |
| 91 | +if [[ -n "${SUDO_USER}" ]]; then |
| 92 | + echo -e "\n===== Adding ${SUDO_USER} to ${WORKSPACE_GROUP} group =====\n\n" |
| 93 | + usermod -a -G "${WORKSPACE_GROUP}" "${SUDO_USER}"; |
| 94 | +fi |
| 95 | + |
| 96 | +echo -e "\n===== Provision Ansible =====\n" |
| 97 | +source /opt/cdp-navigator/bin/activate |
| 98 | +pip install --upgrade pip |
| 99 | +pip install wheel |
| 100 | +pip install "ansible-core<2.17" ansible-navigator |
| 101 | + |
| 102 | +echo -e "\n===== Provision the project and its requirements =====\n" |
| 103 | +if [ $# -eq 0 ]; then |
| 104 | + echo "Initializing default cldr-runner/base" |
| 105 | + clone_repo "https://github.com/cloudera-labs/cldr-runner.git" "" "$DEST_DIR" "${WORKSPACE_GROUP}" |
| 106 | + pushd "$DEST_DIR/base" |
| 107 | +else |
| 108 | + REPO_URL="$1" |
| 109 | + BRANCH_NAME="" |
| 110 | + if [ $# -eq 2 ]; then |
| 111 | + BRANCH_NAME="$2" |
| 112 | + fi |
| 113 | + |
| 114 | + MESSAGE="Initializing from custom repository: $REPO_URL" |
| 115 | + if [ -n "$BRANCH_NAME" ]; then |
| 116 | + MESSAGE+=" on branch: $BRANCH_NAME" |
| 117 | + fi |
| 118 | + echo "$MESSAGE" |
| 119 | + |
| 120 | + clone_repo "$REPO_URL" "$BRANCH_NAME" "$DEST_DIR" "${WORKSPACE_GROUP}" |
| 121 | + pushd "$DEST_DIR" |
| 122 | +fi |
| 123 | + |
| 124 | +mkdir -p /usr/share/ansible/collections /usr/share/ansible/roles |
| 125 | +ansible-galaxy collection install -r requirements.yml -p /usr/share/ansible/collections --force |
| 126 | +ansible-galaxy role install -r requirements.yml -p /usr/share/ansible/roles --force |
| 127 | + |
| 128 | +popd > /dev/null |
| 129 | + |
| 130 | +ansible-builder introspect --write-pip final_python.txt --write-bindep final_bindep.txt /usr/share/ansible/collections |
| 131 | +[[ -f final_python.txt ]] && pip install -r final_python.txt || echo "No Python dependencies found." |
| 132 | +[[ -f final_bindep.txt ]] && bindep --file final_bindep.txt || echo "No system dependencies found." |
| 133 | + |
| 134 | +echo -e "\n===== Provision profile instructions and alias =====\n" |
| 135 | +cat <<EOF > /etc/profile.d/cdp-navigator.sh |
| 136 | +export WORKSPACE=${DEST_DIR} |
| 137 | +alias cdp-navigator='source /opt/cdp-navigator/bin/activate && cd "\${WORKSPACE}"' |
| 138 | +
|
| 139 | +cat <<EOM |
| 140 | +======================================================================= |
| 141 | +'ansible-navigator' PLATFORM mode installed as a shared resource. Add |
| 142 | +your user to the '${WORKSPACE_GROUP}' group to enable. For example: |
| 143 | +
|
| 144 | + sudo usermod -aG ${WORKSPACE_GROUP} \$(whoami) |
| 145 | +
|
| 146 | +Run 'cdp-navigator' to enable the environment and switch to the |
| 147 | +installed workspace. |
| 148 | +
|
| 149 | +Your workspace is ${DEST_DIR} |
| 150 | +======================================================================= |
| 151 | +EOM |
| 152 | +EOF |
| 153 | + |
| 154 | +cat /etc/profile.d/cdp-navigator.sh |
| 155 | + |
| 156 | +echo -e "\n===== Setup completed =====" |
0 commit comments