containerAdmin/run_admin_container.sh
2025-05-20 08:03:03 +00:00

79 lines
3.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
set -o pipefail # Causes a pipeline to return the exit status of the last command in the pipe that failed.
# set -x # Uncomment for verbose debugging
# vars via Semaphore UI
MAIN_BR="${MAIN_BR:-main}"
ENV="${ENV:-staging}"
ENV_BR="${ENV_BR:-test_feature}"
PLAYBOOK_FILE="${PLAYBOOK_FILE:-base.yaml}"
REPO_CORE="https://git.felcloud.io/felcloud/ansible_core_init_ansible.git"
REPO_ENV="https://git.felcloud.io/felcloud/ansible_env_staging.git"
echo "[INFO] Setting up SSH environment..."
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Add git.felcloud.io to known_hosts (good practice, but we'll override for clone if needed)
echo "[INFO] Adding git.felcloud.io to known_hosts..."
ssh-keyscan git.felcloud.io >> ~/.ssh/known_hosts || echo "[WARN] ssh-keyscan failed but continuing."
chmod 644 ~/.ssh/known_hosts
echo "[DEBUG] Content of ~/.ssh/known_hosts after keyscan:"
cat ~/.ssh/known_hosts || echo "[WARN] Could not cat known_hosts"
# Debug SSH keys available from Semaphore Secrets
echo "[DEBUG] Available SSH keys in ~/.ssh:"
ls -la ~/.ssh
# Define the SSH command for Git to use
# This will bypass strict host key checking and not use the system known_hosts file for these specific git commands.
# It will still use any identity files (private keys) found in ~/.ssh/
export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
# 1/Cloner les dépôts nécessaires:
echo "[INFO] Cloning repositories using GIT_SSH_COMMAND..."
echo "[INFO] Cloning $REPO_CORE on branch $ENV_BR..."
git clone --branch "$ENV_BR" "$REPO_CORE" ansible_core_init_ansible
echo "[INFO] Cloning $REPO_ENV..."
# If ansible_env_staging also needs a specific branch:
# git clone --branch "<some_branch_for_env>" "$REPO_ENV" ansible_env_staging
git clone "$REPO_ENV" ansible_env_staging
# Unset GIT_SSH_COMMAND if you want subsequent ssh operations to use default behavior
# unset GIT_SSH_COMMAND
cd ansible_core_init_ansible
# git checkout "$ENV_BR" # Already done by --branch in clone
# 2/Build local de limage Docker admin:
echo "[INFO] Building Docker image locally..."
# Build context is the current directory: ./ansible_core_init_ansible
docker build --no-cache --build-arg branch=fix_packages_dependencies -t ansible-pulumi:local .
cd .. # Go back to the parent directory (where ansible_env_staging also is)
# 3/Lancer le conteneur admin avec le playbook choisi:
echo "[INFO] Running the admin container with playbook: $PLAYBOOK_FILE"
docker run --rm -it \
-v "$(pwd)/ansible_env_staging:/home/cloud/ansible_env_staging" \
-e MAIN_BR="$MAIN_BR" \
-e ENV="$ENV" \
-e ENV_BR="$ENV_BR" \
ansible-pulumi:local bash -c "
set -e; \
echo '[CONTAINER] Current directory: \$(pwd)'; \
echo '[CONTAINER] Listing /home/cloud:'; ls -la /home/cloud; \
echo '[CONTAINER] Listing /home/cloud/ansible_env_staging:'; ls -la /home/cloud/ansible_env_staging; \
echo '[CONTAINER] Activating venv...'; \
source /home/cloud/venv/bin/activate; \
echo '[CONTAINER] Changing to playbook directory /home/cloud/ansible-pulumi...'; \
cd /home/cloud/ansible-pulumi; \
echo '[CONTAINER] Running ansible-playbook...'; \
ansible-playbook playbooks/${PLAYBOOK_FILE} -i /home/cloud/ansible_env_staging/hosts"
echo "[INFO] Task completed."