lxcbuild 4.2 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. 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. cprint "Ended child"
  101. exit 1
  102. fi
  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