lxcbuild 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bash
  2. CONTAINER_OS=ubuntu
  3. CONTAINER_VERSION=18.04
  4. CONTAINER_NAME=$(basename `git rev-parse --show-toplevel`)
  5. if [ "${CONTAINER_NAME}" != "$(basename $(pwd))" ]; then
  6. cprint "err" "Run script from root folder of project"
  7. exit 1
  8. fi
  9. function cprint {
  10. ERR='\033[0;31m'
  11. OK='\033[0;32m'
  12. INFO='\033[0;33m'
  13. END='\033[0m'
  14. while IFS= read -r L; do
  15. case "${1}" in
  16. err) echo -e "LXDBuild ${ERR}Error: ${L}${END}";;
  17. ok) echo -e "LXDBuild ${OK}${L}${END}";;
  18. info) echo -e "LXDBuild ${INFO}${L}${END}";;
  19. esac
  20. done < <(printf "%s\n" "${2}")
  21. }
  22. function container_sh {
  23. lxc exec ${CONTAINER_NAME} -- sh -c "${1}"
  24. return $?
  25. }
  26. function container_home {
  27. container_sh "echo $(echo '${HOME}')"
  28. return $?
  29. }
  30. function container_snap_sh {
  31. container_sh "cd $(container_home)/${CONTAINER_NAME} && ${1}"
  32. return $?
  33. }
  34. function check_installed {
  35. cprint "ok" "Checks if ${1} '${2}' is installed"
  36. case ${1} in
  37. dpkg) CHECK="$(dpkg-query -W --showformat='${Status}\n' ${2})";;
  38. snap) CHECK="$(snap list ${2})";;
  39. esac
  40. if [ $? -ne 0 ]; then
  41. if [ $# -eq 3 ]; then
  42. cprint "err" "Missing ${2}: Exiting.";
  43. exit 1
  44. fi
  45. cprint "err" "Missing ${2}."
  46. while true; do
  47. read -p "Install? Y/N: " A
  48. case ${A} in
  49. [Yy]* )
  50. if [ "${1}" == "dpkg" ]; then
  51. sudo apt install "${2}"
  52. elif [ "${1}" == "snap" ]; then
  53. sudo snap install "${2}"
  54. fi
  55. break;;
  56. [Nn]* ) echo "no"; break;;
  57. * ) echo "Unknown";;
  58. esac
  59. done
  60. check_installed "${1}" "${2}" "test"
  61. return $?
  62. else
  63. cprint "ok" "${CHECK}"
  64. return 0
  65. fi
  66. }
  67. function init_container {
  68. RETRY=$( [ $# -eq 2 ] && echo 1 || echo "0" )
  69. echo "${RETRY}"
  70. if [ $(lxc list "${CONTAINER_NAME}" | grep "${CONTAINER_NAME}" | wc -l) -eq 0 ]; then
  71. lxc launch "${CONTAINER_OS}:${CONTIANER_VERSION}" "${CONTAINER_NAME}"
  72. RET=$?
  73. echo ${RET}
  74. if [ ${RET} -ne 0 ] && [ "${RETRY}" -eq 0 ]; then
  75. cprint "err" "${RET}|${RETRY}Must init LXD"
  76. lxd init
  77. init_container "false"
  78. elif [ ${RET} -ne 0 ]; then
  79. cprint "err" "${RET} Can not init LDX. Exiting."
  80. exit 1
  81. fi
  82. else
  83. lxc start "${CONTAINER_NAME}"
  84. fi
  85. container_sh "echo '* Container initialized'"
  86. if [ $? -ne 0 ]; then
  87. cprint "err" "Can not init container ${CONTAINER_NAME}. Exiting."
  88. exit 1
  89. fi
  90. return 0;
  91. }
  92. check_installed "dpkg" "snapd"
  93. check_installed "snap" "lxd"
  94. init_container
  95. while true; do
  96. container_sh "ping -c 1 google.no 2> /dev/null"
  97. [ $? -eq 0 ] && break || cprint "info" "Waiting for network"
  98. done
  99. cprint "info" "Updating container"
  100. container_sh "apt update && apt upgrade -y && apt dist-upgrade -y"
  101. cprint "info" "Looking for snapcraft"
  102. container_sh "snap list snapcraft"
  103. if [ $? -ne 0 ]; then
  104. cprint "ok" " - Installing snapcraft"
  105. container_sh "snap install snapcraft --classic"
  106. fi
  107. container_home_sh 'ls'
  108. if [ $? -ne 0 ]; then
  109. cprint "info" "Pushing $(pwd) to ${CONTAINER_NAME}:$(container_home)"
  110. $(lxc file push "$(pwd)" "${CONTAINER_NAME}/$(container_home)" -r)
  111. else
  112. cprint "Pulling repo"
  113. git pull
  114. TAG=$(git tag | tail -n 1)
  115. while true; do
  116. read -p "Check out latest tag ${TAG}? Y/N/M (M=master): " A
  117. case ${A} in
  118. [Yy]* ) git checkout "${TAG}"; break;;
  119. [Nn]* ) cprint "info" "Not checking out anything"; break;;
  120. [Mm]* ) git checkout master
  121. esac
  122. done
  123. fi
  124. cprint "info" "Building snap"
  125. RET=-1
  126. if [ $(container_snap_sh "ls | grep prime | wc -l") -ne 0 ]; then
  127. container_snap_sh "snapcraft prime --destructive-mode"
  128. if [ $? -ne 0 ]; then
  129. container_snap_sh "snapcraft clean --destructive-mode"
  130. container_snap_sh "snapcraft --destructive-mode"
  131. RET=$?
  132. fi
  133. else
  134. container_snap_sh "snapcraft --destructive-mode"
  135. RET=$?
  136. fi
  137. if [ ${RET} -eq 0 ]; then
  138. FILE=$(container_snap_sh "ls | grep .snap --max-count=1")
  139. lxc file pull "${CONTAINER_NAME}/$(container_home)/${CONTAINER_NAME}/${FILE}" "$(pwd)"
  140. else
  141. cprint "err" "Failed to build snap"
  142. fi