docker.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. ## @description download docker rmp
  18. ## @audience public
  19. ## @stability stable
  20. function download_docker_rpm()
  21. {
  22. # download http server
  23. if [[ -n "$DOWNLOAD_HTTP" ]]; then
  24. MY_DOCKER_ENGINE_SELINUX_RPM="${DOWNLOAD_HTTP}/downloads/docker/${DOCKER_ENGINE_SELINUX_RPM}"
  25. MY_DOCKER_ENGINE_RPM="${DOWNLOAD_HTTP}/downloads/docker/${DOCKER_ENGINE_RPM}"
  26. else
  27. MY_DOCKER_ENGINE_SELINUX_RPM=${DOCKER_REPO}/${DOCKER_ENGINE_SELINUX_RPM}
  28. MY_DOCKER_ENGINE_RPM=${DOCKER_REPO}/${DOCKER_ENGINE_RPM}
  29. fi
  30. # download docker rpm
  31. if [[ -f ${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_SELINUX_RPM} ]]; then
  32. echo "${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_SELINUX_RPM} is exist."
  33. else
  34. echo "download ${MY_DOCKER_ENGINE_SELINUX_RPM} ..."
  35. wget -P ${DOWNLOAD_DIR}/docker/ ${MY_DOCKER_ENGINE_SELINUX_RPM}
  36. fi
  37. if [[ -f ${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_RPM} ]]; then
  38. echo "${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_RPM} is exist."
  39. else
  40. echo "download ${MY_DOCKER_ENGINE_RPM} ..."
  41. wget -P ${DOWNLOAD_DIR}/docker/ ${MY_DOCKER_ENGINE_RPM}
  42. fi
  43. }
  44. ## @description install docker bin
  45. ## @audience public
  46. ## @stability stable
  47. function install_docker_bin()
  48. {
  49. download_docker_rpm
  50. yum -y localinstall ${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_SELINUX_RPM}
  51. yum -y localinstall ${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_RPM}
  52. }
  53. ## @description uninstall docker bin
  54. ## @audience public
  55. ## @stability stable
  56. function uninstall_docker_bin()
  57. {
  58. download_docker_rpm
  59. yum -y remove ${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_SELINUX_RPM}
  60. yum -y remove ${DOWNLOAD_DIR}/docker/${DOCKER_ENGINE_RPM}
  61. }
  62. ## @description install docker config
  63. ## @audience public
  64. ## @stability stable
  65. function install_docker_config()
  66. {
  67. rm -rf ${INSTALL_TEMP_DIR}/docker
  68. cp -rf ${PACKAGE_DIR}/docker ${INSTALL_TEMP_DIR}/
  69. # replace cluster-store
  70. # "cluster-store":"etcd://10.196.69.173:2379,10.196.69.174:2379,10.196.69.175:2379"
  71. # char '/' need to escape '\/'
  72. clusterStore="etcd:\/\/"
  73. index=1
  74. etcdHostsSize=${#ETCD_HOSTS[@]}
  75. for item in ${ETCD_HOSTS[@]}
  76. do
  77. clusterStore="${clusterStore}${item}:2379"
  78. if [[ ${index} -lt ${etcdHostsSize}-1 ]]; then
  79. clusterStore=${clusterStore}","
  80. fi
  81. index=$(($index+1))
  82. done
  83. echo "clusterStore=${clusterStore}"
  84. sed -i "s/CLUSTER_STORE_REPLACE/${clusterStore}/g" $INSTALL_TEMP_DIR/docker/daemon.json >>$LOG
  85. sed -i "s/DOCKER_REGISTRY_REPLACE/${DOCKER_REGISTRY}/g" $INSTALL_TEMP_DIR/docker/daemon.json >>$LOG
  86. sed -i "s/LOCAL_HOST_IP_REPLACE/${LOCAL_HOST_IP}/g" $INSTALL_TEMP_DIR/docker/daemon.json >>$LOG
  87. sed -i "s/YARN_DNS_HOST_REPLACE/${YARN_DNS_HOST}/g" $INSTALL_TEMP_DIR/docker/daemon.json >>$LOG
  88. sed -i "s/LOCAL_DNS_HOST_REPLACE/${LOCAL_DNS_HOST}/g" $INSTALL_TEMP_DIR/docker/daemon.json >>$LOG
  89. # Delete the ASF license comment in the daemon.json file, otherwise it will cause a json format error.
  90. sed -i '1,16d' $INSTALL_TEMP_DIR/docker/daemon.json
  91. if [ ! -d "/etc/docker" ]; then
  92. mkdir /etc/docker
  93. fi
  94. cp $INSTALL_TEMP_DIR/docker/daemon.json /etc/docker/
  95. cp $INSTALL_TEMP_DIR/docker/docker.service /etc/systemd/system/ >>$LOG
  96. }
  97. ## @description install docker
  98. ## @audience public
  99. ## @stability stable
  100. function install_docker()
  101. {
  102. install_docker_bin
  103. install_docker_config
  104. systemctl daemon-reload
  105. systemctl enable docker.service
  106. }
  107. ## @description unstall docker
  108. ## @audience public
  109. ## @stability stable
  110. function uninstall_docker()
  111. {
  112. echo "stop docker service"
  113. systemctl stop docker
  114. echo "remove docker"
  115. uninstall_docker_bin
  116. rm /etc/docker/daemon.json >>$LOG
  117. rm /etc/systemd/system/docker.service >>$LOG
  118. systemctl daemon-reload
  119. }
  120. ## @description start docker
  121. ## @audience public
  122. ## @stability stable
  123. function start_docker()
  124. {
  125. systemctl restart docker
  126. systemctl status docker
  127. docker info
  128. }
  129. ## @description stop docker
  130. ## @audience public
  131. ## @stability stable
  132. function stop_docker()
  133. {
  134. systemctl stop docker
  135. systemctl status docker
  136. }
  137. ## @description check if the containers exist
  138. ## @audience public
  139. ## @stability stable
  140. function containers_exist()
  141. {
  142. local dockerContainersInfo=`docker ps -a --filter NAME=$1`
  143. echo ${dockerContainersInfo} | grep $1
  144. }