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