lxcbuild 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if [ $(lxc info "${CONTAINER_NAME}" | grep "Running" | wc -l) -eq 0 ]; then
  86. lxc start "${CONTAINER_NAME}"
  87. fi
  88. fi
  89. container_sh "echo '* Container initialized'"
  90. if [ $? -ne 0 ]; then
  91. cprint "err" "Can not init container ${CONTAINER_NAME}. Exiting."
  92. exit 1
  93. fi
  94. return 0;
  95. }
  96. check_installed "dpkg" "snapd"
  97. check_installed "snap" "lxd"
  98. init_container
  99. while true; do
  100. container_sh "ping -c 1 google.no 2> /dev/null"
  101. [ $? -eq 0 ] && break || cprint "info" "Waiting for network"
  102. done
  103. cprint "info" "Updating container"
  104. container_sh "apt update && apt upgrade -y && apt dist-upgrade -y"
  105. cprint "info" "Looking for snapcraft"
  106. container_sh "snap list snapcraft"
  107. if [ $? -ne 0 ]; then
  108. cprint "ok" " - Installing snapcraft"
  109. container_sh "snap install snapcraft --classic"
  110. fi
  111. container_snap_sh 'ls'
  112. if [ $? -ne 0 ]; then
  113. cprint "info" "Pushing $(pwd) to ${CONTAINER_NAME}:$(container_home)"
  114. lxc file push "$(pwd)" "${CONTAINER_NAME}/$(container_home)" -rq
  115. else
  116. cprint "Pulling repo"
  117. git pull
  118. TAG=$(git tag | tail -n 1)
  119. while true; do
  120. read -p "Check out latest tag ${TAG}? Y/N/M (M=master): " A
  121. case ${A} in
  122. [Yy]* ) git checkout "${TAG}"; break;;
  123. [Nn]* ) cprint "info" "Not checking out anything"; break;;
  124. [Mm]* ) git checkout master
  125. esac
  126. done
  127. fi
  128. cprint "info" "Building snap"
  129. RET=-1
  130. if [ "$(container_sh "ls $(container_home)/${CONTAINER_NAME} | grep prime | wc -l")" == "1" ]; then
  131. container_snap_sh "snapcraft prime --destructive-mode"
  132. if [ $? -ne 0 ]; then
  133. container_snap_sh "snapcraft clean --destructive-mode"
  134. container_snap_sh "snapcraft --destructive-mode"
  135. RET=$?
  136. fi
  137. else
  138. container_snap_sh "snapcraft --destructive-mode"
  139. RET=$?
  140. fi
  141. if [ ${RET} -eq 0 ]; then
  142. FILE=$(container_snap_sh "ls | grep .snap --max-count=1")
  143. lxc file pull "${CONTAINER_NAME}/$(container_home)/${CONTAINER_NAME}/${FILE}" "$(pwd)"
  144. else
  145. cprint "err" "Failed to build snap"
  146. fi