-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·198 lines (160 loc) · 7.56 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·198 lines (160 loc) · 7.56 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
# setup.sh — One-time environment setup for DataCollector development.
#
# What this script does:
# 1. Verifies or installs Go (>= MIN_GO_VERSION) to /usr/local/go
# 2. Adds Go and GOPATH/bin to PATH for the current shell and ~/.bashrc
# 3. Installs the OCB (OpenTelemetry Collector Builder) binary at the
# version required to build the asap-otel distribution
# 4. Initialises git submodules (if not already done)
#
# Usage:
# ./setup.sh # full setup
# ./setup.sh --no-go # skip Go installation (if already managed externally)
#
# Safe to re-run: each step is skipped if already satisfied.
set -euo pipefail
# ─── Versions ────────────────────────────────────────────────────────────────
# Minimum Go version required by opentelemetry-collector-contrib (go 1.25.4).
MIN_GO_VERSION="1.25.4"
# Go toolchain version to install when Go is absent or below the minimum.
GO_INSTALL_VERSION="1.26.0"
# OCB version that matches the asap-otel distribution target (v0.141.0).
OCB_VERSION="0.141.0"
# ─────────────────────────────────────────────────────────────────────────────
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_GO=true
# Parse flags
for arg in "$@"; do
case "$arg" in
--no-go) INSTALL_GO=false ;;
*) echo "Unknown argument: $arg" >&2; exit 1 ;;
esac
done
# ─── Helpers ─────────────────────────────────────────────────────────────────
info() { echo " $*"; }
success() { echo "[OK] $*"; }
section() { echo; echo "==> $*"; }
# Compare two semver strings; returns 0 if $1 >= $2.
version_ge() {
# Use sort -V (version sort) to determine order.
[ "$(printf '%s\n%s' "$1" "$2" | sort -V | head -1)" = "$2" ]
}
# Detect machine architecture and return the Go download suffix.
go_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64) echo "amd64" ;;
aarch64|arm64) echo "arm64" ;;
armv6l) echo "armv6l" ;;
i686) echo "386" ;;
*) echo "Unsupported architecture: $arch" >&2; exit 1 ;;
esac
}
# ─── Step 1: Go ──────────────────────────────────────────────────────────────
section "Checking Go installation"
if [[ "$INSTALL_GO" == false ]]; then
info "Skipping Go installation (--no-go)."
else
CURRENT_GO_VERSION=""
if command -v go &>/dev/null; then
CURRENT_GO_VERSION="$(go version | grep -oP '\d+\.\d+(\.\d+)?' | head -1)"
info "Found go ${CURRENT_GO_VERSION} at $(command -v go)"
else
info "Go not found on PATH."
fi
if [[ -n "$CURRENT_GO_VERSION" ]] && version_ge "$CURRENT_GO_VERSION" "$MIN_GO_VERSION"; then
success "Go ${CURRENT_GO_VERSION} satisfies minimum ${MIN_GO_VERSION}. Skipping installation."
else
ARCH="$(go_arch)"
TARBALL="go${GO_INSTALL_VERSION}.linux-${ARCH}.tar.gz"
DOWNLOAD_URL="https://dl.google.com/go/${TARBALL}"
info "Downloading Go ${GO_INSTALL_VERSION} (${ARCH}) from ${DOWNLOAD_URL} ..."
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT
curl -fsSL "${DOWNLOAD_URL}" -o "${TMP_DIR}/${TARBALL}"
info "Installing to /usr/local/go (requires sudo) ..."
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf "${TMP_DIR}/${TARBALL}"
success "Go ${GO_INSTALL_VERSION} installed to /usr/local/go"
fi
fi
# ─── Step 2: PATH setup ──────────────────────────────────────────────────────
section "Setting up PATH"
GO_BIN="/usr/local/go/bin"
GOPATH_BIN="$(go env GOPATH 2>/dev/null || echo "${HOME}/go")/bin"
# Export for the current shell session
export PATH="${GO_BIN}:${GOPATH_BIN}:${PATH}"
# Persist to ~/.bashrc if not already present
BASHRC="${HOME}/.bashrc"
ADDED_TO_BASHRC=false
add_to_bashrc() {
local line="$1"
if ! grep -qxF "$line" "${BASHRC}" 2>/dev/null; then
echo "$line" >> "${BASHRC}"
ADDED_TO_BASHRC=true
fi
}
add_to_bashrc "export PATH=\"${GO_BIN}:\${PATH}\""
add_to_bashrc "export PATH=\"${GOPATH_BIN}:\${PATH}\""
if [[ "$ADDED_TO_BASHRC" == true ]]; then
info "Added Go paths to ${BASHRC}"
info "Run 'source ~/.bashrc' (or open a new terminal) to pick them up permanently."
else
info "Go paths already present in ${BASHRC}"
fi
success "PATH: ${GO_BIN} and ${GOPATH_BIN} are active for this session."
# Verify Go is usable
go version
# ─── Step 3: OCB (OpenTelemetry Collector Builder) ───────────────────────────
section "Checking OCB (OpenTelemetry Collector Builder) v${OCB_VERSION}"
OCB_BINARY="${GOPATH_BIN}/builder"
NEED_OCB=true
if [[ -x "${OCB_BINARY}" ]]; then
INSTALLED_OCB="$("${OCB_BINARY}" version 2>&1 | grep -oP 'v[\d.]+' | head -1 || true)"
if [[ "${INSTALLED_OCB}" == "v${OCB_VERSION}" ]]; then
success "OCB ${INSTALLED_OCB} already installed at ${OCB_BINARY}."
NEED_OCB=false
else
info "Found OCB ${INSTALLED_OCB:-unknown} — need v${OCB_VERSION}. Reinstalling."
fi
fi
if [[ "$NEED_OCB" == true ]]; then
info "Installing OCB v${OCB_VERSION} ..."
GOBIN="${GOPATH_BIN}" go install \
"go.opentelemetry.io/collector/cmd/builder@v${OCB_VERSION}"
success "OCB v${OCB_VERSION} installed at ${OCB_BINARY}."
fi
# ─── Step 4: Git submodules ──────────────────────────────────────────────────
section "Initialising git submodules"
cd "${ROOT_DIR}"
# Check whether the OTel submodules required by the MVP are populated.
if [[ ! -f "${ROOT_DIR}/opentelemetry-collector/go.mod" ]]; then
info "Submodules not yet initialised. Running git submodule update --init --recursive ..."
git submodule update --init opentelemetry-collector opentelemetry-collector-contrib opentelemetry-go opentelemetry-proto
success "Submodules initialised."
else
info "Submodules already initialised."
# Still update in case the recorded commit has advanced
info "Syncing submodules to recorded commits ..."
git submodule update opentelemetry-collector opentelemetry-collector-contrib opentelemetry-go opentelemetry-proto
success "Submodules up-to-date."
fi
# ─── Done ────────────────────────────────────────────────────────────────────
echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Setup complete."
echo ""
echo " Go: $(go version)"
echo " OCB: $("${OCB_BINARY}" version 2>&1)"
echo " Builder: ${OCB_BINARY}"
echo ""
echo " To build asap-otel:"
echo " ./build_asap_otel.sh"
echo ""
if [[ "$ADDED_TO_BASHRC" == true ]]; then
echo " NOTE: Run 'source ~/.bashrc' to make Go available in new terminals."
echo ""
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"