CCCPaste

ZorinOS OCS Inventory

#!/usr/bin/env bash
set -euo pipefail

# ------------------------------------------------------------
# OCS Inventory "quick test" on ZorinOS (Ubuntu-like)
# - Server via Docker Compose (OCS + MariaDB)
# - Agent installed only on this workstation, pointing to localhost
# ------------------------------------------------------------

if [[ $EUID -ne 0 ]]; then
echo "❌ Lance ce script en root (ex: sudo bash $0)"
exit 1
fi

OCS_DIR="/opt/ocs-test"
HTTP_PORT="8080"
DB_NAME="ocsweb"
DB_USER="ocs"
DB_ROOT_PASS="$(openssl rand -hex 16)"
DB_PASS="$(openssl rand -hex 16)"
OCS_IMAGE_TAG="latest" # stable "latest" according to OCS docs :contentReference[oaicite:1]{index=1}

echo "==> 1) Dépendances (Docker, compose, outils)..."
apt-get update -y
apt-get install -y \
ca-certificates curl gnupg lsb-release \
docker.io docker-compose-plugin \
openssl sed grep coreutils \
ocsinventory-agent dmidecode

echo "==> 2) Activation Docker..."
systemctl enable --now docker

echo "==> 3) Création du dossier: $OCS_DIR"
mkdir -p "$OCS_DIR"
cd "$OCS_DIR"

echo "==> 4) Génération docker-compose.yml..."
cat > docker-compose.yml <<EOF
services:
db:
image: mariadb:10.11
container_name: ocs_db
environment:
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASS}"
MYSQL_DATABASE: "${DB_NAME}"
MYSQL_USER: "${DB_USER}"
MYSQL_PASSWORD: "${DB_PASS}"
volumes:
- ocs_db_data:/var/lib/mysql
restart: unless-stopped

ocs:
image: ocsinventory/ocsinventory-docker-image:${OCS_IMAGE_TAG}
container_name: ocs_server
depends_on:
- db
environment:
OCS_DB_SERVER: "db"
OCS_DB_PORT: "3306"
OCS_DB_USER: "${DB_USER}"
OCS_DB_PASS: "${DB_PASS}"
OCS_DB_NAME: "${DB_NAME}"
ports:
- "${HTTP_PORT}:80"
restart: unless-stopped

volumes:
ocs_db_data:
EOF

echo "==> 5) Démarrage des conteneurs OCS..."
docker compose up -d

echo "==> 6) Attente (court) du service HTTP..."
for i in {1..30}; do
if curl -fsS "http://127.0.0.1:${HTTP_PORT}/" >/dev/null 2>&1; then
echo "✅ OCS répond sur http://127.0.0.1:${HTTP_PORT}/"
break
fi
sleep 2
done

echo "==> 7) Configuration de l'agent (local uniquement)..."
CFG="/etc/ocsinventory-agent/ocsinventory-agent.cfg"
mkdir -p "$(dirname "$CFG")"
touch "$CFG"

# Commente d'éventuelles lignes existantes
sed -i 's/^[[:space:]]*server[[:space:]]*=/# server =/I' "$CFG" || true
sed -i 's/^[[:space:]]*local[[:space:]]*$/# local/I' "$CFG" || true

# Ajoute notre config à la fin
cat >> "$CFG" <<EOF

# --- Added by install-ocs-zorin.sh ---
server = http://127.0.0.1:${HTTP_PORT}/ocsinventory
# local
EOF

echo "==> 8) Envoi d’un inventaire tout de suite..."
ocsinventory-agent --force --debug || true

cat <<INFO

============================================================
✅ Installation terminée !

OCS Server (web) :
- Console OCS Reports : http://127.0.0.1:${HTTP_PORT}/ocsreports
- Endpoint agent (inventory) : http://127.0.0.1:${HTTP_PORT}/ocsinventory

Base de données (si besoin pendant un wizard/setup) :
- DB host : db (depuis le container) / 127.0.0.1 (depuis l’hôte, via docker network ce n'est pas direct)
- DB name : ${DB_NAME}
- DB user : ${DB_USER}
- DB pass : ${DB_PASS}
- DB root pass : ${DB_ROOT_PASS}

Fichiers :
- Compose : ${OCS_DIR}/docker-compose.yml
- Agent cfg : ${CFG}

Commandes utiles :
- Voir les logs : cd ${OCS_DIR} && docker compose logs -f
- Stopper : cd ${OCS_DIR} && docker compose down
- Stopper + supprimer données DB : cd ${OCS_DIR} && docker compose down -v

Astuce :
- Si le navigateur affiche une page d'installation/config, suis le wizard.
- Ensuite, côté OCS Reports, tu devrais voir ton poste apparaître après l'inventaire.

============================================================
INFO