-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.sh
More file actions
124 lines (97 loc) · 3.94 KB
/
commands.sh
File metadata and controls
124 lines (97 loc) · 3.94 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
# --- Environment Setup ---
sudo apt update
sudo apt install -y python3 python3-pip python3-venv
mkdir -p ~/config-generator
cd ~/config-generator
python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install jinja2 pyyaml jsonschema
# --- Step 1: Create Project Structure ---
cd ~/config-generator
mkdir -p templates configs data schemas
touch generator.py validator.py
ls -la
# --- Step 2: Create Configuration Templates ---
nano templates/nginx.conf.j2
# --- Environment: {{ environment }} ---
ls templates/
nano templates/app_config.yaml.j2
# --- Application Configuration ---
ls templates/
# --- Step 3: Create Sample Data Files ---
nano data/dev_config.yaml
cat data/dev_config.yaml
nano data/prod_config.yaml
cat data/prod_config.yaml
# --- Step 4: Implement Configuration Generator ---
nano generator.py
chmod +x generator.py
ls -l generator.py
# --- Step 5: Create Validation Schema ---
nano schemas/app_config_schema.json
jq . schemas/app_config_schema.json
# --- Step 6: Implement Configuration Validator ---
nano validator.py
chmod +x validator.py
ls -l validator.py
# --- Step 1: Complete the Generator Implementation ---
python3 -m py_compile generator.py
echo $?
# --- Step 2: Complete the Validator Implementation ---
python3 -m py_compile validator.py
echo $?
# --- Step 3: Generate Development Configurations ---
python3 generator.py data/dev_config.yaml
ls -lh configs/
cat configs/nginx.conf
# --- Environment: development ---
cat configs/app_config.yaml
# --- Step 4: Generate Production Configurations ---
python3 generator.py data/prod_config.yaml
cp configs/nginx.conf configs/nginx_prod.conf
python3 generator.py data/dev_config.yaml nginx.conf.j2
cp configs/nginx.conf configs/nginx_dev.conf
diff -u configs/nginx_dev.conf configs/nginx_prod.conf
python3 generator.py data/prod_config.yaml
# --- Step 5: Validate Generated Configurations ---
python3 validator.py configs/app_config.yaml app_config_schema.json
python3 validator.py configs/nginx.conf nginx
# --- Step 6: Test Edge Cases ---
nano data/invalid_config.yaml
# --- Missing version field ---
cat data/invalid_config.yaml
cp configs/app_config.yaml configs/app_config.valid.bak
python3 generator.py data/invalid_config.yaml app_config.yaml.j2
cat configs/app_config.yaml
# --- Application Configuration ---
python3 validator.py configs/app_config.yaml app_config_schema.json
mv configs/app_config.valid.bak configs/app_config.yaml
python3 validator.py configs/app_config.yaml app_config_schema.json
# --- Verify Generator Functionality ---
test -f configs/nginx.conf && echo "Nginx config: OK" || echo "Nginx config: MISSING"
test -f configs/app_config.yaml && echo "App config: OK" || echo "App config: MISSING"
grep "myapp" configs/nginx.conf && echo "App name found: OK"
# --- Nginx Configuration for myapp ---
grep "environment: production" configs/app_config.yaml && echo "Environment set: OK"
# --- Verify Validator Functionality ---
python3 validator.py configs/app_config.yaml app_config_schema.json
echo "Exit code: $?"
grep -c "server {" configs/nginx.conf
grep -c "location" configs/nginx.conf
# --- Create Verification Script ---
nano verify_lab.sh
# --- Check file content ---
chmod +x verify_lab.sh
./verify_lab.sh
# --- Issue: Jinja2 template not found ---
python3 -c "from generator import ConfigGenerator; g=ConfigGenerator(); print(sorted(g.env.list_templates()))"
# --- Issue: YAML parsing errors ---
python3 -c "import yaml; yaml.safe_load(open('data/dev_config.yaml'))"
echo $?
# --- Issue: Validation fails unexpectedly ---
python3 -c "import yaml; from pprint import pprint; pprint(yaml.safe_load(open('configs/app_config.yaml')))"
# --- Issue: Generated config has extra whitespace ---
python3 -c "from generator import ConfigGenerator; g=ConfigGenerator(); data=g.load_data('data/prod_config.yaml'); print(g.generate_config('nginx.conf.j2', data, output_file='nginx.test.conf'))"
# --- Final Directory Snapshot ---
find . -maxdepth 2 -type f | sort