Skip to content

Commit 95a10b2

Browse files
heschigopherbot
authored andcommitted
env/openbsd-amd64,cmd/bootstrapswarm: get openbsd working with LUCI
Teach bootstrapswarm to read the hostname from GCE when sending a VM token, since that's all that'll work. Update openbsd's make.bash: - Do all work in a temp directory so that we don't have to manage individual files. - Pass -I to pkg_add to avoid prompts. - Increase ulimits on the worker user. - Install python and sudo. - Use rc.firsttime rather than reinventing it with /firstboot. - Decide whether to start the buildlet or Swarming bot based on the GCE project as usual. - Package bootstrapswarm into the image. Change-Id: I61bbc1903f8eab596bf0d19a316993a92f6bd599 Reviewed-on: https://go-review.googlesource.com/c/build/+/526618 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Heschi Kreinick <[email protected]>
1 parent 5105b99 commit 95a10b2

File tree

2 files changed

+81
-50
lines changed

2 files changed

+81
-50
lines changed

cmd/bootstrapswarm/bootstrapswarm.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ import (
4141
"os/exec"
4242
"path/filepath"
4343
"runtime"
44+
"strings"
4445

4546
"cloud.google.com/go/compute/metadata"
4647
)
4748

4849
var (
4950
tokenFilePath = flag.String("token-file-path", defaultTokenLocation(), "Path to the token file (used when not on GCE)")
50-
hostname = flag.String("hostname", os.Getenv("HOSTNAME"), "Hostname of machine to bootstrap (required)")
51+
hostname = flag.String("hostname", os.Getenv("HOSTNAME"), "Hostname of machine to bootstrap")
5152
swarming = flag.String("swarming", "chromium-swarm.appspot.com", "Swarming server to connect to")
5253
)
5354

@@ -70,7 +71,7 @@ func main() {
7071
var httpClient = http.DefaultClient
7172

7273
func bootstrap(ctx context.Context, hostname, tokenPath string) error {
73-
httpHeaders := map[string]string{"X-Luci-Swarming-Bot-ID": hostname}
74+
httpHeaders := map[string]string{}
7475
if metadata.OnGCE() {
7576
log.Println("Bootstrapping the swarming bot with GCE authentication")
7677
log.Println("retrieving the GCE VM token")
@@ -79,6 +80,14 @@ func bootstrap(ctx context.Context, hostname, tokenPath string) error {
7980
return fmt.Errorf("unable to retrieve GCE Machine Token: %w", err)
8081
}
8182
httpHeaders["X-Luci-Gce-Vm-Token"] = token
83+
84+
// Override the hostname flag with the GCE hostname. This is a hard
85+
// requirement for LUCI, so there's no point in trying anything else.
86+
fullHost, err := metadata.Hostname()
87+
if err != nil {
88+
return fmt.Errorf("retrieving hostname: %w", err)
89+
}
90+
hostname = strings.Split(fullHost, ".")[0]
8291
} else {
8392
log.Println("Bootstrapping the swarming bot with certificate authentication")
8493
log.Println("retrieving the luci-machine-token from the token file")
@@ -98,6 +107,7 @@ func bootstrap(ctx context.Context, hostname, tokenPath string) error {
98107
}
99108
httpHeaders["X-Luci-Machine-Token"] = tok.LuciMachineToken
100109
}
110+
httpHeaders["X-Luci-Swarming-Bot-ID"] = hostname
101111
log.Println("Downloading the swarming bot")
102112
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+*swarming+"/bot_code", nil)
103113
if err != nil {

env/openbsd-amd64/make.bash

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ readonly SNAPSHOT=false
1414
readonly ARCH="${ARCH:-amd64}"
1515
readonly MIRROR="${MIRROR:-cdn.openbsd.org}"
1616

17+
readonly WORK="$(mktemp -d)"
18+
readonly SITE="${WORK}/site"
19+
1720
if [[ "${ARCH}" != "amd64" && "${ARCH}" != "i386" ]]; then
1821
echo "ARCH must be amd64 or i386"
1922
exit 1
@@ -31,45 +34,48 @@ if [[ ! -f "${ISO}" ]]; then
3134
fi
3235

3336
function cleanup() {
34-
rm -f "${ISO_PATCHED}"
35-
rm -f auto_install.conf
36-
rm -f boot.conf
37-
rm -f disk.raw
38-
rm -f disklabel.template
39-
rm -f etc/{installurl,rc.local,sysctl.conf}
40-
rm -f install.site
41-
rm -f random.seed
42-
rm -f site${RELNO}.tgz
43-
rmdir etc
37+
rm -rf "${WORK}"
4438
}
4539

4640
trap cleanup EXIT INT
4741

4842
# Create custom siteXX.tgz set.
49-
PKG_ADD_OPTIONS=""
43+
PKG_ADD_OPTIONS="-I"
5044
if [[ "$SNAPSHOT" = true ]]; then
51-
PKG_ADD_OPTIONS="-D snap"
45+
PKG_ADD_OPTIONS="-I -D snap"
5246
fi
53-
mkdir -p etc
54-
cat >install.site <<EOF
47+
mkdir -p ${SITE}/etc
48+
cat >${SITE}/install.site <<EOF
5549
#!/bin/sh
5650
touch /firstboot
5751
echo 'set tty com0' > boot.conf
5852
EOF
5953

60-
cat >etc/installurl <<EOF
54+
cat >${SITE}/etc/installurl <<EOF
6155
https://${MIRROR}/pub/OpenBSD
6256
EOF
63-
cat >etc/rc.local <<EOF
64-
if [[ -f /firstboot ]]; then
65-
syspatch
66-
# Run syspatch twice in case syspatch itself needs patching (this is the case with OpenBSD
67-
# 7.1: https://www.openbsd.org/errata71.html )
68-
syspatch
69-
pkg_add -iv ${PKG_ADD_OPTIONS} bash curl git
70-
rm -f /firstboot
71-
fi
57+
cat >${SITE}/etc/rc.firsttime <<EOF
58+
set -x
59+
cat > /etc/login.conf.d/moreres <<'EOLOGIN'
60+
moreres:\
61+
:maxproc-max=1024: \
62+
:maxproc-cur=1024: \
63+
:openfiles-max=4096: \
64+
:openfiles-cur=4096: \
65+
:tc=default:
66+
EOLOGIN
67+
cap_mkdb
68+
usermod -L moreres swarming
69+
syspatch
70+
# Run syspatch twice in case syspatch itself needs patching (this is the case with OpenBSD
71+
# 7.1: https://www.openbsd.org/errata71.html )
72+
syspatch
73+
pkg_add -iv ${PKG_ADD_OPTIONS} bash curl git python3 sudo--gettext
74+
chown root:wheel /etc/sudoers
75+
halt -p
76+
EOF
7277

78+
cat >${SITE}/etc/rc.local <<EOF
7379
(
7480
set -x
7581
@@ -83,37 +89,52 @@ fi
8389
(
8490
set -e
8591
export PATH="\$PATH:/usr/local/bin"
86-
/usr/local/bin/curl -o /buildlet \$(/usr/local/bin/curl --fail -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/buildlet-binary-url)
87-
chmod +x /buildlet
88-
exec /buildlet
92+
project=\$(curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/project/project-id)
93+
case "\$project" in
94+
*luci*)
95+
gcehost=\$(curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/hostname | cut -d . -f 1)
96+
su -l swarming -c "/usr/local/bin/bootstrapswarm --hostname \$gcehost"
97+
;;
98+
*)
99+
/usr/local/bin/curl -o /buildlet \$(/usr/local/bin/curl --fail -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/buildlet-binary-url)
100+
chmod +x /buildlet
101+
exec /buildlet
102+
;;
103+
esac
89104
)
90105
echo "giving up"
91106
(
92-
sleep 10
107+
sleep 60
93108
halt -p
94109
)&
95110
)
96111
EOF
97-
cat >etc/sysctl.conf <<EOF
112+
cat >${SITE}/etc/sysctl.conf <<EOF
98113
hw.smt=1
99114
kern.timecounter.hardware=tsc
100115
EOF
101-
chmod +x install.site
102-
tar -zcvf site${RELNO}.tgz install.site etc/{installurl,rc.local,sysctl.conf}
116+
cat >${SITE}/etc/sudoers <<EOF
117+
root ALL=(ALL:ALL) ALL
118+
swarming ALL=NOPASSWD:/sbin/shutdown -r now
119+
EOF
120+
chmod +x ${SITE}/install.site
121+
mkdir -p ${SITE}/usr/local/bin
122+
CGO_ENABLED=0 GOOS=openbsd GOARCH=amd64 go1.21.0 build -o ${SITE}/usr/local/bin/bootstrapswarm golang.org/x/build/cmd/bootstrapswarm
123+
tar -C ${SITE} -zcf ${WORK}/site${RELNO}.tgz .
103124

104125
# Autoinstall script.
105-
cat >auto_install.conf <<EOF
106-
System hostname = buildlet
126+
cat >${WORK}/auto_install.conf <<EOF
127+
System hostname = openbsd-amd64
107128
Which network interface = vio0
108129
IPv4 address for vio0 = dhcp
109130
IPv6 address for vio0 = none
110131
Password for root account = root
111132
Do you expect to run the X Window System = no
112133
Change the default console to com0 = yes
113134
Which speed should com0 use = 115200
114-
Setup a user = gopher
115-
Full name for user gopher = Gopher Gopherson
116-
Password for user gopher = gopher
135+
Setup a user = swarming
136+
Full name for user swarming = Swarming Gopher Gopherson
137+
Password for user swarming = swarming
117138
Allow root ssh login = no
118139
What timezone = US/Pacific
119140
Which disk = sd0
@@ -126,32 +147,32 @@ Directory does not contain SHA256.sig. Continue without verification = yes
126147
EOF
127148

128149
# Disklabel template.
129-
cat >disklabel.template <<EOF
150+
cat >${WORK}/disklabel.template <<EOF
130151
/ 5G-* 95%
131152
swap 1G
132153
EOF
133154

134155
# Hack install CD a bit.
135-
echo 'set tty com0' > boot.conf
136-
dd if=/dev/urandom of=random.seed bs=4096 count=1
156+
echo 'set tty com0' > ${WORK}/boot.conf
157+
dd if=/dev/urandom of=${WORK}/random.seed bs=4096 count=1
137158
cp "${ISO}" "${ISO_PATCHED}"
138159
growisofs -M "${ISO_PATCHED}" -l -R -graft-points \
139-
/${VERSION}/${ARCH}/site${RELNO}.tgz=site${RELNO}.tgz \
140-
/auto_install.conf=auto_install.conf \
141-
/disklabel.template=disklabel.template \
142-
/etc/boot.conf=boot.conf \
143-
/etc/random.seed=random.seed
160+
/${VERSION}/${ARCH}/site${RELNO}.tgz=${WORK}/site${RELNO}.tgz \
161+
/auto_install.conf=${WORK}/auto_install.conf \
162+
/disklabel.template=${WORK}/disklabel.template \
163+
/etc/boot.conf=${WORK}/boot.conf \
164+
/etc/random.seed=${WORK}/random.seed
144165

145166
# Initialize disk image.
146-
rm -f disk.raw
147-
qemu-img create -f raw disk.raw 10G
167+
rm -f ${WORK}/disk.raw
168+
qemu-img create -f raw ${WORK}/disk.raw 30G
148169

149170
# Run the installer to create the disk image.
150171
expect <<EOF
151172
set timeout 1800
152173
153174
spawn qemu-system-x86_64 -nographic -smp 2 \
154-
-drive if=virtio,file=disk.raw,format=raw -cdrom "${ISO_PATCHED}" \
175+
-drive if=virtio,file=${WORK}/disk.raw,format=raw -cdrom "${ISO_PATCHED}" \
155176
-net nic,model=virtio -net user -boot once=d
156177
157178
expect timeout { exit 1 } "boot>"
@@ -174,6 +195,6 @@ EOF
174195

175196
# Create Compute Engine disk image.
176197
echo "Archiving disk.raw... (this may take a while)"
177-
tar -Szcf "openbsd-${VERSION}-${ARCH}-gce.tar.gz" disk.raw
198+
tar -C ${WORK} -Szcf "openbsd-${VERSION}-${ARCH}-gce.tar.gz" disk.raw
178199

179200
echo "Done. GCE image is openbsd-${VERSION}-${ARCH}-gce.tar.gz."

0 commit comments

Comments
 (0)