Shell實現Docker環境準備和應用服務執行
阿新 • • 發佈:2018-11-09
考慮問題
還是根據之前的Shell編寫經驗,本文需要注意幾個問題:
- Docker 的容器IP獲取
- 如何進入MySQL容器執行資料庫指令碼
- Docker應用映象的匯入匯出
- Shell 指令碼sed -i 修改XML引數空格問題導致引數個數解析多於預期
- 如何設計指令碼執行的命令方式入口及幫助文件
示例指令碼
下面是回答上面問題的一個指令碼:install.sh
#! /bin/bash #file:rtvs.sh #company:cvnavi.com #author:Pengjunlin echo "當前執行檔案......$0" ##################################變數定義################################## docker_image_id="f37318a5729b" docker_image_file="rtvsweb-dev.tar" docker_mysql_remote_user_name="rtvsweb" docker_mysql_remote_user_pwd="rtvs2018" docker_mysql_container_name="mysql5.7" docker_mysql_gateway="172.17.0.1" docker_mysql_host="172.17.0.x" install_docker_mysql_db="true" docker_rtvsweb_image_tag_name="rtvsweb:dev" docker_rtvsweb_container_name="rtvsweb-dev" REDIS_CONNECTION_STRING="" MYSQL_CONNECTION_STRING="" VIDEO_CACHE_PATH="" MYSQL_HOST="" CONTAINER_HOST="" ###################################函式定義####################################### function mysql_create_table() { if [[ -f "./mysql_create_table.sh" ]]; then echo "資料庫指令碼賦值許可權......" # 為執行檔案新增許可權 chmod a+x mysql_create_table.sh else echo "缺少./mysql_create_table.sh檔案...已退出安裝!" exit fi # 執行mysql資料庫表建立 ./mysql_create_table.sh if [[ $? -eq 0 ]]; then echo "./mysql_docker_install.sh 執行完成!" else exit fi } function docker_install() { echo "檢查Docker......" docker -v if [ $? -eq 0 ]; then echo "檢查到Docker已安裝!" else echo "安裝docker環境..." curl -sSL https://get.daocloud.io/docker | sh echo "安裝docker環境...安裝完成!" fi # 建立公用網路==bridge模式 #docker network create share_network } function mysql_install(){ install_type=$1 if [[ "$install_type" == "by_local_mysql_installer" ]]; then #statements echo "暫不支援wget本地下載安裝,已退出!" exit 1 fi if [[ "$install_type" == "by_docker_mysql_installer" ]]; then #statements echo "安裝Docker Mysql環境..." if [[ -f "./docker_mysql_install.sh" ]]; then if [[ -f "./docker_mysql_create_table.sh" ]]; then echo "拷貝一份容器執行檔案: ./docker_mysql_create_table.sh /usr/local/docker_mysql_create_table.sh" cp docker_mysql_create_table.sh /usr/local/docker_mysql_create_table.sh else echo "缺少./docker_mysql_create_table.sh檔案...已退出安裝!" exit fi # 為執行檔案新增許可權 chmod a+x docker_mysql_install.sh # Dokcer方式安裝Mysql ./docker_mysql_install.sh if [[ $? -eq 0 ]]; then echo "./docker_mysql_install.sh 執行完成!" # 休10秒鐘 echo "休眠等待10s,等待Docker執行完成......" sleep 10s # 測試列印mysql資訊 print_docker_mysql_info else echo "./docker_mysql_install.sh 執行過程中出現錯誤,已退出安裝!" exit fi else echo "缺少./docker_mysql_install.sh檔案...已退出安裝!" exit fi fi if [[ "$install_type" == "by_smart_installer" ]]; then #statements if [[ `netstat -lanp|grep tcp|grep 3306|wc -l` == 1 ]]; then echo "檢測到mysql已在本機安裝!" # 列印一下mysql版本 echo "檢測到mysql版本:" mysql --version # 執行建立Mysql資料庫指令碼 mysql_create_table # 設定資料庫地址為localhost MYSQL_HOST="localhost" else echo "安裝Docker Mysql環境..." if [[ -f "./docker_mysql_install.sh" ]]; then if [[ -f "./docker_mysql_create_table.sh" ]]; then echo "拷貝一份容器執行檔案: ./docker_mysql_create_table.sh /usr/local/docker_mysql_create_table.sh" cp docker_mysql_create_table.sh /usr/local/docker_mysql_create_table.sh else echo "缺少./docker_mysql_create_table.sh檔案...已退出安裝!" exit fi # 為執行檔案新增許可權 chmod a+x docker_mysql_install.sh # Dokcer方式安裝Mysql ./docker_mysql_install.sh if [[ $? -eq 0 ]]; then echo "./docker_mysql_install.sh 執行完成!" # 休10秒鐘 echo "休眠等待10s,等待Docker執行完成......" sleep 10s # 測試列印mysql資訊 print_docker_mysql_info else echo "./docker_mysql_install.sh 執行過程中出現錯誤,已退出安裝!" exit fi else echo "缺少./docker_mysql_install.sh檔案...已退出安裝!" exit fi fi fi } function docker_container_ip() { CONTAINER_HOST=` docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1` } function print_docker_mysql_info() { echo "指令碼執行Mysql資訊驗證:..." if [[ -f "./docker_mysql_validator.sh" ]]; then echo "拷貝一份容器執行檔案: ./docker_mysql_validator.sh /usr/local/docker_mysql_validator.sh" cp docker_mysql_validator.sh /usr/local/docker_mysql_validator.sh else echo "缺少./docker_mysql_validator.sh檔案...已退出安裝!" exit fi # 對映copy檔案路徑到docker容器 docker cp /usr/local/docker_mysql_validator.sh mysql5.7:/usr/local/docker_mysql_validator.sh docker exec -it mysql5.7 /bin/bash -c "sh /usr/local/docker_mysql_validator.sh" if [[ $? -eq 0 ]]; then echo "./docker_mysql_validator.sh 執行完成!" echo "MySQL容器Host:" docker_container_ip mysql5.7 MYSQL_HOST="mysql5.7" #echo "MySQL容器network 相關資訊:" #docker network inspect share_network echo "MySQL容器link相關資訊:" cat /etc/hosts else echo "./docker_mysql_validator.sh 執行過程中出現錯誤,已退出安裝!" exit fi } function docker_image_need_file_path() { # 建立rtvs目錄 if [[ ! -d "/usr/local/rtvs" ]]; then mkdir /usr/local/rtvs fi # 建立mydata目錄 if [[ ! -d "/usr/local/rtvs/mydata" ]]; then mkdir /usr/local/rtvs/mydata fi # 建立video目錄 if [[ ! -d "/usr/local/rtvs/videocache" ]]; then mkdir /usr/local/rtvs/videocache fi if [[ -f "./SettingConfig.xml" ]]; then echo "拷貝一份XML配置檔案: ./SettingConfig.xml /usr/local/rtvs/SettingConfig.xml" cp SettingConfig.xml /usr/local/rtvs/SettingConfig.xml else echo "缺少./SettingConfig.xml檔案...已退出安裝!" exit fi } function docker_image_load() { #處理映象需要的檔案 docker_image_need_file_path echo "Docker映象載入......" echo "Docker映象匯入......$1 執行指令: sudo docker load < $2 " sudo docker load < $2 echo "匯入映象後的docker images列表:" docker images # 判斷是否有映象,存在時建立相應的容器例項 for i in [ `docker images` ]; do #statements if [[ "$i" == "$1" ]]; then docker tag $1 $3 echo "已經找到匯入的映象!" echo "嘗試停止$4容器..." docker stop $4 echo "嘗試刪除$4容器..." docker rm $4 echo "啟動容器..." # run 的引數設定參考VS專案啟動的設定 # ===bridge 橋接模式 #docker run -it --name=$4 --net="bridge" --dns 8.8.8.8 --network share_network --network-alias $4 -v /usr/local/rtvs/mydata:/MyData -v /usr/local/rtvs/videocache:/MyVideo -v /usr/local/rtvs/SettingConfig.xml:/app/SettingConfig.xml -p 38067:80 -p 44383:443 -p 18000:18000 -p 18002:18002 -p 19700:19700 -p 19702:19702 -p 19703:19703 -d $3 # ===link 連結模式 docker run -it --name $4 --link mysql5.7:mysql5.7 -v /usr/local/rtvs/mydata:/MyData -v /usr/local/rtvs/videocache:/MyVideo -v /usr/local/rtvs/SettingConfig.xml:/app/SettingConfig.xml -p 38067:80 -p 44383:443 -p 18000:18000 -p 18002:18002 -p 19700:19700 -p 19702:19702 -p 19703:19703 -d $3 break fi done echo "當前正在執行的Docker容器例項列表:" docker ps } ####################工具類和流程定製################################ function editXml() { val=`echo ${@:3}` echo "正在修改XML檔案:$1...." echo "正在修改XML檔案:[0]=$1,[1]=$2,[2]=$val" echo "XML檔案$2標籤 value=$val" sed -i "s/<$2>.*<\/$2>/<$2>${val}<\/$2></g" $1 } function editSpecificConfig() { echo "獲取/usr/local/rtvs/SettingConfig.xml資訊..." cat /usr/local/rtvs/SettingConfig.xml echo "" echo "修改Redis和MySQL連線字串..." #設定視訊快取路徑並列印預設資訊 #editXml VideoCachePath /MyVideo /usr/local/rtvs/SettingConfig.xml sed -i "s/<VideoCachePath>.*<\/VideoCachePath>/<VideoCachePath>\/MyVideo<\/VideoCachePath></g" /usr/local/rtvs/SettingConfig.xml VIDEO_CACHE_PATH=`grep -E -o -e '<VideoCachePath>.+</VideoCachePath>' /usr/local/rtvs/SettingConfig.xml | sed 's/<VideoCachePath>//g'|sed 's/<\/VideoCachePath>//g'` echo "預設視訊快取目錄:VideoCachePath=$VIDEO_CACHE_PATH" REDIS_CONNECTON_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "預設Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTON_STRING" MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "預設Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" read -p "是否修改Redis配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Redis配置連線字串:" redis echo "新的Redis連線字串:$redis" sed -i "s/<RedisExchangeHosts>.*<\/RedisExchangeHosts>/<RedisExchangeHosts>$redis<\/RedisExchangeHosts></g" /usr/local/rtvs/SettingConfig.xml REDIS_CONNECTON_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "獲取已修改的Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTON_STRING" fi #Mysql配置連線字串 echo "根據安裝的Mysql配置連線字串...MySQL Host:$MYSQL_HOST" mysql="Database=filecache;Data Source=$MYSQL_HOST;port=3306;User Id=rtvsweb;Password=rtvs2018;charset=utf8;pooling=true" echo "安裝MySQL的連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "根據安裝條件修改Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" read -p "是否修改Mysql配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Mysql配置連線字串:" mysql echo "新的Mysql連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "獲取已修改的Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" fi } function editRedisAndMysqlConfig() { echo "獲取/usr/local/rtvs/SettingConfig.xml資訊..." cat /usr/local/rtvs/SettingConfig.xml echo "" echo "修改Redis和MySQL連線字串..." #設定視訊快取路徑並列印預設資訊 #editXml VideoCachePath /MyVideo /usr/local/rtvs/SettingConfig.xml sed -i "s/<VideoCachePath>.*<\/VideoCachePath>/<VideoCachePath>\/MyVideo<\/VideoCachePath></g" /usr/local/rtvs/SettingConfig.xml VIDEO_CACHE_PATH=`grep -E -o -e '<VideoCachePath>.+</VideoCachePath>' /usr/local/rtvs/SettingConfig.xml | sed 's/<VideoCachePath>//g'|sed 's/<\/VideoCachePath>//g'` echo "預設視訊快取目錄:VideoCachePath=$VIDEO_CACHE_PATH" REDIS_CONNECTON_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "預設Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTON_STRING" MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "預設Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" # 設定Redis連線字串 read -p "是否修改Redis配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Redis連線字串:" redis echo "新的Redis連線字串:$redis" sed -i "s/<RedisExchangeHosts>.*<\/RedisExchangeHosts>/<RedisExchangeHosts>$redis<\/RedisExchangeHosts></g" /usr/local/rtvs/SettingConfig.xml REDIS_CONNECTON_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "獲取已修改的Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTON_STRING" fi read -p "是否修改Mysql配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Mysql連線字串:" mysql echo "新的Mysql連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "獲取已修改的Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" fi } function help() { echo "*******************************************" echo "********* 呼叫方法說明 ************" echo "*******************************************" echo "檢視核心配置檔案:cat ./SettingConfig.xml" echo "幫助方法說明:sh ./install.sh help" echo "修改Redis和MySQL連線字串:sh ./install.sh editXml [XMLFilePath] [tag] ['value'] " echo "********************************************" echo "********* 快速定製安裝 *************" echo "********************************************" echo "1、執行本地Mysql安裝(wget下載檔案安裝...比較費時): sh ./install.sh by_local_mysql_installer" echo "2、執行Docker Mysql安裝 : sh ./install.sh by_docker_mysql_installer" echo "3、智慧檢測決定Mysql安裝方式(如果本機已有MySQL則直接使用): sh ./install.sh by_smart_installer" echo "4、簡化安裝只需要給出Redis和MySQL連線字串即可(極簡方式): sh ./install.sh by_simple_installer" echo "說明:\r 以上4種方式已包含了Docker應用的安裝!" } function completed() { echo "******************安裝結果:*****************" # bridge 橋接模式 #echo "network例項, share_network相關容器資訊:" #docker network inspect share_network echo "正在執行的容器例項:" docker ps echo "容器對應的Ip地址:" docker_container_ip mysql5.7 echo "mysql5.7 ---------------------$CONTAINER_HOST" docker_container_ip rtvsweb-dev echo "rtvsweb-dev ---------------------$CONTAINER_HOST" echo "驗證rtvs訪問:curl http://localhost:38067/" echo "******************安裝完成!*****************" } function by_local_mysql_installer() { # Docker 安裝 docker_install # Mysql 安裝及其指令碼建立 mysql_install "by_local_mysql_installer" # 映象匯入和例項構建 if [[ $? -eq 0 ]]; then docker_image_load $1 $2 $3 $4 else exit 1 fi # 修改映象使用的路徑和資料來源 editSpecificConfig # 完成後輸出 completed } function by_docker_mysql_installer() { # Docker 安裝 docker_install # Mysql 安裝及其指令碼建立 mysql_install "by_docker_mysql_installer" # 映象匯入和例項構建 if [[ $? -eq 0 ]]; then docker_image_load $1 $2 $3 $4 else exit 1 fi # 修改映象使用的路徑和資料來源 editSpecificConfig # 完成後輸出 completed } function by_smart_installer() { # Docker 安裝 docker_install # Mysql 安裝及其指令碼建立 mysql_install "by_smart_installer" # 映象匯入和例項構建 if [[ $? -eq 0 ]]; then docker_image_load $1 $2 $3 $4 else exit 1 fi # 修改映象使用的路徑和資料來源 editSpecificConfig # 完成後輸出 completed } function by_simple_installer() { # Docker 安裝 docker_install # 映象匯入和例項構建 docker_image_load $1 $2 $3 $4 # 修改映象使用的路徑和資料來源 editRedisAndMysqlConfig # 完成後輸出 completed } if [[ $# -gt 0 ]]; then #statements if [[ "$1" == "by_local_mysql_installer" ]]; then #statements by_local_mysql_installer $docker_image_id $docker_image_file $docker_rtvsweb_image_tag_name $docker_rtvsweb_container_name fi if [[ "$1" == "by_docker_mysql_installer" ]]; then #statements by_docker_mysql_installer $docker_image_id $docker_image_file $docker_rtvsweb_image_tag_name $docker_rtvsweb_container_name fi if [[ "$1" == "by_smart_installer" ]]; then #statements by_smart_installer $docker_image_id $docker_image_file $docker_rtvsweb_image_tag_name $docker_rtvsweb_container_name fi if [[ "$1" == "by_simple_installer" ]]; then #statements by_simple_installer $docker_image_id $docker_image_file $docker_rtvsweb_image_tag_name $docker_rtvsweb_container_name fi if [[ "$1" == "editXml" ]]; then if [[ $# -eq 4 ]]; then #statements editXml $2 $3 $4 else echo "editXml引數個數不匹配!" fi fi if [[ "$1" == "help" ]]; then #statements help fi else help fi
示例效果
幫助指令:
執行結果:
關於指令碼和應用服務的說明:
上面缺失的檔案我在這裡就不提供了,主要看流程設計和幫助文件。我上面實現的是ASP.NET core web的
docker應用程式的部署環境,因為涉及到Nugget包,專案釋出的時候採用的部署方式是框架依賴模式,它可以將
需要的*.dll檔案都生成到publish目錄下。其他語言如Java的應該比這個更簡單(如採用微服務框架Spring
Boot 打成jar包即可)。
示例改進
程式碼改進:
#! /bin/bash ##file:rtvs.sh ##company:cvnavi.com ##author:Pengjunlin echo "當前執行檔案......$0" ##################################變數定義################################## DOCKER_IMAGE_ID="f37318a5729b" DOCKER_IMAGE_FILE="rtvsweb-publish.tar" DOCKER_RTVSWEB_IMAGE_TAG_NAME="rtvsweb:publish" DOCKER_RTVSWEB_CONTAINER_NAME="rtvsweb-publish" REDIS_CONNECTION_STRING="" MYSQL_CONNECTION_STRING="" VIDEO_CACHE_PATH="" MYSQL_HOST="" CONTAINER_HOST="" LINK_MYSQL="false" ###################################函式定義####################################### function mysql_create_table() { if [[ -f "./mysql_create_table.sh" ]]; then echo "資料庫指令碼賦值許可權......" # 為執行檔案新增許可權 chmod a+x mysql_create_table.sh else echo "缺少./mysql_create_table.sh檔案...已退出安裝!" exit fi # 執行mysql資料庫表建立 ./mysql_create_table.sh if [[ $? -eq 0 ]]; then echo "./mysql_docker_install.sh 執行完成!" else exit fi } function docker_install() { echo "檢查Docker......" docker -v if [ $? -eq 0 ]; then echo "檢查到Docker已安裝!" else echo "安裝docker環境..." curl -sSL https://get.daocloud.io/docker | sh echo "安裝docker環境...安裝完成!" fi # 建立公用網路==bridge模式 #docker network create share_network } function mysql_install(){ install_type=$1 if [[ "$install_type" == "by_local_mysql_installer" ]]; then #statements echo "暫不支援wget本地下載安裝,已退出!" exit 1 fi if [[ "$install_type" == "by_docker_mysql_installer" ]]; then #statements docker_mysql_install fi if [[ "$install_type" == "by_smart_installer" ]]; then #statements if [[ `netstat -lanp|grep tcp|grep 3306|wc -l` == 1 ]]; then echo "檢測到mysql已在本機安裝!" # 列印一下mysql版本 echo "檢測到mysql版本:" mysql --version # 執行建立Mysql資料庫指令碼 mysql_create_table if [[ $? -eq 0 ]]; then echo "本地MySQL指令碼初始化完成!" else echo "本地MySQL指令碼初始化失敗!" exit 1 fi MYSQL_HOST="localhost" else docker_mysql_install fi fi } function docker_mysql_install(){ echo "安裝Docker Mysql環境..." if [[ -f "./docker_mysql_install.sh" ]]; then if [[ -f "./docker_mysql_create_table.sh" ]]; then echo "拷貝一份容器執行檔案: ./docker_mysql_create_table.sh /usr/local/docker_mysql_create_table.sh" cp docker_mysql_create_table.sh /usr/local/docker_mysql_create_table.sh else echo "缺少./docker_mysql_create_table.sh檔案...已退出安裝!" exit 1 fi # 為執行檔案新增許可權 chmod a+x docker_mysql_install.sh # Dokcer方式安裝Mysql ./docker_mysql_install.sh if [[ $? -eq 0 ]]; then echo "./docker_mysql_install.sh 執行完成!" # 休10秒鐘 echo "休眠等待10s,等待Docker執行完成......" sleep 10s # 測試列印mysql資訊 print_docker_mysql_info else echo "./docker_mysql_install.sh 執行過程中出現錯誤,已退出安裝!" exit 1 fi else echo "缺少./docker_mysql_install.sh檔案...已退出安裝!" exit 1 fi } function docker_container_ip() { CONTAINER_HOST=` docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1` } function print_docker_mysql_info() { echo "指令碼執行Mysql資訊驗證:..." if [[ -f "./docker_mysql_validator.sh" ]]; then echo "拷貝一份容器執行檔案: ./docker_mysql_validator.sh /usr/local/docker_mysql_validator.sh" cp docker_mysql_validator.sh /usr/local/docker_mysql_validator.sh else echo "缺少./docker_mysql_validator.sh檔案...已退出安裝!" exit fi # 對映copy檔案路徑到docker容器 docker cp /usr/local/docker_mysql_validator.sh mysql5.7:/usr/local/docker_mysql_validator.sh docker exec -it mysql5.7 /bin/bash -c "sh /usr/local/docker_mysql_validator.sh" if [[ $? -eq 0 ]]; then echo "./docker_mysql_validator.sh 執行完成!" echo "MySQL容器Host:" docker_container_ip mysql5.7 echo "當前mysql5.7例項IP=$CONTAINER_HOST" #echo "MySQL容器network 相關資訊:" #docker network inspect share_network #echo "MySQL容器link相關資訊:" #cat /etc/hosts else echo "./docker_mysql_validator.sh 執行過程中出現錯誤,已退出安裝!" exit fi #link 訪問方式 MYSQL_HOST="mysql5.7" } function docker_image_need_file_path() { # 建立rtvs目錄 if [[ ! -d "/usr/local/rtvs" ]]; then mkdir /usr/local/rtvs fi # 建立mydata目錄 if [[ ! -d "/usr/local/rtvs/mydata" ]]; then mkdir /usr/local/rtvs/mydata fi # 建立video目錄 if [[ ! -d "/usr/local/rtvs/videocache" ]]; then mkdir /usr/local/rtvs/videocache fi if [[ -f "./SettingConfig.xml" ]]; then echo "拷貝一份XML配置檔案: ./SettingConfig.xml /usr/local/rtvs/SettingConfig.xml" cp SettingConfig.xml /usr/local/rtvs/SettingConfig.xml else echo "缺少./SettingConfig.xml檔案...已退出安裝!" exit fi } function docker_image_load() { #處理映象需要的檔案 docker_image_need_file_path #驗證映象是否存在 exists_image_name="false" exists_image_tag="false" image_id_is_equal="false" for i in [ `docker images ` ]; do if [[ "$i" == "rtvsweb" ]]; then echo "$i" exists_image_name="true" fi if [[ "$i" == "publish" ]]; then echo "$i" exists_image_tag="true" fi done if [[ $exists_image_name == "true" && $exists_image_tag == "true" ]]; then echo "本地已存在映象:$DOCKER_RTVSWEB_IMAGE_TAG_NAME" echo "嘗試刪除映象:$DOCKER_RTVSWEB_IMAGE_TAG_NAME..." docker rmi $DOCKER_RTVSWEB_IMAGE_TAG_NAME fi echo "Docker映象載入......" if [[ ! -f "./$DOCKER_IMAGE_FILE" ]]; then echo "Docker映象檔案$DOCKER_IMAGE_FILE ......不存在,已退出安裝!" exit 1 fi echo "Docker映象匯入......$DOCKER_IMAGE_ID 執行指令: sudo docker load < $DOCKER_IMAGE_FILE " sudo docker load < $DOCKER_IMAGE_FILE echo "匯入映象後的docker images列表:" docker images # 判斷是否有映象,存在時建立相應的容器例項 for i in [ `docker images` ]; do #statements if [[ "$i" == "$DOCKER_IMAGE_ID" ]]; then image_id_is_equal="true" echo "已經找到匯入的映象!" run_service_image break fi done if [[ $image_id_is_equal == "false" ]]; then echo "匯入映象的ID不是$DOCKER_IMAGE_ID,已退出安裝!" exit 1 fi echo "當前正在執行的Docker容器例項列表:" docker ps } function run_service_image() { docker tag $DOCKER_IMAGE_ID $DOCKER_RTVSWEB_IMAGE_TAG_NAME echo "嘗試停止$DOCKER_RTVSWEB_CONTAINER_NAME容器..." docker stop $DOCKER_RTVSWEB_CONTAINER_NAME echo "嘗試刪除$DOCKER_RTVSWEB_CONTAINER_NAME容器..." docker rm $DOCKER_RTVSWEB_CONTAINER_NAME echo "啟動容器..." # run 的引數設定參考VS專案啟動的設定 # ===bridge 橋接模式 #docker run -it --name=$DOCKER_RTVSWEB_CONTAINER_NAME --net="bridge" --dns 8.8.8.8 --network share_network --network-alias $4 -v /usr/local/rtvs/mydata:/MyData -v /usr/local/rtvs/videocache:/MyVideo -v /usr/local/rtvs/SettingConfig.xml:/app/SettingConfig.xml -p 38067:80 -p 44383:443 -p 18000:18000 -p 18002:18002 -p 19700:19700 -p 19702:19702 -p 19703:19703 -d $DOCKER_RTVSWEB_IMAGE_TAG_NAME if [[ $LINK_MYSQL == "true" ]]; then # ===link 連結模式 docker run -it --name $DOCKER_RTVSWEB_CONTAINER_NAME --link mysql5.7:mysql5.7 -v /usr/local/rtvs/mydata:/MyData -v /usr/local/rtvs/videocache:/MyVideo -v /usr/local/rtvs/SettingConfig.xml:/app/SettingConfig.xml -p 38067:80 -p 44383:443 -p 18000:18000 -p 18002:18002 -p 19700:19700 -p 19702:19702 -p 19703:19703 -d $DOCKER_RTVSWEB_IMAGE_TAG_NAME else # ===普通方式 docker run -it --name $DOCKER_RTVSWEB_CONTAINER_NAME -v /usr/local/rtvs/mydata:/MyData -v /usr/local/rtvs/videocache:/MyVideo -v /usr/local/rtvs/SettingConfig.xml:/app/SettingConfig.xml -p 38067:80 -p 44383:443 -p 18000:18000 -p 18002:18002 -p 19700:19700 -p 19702:19702 -p 19703:19703 -d $DOCKER_RTVSWEB_IMAGE_TAG_NAME fi } ####################工具類和流程定製################################ function editXml() { val=`echo ${@:3}` echo "正在修改XML檔案:$1...." echo "正在修改XML檔案:[0]=$1,[1]=$2,[2]=$val" echo "XML檔案$2標籤 value=$val" sed -i "s/<$2>.*<\/$2>/<$2>${val}<\/$2></g" $1 } function editSpecificConfig() { echo "獲取/usr/local/rtvs/SettingConfig.xml資訊..." cat /usr/local/rtvs/SettingConfig.xml echo "" echo "修改Redis和MySQL連線字串..." #設定視訊快取路徑並列印預設資訊 #editXml VideoCachePath /MyVideo /usr/local/rtvs/SettingConfig.xml sed -i "s/<VideoCachePath>.*<\/VideoCachePath>/<VideoCachePath>\/MyVideo<\/VideoCachePath></g" /usr/local/rtvs/SettingConfig.xml VIDEO_CACHE_PATH=`grep -E -o -e '<VideoCachePath>.+</VideoCachePath>' /usr/local/rtvs/SettingConfig.xml | sed 's/<VideoCachePath>//g'|sed 's/<\/VideoCachePath>//g'` echo "預設視訊快取目錄:VideoCachePath=$VIDEO_CACHE_PATH" REDIS_CONNECTION_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "預設Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTION_STRING" MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "預設Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" read -p "是否修改Redis配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Redis配置連線字串:" redis echo "新的Redis連線字串:$redis" sed -i "s/<RedisExchangeHosts>.*<\/RedisExchangeHosts>/<RedisExchangeHosts>$redis<\/RedisExchangeHosts></g" /usr/local/rtvs/SettingConfig.xml REDIS_CONNECTION_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "獲取已修改的Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTION_STRING" fi #Mysql配置連線字串 echo "mysql---------settings----------init----------------start!" echo "根據安裝的Mysql配置連線字串...MySQL Host:$MYSQL_HOST" if [[ $MYSQL_HOST == "localhost" ]]; then mysql="Database=filecache;Data Source=$MYSQL_HOST;port=3306;User Id=rtvsweb;Password=rtvs2018;charset=utf8;pooling=true" echo "安裝MySQL的連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml else mysql="Database=filecache;Data Source=$MYSQL_HOST;port=3366;User Id=rtvsweb;Password=rtvs2018;charset=utf8;pooling=true" echo "安裝MySQL的連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml fi echo "mysql---------settings-----------init---------------end!" MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "根據安裝條件修改Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" read -p "是否修改Mysql配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Mysql配置連線字串:" mysql echo "新的Mysql連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "獲取已修改的Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" fi } function editRedisAndMysqlConfig() { echo "獲取/usr/local/rtvs/SettingConfig.xml資訊..." cat /usr/local/rtvs/SettingConfig.xml echo "" echo "修改Redis和MySQL連線字串..." #設定視訊快取路徑並列印預設資訊 #editXml VideoCachePath /MyVideo /usr/local/rtvs/SettingConfig.xml sed -i "s/<VideoCachePath>.*<\/VideoCachePath>/<VideoCachePath>\/MyVideo<\/VideoCachePath></g" /usr/local/rtvs/SettingConfig.xml VIDEO_CACHE_PATH=`grep -E -o -e '<VideoCachePath>.+</VideoCachePath>' /usr/local/rtvs/SettingConfig.xml | sed 's/<VideoCachePath>//g'|sed 's/<\/VideoCachePath>//g'` echo "預設視訊快取目錄:VideoCachePath=$VIDEO_CACHE_PATH" REDIS_CONNECTION_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "預設Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTION_STRING" MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "預設Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" # 設定Redis連線字串 read -p "是否修改Redis配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Redis連線字串:" redis echo "新的Redis連線字串:$redis" sed -i "s/<RedisExchangeHosts>.*<\/RedisExchangeHosts>/<RedisExchangeHosts>$redis<\/RedisExchangeHosts></g" /usr/local/rtvs/SettingConfig.xml REDIS_CONNECTION_STRING=`grep -E -o -e '<RedisExchangeHosts>.+</RedisExchangeHosts>' /usr/local/rtvs/SettingConfig.xml | sed 's/<RedisExchangeHosts>//g'|sed 's/<\/RedisExchangeHosts>//g'` echo "獲取已修改的Redis配置連線字串...RedisExchangeHosts=$REDIS_CONNECTION_STRING" fi read -p "是否修改Mysql配置連線字串?y/n:" ans echo "$ans" if [[ "$ans" == "y" ]]; then read -p "請輸入Mysql連線字串:" mysql echo "新的Mysql連線字串:$mysql" sed -i "s/<MysqlConnectionString>.*<\/MysqlConnectionString>/<MysqlConnectionString>$mysql<\/MysqlConnectionString></g" /usr/local/rtvs/SettingConfig.xml MYSQL_CONNECTION_STRING=`grep -E -o -e '<MysqlConnectionString>.+</MysqlConnectionString>' /usr/local/rtvs/SettingConfig.xml | sed 's/<MysqlConnectionString>//g'|sed 's/<\/MysqlConnectionString>//g'` echo "獲取已修改的Mysql配置連線字串...MysqlConnectionString=$MYSQL_CONNECTION_STRING" fi } function help() { echo "*******************************************" echo "********* 呼叫方法說明 ************" echo "*******************************************" echo "檢視核心配置檔案:cat ./SettingConfig.xml" echo "幫助方法說明:sh ./install.sh help" echo "修改Redis和MySQL連線字串:sh ./install.sh editXml [XMLFilePath] [tag] ['value'] " echo "********************************************" echo "********* 快速定製安裝 *************" echo "********************************************" echo "1、執行本地Mysql安裝(wget下載檔案安裝...比較費時): sh ./install.sh by_local_mysql_installer" echo "2、執行Docker Mysql安裝 : sh ./install.sh by_docker_mysql_installer" echo "3、智慧檢測決定Mysql安裝方式(如果本機已有MySQL則直接使用): sh ./install.sh by_smart_installer" echo "4、簡化安裝只需要給出Redis和MySQL連線字串即可(極簡方式): sh ./install.sh by_simple_installer" echo "說明:\r 以上4種方式已包含了Docker應用的安裝!" } function completed() { echo "******************安裝結果:*****************" # bridge 橋接模式 #echo "network例項, share_network相關容器資訊:" #docker network inspect share_network echo "Redis連線字串:$REDIS_CONNECTION_STRING" echo "Mysql連線字串:$MYSQL_CONNECTION_STRING" echo "預設視訊快取目錄:$VIDEO_CACHE_PATH" echo "正在執行的容器例項:" docker ps echo "容器對應的Ip地址:" docker_container_ip mysql5.7 if [[ "$CONTAINER_HOST" != "" ]]; then echo "mysql5.7 ---------------------$CONTAINER_HOST" fi docker_container_ip $DOCKER_RTVSWEB_CONTAINER_NAME echo "$DOCKER_RTVSWEB_CONTAINER_NAME ---------------------$CONTAINER_HOST" echo "驗證rtvs訪問:curl http://localhost:38067/" echo "******************安裝完成!*****************" } function by_local_mysql_installer() { # Docker 安裝 docker_install # Mysql 安裝及其指令碼建立 mysql_install "by_local_mysql_installer" # 映象匯入和例項構建 if [[ $? -eq 0 ]]; then docker_image_load if [[ $? -eq 0 ]]; then echo "映象匯入成功!" else echo "映象匯入失敗!" exit fi else exit fi # 修改映象使用的路徑和資料來源 editSpecificConfig # 完成後輸出 completed } function by_docker_mysql_installer() { # Docker 安裝 docker_install # Mysql 安裝及其指令碼建立 mysql_install "by_docker_mysql_installer" # 映象匯入和例項構建 if [[ $? -eq 0 ]]; then docker_image_load else exit fi # 修改映象使用的路徑和資料來源 editSpecificConfig # 完成後輸出 completed } function by_smart_installer() { # Docker 安裝 docker_install # Mysql 安裝及其指令碼建立 mysql_install "by_smart_installer" # 映象匯入和例項構建 if [[ $? -eq 0 ]]; then docker_image_load else exit fi # 修改映象使用的路徑和資料來源 editSpecificConfig # 完成後輸出 completed } function by_simple_installer() { # Docker 安裝 docker_install # 映象匯入和例項構建 docker_image_load # 修改映象使用的路徑和資料來源 editRedisAndMysqlConfig # 完成後輸出 completed } if [[ $# -gt 0 ]]; then #statements if [[ "$1" == "by_local_mysql_installer" ]]; then #statements by_local_mysql_installer fi if [[ "$1" == "by_docker_mysql_installer" ]]; then #statements by_docker_mysql_installer fi if [[ "$1" == "by_smart_installer" ]]; then #statements by_smart_installer fi if [[ "$1" == "by_simple_installer" ]]; then #statements by_simple_installer fi if [[ "$1" == "editXml" ]]; then if [[ $# -eq 4 ]]; then #statements editXml $2 $3 $4 else echo "editXml引數個數不匹配!" fi fi if [[ "$1" == "help" ]]; then #statements help fi else help fi
最終Docker應用跑起來了: