#!/bin/bash
echo "$1"
echo "$2"
database_user="root"
database_password="root"
# 1.创建 /share/data/docker/bmcinit 目录
echo "mkdir -p /share/data/docker/bmcinit"
rm -rf /share/data/docker/bmc/uploadPath/hardBrush/*
rm -rf /share/data/docker/redis/redis_data/*
mkdir -p /share/data/docker/bmcinit
mkdir -p /share/data/docker/bmc/uploadPath
mkdir -p /share/data/docker/redis/redis_conf
mkdir -p /share/data/docker/mariadb/mysql_conf
mkdir -p /share/data/docker/nginx/nginx_conf
#2. 上传 bmc.yml,bmc_html_2.4.tar,bmc_java_3.3.tar,mariadb.tar,redis.tar 到 /share/data/docker/bmcinit 目录下
#update.sh 文件需要存放到/share/data/docker/bmc/uploadPath 目录下
cp update.sh /share/data/docker/bmc/uploadPath
echo "bmc.yml /share/data/docker/bmcinit"
cp bmc.yml /share/data/docker/bmcinit
echo "bmc_htmltar /share/data/docker/bmcinit"
cp bmc_html_$1.tar /share/data/docker/bmcinit
echo "bmc_java_tar /share/data/docker/bmcinit"
cp bmc_java_$2.tar /share/data/docker/bmcinit
echo "mariadb.tar /share/data/docker/bmcinit"
cp mariadb.tar /share/data/docker/bmcinit
echo "redis.tar /share/data/docker/bmcinit"
cp redis.tar /share/data/docker/bmcinit
cp my.cnf /share/data/docker/mariadb/mysql_conf
rm -rf /share/data/docker/nginx/nginx_conf/*
cp nginx/* /share/data/docker/nginx/nginx_conf

#5. 更新arm_fix_tool 脚本
echo "cp arm_fix_tool /usr/local/bin"
cp arm_fix_tool /usr/local/bin
chmod 755 /usr/local/bin/arm_fix_tool

#4. 上传redis.conf 到 /share/data/docker/redis/redis_conf 目录下
echo "redis.conf /share/data/docker/redis/redis_conf"
cp redis.conf /share/data/docker/redis/redis_conf
cp bmc_iaas.sql /share/data/docker/bmcinit
cp bmc_iaas_v2.0.sql /share/data/docker/bmcinit
cp bmc_iaas_v2.1.sql /share/data/docker/bmcinit
cp bmc_iaas_v2.2.sql /share/data/docker/bmcinit
cp bmc_iaas_v2.3.sql /share/data/docker/bmcinit
cd /share/data/docker/bmc/uploadPath
chmod 755 update.sh

#3. 停止docker 镜像  docker-compose -f /share/data/docker/bmcinit/bmc.yml down (不存在可忽略)
echo "/share/data/docker/bmcinit"
cd /share/data/docker/bmcinit
echo "docker-compose -f /share/data/docker/bmcinit/bmc.yml down"
docker-compose -f /share/data/docker/bmcinit/bmc.yml down
#5. 加载mariadb容器 docker load -i mariadb.tar
echo "docker load -i mariadb.tar"
docker load -i mariadb.tar
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "加载mariadb容器失败"
  exit
fi

#6. 启动mariadb容器 docker-compose -f /share/data/docker/bmcinit/bmc.yml up -d mariadb
echo "docker-compose -f /share/data/docker/bmcinit/bmc.yml up -d mariadb"
docker-compose -f /share/data/docker/bmcinit/bmc.yml up -d mariadb
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "启动mariadb容器失败"
  exit
fi

sleep 10
#8. 导入bmc_iaas.sql 到数据库
#echo "docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas.sql"
#docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas.sql
#9. 加载redis容器 docker load -i redis.tar
echo "load -i redis.tar"
docker load -i redis.tar
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "加载redis容器失败"
  exit
fi
#10. 加载html容器 docker load -i bmc_html_2.4.tar
echo "load -i bmc_html_tar"
docker load -i bmc_html_$1.tar
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "加载html容器失败"
  exit
fi
#11. 加载java容器 docker load -i bmc_java_3.3.tar
echo "load -i bmc_java_tar"
docker load -i bmc_java_$2.tar
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "加载java容器失败"
  exit
fi

if [ ! -d "/tmp/" ];then
    mkdir /tmp
    echo "create /tmp ..."
fi


echo "docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas.sql"
docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas.sql
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "bmc_iaas.sql注入失败"
  exit
fi



sleep 5
echo "docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.0.sql"
docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.0.sql
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "bmc_iaas_v2.0.sql注入失败"
  exit
fi
sleep 5
echo "docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.1.sql"
docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.1.sql
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "bmc_iaas_v2.1.sql注入失败"
  exit
fi

sleep 5
echo "docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.2.sql"
docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.2.sql
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "bmc_iaas_v2.2.sql注入失败"
  exit
fi

sleep 5
echo "docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.3.sql"
docker exec -i mariadb sh -c 'mysql -uroot -proot' < bmc_iaas_v2.3.sql
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "bmc_iaas_v2.3.sql注入失败"
  exit
fi


#12. 启动docker 镜像  docker-compose -f /share/data/docker/bmcinit/bmc.yml up -d
echo "-f /share/data/docker/bmcinit/bmc.yml up -d"
docker-compose -f /share/data/docker/bmcinit/bmc.yml up -d
if  [[ $? -eq 0 ]]
then
  echo "success"
else
  echo "启动docker 镜像失败"
  exit
fi

