Files
linux-sysadmin-journey/projects/02-command-line-mastery/setup/level1-setup.sh
T

178 lines
4.7 KiB
Bash
Raw Normal View History

2025-02-09 02:01:58 -06:00
#!/bin/bash
# Import common functions and variables
source "$(dirname "$0")/common.sh"
# Challenge-specific setup
setup_file_challenges() {
log_message "Setting up file system challenges..."
# Create complex directory structure
for dir in {1..5}; do
mkdir -p "$CHALLENGE_DIR/files/project$dir/{src,config,logs,data}"
# Create various file types
touch "$CHALLENGE_DIR/files/project$dir/src/main.py"
touch "$CHALLENGE_DIR/files/project$dir/src/test.py"
echo "debug=true" > "$CHALLENGE_DIR/files/project$dir/config/settings.conf"
# Create log files with realistic content
for severity in INFO WARN ERROR DEBUG; do
for i in {1..10}; do
echo "[$(date -d "@$(($(date +%s) - RANDOM % 86400))" '+%Y-%m-%d %H:%M:%S')] [$severity] Message $i" >> "$CHALLENGE_DIR/files/project$dir/logs/app.log"
done
done
done
}
setup_permission_challenges() {
log_message "Setting up permission challenges..."
# Create test users and groups
for i in {1..3}; do
sudo groupadd -f "app_group$i" 2>/dev/null
sudo useradd -M -N -g "app_group$i" "app_user$i" 2>/dev/null
done
# Create files with specific permissions
mkdir -p "$CHALLENGE_DIR/files/permissions_test"
for i in {1..5}; do
echo "content$i" > "$CHALLENGE_DIR/files/permissions_test/file$i.txt"
chmod $((600 + i)) "$CHALLENGE_DIR/files/permissions_test/file$i.txt"
done
}
create_verification_functions() {
cat << 'EOF' > "$CHALLENGE_DIR/.tracking/verify-challenges.sh"
#!/bin/bash
source "$(dirname "$0")/common.sh"
# Verification functions for each challenge
verify_file_search() {
local challenge_num=$1
local description=$2
local command=$3
local expected=$4
log_message "Verifying challenge $challenge_num: $description"
local result
result=$(eval "$command")
if [[ "$result" == "$expected" ]]; then
award_points $challenge_num 100
return 0
fi
return 1
}
verify_permission_setup() {
local file=$1
local expected_perms=$2
local expected_owner=$3
local actual_perms=$(stat -c "%a" "$file")
local actual_owner=$(stat -c "%U:%G" "$file")
[[ "$actual_perms" == "$expected_perms" && "$actual_owner" == "$expected_owner" ]]
}
# Challenge verification functions
verify_challenge_1() {
# Find recent conf files
verify_file_search 1 "Find recent configuration files" \
"find \$CHALLENGE_DIR/files -name '*.conf' -mtime -1 | wc -l" \
"10"
}
verify_challenge_2() {
# Count ERROR entries in logs
verify_file_search 2 "Count ERROR messages" \
"grep -r 'ERROR' \$CHALLENGE_DIR/files/*/logs | wc -l" \
"15"
}
# Add more verification functions...
EOF
}
create_challenge_tracker() {
cat << 'EOF' > "$CHALLENGE_DIR/.tracking/challenge-tracker.sh"
#!/bin/bash
source "$(dirname "$0")/common.sh"
# Track command usage
track_command() {
local command=$1
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $command" >> "$CHALLENGE_DIR/.tracking/commands.log"
}
# Track time spent
track_time() {
local challenge=$1
local start_time=$2
local end_time=$3
local duration=$((end_time - start_time))
echo "$challenge,$duration" >> "$CHALLENGE_DIR/.tracking/time.log"
}
# Show challenge hints
show_hint() {
local challenge=$1
case $challenge in
1) echo "Try using 'find' with -mtime flag" ;;
2) echo "Consider 'grep' with -r flag" ;;
*) echo "No hint available" ;;
esac
}
# Show challenge status
show_status() {
local total_score=0
local completed=0
echo "Challenge Status:"
echo "----------------"
while IFS=, read -r challenge score; do
total_score=$((total_score + score))
[[ $score -gt 0 ]] && ((completed++))
printf "Challenge %s: %s points\n" "$challenge" "$score"
done < "$SCORE_FILE"
echo "----------------"
printf "Total Score: %s/1500\n" "$total_score"
printf "Completed: %s/10 challenges\n" "$completed"
}
EOF
}
# Main setup function
main() {
init_environment
setup_file_challenges
setup_permission_challenges
create_verification_functions
create_challenge_tracker
log_message "Challenge environment ready!"
cat << EOF
${GREEN}Level 1 Setup Complete!${NC}
Available commands:
- ${YELLOW}check-progress${NC}: Show current progress
- ${YELLOW}verify-challenge <num>${NC}: Verify specific challenge
- ${YELLOW}show-hint <num>${NC}: Get hint for challenge
- ${YELLOW}show-status${NC}: Display challenge status
Start with challenge 1: Find all recent configuration files
Good luck!
EOF
}
# Run setup
main