docker.build.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. MSG="";
  3. PRUNE="false";
  4. OVERRIDE_TAGS="false";
  5. ENABLE_LOG="false";
  6. LOG_FILE="$(dirname -- "$( readlink -f -- "$0"; )")/build.log";
  7. function appendOutput()
  8. {
  9. if [ -z "${MSG}" ]; then echo -e "\n" > /dev/tty; fi
  10. ARGS=$@;
  11. LINE="${ARGS}\n";
  12. if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then echo -e "${LINE}" > /dev/tty; else echo -e "${LINE}" 2>&1 | tee -a ${LOG_FILE}; fi
  13. MSG="${MSG}${LINE}";
  14. }
  15. function runDockerBuild()
  16. {
  17. if [ "${PRUNE}" == "true" ]; then
  18. if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then docker system prune -a -f;
  19. else docker system prune -a -f | tee -a ${LOG_FILE}; fi
  20. fi
  21. STARTTS=$(date +%s);
  22. ARGS=$@;
  23. APP_VERSION=$(grep -o '"version":\s*"[^"]*"' ./package.json | cut -f4- -d\" | tr -d '"');
  24. BASE_TAGS="";
  25. if [ -z "${OVERRIDE_TAGS}" ] || [ "${OVERRIDE_TAGS}" != "true" ]; then
  26. BASE_TAGS="-t meshcentral:latest -t meshcentral:${APP_VERSION}";
  27. fi
  28. BUILD_CMD="docker build -f docker/Dockerfile --force-rm --no-cache ${ARGS} ${BASE_TAGS} .";
  29. appendOutput "Current build: ${BUILD_CMD}";
  30. if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then ${BUILD_CMD}; else ${BUILD_CMD} | tee -a ${LOG_FILE}; fi
  31. if [ $? -ne 0 ]; then exit $?; fi
  32. IMAGEID=$(docker images --format "{{.ID}} {{.CreatedAt}}" | sort -rk 2 | awk 'NR==1{print $1}');
  33. appendOutput "\tImageId: ${IMAGEID}";
  34. ENDTS=$(date +%s);
  35. DIFSEC=$((${ENDTS}-${STARTTS}));
  36. if [ ${DIFSEC} -ge 60 ]; then
  37. TMPMIN=$((${DIFSEC}/60));
  38. TMPSEC=$((${DIFSEC}%60));
  39. if [ ${TMPMIN} -ge 60 ]; then
  40. TMPHOUR=$((${TMPMIN}/60));
  41. TMPMIN=$((${TMPMIN}%60));
  42. appendOutput "\tBuild time: ${TMPHOUR} hr ${TMPMIN} min ${TMPSEC} sec";
  43. else appendOutput "\tBuild time: ${TMPMIN} min ${TMPSEC} sec"; fi
  44. else appendOutput "\tBuild time: ${DIFSEC} sec"; fi
  45. IMG_SIZE=$(docker image inspect ${IMAGEID} | grep -o '"Size":\s*[^,]*' | cut -f2- -d ':' | tr -d ' ');
  46. expr $IMG_SIZE + 0 > /dev/null;
  47. appendOutput "\tImage size: ${IMG_SIZE} ($((${IMG_SIZE}/1024/1024))M)\n";
  48. return 0;
  49. }
  50. parent_path=$(dirname -- $(dirname -- "$( readlink -f -- "$0"; )"));
  51. if [ "${parent_path}" != "$(pwd -P)" ]; then
  52. echo -e "change working directory to: ${parent_path}" > /dev/tty;
  53. cd "${parent_path}";
  54. fi
  55. if ! [ -z $1 ]; then
  56. for arg in "$@"
  57. do
  58. case "${arg}" in
  59. --prune)
  60. PRUNE="true";
  61. shift 1;
  62. ;;
  63. --log)
  64. ENABLE_LOG="true";
  65. shift 1;
  66. ;;
  67. --no-tags)
  68. OVERRIDE_TAGS="true";
  69. shift 1;
  70. ;;
  71. --help)
  72. __usage="\n
  73. Usage: ./$(basename ${0}) [OPTIONS] [BUILD ARGUMENTS]\n
  74. \n
  75. Options:\n
  76. \t--log \t\twrite output to build.log file\n
  77. \t--no-tags \tdo not use default tags (meshcentral:latest and meshcentral:%VERSION%)\n
  78. \t--prune \tWARNING: This will remove:\n
  79. \t\t\t - all stopped docker containers\n
  80. \t\t\t - all docker networks not used by at least one container\n
  81. \t\t\t - all docker images without at least one container associated to them\n
  82. \t\t\t - all docker build cache\n
  83. \n
  84. Build arguments: \tAll build arguments are forwarded to the docker build command, so you can use any option accepted by 'docker build'\n
  85. \t\t\t(https://docs.docker.com/engine/reference/commandline/build/#options)\n\n
  86. \t--build-arg INCLUDE_MONGODBTOOLS=yes \tIncludes mongodb-tools (mongodump, ...) in the image\n
  87. \t--build-arg DISABLE_MINIFY=yes \t\tDisables minification of files\n
  88. \t--build-arg DISABLE_TRANSLATE=yes \tDisables translation of files\n
  89. ";
  90. echo -e $__usage;
  91. exit 0;
  92. ;;
  93. *)
  94. break;
  95. ;;
  96. esac
  97. done
  98. fi
  99. MAINARGS=$@;
  100. #runDockerBuild --build-arg DISABLE_MINIFY=yes --build-arg DISABLE_TRANSLATE=yes ${MAINARGS};
  101. #runDockerBuild --build-arg DISABLE_TRANSLATE=yes ${MAINARGS};
  102. #runDockerBuild --build-arg DISABLE_MINIFY=yes ${MAINARGS};
  103. runDockerBuild ${MAINARGS};
  104. #runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_MINIFY=yes --build-arg DISABLE_TRANSLATE=yes ${MAINARGS};
  105. #runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_TRANSLATE=yes ${MAINARGS};
  106. #runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes --build-arg DISABLE_MINIFY=yes ${MAINARGS};
  107. #runDockerBuild --build-arg INCLUDE_MONGODBTOOLS=yes ${MAINARGS};
  108. echo "";
  109. if [ -z "${ENABLE_LOG}" ] || [ "${ENABLE_LOG}" != "true" ]; then echo -e "${MSG}"; else echo -e "${MSG}" 2>&1 | tee -a ${LOG_FILE}; fi
  110. exit 0;