lxcbuild 4.1 KB

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