lxcbuild 4.1 KB

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