lxcbuild 3.8 KB

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