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. function cprint {
  6. ERR='\033[0;31m'
  7. OK='\033[0;32m'
  8. INFO='\033[0;33m'
  9. END='\033[0m'
  10. while IFS= read -r L; do
  11. case "${1}" in
  12. err) echo -e "LXDBuild ${ERR}Error: ${L}${END}";;
  13. ok) echo -e "LXDBuild ${OK}${L}${END}";;
  14. info) echo -e "LXDBuild ${INFO}${L}${END}";;
  15. esac
  16. done < <(printf "%s\n" "${2}")
  17. }
  18. function container_sh {
  19. if [ $# -eq 1 ]; then
  20. cprint "info" "${1}"
  21. fi
  22. lxc exec ${CONTAINER_NAME} -- sh -c "${1}"
  23. return $?
  24. }
  25. function container_home {
  26. container_sh "echo $(echo '${HOME}')" 1
  27. return $?
  28. }
  29. function container_snap_sh {
  30. container_sh "cd $(container_home)/${CONTAINER_NAME} && ${1}"
  31. return $?
  32. }
  33. function check_installed {
  34. cprint "ok" "Checks if ${1} '${2}' is installed"
  35. case ${1} in
  36. dpkg) CHECK="$(dpkg-query -W --showformat='${Status}\n' ${2})";;
  37. snap) CHECK="$(snap list ${2})";;
  38. esac
  39. if [ $? -ne 0 ]; then
  40. if [ $# -eq 3 ]; then
  41. cprint "err" "Missing ${2}: Exiting.";
  42. exit 1
  43. fi
  44. cprint "err" "Missing ${2}."
  45. while true; do
  46. read -p "Install? Y/N: " A
  47. case ${A} in
  48. [Yy]* )
  49. if [ "${1}" == "dpkg" ]; then
  50. sudo apt install "${2}"
  51. elif [ "${1}" == "snap" ]; then
  52. sudo snap install "${2}"
  53. fi
  54. break;;
  55. [Nn]* ) echo "no"; break;;
  56. * ) echo "Unknown";;
  57. esac
  58. done
  59. check_installed "${1}" "${2}" "test"
  60. return $?
  61. else
  62. cprint "ok" "${CHECK}"
  63. return 0
  64. fi
  65. }
  66. function init_container {
  67. RETRY=$( [ $# -eq 2 ] && echo 1 || echo "0" )
  68. if [ $(lxc list "${CONTAINER_NAME}" | grep "${CONTAINER_NAME}" | wc -l) -eq 0 ]; then
  69. lxc launch "${CONTAINER_OS}:${CONTIANER_VERSION}" "${CONTAINER_NAME}"
  70. RET=$?
  71. if [ ${RET} -ne 0 ] && [ "${RETRY}" -eq 0 ]; then
  72. cprint "err" "${RET}|${RETRY}Must init LXD"
  73. lxd init
  74. init_container "false"
  75. elif [ ${RET} -ne 0 ]; then
  76. cprint "err" "${RET} Can not init LDX. Exiting."
  77. exit 1
  78. fi
  79. else
  80. if [ $(lxc info "${CONTAINER_NAME}" | grep "Running" | wc -l) -eq 0 ]; then
  81. lxc start "${CONTAINER_NAME}"
  82. fi
  83. fi
  84. container_sh "echo '* Container initialized'"
  85. if [ $? -ne 0 ]; then
  86. cprint "err" "Can not init container ${CONTAINER_NAME}. Exiting."
  87. exit 1
  88. fi
  89. return 0;
  90. }
  91. if [ "${CONTAINER_NAME}" != "$(basename $(pwd))" ]; then
  92. cprint "err" "Run script from root folder of project"
  93. exit 1
  94. fi
  95. CURRF="$(md5sum "${0}")"
  96. git pull
  97. if [ "${CURRF}" != "$(md5sum "${0}")" ]; then
  98. cprint "File changes on last pull. Restarting..."
  99. eval "${SELF}"
  100. exit 1
  101. fi
  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