#!/bin/bash set -euo pipefail BIN_DIR="/usr/local/bin" SOCKET_FILE="/etc/systemd/system/buildkitd.socket" SERVICE_FILE="/etc/systemd/system/buildkitd.service" _detect_arch() { ARCH=$(uname -m) case $ARCH in x86_64) echo "linux-amd64" ;; aarch64) echo "linux-arm64" ;; armv7l) echo "linux-arm-v7" ;; *) echo "Unsupported architecture: $ARCH" exit 1 ;; esac } _install_buildkit() { local arch arch=$(_detect_arch) echo "Fetching latest BuildKit release..." local latest_url latest_url=$(curl -s https://api.github.com/repos/moby/buildkit/releases/latest | grep "browser_download_url" | grep "$arch.tar.gz" | cut -d '"' -f 4) if [[ -z $latest_url ]]; then echo "Failed to find a download URL for architecture $arch" exit 1 fi echo "Downloading BuildKit binary from $latest_url..." local tmp_dir tmp_dir=$(mktemp -d) curl -L -o "$tmp_dir/buildkit.tar.gz" "$latest_url" echo "Installing BuildKit binary to $BIN_DIR..." tar -xzf "$tmp_dir/buildkit.tar.gz" -C "$tmp_dir" mv -f $tmp_dir/bin/* "$BIN_DIR/" rm -rf "$tmp_dir" echo "BuildKit installation complete!" } _setup_systemd() { echo "Creating $SOCKET_FILE..." cat <"$SOCKET_FILE" [Unit] Description=Socket for BuildKit Daemon Before=buildkitd.service [Socket] ListenStream=/run/buildkit/buildkitd.sock SocketMode=0660 DirectoryMode=0755 ExecStartPre=-/bin/mkdir -p /run/buildkit ExecStartPost=/bin/chmod 0660 /run/buildkit/buildkitd.sock [Install] WantedBy=sockets.target EOF echo "Creating $SERVICE_FILE..." cat <"$SERVICE_FILE" [Unit] Description=BuildKit Daemon Requires=buildkitd.socket After=buildkitd.socket [Service] ExecStart=$BIN_DIR/buildkitd Restart=always RestartSec=5s [Install] WantedBy=multi-user.target EOF echo "Enabling and starting buildkitd.socket..." systemctl daemon-reload systemctl enable buildkitd.socket systemctl start buildkitd.socket echo "Systemd configuration complete!" } _install_buildkit _setup_systemd