|
@@ -4,62 +4,116 @@ CONTAINER_OS=ubuntu
|
|
CONTAINER_VERSION=18.04
|
|
CONTAINER_VERSION=18.04
|
|
CONTAINER_NAME=$(basename `git rev-parse --show-toplevel`)
|
|
CONTAINER_NAME=$(basename `git rev-parse --show-toplevel`)
|
|
|
|
|
|
|
|
+if [ "${CONTAINER_NAME}" != "$(basename $(pwd))" ]; then
|
|
|
|
+ cprint "err" "Run script from root folder of project"
|
|
|
|
+ exit 1
|
|
|
|
+fi
|
|
|
|
+
|
|
|
|
+function cprint {
|
|
|
|
+ ERR='\033[0;31m'
|
|
|
|
+ OK='\033[0;32m'
|
|
|
|
+ INFO='\033[0;33m'
|
|
|
|
+ END='\033[0m'
|
|
|
|
+ while IFS= read -r L; do
|
|
|
|
+ case "${1}" in
|
|
|
|
+ err) echo -e "LXDBuild ${ERR}Error: ${L}${END}";;
|
|
|
|
+ ok) echo -e "LXDBuild ${OK}${L}${END}";;
|
|
|
|
+ info) echo -e "LXDBuild ${INFO}${L}${END}";;
|
|
|
|
+ esac
|
|
|
|
+ done < <(printf "%s\n" "${2}")
|
|
|
|
+}
|
|
|
|
+
|
|
function container_sh {
|
|
function container_sh {
|
|
lxc exec ${CONTAINER_NAME} -- sh -c "${1}"
|
|
lxc exec ${CONTAINER_NAME} -- sh -c "${1}"
|
|
return $?
|
|
return $?
|
|
}
|
|
}
|
|
|
|
|
|
-function container_cmd {
|
|
|
|
- lxc exec ${CONTAINER_NAME} -- ${1}
|
|
|
|
|
|
+function container_home {
|
|
|
|
+ container_sh "echo $(echo '${HOME}')"
|
|
return $?
|
|
return $?
|
|
}
|
|
}
|
|
|
|
|
|
-function container_snap_cmd {
|
|
|
|
|
|
+function container_snap_sh {
|
|
container_sh "cd $(container_home)/${CONTAINER_NAME} && ${1}"
|
|
container_sh "cd $(container_home)/${CONTAINER_NAME} && ${1}"
|
|
return $?
|
|
return $?
|
|
}
|
|
}
|
|
|
|
|
|
-function container_home {
|
|
|
|
- container_sh "echo $(echo '${HOME}')"
|
|
|
|
- return $?
|
|
|
|
-}
|
|
|
|
|
|
+function check_installed {
|
|
|
|
+ cprint "ok" "Checks if ${1} '${2}' is installed"
|
|
|
|
|
|
-function cprint {
|
|
|
|
- ERR='\033[0;31m'
|
|
|
|
- OK='\033[0;32m'
|
|
|
|
- END='\033[0m'
|
|
|
|
- case "${1}" in
|
|
|
|
- err)
|
|
|
|
- echo -e "LXDBuild ${ERR}Error: ${2}${END}"
|
|
|
|
- ;;
|
|
|
|
- ok)
|
|
|
|
- echo -e "LXDBuild ${OK}${2}${END}"
|
|
|
|
- ;;
|
|
|
|
|
|
+ case ${1} in
|
|
|
|
+ dpkg) CHECK="$(dpkg-query -W --showformat='${Status}\n' ${2})";;
|
|
|
|
+ snap) CHECK="$(snap list ${2})";;
|
|
esac
|
|
esac
|
|
-}
|
|
|
|
|
|
|
|
-if [ "${CONTAINER_NAME}" != "$(basename $(pwd))" ]; then
|
|
|
|
- cprint "err" "Script must be ran from root folder of project."
|
|
|
|
- exit 1
|
|
|
|
-fi;
|
|
|
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
|
+ if [ $# -eq 3 ]; then
|
|
|
|
+ cprint "err" "Missing ${2}: Exiting.";
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
+ cprint "err" "Missing ${2}."
|
|
|
|
+ while true; do
|
|
|
|
+ read -p "Install? Y/N: " A
|
|
|
|
+ case ${A} in
|
|
|
|
+ [Yy]* )
|
|
|
|
+ if [ "${1}" == "dpkg" ]; then
|
|
|
|
+ sudo apt install "${2}"
|
|
|
|
+ elif [ "${1}" == "snap" ]; then
|
|
|
|
+ sudo snap install "${2}"
|
|
|
|
+ fi
|
|
|
|
+ break;;
|
|
|
|
+ [Nn]* ) echo "no"; break;;
|
|
|
|
+ * ) echo "Unknown";;
|
|
|
|
+ esac
|
|
|
|
+ done
|
|
|
|
+ check_installed "${1}" "${2}" "test"
|
|
|
|
+ return $?
|
|
|
|
+ else
|
|
|
|
+ cprint "ok" "${CHECK}"
|
|
|
|
+ return 0
|
|
|
|
+ fi
|
|
|
|
+}
|
|
|
|
|
|
-cprint "ok" "Checking for snap and lxd compability"
|
|
|
|
-[[ ! -z $(which snap) ]] || sudo apt install snapd
|
|
|
|
-[[ ! -z $(which lxd) ]] || sudo snap install lxd
|
|
|
|
|
|
+function init_container {
|
|
|
|
+ RETRY=$( [ $# -eq 2 ] && echo 1 || echo "0" )
|
|
|
|
+ echo "${RETRY}"
|
|
|
|
+ if [ $(lxc list "${CONTAINER_NAME}" | grep "${CONTAINER_NAME}" | wc -l) -eq 0 ]; then
|
|
|
|
+ lxc launch "${CONTAINER_OS}:${CONTIANER_VERSION}" "${CONTAINER_NAME}"
|
|
|
|
+ RET=$?
|
|
|
|
+ echo ${RET}
|
|
|
|
+ if [ ${RET} -ne 0 ] && [ "${RETRY}" -eq 0 ]; then
|
|
|
|
+ cprint "err" "${RET}|${RETRY}Must init LXD"
|
|
|
|
+ lxd init
|
|
|
|
+ init_container "false"
|
|
|
|
+ elif [ ${RET} -ne 0 ]; then
|
|
|
|
+ cprint "err" "${RET} Can not init LDX. Exiting."
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
+
|
|
|
|
+ else
|
|
|
|
+ lxc start "${CONTAINER_NAME}"
|
|
|
|
+ fi
|
|
|
|
+ container_sh "echo '* Container initialized'"
|
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
|
+ cprint "err" "Can not init container ${CONTAINER_NAME}. Exiting."
|
|
|
|
+ exit 1
|
|
|
|
+ fi
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
|
|
|
|
+check_installed "dpkg" "snapd"
|
|
|
|
+check_installed "snap" "lxd"
|
|
|
|
+init_container
|
|
|
|
|
|
-cprint "ok" "Checking LXD configuration"
|
|
|
|
-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)
|
|
|
|
|
|
+while true; do
|
|
|
|
+ container_sh "ping -c 1 google.no 2> /dev/null"
|
|
|
|
+ [ $? -eq 0 ] && break || cprint "info" "Waiting for network"
|
|
|
|
+done
|
|
|
|
|
|
-cprint "ok" "Updating container"
|
|
|
|
|
|
+cprint "info" "Updating container"
|
|
container_sh "apt update && apt upgrade -y && apt dist-upgrade -y"
|
|
container_sh "apt update && apt upgrade -y && apt dist-upgrade -y"
|
|
|
|
|
|
-if [ $? -ne 0 ]; then
|
|
|
|
- cprint "err" "Cannot create container ${CONTAINER_OS}:${CONTAINER_VERSION} for ${CONTAINER_NAME}"
|
|
|
|
- exit 1
|
|
|
|
-fi
|
|
|
|
-
|
|
|
|
-cprint "ok" "Looking for snapcraft"
|
|
|
|
|
|
+cprint "info" "Looking for snapcraft"
|
|
container_sh "snap list snapcraft"
|
|
container_sh "snap list snapcraft"
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
if [ $? -ne 0 ]; then
|
|
@@ -67,16 +121,40 @@ if [ $? -ne 0 ]; then
|
|
container_sh "snap install snapcraft --classic"
|
|
container_sh "snap install snapcraft --classic"
|
|
fi
|
|
fi
|
|
|
|
|
|
-container_sh "[ -d $(container_home)/${CONTAINER_NAME} ] && rm -rf $(container_home)/${CONTAINER_NAME}"
|
|
|
|
-
|
|
|
|
-cprint "ok" "Pushing $(pwd) to container"
|
|
|
|
-lxc file push "$(pwd)" "${CONTAINER_NAME}/$(container_home)/" -r
|
|
|
|
|
|
+container_home_sh 'ls'
|
|
|
|
+if [ $? -ne 0 ]; then
|
|
|
|
+ cprint "info" "Pushing $(pwd) to ${CONTAINER_NAME}:$(container_home)"
|
|
|
|
+ $(lxc file push "$(pwd)" "${CONTAINER_NAME}/$(container_home)" -r)
|
|
|
|
+else
|
|
|
|
+ cprint "Pulling repo"
|
|
|
|
+ git pull
|
|
|
|
+ TAG=$(git tag | tail -n 1)
|
|
|
|
+ while true; do
|
|
|
|
+ read -p "Check out latest tag ${TAG}? Y/N/M (M=master): " A
|
|
|
|
+ case ${A} in
|
|
|
|
+ [Yy]* ) git checkout "${TAG}"; break;;
|
|
|
|
+ [Nn]* ) cprint "info" "Not checking out anything"; break;;
|
|
|
|
+ [Mm]* ) git checkout master
|
|
|
|
+ esac
|
|
|
|
+ done
|
|
|
|
+fi
|
|
|
|
|
|
-cprint "ok" "Building snap"
|
|
|
|
-container_snap_cmd "snapcraft --destructive-mode"
|
|
|
|
|
|
+cprint "info" "Building snap"
|
|
|
|
|
|
-if [ $? -eq 0 ]; then
|
|
|
|
- FILE=$(container_snap_cmd "ls | grep .snap --max-count=1")
|
|
|
|
|
|
+RET=-1
|
|
|
|
+if [ $(container_snap_sh "ls | grep prime | wc -l") -ne 0 ]; then
|
|
|
|
+ container_snap_sh "snapcraft prime --destructive-mode"
|
|
|
|
+ if [ $? -ne 0 ]; then
|
|
|
|
+ container_snap_sh "snapcraft clean --destructive-mode"
|
|
|
|
+ container_snap_sh "snapcraft --destructive-mode"
|
|
|
|
+ RET=$?
|
|
|
|
+ fi
|
|
|
|
+else
|
|
|
|
+ container_snap_sh "snapcraft --destructive-mode"
|
|
|
|
+ RET=$?
|
|
|
|
+fi
|
|
|
|
+if [ ${RET} -eq 0 ]; then
|
|
|
|
+ FILE=$(container_snap_sh "ls | grep .snap --max-count=1")
|
|
lxc file pull "${CONTAINER_NAME}/$(container_home)/${CONTAINER_NAME}/${FILE}" "$(pwd)"
|
|
lxc file pull "${CONTAINER_NAME}/$(container_home)/${CONTAINER_NAME}/${FILE}" "$(pwd)"
|
|
else
|
|
else
|
|
cprint "err" "Failed to build snap"
|
|
cprint "err" "Failed to build snap"
|