-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporker
More file actions
executable file
·194 lines (170 loc) · 5.55 KB
/
Copy pathreporker
File metadata and controls
executable file
·194 lines (170 loc) · 5.55 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
#!/usr/bin/env bash
# reporker — scan and change files across a GitLab group
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ANSIBLE_DIR="$ROOT/ansible"
PLAYBOOK="$ANSIBLE_DIR/playbooks/run.yml"
INVENTORY="$ANSIBLE_DIR/inventory.ini"
CONFIG="$ANSIBLE_DIR/group_vars/all.yml"
CONFIG_EXAMPLE="$ANSIBLE_DIR/group_vars/all.yml.example"
TOKEN_FILE="$ROOT/glab/token"
# Extra args forwarded to ansible-playbook (filled by --dry-run and `-- ...`).
EXTRA_ARGS=()
usage() {
cat <<'EOF'
reporker — scan and change files across every repo in a GitLab group
Usage:
reporker init Create local config from the example
reporker check Verify tools, config, and token are ready
reporker clone Discover repos and clone/update them
reporker scan Find target files (writes ansible/reports/04-scan.json)
reporker action Scan, run your action, write reports
reporker publish Branch, commit, and push changed repos
reporker run clone → action (audit or modify, no push)
reporker all Full pipeline including publish
Flags:
--dry-run Preview changes without writing them (ansible --check --diff)
-- <args...> Forward the rest straight to ansible-playbook
Setup (once):
1. reporker init
2. Edit ansible/group_vars/all.yml (GitLab host, group ID, action)
3. printf '%s' 'glpat-xxx' > glab/token && chmod 600 glab/token
4. reporker check
Examples:
reporker clone && reporker action # audit a group (read-only action)
reporker run # same, from scratch
reporker action --dry-run # preview a write action's changes
reporker action && reporker publish # apply changes and open branches
reporker action -- -e reporker_action.name=grep # override config on the fly
Config: ansible/group_vars/all.yml
Reports: ansible/reports/01-summary.txt (start here)
Docs: ansible/actions/README.md
EOF
}
die() {
echo "reporker: $*" >&2
exit 1
}
require_config() {
[[ -f "$CONFIG" ]] || die "config not found — run: reporker init"
}
require_ansible() {
command -v ansible-playbook >/dev/null 2>&1 \
|| die "ansible-playbook not found (need Ansible ≥ 2.14)"
}
# Split command flags from playbook pass-through args.
# Sets the global EXTRA_ARGS array.
parse_flags() {
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
EXTRA_ARGS+=(--check --diff)
shift
;;
--)
shift
EXTRA_ARGS+=("$@")
break
;;
*)
die "unknown option: $1 (try: reporker --help)"
;;
esac
done
}
run_playbook() {
local tags="$1"
require_ansible
require_config
(
cd "$ANSIBLE_DIR"
ansible-playbook -i "$INVENTORY" "$PLAYBOOK" --tags "$tags" "${EXTRA_ARGS[@]}"
)
}
cmd_init() {
mkdir -p "$ROOT/glab" "$ANSIBLE_DIR/group_vars"
if [[ ! -f "$CONFIG" ]]; then
cp "$CONFIG_EXAMPLE" "$CONFIG"
echo "Created ansible/group_vars/all.yml"
else
echo "Config already exists: ansible/group_vars/all.yml"
fi
if [[ ! -f "$TOKEN_FILE" ]]; then
cat <<EOF
Next steps:
1. Edit ansible/group_vars/all.yml
2. Store your GitLab token (this is the only auth step):
printf '%s' 'glpat-xxx' > glab/token && chmod 600 glab/token
3. Check your setup:
reporker check
4. Run: reporker clone && reporker action
EOF
fi
}
# Print a single check line: check_item "label" "command-to-test"
check_item() {
local label="$1"; shift
if "$@" >/dev/null 2>&1; then
echo " [ok] $label"
return 0
fi
echo " [--] $label"
return 1
}
cmd_check() {
local fails=0
echo "Tools:"
check_item "ansible-playbook" command -v ansible-playbook || fails=$((fails + 1))
check_item "glab" command -v glab || fails=$((fails + 1))
check_item "git" command -v git || fails=$((fails + 1))
check_item "jq" command -v jq || fails=$((fails + 1))
echo "Config:"
if [[ -f "$CONFIG" ]]; then
echo " [ok] ansible/group_vars/all.yml exists"
if grep -qE '^\s*group_id:\s*12345\s*$' "$CONFIG"; then
echo " [--] group_id is still the example value (12345) — edit it"
fails=$((fails + 1))
else
echo " [ok] group_id has been set"
fi
else
echo " [--] config missing — run: reporker init"
fails=$((fails + 1))
fi
echo "Token:"
if [[ -s "$TOKEN_FILE" ]]; then
echo " [ok] glab/token exists"
else
echo " [--] glab/token missing or empty — printf '%s' 'glpat-xxx' > glab/token"
fails=$((fails + 1))
fi
echo
if [[ "$fails" -eq 0 ]]; then
echo "All checks passed. You're ready: reporker clone && reporker action"
else
echo "$fails check(s) failed. Fix the items marked [--] above."
return 1
fi
}
main() {
local cmd="${1:-}"
shift || true
case "$cmd" in
init) cmd_init ;;
check) cmd_check ;;
doctor) echo "reporker: 'doctor' was renamed to 'check'" >&2; cmd_check ;;
clone) parse_flags "$@"; run_playbook "discovery,workspace" ;;
scan) parse_flags "$@"; run_playbook "scan" ;;
action) parse_flags "$@"; run_playbook "scan,action,report" ;;
publish) parse_flags "$@"; run_playbook "publish" ;;
run) parse_flags "$@"; run_playbook "discovery,workspace,scan,action,report" ;;
all) parse_flags "$@"; run_playbook "discovery,workspace,scan,action,report,publish" ;;
-h|--help|help|"")
usage
;;
*)
die "unknown command: $cmd (try: reporker --help)"
;;
esac
}
main "$@"