lxcbuild 4.1 KB

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