1
0

lxcbuild 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 container_sh {
  6. lxc exec ${CONTAINER_NAME} -- sh -c "${1}"
  7. return $?
  8. }
  9. function container_cmd {
  10. lxc exec ${CONTAINER_NAME} -- ${1}
  11. return $?
  12. }
  13. function container_snap_cmd {
  14. container_sh "cd $(container_home)/${CONTAINER_NAME} && ${1}"
  15. return $?
  16. }
  17. function container_home {
  18. container_sh "echo $(echo '${HOME}')"
  19. return $?
  20. }
  21. function cprint {
  22. ERR='\033[0;31m'
  23. OK='\033[0;32m'
  24. END='\033[0m'
  25. case "${1}" in
  26. err)
  27. echo -e "LXDBuild ${ERR}Error: ${2}${END}"
  28. ;;
  29. ok)
  30. echo -e "LXDBuild ${OK}${2}${END}"
  31. ;;
  32. esac
  33. }
  34. if [ "${CONTAINER_NAME}" != "$(basename $(pwd))" ]; then
  35. cprint "err" "Script must be ran from root folder of project."
  36. exit 1
  37. fi;
  38. cprint "ok" "Checking for snap and lxd compability"
  39. [[ ! -z $(which snap) ]] || sudo apt install snapd
  40. [[ ! -z $(which lxd) ]] || sudo snap install lxd
  41. cprint "ok" "Checking LXD configuration"
  42. container_sh "echo true" || (lxc launch ${CONTAINER_OS}:${CONTIANER_VERSION} ${CONTAINER_NAME} && sleep 5) || (lxd init && lxc launch ${CONTAINER_OS}:${CONTIANER_VERSION} ${CONTAINER_NAME} || sleep 5)
  43. cprint "ok" "Updating container"
  44. container_sh "apt update && apt upgrade -y && apt dist-upgrade -y"
  45. if [ $? -ne 0 ]; then
  46. cprint "err" "Cannot create container ${CONTAINER_OS}:${CONTAINER_VERSION} for ${CONTAINER_NAME}"
  47. exit 1
  48. fi
  49. cprint "ok" "Looking for snapcraft"
  50. container_sh "snap list snapcraft"
  51. if [ $? -ne 0 ]; then
  52. cprint "ok" " - Installing snapcraft"
  53. container_sh "snap install snapcraft --classic"
  54. fi
  55. container_sh "[ -d $(container_home)/${CONTAINER_NAME} ] && rm -rf $(container_home)/${CONTAINER_NAME}"
  56. cprint "ok" "Pushing $(pwd) to container"
  57. lxc file push "$(pwd)" "${CONTAINER_NAME}/$(container_home)/" -r
  58. cprint "ok" "Building snap"
  59. container_snap_cmd "snapcraft --destructive-mode"
  60. if [ $? -eq 0 ]; then
  61. FILE=$(container_snap_cmd "ls | grep .snap --max-count=1")
  62. lxc file pull "${CONTAINER_NAME}/$(container_home)/${CONTAINER_NAME}/${FILE}" "$(pwd)"
  63. else
  64. cprint "err" "Failed to build snap"
  65. fi