-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvendor-to-netdata.sh
More file actions
executable file
·158 lines (137 loc) · 6.56 KB
/
Copy pathvendor-to-netdata.sh
File metadata and controls
executable file
·158 lines (137 loc) · 6.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
#!/usr/bin/env bash
#
# vendor-to-netdata.sh — copy netipc library sources into a Netdata repo.
#
# Usage:
# ./vendor-to-netdata.sh [netdata-repo-root] (defaults to .)
#
# Copies C, Rust, and Go sources from this plugin-ipc repo into the
# corresponding paths inside a Netdata checkout. Does NOT touch
# Netdata-specific wrapper files (netipc_netdata.c/h) or build system
# files — those are maintained in the Netdata repo.
#
# All C files are included (including Windows transport). The build
# system decides what to compile per platform.
set -euo pipefail
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' GRAY='\033[0;90m' NC='\033[0m'
run() {
printf >&2 "${GRAY}$(pwd) >${NC} ${YELLOW}"; printf >&2 "%q " "$@"; printf >&2 "${NC}\n"
if ! "$@"; then
local exit_code=$?
echo -e >&2 "${RED}[ERROR]${NC} Exit code ${exit_code}: ${YELLOW}$*${NC} (in $(pwd))"
return $exit_code
fi
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SRC="$SCRIPT_DIR/src"
DST="$(cd "${1:-.}" && pwd)"
if [ "$SCRIPT_DIR" = "$DST" ]; then
echo -e >&2 "${RED}[ERROR]${NC} Source and destination are the same directory: $DST"
exit 1
fi
if [ ! -d "$DST/src/libnetdata" ]; then
echo -e >&2 "${RED}[ERROR]${NC} $DST does not look like a Netdata repo (missing src/libnetdata/)"
exit 1
fi
# ── Rust crate (entire src/ tree + Cargo files) ──────────────────────
# ── Go package (entire pkg/netipc/ tree + go.mod) ────────────────────
echo -e "${GREEN}Vendoring netipc from${NC} $SRC"
echo -e "${GREEN}Vendoring netipc to${NC} $DST"
echo ""
# ── C ────────────────────────────────────────────────────────────────
echo -e "${YELLOW}=== C headers ===${NC}"
run mkdir -p "$DST/src/libnetdata/netipc/include/netipc"
run rsync -a --delete \
"$SRC/libnetdata/netipc/include/netipc/" \
"$DST/src/libnetdata/netipc/include/netipc/"
echo -e "${YELLOW}=== C sources ===${NC}"
run mkdir -p "$DST/src/libnetdata/netipc/src"
run rsync -a --delete \
"$SRC/libnetdata/netipc/src/" \
"$DST/src/libnetdata/netipc/src/"
# ── Rust ─────────────────────────────────────────────────────────────
# Only the src/ tree is synced. Cargo.toml and Cargo.lock are NOT
# copied because the Netdata repo has its own workspace-level Cargo
# config with different path dependencies.
echo -e "${YELLOW}=== Rust crate ===${NC}"
RUST_SRC="$SRC/crates/netipc"
RUST_DST="$DST/src/crates/netipc"
run mkdir -p "$RUST_DST"
# Format before copying
if command -v cargo >/dev/null 2>&1; then
echo -e " ${GRAY}running cargo fmt...${NC}"
(cd "$RUST_SRC" && cargo fmt --quiet 2>/dev/null) || true
else
echo -e " ${YELLOW}cargo not found — skipping Rust formatting${NC}"
fi
# Sync the Rust src/ tree (preserves structure, deletes stale files)
run rsync -a --delete \
--exclude='target/' \
"$RUST_SRC/src/" "$RUST_DST/src/"
# ── Go ───────────────────────────────────────────────────────────────
# Only pkg/netipc/ is synced. go.mod is NOT copied because the Netdata
# repo has its own module path and dependencies. After syncing, Go
# import paths (github.com/netdata/plugin-ipc/go/...) must be adjusted
# to match the Netdata module path if they differ.
echo -e "${YELLOW}=== Go package ===${NC}"
GO_SRC="$SRC/go"
GO_DST="$DST/src/go"
run mkdir -p "$GO_DST/pkg/netipc"
if [ ! -f "$GO_SRC/go.mod" ]; then
echo -e >&2 "${RED}[ERROR]${NC} Missing required file: $GO_SRC/go.mod"
exit 1
fi
# Format before copying
if command -v gofmt >/dev/null 2>&1; then
echo -e " ${GRAY}running gofmt...${NC}"
find "$GO_SRC/pkg/netipc" -name '*.go' -exec gofmt -w {} +
else
echo -e " ${YELLOW}gofmt not found — skipping Go formatting${NC}"
fi
# Sync only the netipc package (not the entire pkg/ tree — Netdata
# has many other Go packages under src/go/pkg/ that must not be touched).
run rsync -a --delete \
"$GO_SRC/pkg/netipc/" "$GO_DST/pkg/netipc/"
# ── Summary ──────────────────────────────────────────────────────────
echo ""
C_COUNT=$(find "$SRC/libnetdata/netipc/include/netipc" "$SRC/libnetdata/netipc/src" -type f \( -name '*.c' -o -name '*.h' \) | wc -l)
RUST_COUNT=$(find "$RUST_SRC/src" -type f -name '*.rs' | wc -l)
GO_COUNT=$(find "$GO_SRC/pkg/netipc" -type f -name '*.go' | wc -l)
echo -e "${GREEN}Done.${NC} Vendored ${C_COUNT} C files, ${RUST_COUNT} Rust files, ${GO_COUNT} Go files."
echo ""
# ── Post-vendor instructions ─────────────────────────────────────────
GO_SRC_MODULE=$(awk '$1=="module"{print $2; exit}' "$GO_SRC/go.mod")
if [ -z "$GO_SRC_MODULE" ]; then
echo -e >&2 "${RED}[ERROR]${NC} Invalid go.mod: could not find module path in $GO_SRC/go.mod"
exit 1
fi
GO_DST_MODULE=""
if [ -f "$GO_DST/go.mod" ]; then
GO_DST_MODULE=$(awk '$1=="module"{print $2; exit}' "$GO_DST/go.mod")
if [ -z "$GO_DST_MODULE" ]; then
echo -e "${YELLOW}[WARN]${NC} Could not parse module path from $GO_DST/go.mod; skipping auto import-path suggestion"
fi
fi
echo -e "${YELLOW}=== Next steps ===${NC}"
echo ""
echo " 1. Fix Go import paths:"
if [ -n "$GO_DST_MODULE" ] && [ "$GO_SRC_MODULE" != "$GO_DST_MODULE" ]; then
echo -e " ${GRAY}cd $DST${NC}"
echo -e " ${GRAY}find src/go/pkg/netipc -name '*.go' -exec sed -i.bak 's|${GO_SRC_MODULE}|${GO_DST_MODULE}|g' {} +${NC}"
echo -e " ${GRAY}git diff -- src/go/pkg/netipc${NC}"
echo -e " ${GRAY}find src/go/pkg/netipc -name '*.bak' -delete${NC}"
else
echo -e " ${GRAY}(check if Go module path differs between repos and sed-replace)${NC}"
fi
echo ""
echo " 2. Verify C build integration:"
echo " - Ensure CMakeLists.txt includes any new .c files"
echo " - Netdata wrapper files (netipc_netdata.c/h) are NOT overwritten"
echo ""
echo " 3. Verify Rust build integration:"
echo " - Ensure Cargo.toml in the Netdata workspace references any new"
echo " dependencies or features added in the plugin-ipc crate"
echo ""
echo " 4. Build and test:"
echo -e " ${GRAY}cd $DST && cmake --build build && go test ./src/go/pkg/netipc/...${NC}"
echo ""