|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# Script for preparing lxd enviorment and building Hestia packages for all supported distros |
| 5 | +# - Run with sudo, not directly as root! |
| 6 | +# |
| 7 | +# Arguments: |
| 8 | +# ./lxd_build_all --cleanup |
| 9 | +# - Stop and delete all containers |
| 10 | +# |
| 11 | +# ./lxd_build_all --background |
| 12 | +# - Execute the build script on all containers simultaneously |
| 13 | +# |
| 14 | + |
| 15 | +# Configs: |
| 16 | +oslist=('debian=9,10' 'ubuntu=16.04,18.04,20.04') |
| 17 | +branch='master' |
| 18 | + |
| 19 | + |
| 20 | +function setup_container() { |
| 21 | + if [ "$osname" = 'ubuntu' ]; then |
| 22 | + lxc init $osname:$osver "${containername}" |
| 23 | + else |
| 24 | + lxc init images:$osname/$osver "${containername}" |
| 25 | + fi |
| 26 | + |
| 27 | + mkdir -p "${__DIR__}/build/${containername}" |
| 28 | + chown $user: "${__DIR__}/build/${containername}" |
| 29 | + |
| 30 | + lxc config set ${containername} raw.idmap "both $user_id $user_gid" |
| 31 | + lxc config device add ${containername} debdir disk path=/opt/hestiacp source=${__DIR__}/build/${containername} |
| 32 | +} |
| 33 | + |
| 34 | +cmd=$1 |
| 35 | +__DIR__="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" #" |
| 36 | + |
| 37 | +# user=$(logname) |
| 38 | +user=$SUDO_USER |
| 39 | +user_id=$(id -u $user) |
| 40 | +user_gid=$(id -g $user) |
| 41 | + |
| 42 | +if [ -z "$user" ] || [ -z "$user_id" ] || [ -z "$user_gid" ] || [ "$user" = 'root' ]; then |
| 43 | + echo "Script must be run with sudo, not directly as root" && exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | + |
| 47 | +if ! dpkg-query -s lxd >/dev/null 2>&1; then |
| 48 | + apt -y install lxd |
| 49 | + lxd init --auto |
| 50 | + |
| 51 | + echo "root:$user_id:1" | sudo tee -a /etc/subuid |
| 52 | + echo "root:$user_gid:1" | sudo tee -a /etc/subgid |
| 53 | +fi |
| 54 | + |
| 55 | +for osdef in "${oslist[@]}"; do |
| 56 | + osname=${osdef%%=*} |
| 57 | + osversions=$(echo ${osdef##*=} | tr "," "\n") |
| 58 | + |
| 59 | + for osver in $osversions; do |
| 60 | + |
| 61 | + containername="hst-${osname}-${osver/\./}" |
| 62 | + container_ip="" |
| 63 | + echo "Container $containername" |
| 64 | + |
| 65 | + if [ "$cmd" = '--cleanup' ]; then |
| 66 | + # Stop and delete container |
| 67 | + lxc stop $containername |
| 68 | + lxc rm $containername |
| 69 | + continue |
| 70 | + fi |
| 71 | + |
| 72 | + if ! lxc info $containername > /dev/null 2>&1; then |
| 73 | + setup_container |
| 74 | + fi |
| 75 | + |
| 76 | + lxc start $containername > /dev/null 2>&1 |
| 77 | + |
| 78 | + # Wait for container to start |
| 79 | + while [ -z "$container_ip" ]; do |
| 80 | + sleep 1 |
| 81 | + container_ip=$(lxc list --format csv -c 4,n |grep ",$containername$"| cut -d "," -f 1) |
| 82 | + done |
| 83 | + echo $container_ip |
| 84 | + |
| 85 | + cp -f "${__DIR__}/lxd_compile.sh" "${__DIR__}/build/${containername}/lxd_compile.sh" |
| 86 | + |
| 87 | + if [ "$cmd" = '--background' ]; then |
| 88 | + # Run build script in background |
| 89 | + lxc exec $containername -- /opt/hestiacp/lxd_compile.sh $branch >/dev/null 2>&1 & |
| 90 | + else |
| 91 | + lxc exec $containername -- /opt/hestiacp/lxd_compile.sh $branch |
| 92 | + fi |
| 93 | + |
| 94 | + done |
| 95 | +done |
0 commit comments