From 5710531b445361a60cfe35fefbfbaa2453bdb72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 26 Mar 2019 14:37:03 +0100 Subject: [PATCH 1/2] Support snapcraft subdirectory --- imagebuilder | 9 +++++---- snapbuilder | 56 +++++++++++++++++++++++++++++----------------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/imagebuilder b/imagebuilder index f66ae21..48c6220 100755 --- a/imagebuilder +++ b/imagebuilder @@ -156,8 +156,9 @@ createContainer() { lxc config device add ${CONTAINERNAME} loop-control unix-char path=/dev/loop-control # Give permissions to the container - lxc config set ${CONTAINERNAME} raw.apparmor "mount," + #lxc config set ${CONTAINERNAME} raw.apparmor "mount," lxc config set ${CONTAINERNAME} security.privileged true + lxc config set ${CONTAINERNAME} security.nesting true startContainer waitContainerNetwork @@ -434,9 +435,9 @@ finish() { return 0 fi - if containerRunning; then - execContainerRoot chown -R $USERNAME:$GROUPNAME ${BUILDDIR_MOUNT} - fi + #if containerRunning; then + # execContainerRoot chown -R $USERNAME:$GROUPNAME ${BUILDDIR_MOUNT} + #fi } #------------------------------------------------------------------------------------------ diff --git a/snapbuilder b/snapbuilder index 9b50e96..dc04ec3 100755 --- a/snapbuilder +++ b/snapbuilder @@ -1,36 +1,28 @@ #!/bin/bash -e PROGRAM_NAME=`basename $0` +SNAPCRAFT_DIR=. display_help () { echo "Usage: $PROGRAM_NAME [options] [command]" echo "" echo "Available commands:" - echo " help - Display this help." - echo " delete - Delete a container." - echo " shell - Enter a container." - echo " clean - Clean the snap build dir." + echo " help Display this help." + echo " delete Delete a container." + echo " shell Enter a container." + echo " clean Clean the snap build dir." echo "Options:" - echo " --help, -h - Display this help." - echo " --mount-ssh - Mount ~/.ssh into the container (if snapcraft requires ssh keys to access repositories)." - echo " --silo, -s - Add ci-repo.nymea.io/... to the apt sources" + echo " --help, -h Display this help." + echo " --mount-ssh Mount ~/.ssh into the container (if snapcraft requires ssh keys to access repositories)." + echo " --silo, -s [NAME] Add the silo ci-repo.nymea.io/[NAME] to the apt sources" + echo " --snapcraft-dir [DIR] Specify the directory where the snapcraft command should be started" } variables () { - - SNAPCRAFT_FILE=snapcraft.yaml + SNAPCRAFT_FILE=${SNAPCRAFT_DIR}/snapcraft.yaml if [ ! -e $SNAPCRAFT_FILE ]; then - if [ ! -d snap ]; then - echo "${NEGATIVE_COLOR}snapcraft.yaml not found.${NC}" - exit 1 - else - SNAPCRAFT_FILE=snap/snapcraft.yaml - if [ ! -e $SNAPCRAFT_FILE ]; then - echo "${NEGATIVE_COLOR}snap/snapcraft.yaml not found.${NC}" - exit 1 - fi - fi - + echo "${NEGATIVE_COLOR}Could not find ${SNAPCRAFT_FILE}${NC}" + exit 1 fi if grep "base: core18" $SNAPCRAFT_FILE > /dev/null; then @@ -38,6 +30,7 @@ variables () { else DISTRO=xenial fi + PACKAGE=`cat $SNAPCRAFT_FILE | grep name | head -n 1 | cut -d " " -f 2` VERSION=`cat $SNAPCRAFT_FILE | grep version | head -n 1 | cut -d " " -f 2` RED='\033[0;31m' @@ -176,9 +169,15 @@ exec_container_root () { lxc exec $LXD_CONTAINER -- sh -c "cd $SOURCE_PATH_CONTAINER; $command" } +exec_snapcraft() { + params="$@" + echo lxc exec $LXD_CONTAINER "$@" + lxc exec $LXD_CONTAINER -- su -l -c "cd $SOURCE_PATH_CONTAINER/${SNAPCRAFT_DIR}; snapcraft $params" $USERNAME +} + exec_container () { command="$@" - #echo lxc exec $LXD_CONTAINER "$@" + echo lxc exec $LXD_CONTAINER "$@" lxc exec $LXD_CONTAINER -- su -l -c "cd $SOURCE_PATH_CONTAINER; $command" $USERNAME } @@ -200,14 +199,15 @@ check_for_container_network() { build () { exec_container_root rm /var/lib/dpkg/lock || true - exec_container snapcraft + exec_snapcraft } clean () { - exec_container snapcraft clean $@ + exec_snapcraft clean $@ } - -variables +######################################################################## +# Main +######################################################################## while [ "$1" != "" ]; do OPTION=`echo $1 | awk -F= '{print $1}'` @@ -224,6 +224,10 @@ while [ "$1" != "" ]; do --silo) SILO_NAME=$VALUE ;; + --snapcraft-dir) + shift; + SNAPCRAFT_DIR=$1 + ;; -s) shift; SILO_NAME=$1 @@ -247,6 +251,8 @@ while [ "$1" != "" ]; do esac done +variables + COMMAND=$1 if [ -n "$COMMAND" ] ; then shift From 12f8e9a2890dd525bfffe6d92e10267482fa5fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 26 Mar 2019 14:39:05 +0100 Subject: [PATCH 2/2] Add bash completion --- data/bash-completion/snapbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/bash-completion/snapbuilder b/data/bash-completion/snapbuilder index b000b56..c66e8fb 100644 --- a/data/bash-completion/snapbuilder +++ b/data/bash-completion/snapbuilder @@ -6,7 +6,7 @@ _snapbuilder() COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" - opts="help delete shell clean --help -h --mount-ssh --silo -s" + opts="help delete shell clean --help -h --mount-ssh --silo -s --snapcraft-dir" COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) }