forked from Dstack-TEE/dstack-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·268 lines (223 loc) · 6.63 KB
/
dev.sh
File metadata and controls
executable file
·268 lines (223 loc) · 6.63 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
# Dstack Examples - Development Helper Script
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXAMPLES_DIR="${SCRIPT_DIR}"
# Helper functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Find all docker-compose files
find_compose_files() {
find "${EXAMPLES_DIR}" -name "docker-compose.y*ml" -not -path "./.github/*" -not -path "./.git/*"
}
# Show help
show_help() {
cat << EOF
Dstack Examples Development Helper
Usage: $0 <command> [options]
Commands:
list List all examples
validate <example> Validate a specific example
validate-all Validate all examples
security Run security checks
lint Run linting (shellcheck, yamllint)
check-all Run all checks (validate, security, lint)
docs Show documentation status
help Show this help message
Examples:
$0 validate attestation/configid-based
$0 check-all
EOF
}
# List examples
list_examples() {
log_info "Available examples:"
find_compose_files | xargs dirname | sort | uniq | sed 's/^/ - /'
}
# Validate structure
validate_structure() {
local example=$1
if [ ! -d "${example}" ]; then
log_error "Example directory not found: ${example}"
return 1
fi
if [ ! -f "${example}/README.md" ]; then
log_error "Missing README.md in: ${example}"
return 1
fi
if [ ! -f "${example}/docker-compose.yml" ] && [ ! -f "${example}/docker-compose.yaml" ]; then
log_error "Missing docker-compose file in: ${example}"
return 1
fi
log_success "Structure validation passed"
return 0
}
# Validate compose syntax
validate_compose() {
local example=$1
log_info "Validating Docker Compose syntax..."
if cd "${example}" && docker compose config --quiet > /dev/null 2>&1; then
log_success "Docker Compose syntax is valid"
return 0
else
log_error "Docker Compose syntax is invalid"
return 1
fi
}
# Validate single example
validate_example() {
local example=$1
if [ -z "${example}" ]; then
log_error "Please specify an example: $0 validate <path/to/example>"
exit 1
fi
log_info "Validating example: ${example}"
local failed=0
validate_structure "${example}" || failed=$((failed + 1))
validate_compose "${example}" || failed=$((failed + 1))
if [ ${failed} -eq 0 ]; then
log_success "All validations passed for: ${example}"
return 0
else
log_error "${failed} validation(s) failed for: ${example}"
return 1
fi
}
# Validate all examples
validate_all() {
log_info "Validating all examples..."
local failed=0
find_compose_files | while read -r compose_file; do
example_dir=$(dirname "${compose_file}")
echo ""
validate_example "${example_dir}" || failed=$((failed + 1))
done
if [ ${failed} -eq 0 ]; then
log_success "All examples validated successfully!"
else
log_error "${failed} example(s) failed validation"
fi
}
# Security checks
security_checks() {
log_info "Running security checks..."
# Check for hardcoded secrets
log_info "Scanning for potential secrets..."
if grep -r -i -E "(password|secret|key|token|api_key).*=.*['\"][^'\"]{8,}['\"]" "${EXAMPLES_DIR}" \
--exclude-dir=.git --exclude-dir=.github --exclude="*.md" --exclude="Makefile" --exclude="dev.sh" 2>/dev/null; then
log_warning "Potential hardcoded secrets found!"
else
log_success "No obvious hardcoded secrets detected"
fi
# Check Docker Compose security
log_info "Checking Docker Compose security..."
find_compose_files | while read -r compose_file; do
if grep -q "privileged.*true" "${compose_file}"; then
log_error "Privileged container found in: ${compose_file}"
fi
if grep -q "network_mode.*host" "${compose_file}"; then
log_warning "Host network mode found in: ${compose_file}"
fi
if grep -q "/var/run/docker.sock" "${compose_file}"; then
log_error "Docker socket mount found in: ${compose_file}"
fi
done
log_success "Security checks completed"
}
# Lint checks
lint_checks() {
log_info "Running lint checks..."
# Shellcheck
if command -v shellcheck >/dev/null 2>&1; then
log_info "Linting shell scripts..."
find "${EXAMPLES_DIR}" -name "*.sh" -not -path "./.git/*" -exec shellcheck {} \; || log_warning "ShellCheck issues found"
else
log_warning "shellcheck not installed, skipping shell script linting"
fi
# Yamllint
if command -v yamllint >/dev/null 2>&1; then
log_info "Linting YAML files..."
find "${EXAMPLES_DIR}" -name "*.yml" -o -name "*.yaml" | grep -v ".git" | xargs yamllint || log_warning "YAML linting issues found"
else
log_warning "yamllint not installed, skipping YAML linting"
fi
log_success "Lint checks completed"
}
# Documentation status
docs_status() {
log_info "Documentation status:"
echo ""
find_compose_files | while read -r compose_file; do
example_dir=$(dirname "${compose_file}")
if [ -f "${example_dir}/README.md" ]; then
echo -e " ${GREEN}✓${NC} ${example_dir}"
else
echo -e " ${RED}✗${NC} ${example_dir} (missing README.md)"
fi
done
}
# Run all checks
check_all() {
log_info "Running comprehensive checks..."
echo ""
validate_all
echo ""
security_checks
echo ""
lint_checks
echo ""
log_success "All checks completed!"
}
# Main script logic
main() {
case "${1:-help}" in
list)
list_examples
;;
validate)
validate_example "${2:-}"
;;
validate-all)
validate_all
;;
security)
security_checks
;;
lint)
lint_checks
;;
check-all)
check_all
;;
docs)
docs_status
;;
help|--help|-h)
show_help
;;
*)
log_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
# Run main function
main "$@"