oto/apps/runner/assets/script/shell/oto_agent_bootstrap.sh

177 lines
4.2 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
server_url=""
agent_id=""
enrollment_token=""
release_base_url=""
agent_alias=""
install_dir="${HOME}/.oto/bin"
config_path="${HOME}/.oto/agent/config.yaml"
workspace_root="${HOME}/.oto/workspace"
log_dir="${HOME}/.oto/agent/log"
background="true"
usage() {
echo "Usage: $0 --server-url <url> --agent-id <id> --enrollment-token <token> --release-base-url <url> [options]"
echo "Options:"
echo " --agent-alias <alias> Alias for the agent"
echo " --install-dir <path> Directory to install OTO agent (default: \$HOME/.oto/bin)"
echo " --config-path <path> Path to config file (default: \$HOME/.oto/agent/config.yaml)"
echo " --workspace-root <path> Workspace root directory (default: \$HOME/.oto/workspace)"
echo " --log-dir <path> Log directory (default: \$HOME/.oto/agent/log)"
echo " --no-background Run in the foreground"
}
yaml_escape() {
local value="$1"
value="${value//\\/\\\\}"
value="${value//\"/\\\"}"
value="${value//$'\t'/\\t}"
value="${value//$'\r'/\\r}"
value="${value//$'\n'/\\n}"
printf '%s' "$value"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--server-url|--edge-url)
server_url="$2"
shift 2
;;
--agent-id)
agent_id="$2"
shift 2
;;
--enrollment-token)
enrollment_token="$2"
shift 2
;;
--release-base-url)
release_base_url="$2"
shift 2
;;
--agent-alias)
agent_alias="$2"
shift 2
;;
--install-dir)
install_dir="$2"
shift 2
;;
--config-path)
config_path="$2"
shift 2
;;
--workspace-root)
workspace_root="$2"
shift 2
;;
--log-dir)
log_dir="$2"
shift 2
;;
--no-background)
background="false"
shift 1
;;
*)
echo "Unknown option: $1" >&2
usage
exit 1
;;
esac
done
if [[ -z "$server_url" || -z "$agent_id" || -z "$enrollment_token" || -z "$release_base_url" ]]; then
echo "Error: Missing required arguments." >&2
usage
exit 1
fi
case "$release_base_url" in
https://*) ;;
*)
echo "Error: --release-base-url must use https://" >&2
exit 1
;;
esac
os=$(uname -s)
case "$os" in
Linux) os_name="linux" ;;
Darwin) os_name="macos" ;;
*)
echo "Error: Unsupported OS: $os" >&2
exit 1
;;
esac
arch=$(uname -m)
case "$arch" in
x86_64|amd64) arch_name="x64" ;;
aarch64|arm64) arch_name="arm64" ;;
*)
echo "Error: Unsupported architecture: $arch" >&2
exit 1
;;
esac
asset_name="oto-${os_name}-${arch_name}.tar.gz"
download_url="${release_base_url}/${asset_name}"
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
echo "Downloading OTO agent..."
curl -fsSL "$download_url" -o "$tmp_dir/$asset_name"
echo "Extracting OTO agent..."
tar -xzf "$tmp_dir/$asset_name" -C "$tmp_dir"
mkdir -p "$install_dir"
cp "$tmp_dir/oto" "$install_dir/oto"
chmod +x "$install_dir/oto"
echo "Verifying OTO binary..."
if ! "$install_dir/oto" --version > /dev/null 2>&1; then
echo "Error: OTO binary verification failed." >&2
exit 1
fi
config_dir=$(dirname "$config_path")
agent_state_dir="${HOME}/.oto/agent"
pid_path="${agent_state_dir}/oto-agent.pid"
mkdir -p "$config_dir"
mkdir -p "$agent_state_dir"
mkdir -p "$workspace_root"
mkdir -p "$log_dir"
cat <<EOF > "$config_path"
agent:
id: "$(yaml_escape "$agent_id")"
alias: "$(yaml_escape "$agent_alias")"
enrollment_token: "$(yaml_escape "$enrollment_token")"
server:
url: "$(yaml_escape "$server_url")"
runtime:
install_dir: "$(yaml_escape "$install_dir")"
workspace_root: "$(yaml_escape "$workspace_root")"
log_dir: "$(yaml_escape "$log_dir")"
EOF
chmod 600 "$config_path"
if [ "$background" = "true" ]; then
echo "Starting OTO agent in the background..."
nohup "$install_dir/oto" agent run --config "$config_path" >> "$log_dir/oto-agent.log" 2>&1 &
pid=$!
echo "$pid" > "$pid_path"
echo "OTO agent started in the background. PID: $pid"
echo "Log path: $log_dir/oto-agent.log"
else
echo "OTO agent is configured to run in the foreground."
echo "To run it manually, use the following command:"
echo " $install_dir/oto agent run --config $config_path"
fi