#!/bin/bash OMC_CONTAINER_NAME="omc" MYSQL_CONTAINER_NAME="omc_mysql" REDIS_CONTAINER_NAME="omc_redis" # usage usage() { echo "Usage: bash omc.sh [install|uninstall|restart|start|stop]" exit 1 } # install install(){ echo "====================== Install container omc service =====================" echo "Install mysql and redis service program, default N (Y/N):" read DB_SERVICE DB_SERVICE=${DB_SERVICE:-"N"} echo "Select the image in the tar directory (omc_2.2412.1.tar):" read OMC_FILE OMC_FILE=${OMC_FILE:-"omc_2.2412.1.tar"} echo "Container time zone (Asia/Shanghai):" read OMC_TZ OMC_TZ=${OMC_TZ:-"Asia/Shanghai"} echo "Container service port (80):" read OMC_PORT OMC_PORT=${OMC_PORT:-"80"} echo "===================== Install container omc service =====================" echo "==> Checking Docker version" if sudo docker -v > /dev/null 2>&1; then sudo docker -v else echo "Docker is not available or sudo privileges are not granted." exit 1 fi echo "" echo "==> Created service network" NETWORK="omcnet" if ! docker network ls --filter name=$NETWORK -q | grep -q .; then docker network create $NETWORK echo "Network '$NETWORK' created." else echo "Network '$NETWORK' already exists." fi echo "==> Install service mysql and redis container" MYSQL_CONTAINER_NAME="omc_mysql" REDIS_CONTAINER_NAME="omc_redis" if [[ "$DB_SERVICE" =~ ^[Yy]$ ]]; then # MySQL Config mysql_container=$(docker ps --filter "name=$MYSQL_CONTAINER_NAME" --format "{{.Names}}") if [[ -z "$mysql_container" ]]; then echo "MySQL container is not running. Installing MySQL container..." docker load --input $(pwd)/tar/mysql_8.0.39.tar MYSQL_IMAGE="mysql:8.0.39" MYSQL_ROOT_PASSWORD="1000omc@kp!" SQL_FILE_PATH="$(pwd)/sql/install/omc_db.sql" docker run --privileged=true --restart=always -e TZ="$OMC_TZ" \ -e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD \ -v $SQL_FILE_PATH:/docker-entrypoint-initdb.d/database.sql \ --network $NETWORK \ --name $MYSQL_CONTAINER_NAME \ -d $MYSQL_IMAGE else echo "MySQL container is already running: $mysql_container" fi # Redis Config redis_container=$(docker ps --filter "name=$REDIS_CONTAINER_NAME" --format "{{.Names}}") if [[ -z "$redis_container" ]]; then echo "Redis container is not running. Installing Redis container..." docker load --input $(pwd)/tar/redis_7.2.5.tar REDIS_IMAGE="redis:7.2.5" REDIS_PASSWORD="helloearth" docker run --privileged=true --restart=always -e TZ="$OMC_TZ" \ -e REDIS_PASSWORD=$REDIS_PASSWORD \ --network $NETWORK \ --name $REDIS_CONTAINER_NAME \ -d $REDIS_IMAGE else echo "Redis container is already running: $redis_container" fi else echo "You chose not to install MySQL and Redis containers." fi echo "==> Loading service omc container" docker load --input $(pwd)/tar/$OMC_FILE if [ ! -d /usr/local/etc/omc ]; then mkdir -p /usr/local/etc/omc cp -rf ./omc/* /usr/local/etc/omc fi OMC_IMAGE=$(echo "$OMC_FILE" | sed -e 's/_/:/' -e 's/\.tar$//') docker run --privileged=true --restart=always -m 512M \ -v /usr/local/etc/omc:/usr/local/etc/omc \ -v /usr/local/etc/omc/logs:/var/log \ -v /usr/local/etc/omc/nginx/cert:/etc/nginx/cert \ -v /usr/local/etc/omc/nginx/nginx.conf:/etc/nginx/nginx.conf \ -e TZ=$OMC_TZ \ -p $OMC_PORT:80 \ --network $NETWORK \ --name $OMC_CONTAINER_NAME \ -d $OMC_IMAGE echo "Running service $OMC_CONTAINER_NAME container port $OMC_PORT" } # uninstall uninstall(){ docker stop $OMC_CONTAINER_NAME && docker rm $OMC_CONTAINER_NAME docker stop $REDIS_CONTAINER_NAME && docker rm $REDIS_CONTAINER_NAME docker stop $MYSQL_CONTAINER_NAME && docker rm $MYSQL_CONTAINER_NAME } # According to the input parameters, the corresponding method will be selected for execution, and the instructions will be executed without input. case "$1" in "install") install ;; "uninstall") uninstall ;; "restart") docker restart $OMC_CONTAINER_NAME ;; "start") docker start $OMC_CONTAINER_NAME ;; "stop") docker stop $OMC_CONTAINER_NAME ;; *) usage ;; esac