1
0

lxcbuild 4.1 KB

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