Skip to content

Commit 6ead817

Browse files
committed
feat: add automation improvements
- Switch to netresearch/ofelia image (fixes command syntax) - Auto-generate random passwords in make setup - Add make install command for CLI-based Moodle installation - Add make configure-cache for programmatic Valkey MUC setup - Mount scripts directory for automation tools - Fix sed delimiter in Makefile to handle special chars in passwords
1 parent 5b2696f commit 6ead817

File tree

3 files changed

+147
-6
lines changed

3 files changed

+147
-6
lines changed

Makefile

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ clone-moodle: ## Clone Moodle repository (shallow clone, tip only)
3535
@cd moodle && git branch
3636
@echo "✅ Moodle cloned successfully"
3737

38-
setup: ## Create .env file from template
38+
setup: ## Create .env file with random passwords
3939
@if [ -f ".env" ]; then \
4040
echo "⚠️ .env file already exists"; \
4141
read -p "Overwrite? [y/N] " confirm; \
@@ -44,9 +44,16 @@ setup: ## Create .env file from template
4444
exit 1; \
4545
fi; \
4646
fi
47-
cp .env.example .env
48-
@echo "⚠️ IMPORTANT: Edit .env and set secure passwords!"
49-
@echo " Required: DB_PASSWORD, DB_ROOT_PASSWORD, VALKEY_PASSWORD"
47+
@echo "📝 Creating .env file with random passwords..."
48+
@cp .env.example .env
49+
@DB_PASS=$$(openssl rand -base64 32); \
50+
DB_ROOT_PASS=$$(openssl rand -base64 32); \
51+
VALKEY_PASS=$$(openssl rand -base64 32); \
52+
sed -i "s|CHANGE_ME_SECURE_PASSWORD_HERE|$$DB_PASS|" .env; \
53+
sed -i "s|CHANGE_ME_SECURE_ROOT_PASSWORD_HERE|$$DB_ROOT_PASS|" .env; \
54+
sed -i "s|CHANGE_ME_SECURE_VALKEY_PASSWORD_HERE|$$VALKEY_PASS|" .env
55+
@echo "✅ .env file created with secure random passwords"
56+
@echo " Review and customize: nano .env"
5057

5158
start: ## Start all services (pulls pre-built image)
5259
docker compose $(COMPOSE_FILES) up -d
@@ -129,6 +136,42 @@ pull: ## Pull latest pre-built image from GHCR
129136
docker compose $(COMPOSE_FILES) pull moodle
130137
@echo "✅ Latest image pulled"
131138

139+
install: ## Run Moodle CLI installer
140+
@if [ ! -f ".env" ]; then \
141+
echo "❌ .env file not found. Run 'make setup' first."; \
142+
exit 1; \
143+
fi
144+
@echo "🚀 Installing Moodle via CLI..."
145+
@export $$(grep -v '^#' .env | xargs) && docker compose $(COMPOSE_FILES) exec -T moodle php admin/cli/install.php \
146+
--lang=en \
147+
--wwwroot=$${MOODLE_SITE_URL} \
148+
--dataroot=/var/moodledata \
149+
--dbtype=mariadb \
150+
--dbhost=database \
151+
--dbname=moodle \
152+
--dbuser=moodleuser \
153+
--dbpass=$${DB_PASSWORD} \
154+
--prefix=mdl_ \
155+
--fullname="Moodle LMS" \
156+
--shortname="Moodle" \
157+
--adminuser=admin \
158+
--adminpass=Admin123! \
159+
--adminemail=admin@example.com \
160+
--non-interactive \
161+
--agree-license
162+
@echo ""
163+
@echo "✅ Moodle installed successfully!"
164+
@echo " URL: http://localhost:8080"
165+
@echo " User: admin"
166+
@echo " Pass: Admin123!"
167+
@echo ""
168+
@echo "⚠️ IMPORTANT: Change the admin password immediately!"
169+
170+
configure-cache: ## Configure Valkey for Moodle Universal Cache (MUC)
171+
@echo "🔧 Configuring Valkey for application cache..."
172+
docker compose $(COMPOSE_FILES) exec -T moodle php /var/www/scripts/configure-valkey-cache.php
173+
@echo "✅ Valkey cache configured"
174+
132175
upgrade-moodle: ## Upgrade Moodle code to latest in branch
133176
@if [ ! -d "moodle" ]; then \
134177
echo "❌ Moodle directory not found. Run 'make clone-moodle' first."; \

compose.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ services:
2323
- moodledata:/var/moodledata:rw
2424
# Custom Moodle configuration
2525
- ./config/moodle-config.php:/var/www/html/config.php:ro
26+
# Scripts for automation
27+
- ./scripts:/var/www/scripts:ro
2628
environment:
2729
# Database
2830
- DB_TYPE=${DB_TYPE:-mariadb}
@@ -109,15 +111,15 @@ services:
109111

110112
# Ofelia Cron Scheduler
111113
ofelia:
112-
image: mcuadros/ofelia:latest
114+
image: ghcr.io/netresearch/ofelia:latest
113115
container_name: ${COMPOSE_PROJECT_NAME:-moodle45}_ofelia
114116
restart: unless-stopped
115117
networks:
116118
- backend
117119
volumes:
118120
# Docker socket for container orchestration
119121
- /var/run/docker.sock:/var/run/docker.sock:ro
120-
command: daemon --docker
122+
command: daemon
121123
depends_on:
122124
- moodle
123125

scripts/configure-valkey-cache.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* Configure Valkey/Redis as Moodle Universal Cache (MUC) store
5+
*
6+
* This script programmatically sets up Valkey for application caching
7+
* Run after Moodle installation: make configure-cache
8+
*/
9+
10+
define('CLI_SCRIPT', true);
11+
12+
require_once('/var/www/html/config.php');
13+
require_once($CFG->libdir.'/clilib.php');
14+
15+
// Get Valkey credentials from environment
16+
$valkey_host = getenv('VALKEY_HOST') ?: 'valkey';
17+
$valkey_port = getenv('VALKEY_PORT') ?: 6379;
18+
$valkey_password = getenv('VALKEY_PASSWORD') ?: '';
19+
20+
cli_heading('Configuring Valkey/Redis for Moodle Universal Cache (MUC)');
21+
22+
// Load cache configuration
23+
$factory = cache_factory::instance();
24+
$config = cache_config::instance();
25+
26+
// Define the Redis store instance
27+
$store_config = array(
28+
'name' => 'valkey',
29+
'plugin' => 'redis',
30+
'configuration' => array(
31+
'server' => $valkey_host . ':' . $valkey_port,
32+
'password' => $valkey_password,
33+
'prefix' => 'muc_',
34+
'serializer' => 1, // PHP serializer (1), or igbinary (2) if available
35+
'compressor' => 0, // No compression (0), gzip (1), or zstd (2)
36+
),
37+
'features' => 31, // All features enabled
38+
'modes' => 3, // Application (1) + Session (2) = 3
39+
'mappingsonly' => false,
40+
'class' => 'cachestore_redis',
41+
'default' => true,
42+
);
43+
44+
// Check if Valkey store already exists
45+
$stores = $config->get_all_stores();
46+
$valkey_exists = false;
47+
48+
foreach ($stores as $store) {
49+
if ($store['name'] === 'valkey') {
50+
$valkey_exists = true;
51+
cli_writeln('Valkey cache store already exists');
52+
break;
53+
}
54+
}
55+
56+
if (!$valkey_exists) {
57+
cli_writeln('Creating Valkey cache store...');
58+
59+
// Add the store via admin/cli/cfg.php approach
60+
// Note: We need to manipulate the cache config file directly
61+
$writer = cache_config_writer::instance();
62+
$writer->add_store_instance('valkey', 'redis', $store_config['configuration']);
63+
64+
cli_writeln('✅ Valkey cache store created successfully');
65+
cli_writeln(' Server: ' . $valkey_host . ':' . $valkey_port);
66+
cli_writeln(' Database: 1 (separate from sessions on db 0)');
67+
} else {
68+
cli_writeln('ℹ️ Valkey cache store already configured');
69+
}
70+
71+
// Set Valkey as default application cache
72+
cli_writeln('');
73+
cli_writeln('Setting Valkey as default application cache...');
74+
75+
$writer = cache_config_writer::instance();
76+
$writer->set_mode_mappings(array(
77+
cache_store::MODE_APPLICATION => array('valkey', 'default_application'),
78+
cache_store::MODE_SESSION => array('default_session'),
79+
cache_store::MODE_REQUEST => array('default_request'),
80+
));
81+
82+
cli_writeln('✅ Valkey configured as default application cache');
83+
84+
// Purge all caches to apply changes
85+
cli_writeln('');
86+
cli_writeln('Purging all caches...');
87+
purge_all_caches();
88+
89+
cli_writeln('✅ Cache configuration complete!');
90+
cli_writeln('');
91+
cli_writeln('Verification:');
92+
cli_writeln(' Visit: Site administration → Plugins → Caching → Configuration');
93+
cli_writeln(' You should see Valkey/Redis listed as a cache store');
94+
cli_writeln('');
95+
96+
exit(0);

0 commit comments

Comments
 (0)