yunohost-ci/prepare.sh

135 lines
4.1 KiB
Bash
Raw Normal View History

2019-11-25 13:11:18 +01:00
#!/usr/bin/env bash
currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source ${currentDir}/base.sh # Get variables from base.
set -eo pipefail
# trap any error, and mark it as a system failure.
trap "exit $SYSTEM_FAILURE_EXIT_CODE" ERR
2019-11-26 14:15:34 +01:00
clean_containers()
{
for image_to_delete in "yunohost-$DEBIAN_VERSION"{,"-tmp"}
2019-11-26 14:15:34 +01:00
do
if lxc info $image_to_delete &>/dev/null
then
lxc delete $image_to_delete --force
fi
done
for image_to_delete in "yunohost-$DEBIAN_VERSION-"{"before-install","before-postinstall","after-postinstall"}
2019-11-26 14:15:34 +01:00
do
if lxc image info $image_to_delete &>/dev/null
then
lxc image delete $image_to_delete
fi
done
}
2019-11-26 15:43:38 +01:00
wait_container()
{
# Wait for container to start, we are using systemd to check this,
# for the sake of brevity.
for i in $(seq 1 10); do
if lxc exec "$1" -- /bin/bash -c "systemctl isolate multi-user.target" >/dev/null 2>/dev/null; then
break
fi
if [ "$i" == "10" ]; then
echo 'Waited for 10 seconds to start container, exiting..'
# Inform GitLab Runner that this is a system failure, so it
# should be retried.
exit "$SYSTEM_FAILURE_EXIT_CODE"
fi
sleep 1s
done
}
2019-11-25 13:11:18 +01:00
rebuild_base_container()
{
2019-11-26 14:15:34 +01:00
clean_containers
lxc launch images:debian/$DEBIAN_VERSION/$ARCH "yunohost-$DEBIAN_VERSION-tmp"
2019-11-26 14:43:09 +01:00
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "apt-get install curl -y"
2019-11-26 14:01:29 +01:00
# Install Git LFS, git comes pre installed with ubuntu image.
2019-11-26 14:43:09 +01:00
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash"
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "apt-get install git-lfs -y"
2019-11-25 13:11:18 +01:00
# Install gitlab-runner binary since we need for cache/artifacts.
2019-11-26 14:43:09 +01:00
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "curl -s https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | bash"
2019-12-23 07:05:23 +01:00
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "apt-get install gitlab-runner -y"
2019-11-26 14:01:29 +01:00
lxc stop "yunohost-$DEBIAN_VERSION-tmp"
# Create image before install
lxc publish "yunohost-$DEBIAN_VERSION-tmp" --alias "yunohost-$DEBIAN_VERSION-before-install"
lxc start "yunohost-$DEBIAN_VERSION-tmp"
2019-11-26 15:43:38 +01:00
wait_container "yunohost-$DEBIAN_VERSION-tmp"
2019-11-26 14:01:29 +01:00
2019-11-25 13:11:18 +01:00
# Install yunohost
2019-11-26 14:43:09 +01:00
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "curl https://install.yunohost.org | bash -s -- -a -d unstable"
2019-11-26 14:01:29 +01:00
lxc stop "yunohost-$DEBIAN_VERSION-tmp"
# Create image before postinstall
lxc publish "yunohost-$DEBIAN_VERSION-tmp" --alias "yunohost-$DEBIAN_VERSION-before-postinstall"
lxc start "yunohost-$DEBIAN_VERSION-tmp"
2019-11-26 15:43:38 +01:00
wait_container "yunohost-$DEBIAN_VERSION-tmp"
2019-11-26 14:01:29 +01:00
# Running post Install
2019-11-26 14:43:09 +01:00
lxc exec "yunohost-$DEBIAN_VERSION-tmp" -- /bin/bash -c "yunohost tools postinstall -d domain.tld -p the_password --ignore-dyndns"
2019-11-26 14:01:29 +01:00
lxc stop "yunohost-$DEBIAN_VERSION-tmp"
# Create image after postinstall
lxc publish "yunohost-$DEBIAN_VERSION-tmp" --alias "yunohost-$DEBIAN_VERSION-after-postinstall"
lxc delete "yunohost-$DEBIAN_VERSION-tmp"
2019-11-25 13:11:18 +01:00
}
2020-03-19 23:52:39 +01:00
update_image() {
image_to_update=$1
# Start and run upgrade
lxc launch "$image_to_update" "$image_to_update-tmp"
lxc exec "$image_to_update-tmp" -- /bin/bash -c "apt-get update"
lxc exec "$image_to_update-tmp" -- /bin/bash -c "apt-get upgrade -y"
lxc stop "$image_to_update-tmp"
# Remove old image
lxc image delete "$image_to_update"
# Add new image updated
lxc publish "$image_to_update-tmp" --alias "$image_to_update"
lxc delete "$image_to_update-tmp"
}
2019-11-25 13:11:18 +01:00
start_container () {
2019-11-25 14:16:24 +01:00
set -x
2019-11-26 14:15:34 +01:00
if lxc info "$CONTAINER_ID" >/dev/null 2>/dev/null ; then
echo 'Found old container, deleting'
lxc delete -f "$CONTAINER_ID"
fi
2019-11-26 14:01:29 +01:00
if ! lxc image info "yunohost-$DEBIAN_VERSION-$SNAPSHOT_NAME" &>/dev/null
2019-11-25 13:11:18 +01:00
then
rebuild_base_container
fi
2020-03-19 23:52:39 +01:00
update_image "yunohost-$DEBIAN_VERSION-$SNAPSHOT_NAME"
2019-11-26 14:01:29 +01:00
lxc launch "yunohost-$DEBIAN_VERSION-$SNAPSHOT_NAME" "$CONTAINER_ID" 2>/dev/null
2020-03-19 23:52:39 +01:00
2019-12-23 11:36:31 +01:00
mkdir -p ${currentDir}/cache
chmod 777 ${currentDir}/cache
lxc config device add "$CONTAINER_ID" cache-folder disk path=/cache source="${currentDir}/cache"
2019-11-25 14:16:24 +01:00
set +x
2019-11-25 13:11:18 +01:00
2019-11-26 15:43:38 +01:00
wait_container $CONTAINER_ID
2019-11-25 13:11:18 +01:00
}
echo "Running in $CONTAINER_ID"
start_container