405 lines
12 KiB
Bash
Executable file
405 lines
12 KiB
Bash
Executable file
#!/bin/bash
|
|
# Matrix Ansible Controller - Migration Script
|
|
# Target: replicant (.80) - change if desired
|
|
# Run this on the control server (CT 127)
|
|
|
|
set -e
|
|
|
|
SERVICE_NAME="matrix-ansible-controller"
|
|
TARGET_HOST="replicant"
|
|
COMPOSE_DIR=~/clustered-fucks/compose-files/${TARGET_HOST}/${SERVICE_NAME}
|
|
PLAYBOOK_DIR=~/clustered-fucks/playbooks
|
|
|
|
echo "=== Matrix Ansible Controller Setup Script ==="
|
|
echo "Service: ${SERVICE_NAME}"
|
|
echo "Target: ${TARGET_HOST}"
|
|
echo ""
|
|
|
|
# Create directories
|
|
mkdir -p "$COMPOSE_DIR"
|
|
mkdir -p "$PLAYBOOK_DIR"
|
|
|
|
# =============================================================================
|
|
# Dockerfile
|
|
# =============================================================================
|
|
cat > "$COMPOSE_DIR/Dockerfile" << 'EOF'
|
|
# Matrix Ansible Controller
|
|
# Portable container for managing matrix-docker-ansible-deploy playbook
|
|
|
|
FROM python:3.12-alpine
|
|
|
|
LABEL maintainer="maddox"
|
|
LABEL description="Portable Ansible controller for matrix-docker-ansible-deploy"
|
|
|
|
# Install system dependencies
|
|
RUN apk add --no-cache \
|
|
# Core tools
|
|
git \
|
|
openssh-client \
|
|
bash \
|
|
curl \
|
|
rsync \
|
|
# Build dependencies for Python packages
|
|
gcc \
|
|
musl-dev \
|
|
libffi-dev \
|
|
openssl-dev \
|
|
python3-dev \
|
|
# For just command runner
|
|
just \
|
|
# Useful utilities
|
|
nano \
|
|
vim \
|
|
tmux \
|
|
jq
|
|
|
|
# Install Ansible and required Python packages
|
|
RUN pip install --no-cache-dir \
|
|
ansible>=2.17.0 \
|
|
passlib \
|
|
dnspython \
|
|
netaddr \
|
|
jmespath \
|
|
docker \
|
|
requests
|
|
|
|
# Install agru (faster ansible-galaxy alternative used by the playbook)
|
|
RUN pip install --no-cache-dir agru
|
|
|
|
# Create working directories
|
|
RUN mkdir -p /playbook /inventory /ssh
|
|
|
|
# Set up SSH directory with proper permissions
|
|
RUN mkdir -p /root/.ssh && chmod 700 /root/.ssh
|
|
|
|
# Copy entrypoint script
|
|
COPY entrypoint.sh /entrypoint.sh
|
|
RUN chmod +x /entrypoint.sh
|
|
|
|
# Set working directory to playbook
|
|
WORKDIR /playbook
|
|
|
|
# Default environment
|
|
ENV ANSIBLE_HOST_KEY_CHECKING=False
|
|
ENV ANSIBLE_FORCE_COLOR=True
|
|
ENV TERM=xterm-256color
|
|
|
|
ENTRYPOINT ["/entrypoint.sh"]
|
|
CMD ["/bin/bash"]
|
|
EOF
|
|
|
|
echo "✅ Created $COMPOSE_DIR/Dockerfile"
|
|
|
|
# =============================================================================
|
|
# Entrypoint Script
|
|
# =============================================================================
|
|
cat > "$COMPOSE_DIR/entrypoint.sh" << 'EOF'
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE} Matrix Ansible Controller${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
|
|
# --- SSH Key Setup ---
|
|
if [ -d "/ssh" ] && [ "$(ls -A /ssh 2>/dev/null)" ]; then
|
|
echo -e "${GREEN}[SSH]${NC} Setting up SSH keys from /ssh mount..."
|
|
cp -r /ssh/* /root/.ssh/ 2>/dev/null || true
|
|
chmod 700 /root/.ssh
|
|
chmod 600 /root/.ssh/* 2>/dev/null || true
|
|
chmod 644 /root/.ssh/*.pub 2>/dev/null || true
|
|
chmod 644 /root/.ssh/known_hosts 2>/dev/null || true
|
|
chmod 644 /root/.ssh/config 2>/dev/null || true
|
|
echo -e "${GREEN}[SSH]${NC} Keys configured"
|
|
else
|
|
echo -e "${YELLOW}[SSH]${NC} No SSH keys mounted at /ssh"
|
|
echo -e "${YELLOW}[SSH]${NC} Mount with: -v ~/.ssh:/ssh:ro"
|
|
fi
|
|
|
|
# --- Playbook Setup ---
|
|
if [ ! -f "/playbook/setup.yml" ]; then
|
|
echo -e "${GREEN}[PLAYBOOK]${NC} Cloning matrix-docker-ansible-deploy..."
|
|
git clone https://github.com/spantaleev/matrix-docker-ansible-deploy.git /tmp/playbook
|
|
mv /tmp/playbook/* /playbook/
|
|
mv /tmp/playbook/.* /playbook/ 2>/dev/null || true
|
|
rm -rf /tmp/playbook
|
|
echo -e "${GREEN}[PLAYBOOK]${NC} Playbook cloned successfully"
|
|
else
|
|
echo -e "${GREEN}[PLAYBOOK]${NC} Playbook already present"
|
|
fi
|
|
|
|
# --- Inventory Setup ---
|
|
if [ -d "/inventory" ] && [ "$(ls -A /inventory 2>/dev/null)" ]; then
|
|
echo -e "${GREEN}[INVENTORY]${NC} Linking inventory from /inventory mount..."
|
|
# Remove default inventory if it exists
|
|
rm -rf /playbook/inventory 2>/dev/null || true
|
|
# Create symlink to mounted inventory
|
|
ln -sf /inventory /playbook/inventory
|
|
echo -e "${GREEN}[INVENTORY]${NC} Inventory linked: /playbook/inventory -> /inventory"
|
|
else
|
|
echo -e "${YELLOW}[INVENTORY]${NC} No inventory mounted at /inventory"
|
|
echo -e "${YELLOW}[INVENTORY]${NC} Mount with: -v /path/to/inventory:/inventory"
|
|
# Ensure default inventory directory exists
|
|
mkdir -p /playbook/inventory
|
|
fi
|
|
|
|
# --- Install/Update Ansible Roles ---
|
|
if [ -f "/playbook/requirements.yml" ]; then
|
|
if [ ! -d "/playbook/roles/galaxy" ] || [ "${UPDATE_ROLES:-false}" = "true" ]; then
|
|
echo -e "${GREEN}[ROLES]${NC} Installing Ansible Galaxy roles..."
|
|
cd /playbook
|
|
if command -v agru &> /dev/null; then
|
|
# Use agru if available (faster)
|
|
agru
|
|
else
|
|
rm -rf roles/galaxy
|
|
ansible-galaxy install -r requirements.yml -p roles/galaxy/ --force
|
|
fi
|
|
echo -e "${GREEN}[ROLES]${NC} Roles installed successfully"
|
|
else
|
|
echo -e "${GREEN}[ROLES]${NC} Roles already installed (set UPDATE_ROLES=true to refresh)"
|
|
fi
|
|
fi
|
|
|
|
# --- Display Status ---
|
|
echo ""
|
|
echo -e "${BLUE}----------------------------------------${NC}"
|
|
echo -e "${GREEN}Status:${NC}"
|
|
echo -e " Ansible: $(ansible --version | head -1)"
|
|
echo -e " Playbook: /playbook"
|
|
echo -e " Inventory: /playbook/inventory"
|
|
echo ""
|
|
echo -e "${BLUE}Quick Commands:${NC}"
|
|
echo -e " just install-all # Full installation"
|
|
echo -e " just setup-all # Setup all components"
|
|
echo -e " just roles # Update roles"
|
|
echo -e " just update # git pull + update roles"
|
|
echo ""
|
|
echo -e " ansible-playbook -i inventory/hosts setup.yml --tags=install-all,start"
|
|
echo ""
|
|
echo -e "${BLUE}----------------------------------------${NC}"
|
|
echo ""
|
|
|
|
# Execute command or start interactive shell
|
|
exec "$@"
|
|
EOF
|
|
|
|
echo "✅ Created $COMPOSE_DIR/entrypoint.sh"
|
|
|
|
# =============================================================================
|
|
# Docker Compose
|
|
# =============================================================================
|
|
cat > "$COMPOSE_DIR/docker-compose.yml" << 'EOF'
|
|
# Matrix Ansible Controller
|
|
# Portable container for managing matrix-docker-ansible-deploy playbook
|
|
#
|
|
# Usage:
|
|
# docker compose up -d
|
|
# docker exec -it matrix-ansible-controller bash
|
|
# just install-all
|
|
|
|
services:
|
|
controller:
|
|
build: .
|
|
image: matrix-ansible-controller:latest
|
|
container_name: matrix-ansible-controller
|
|
hostname: matrix-controller
|
|
|
|
# Keep container running for interactive use
|
|
stdin_open: true
|
|
tty: true
|
|
|
|
volumes:
|
|
# SSH keys (read-only) - for connecting to matrix server
|
|
- /root/.ssh:/ssh:ro
|
|
|
|
# Persistent playbook directory (survives container rebuilds)
|
|
- ./data/playbook:/playbook
|
|
|
|
# Your inventory configuration (vars.yml, hosts, etc.)
|
|
- ./data/inventory:/inventory
|
|
|
|
# Persist ansible cache/facts
|
|
- ./data/ansible-cache:/root/.ansible
|
|
|
|
environment:
|
|
- ANSIBLE_HOST_KEY_CHECKING=False
|
|
- ANSIBLE_FORCE_COLOR=True
|
|
- UPDATE_ROLES=false
|
|
|
|
network_mode: bridge
|
|
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
memory: 1G
|
|
cpus: '2.0'
|
|
|
|
labels:
|
|
- "com.centurylinklabs.watchtower.enable=false"
|
|
|
|
restart: unless-stopped
|
|
EOF
|
|
|
|
echo "✅ Created $COMPOSE_DIR/docker-compose.yml"
|
|
|
|
# =============================================================================
|
|
# Ansible Deployment Playbook
|
|
# =============================================================================
|
|
cat > "$PLAYBOOK_DIR/deploy-matrix-ansible-controller.yml" << 'EOF'
|
|
---
|
|
# Deploy Matrix Ansible Controller
|
|
#
|
|
# Usage:
|
|
# ansible-playbook playbooks/deploy-matrix-ansible-controller.yml
|
|
#
|
|
# After deployment:
|
|
# ssh replicant
|
|
# docker exec -it matrix-ansible-controller bash
|
|
# # Copy your vars.yml to data/inventory/host_vars/matrix.yourdomain.com/
|
|
|
|
- name: Deploy Matrix Ansible Controller
|
|
hosts: replicant
|
|
vars:
|
|
service_name: matrix-ansible-controller
|
|
service_dir: /home/maddox/docker/appdata/{{ service_name }}
|
|
compose_src: "{{ playbook_dir }}/../compose-files/replicant/{{ service_name }}"
|
|
|
|
tasks:
|
|
- name: Create service directory structure
|
|
ansible.builtin.file:
|
|
path: "{{ item }}"
|
|
state: directory
|
|
mode: '0755'
|
|
loop:
|
|
- "{{ service_dir }}"
|
|
- "{{ service_dir }}/data"
|
|
- "{{ service_dir }}/data/playbook"
|
|
- "{{ service_dir }}/data/inventory"
|
|
- "{{ service_dir }}/data/inventory/host_vars"
|
|
- "{{ service_dir }}/data/ansible-cache"
|
|
|
|
- name: Copy Dockerfile
|
|
ansible.builtin.copy:
|
|
src: "{{ compose_src }}/Dockerfile"
|
|
dest: "{{ service_dir }}/Dockerfile"
|
|
mode: '0644'
|
|
|
|
- name: Copy entrypoint script
|
|
ansible.builtin.copy:
|
|
src: "{{ compose_src }}/entrypoint.sh"
|
|
dest: "{{ service_dir }}/entrypoint.sh"
|
|
mode: '0755'
|
|
|
|
- name: Copy docker-compose.yml
|
|
ansible.builtin.copy:
|
|
src: "{{ compose_src }}/docker-compose.yml"
|
|
dest: "{{ service_dir }}/docker-compose.yml"
|
|
mode: '0644'
|
|
|
|
- name: Build Docker image
|
|
community.docker.docker_image:
|
|
name: matrix-ansible-controller
|
|
tag: latest
|
|
source: build
|
|
build:
|
|
path: "{{ service_dir }}"
|
|
pull: yes
|
|
state: present
|
|
force_source: yes
|
|
|
|
- name: Deploy container
|
|
community.docker.docker_compose_v2:
|
|
project_src: "{{ service_dir }}"
|
|
state: present
|
|
pull: always
|
|
|
|
- name: Display next steps
|
|
ansible.builtin.debug:
|
|
msg: |
|
|
|
|
✅ Matrix Ansible Controller deployed!
|
|
|
|
=== NEXT STEPS ===
|
|
|
|
1. Copy your Matrix inventory to the container:
|
|
|
|
ssh replicant
|
|
cd /home/maddox/docker/appdata/matrix-ansible-controller/data/inventory
|
|
|
|
# Create the structure:
|
|
mkdir -p host_vars/matrix.yourdomain.com
|
|
|
|
# Copy your vars.yml (from wherever it currently lives):
|
|
# Option A: From another machine via scp
|
|
# Option B: Create/paste manually
|
|
|
|
# Also create/copy the hosts file:
|
|
cat > hosts << 'HOSTS'
|
|
[matrix_servers]
|
|
matrix.yourdomain.com ansible_host=YOUR_MATRIX_IP ansible_ssh_user=root
|
|
HOSTS
|
|
|
|
2. Enter the container and test:
|
|
|
|
docker exec -it matrix-ansible-controller bash
|
|
ansible -i inventory/hosts all -m ping
|
|
|
|
3. Run Matrix updates:
|
|
|
|
just update # Update playbook + roles
|
|
just install-all # Deploy changes
|
|
|
|
===================================
|
|
EOF
|
|
|
|
echo "✅ Created $PLAYBOOK_DIR/deploy-matrix-ansible-controller.yml"
|
|
|
|
# =============================================================================
|
|
# Summary
|
|
# =============================================================================
|
|
echo ""
|
|
echo "==========================================="
|
|
echo " FILES CREATED"
|
|
echo "==========================================="
|
|
echo ""
|
|
echo "Compose files:"
|
|
echo " $COMPOSE_DIR/Dockerfile"
|
|
echo " $COMPOSE_DIR/entrypoint.sh"
|
|
echo " $COMPOSE_DIR/docker-compose.yml"
|
|
echo ""
|
|
echo "Playbook:"
|
|
echo " $PLAYBOOK_DIR/deploy-matrix-ansible-controller.yml"
|
|
echo ""
|
|
echo "==========================================="
|
|
echo " NEXT STEPS"
|
|
echo "==========================================="
|
|
echo ""
|
|
echo "1. DEPLOY:"
|
|
echo " ansible-playbook playbooks/deploy-matrix-ansible-controller.yml"
|
|
echo ""
|
|
echo "2. VERIFY:"
|
|
echo " ssh replicant 'docker ps | grep matrix-ansible'"
|
|
echo ""
|
|
echo "3. COPY YOUR MATRIX INVENTORY:"
|
|
echo " ssh replicant"
|
|
echo " cd /home/maddox/docker/appdata/matrix-ansible-controller/data/inventory"
|
|
echo " mkdir -p host_vars/matrix.yourdomain.com"
|
|
echo " # Then copy/create your vars.yml and hosts file"
|
|
echo ""
|
|
echo "4. TEST:"
|
|
echo " docker exec -it matrix-ansible-controller bash"
|
|
echo " ansible -i inventory/hosts all -m ping"
|
|
echo " just install-all"
|
|
echo ""
|
|
echo "5. COMMIT TO GIT:"
|
|
echo " git add -A && git commit -m 'Add matrix-ansible-controller' && git push"
|
|
echo ""
|
|
echo "==========================================="
|