-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·139 lines (112 loc) · 3.64 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# For copyright and license terms, see LICENSE.txt (top level of repository)
# Repository: https://github.com/timegrid/debian-image-builder
#
#% Usage: ./build.sh [OPTIONS] domain ip
#%
#% Builds a debian libvirt domain and provisions it with ansible.
#%
#% Arguments:
#% domain: name of the domain (e.g. domain.test)
#% ip: ipv4 of the domain (with cidr, e.g. 192.168.100.10/24)
#%
#% Options:
#% -s, --suite=SUITE codename of debian version (default: 'bookworm')
set -eu
# Project
scriptDir="$(dirname "$(readlink -vf "$0")")"
source "$scriptDir/.project.sh" \
--short "s:" \
--long "suite:" \
--root \
-- "$@"
aptcacherngDir="$aptcacherngDir"
libvirtDir="$libvirtDir"
ansibleDir="$ansibleDir"
# Options
suite="bookworm"
while true; do
case "$1" in
-s | --suite) suite="$2" ; shift 2 ;;
--) shift; break ;;
*) usage "ERROR: unknown flag '$1'." 1 ;;
esac
done
# Arguments
domain="${1:-}"; shift || usage "missing argument: domain" 1
ip="${1:-}"; shift || usage "missing argument: ip" 1
# Configuration
network="debian"
sshdKeys="$libvirtDir/ssh"
pool="develop"
# Interface
interface="static"
# Ansible
export ANSIBLE_CONFIG="$ansibleDir/ansible.cfg"
#------------------------------------------------------------------------------
#%% Build the libvirt domain
#------------------------------------------------------------------------------
if ! virsh dominfo "$domain" &> /dev/null; then
buildArgs=(
--network-name "$network"
--network-ip4-net "$ip"
--pool-name "$pool"
--domain-name "$domain"
--domain-sshd-keys "$sshdKeys"
--domain-interface "$interface"
--domain-ip4 "$ip"
)
"$libvirtDir/build.sh" "${buildArgs[@]}" "$suite"
fi
#------------------------------------------------------------------------------
#%% Start libvirt network
#------------------------------------------------------------------------------
title "Start libvirt network"
info "name" "$network"
netInfo="$(virsh net-info "$network" 2> /dev/null)"
if grep -q "^Active:.*yes" <<< "$netInfo"; then
info "network already started"
else
virsh net-start "$network"
fi
success
#------------------------------------------------------------------------------
#%% Start the libvirt domain
#------------------------------------------------------------------------------
title "Start the libvirt domain"
domInfo="$(virsh dominfo "$domain" 2> /dev/null)"
if grep -q "^State:.*running" <<< "$domInfo"; then
info "domain already running, skipping start."
else
virsh start "$domain"
fi
success
#------------------------------------------------------------------------------
#%% Wait for ssh connectivity
#------------------------------------------------------------------------------
title "Wait for ssh connectivity"
echo -n " ..."
while ! ssh -q -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 ${ip%/*} 'exit 0'; do
echo -n "."
sleep 1
done;
echo
success
#------------------------------------------------------------------------------
#%% Run an apt-cacher-ng docker service
#------------------------------------------------------------------------------
$aptcacherngDir/start.sh
trap "$aptcacherngDir/stop.sh" EXIT
#------------------------------------------------------------------------------
#%% Run ansible
#------------------------------------------------------------------------------
ansibleArgs=(
--verbose
--timeout 100
--inventory "$domain",
--user root
--tags setup
)
title "Run ansible"
ansible-playbook "${ansibleArgs[@]}" "$ansibleDir/debian.yml"
success