Skip to content

Commit d120679

Browse files
committed
doc update
1 parent 8515f67 commit d120679

File tree

1 file changed

+114
-6
lines changed

1 file changed

+114
-6
lines changed

docs/app-installation-flow.md

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ Handles both `application/yaml` and `application/json` content types:
4949
- Compares against compose app port mappings
5050
- Returns validation errors if conflicts found
5151

52+
#### Pre-Install Command Execution (`executePreInstallScript()`)
53+
Before the actual installation begins, CasaOS checks for and executes optional pre-install commands:
54+
- Looks for `pre-install-cmd` in the compose file's `x-casaos` extension
55+
- Executes shell commands that can prepare the system or download additional resources
56+
- Examples: creating directories, downloading configuration files, setting up external dependencies
57+
- Installation continues even if pre-install command fails (with logging)
58+
5259
### 3. Service Layer Orchestration
5360

5461
**File**: `CasaOS-AppManagement/service/compose_service.go`
@@ -78,10 +85,23 @@ go func(ctx context.Context) {
7885

7986
if err := composeApp.PullAndInstall(ctx); err != nil {
8087
// Error handling
88+
} else {
89+
// Execute post-install command after successful installation
90+
if err := install_cmd.ExecutePostInstallScript((*codegen.ComposeApp)(composeApp)); err != nil {
91+
logger.Error("failed to execute post-install command, but installation was successful", zap.Error(err), zap.String("name", composeApp.Name))
92+
// Don't fail the installation if post-install command fails
93+
}
8194
}
8295
}(ctx)
8396
```
8497

98+
#### Post-Install Command Execution
99+
After successful Docker compose installation, CasaOS executes optional post-install commands:
100+
- Looks for `post-install-cmd` in the compose file's `x-casaos` extension
101+
- Executes shell commands for setup tasks that require running containers
102+
- Examples: database initialization, configuration updates, sending notifications
103+
- Post-install command failures don't mark the installation as failed (graceful degradation)
104+
85105
### 4. Compose App Operations
86106

87107
**File**: `CasaOS-AppManagement/service/compose_app.go`
@@ -158,12 +178,98 @@ func apiService() (api.Service, client.APIClient, error) {
158178

159179
Uses the official Docker Compose v2 Go library (`github.com/docker/compose/v2`) rather than shell command execution.
160180

181+
## Install Command System
182+
183+
**File**: `CasaOS-AppManagement/pkg/install_cmd/install_cmd.go`
184+
185+
CasaOS supports optional pre-install and post-install commands through the `x-casaos` extension in Docker Compose files.
186+
187+
### x-casaos Extension Format
188+
189+
```yaml
190+
x-casaos:
191+
pre-install-cmd: "echo 'Preparing installation...' && mkdir -p /data/config"
192+
post-install-cmd: "echo 'Installation complete!' && curl -X POST http://webhook.example.com/notify"
193+
# ... other casaos extensions
194+
```
195+
196+
### Command Execution Environment
197+
198+
Both pre-install and post-install commands are executed with:
199+
- **Shell**: `/bin/bash -c`
200+
- **Environment**: Full system environment plus Docker access
201+
- **Working Directory**: System dependent
202+
- **Streams**: Connected to standard input/output/error
203+
- **Docker Access**: Available via `DOCKER_HOST=unix:///var/run/docker.sock`
204+
205+
### Pre-Install Commands (`pre-install-cmd`)
206+
207+
**Execution Point**: Before Docker compose installation begins
208+
**Use Cases**:
209+
- System preparation (creating directories, setting permissions)
210+
- Downloading configuration files or additional resources
211+
- Installing system dependencies
212+
- Setting up external databases or services
213+
214+
**Example**:
215+
```yaml
216+
x-casaos:
217+
pre-install-cmd: |
218+
# Create necessary directories
219+
mkdir -p /data/app/config /data/app/logs
220+
# Download configuration template
221+
curl -o /data/app/config/app.conf https://example.com/config/template.conf
222+
# Set permissions
223+
chmod 755 /data/app/config
224+
```
225+
226+
### Post-Install Commands (`post-install-cmd`)
227+
228+
**Execution Point**: After successful Docker compose installation (containers are running)
229+
**Use Cases**:
230+
- Database initialization and schema setup
231+
- Application configuration that requires running containers
232+
- Integration with external services
233+
- Sending completion notifications
234+
- Health checks and validation
235+
236+
**Example**:
237+
```yaml
238+
x-casaos:
239+
post-install-cmd: |
240+
# Wait for database to be ready
241+
until docker exec myapp-db pg_isready; do sleep 2; done
242+
# Initialize database schema
243+
docker exec myapp-db psql -U postgres -d myapp -f /docker-entrypoint-initdb.d/schema.sql
244+
# Send notification
245+
curl -X POST "https://api.example.com/notify" -d '{"status":"installed","app":"myapp"}'
246+
```
247+
248+
### Error Handling
249+
250+
- **Pre-install failures**: Stop installation and return error to user
251+
- **Post-install failures**: Log error but don't mark installation as failed
252+
- All command output is logged with appropriate log levels
253+
- Commands have access to standard streams for interactive operations if needed
254+
255+
### Security Considerations
256+
257+
- Commands are executed with the same privileges as the CasaOS process
258+
- Full Docker socket access is available for container management
259+
- No input sanitization beyond standard bash execution
260+
- Use with trusted compose files only
261+
161262
## Key Files and Functions Reference
162263

163264
| File | Function | Purpose |
164265
|------|----------|---------|
165266
| `route/v2/compose_app.go` | `InstallComposeApp()` | Main API endpoint handler |
166267
| `route/v2/compose_app.go` | `YAMLfromRequest()` | Extract YAML from HTTP request |
268+
| `route/v2/appstore_pcs.go` | `executePreInstallScript()` | Execute pre-install commands |
269+
| `route/v2/appstore_pcs.go` | `executePostInstallScript()` | Execute post-install commands |
270+
| `pkg/install_cmd/install_cmd.go` | `ExecuteInstallCmd()` | Generic install command executor |
271+
| `pkg/install_cmd/install_cmd.go` | `ExecutePreInstallScript()` | Pre-install command wrapper |
272+
| `pkg/install_cmd/install_cmd.go` | `ExecutePostInstallScript()` | Post-install command wrapper |
167273
| `service/compose_service.go` | `Install()` | Service layer orchestration |
168274
| `service/compose_service.go` | `PrepareWorkingDirectory()` | Create app directory |
169275
| `service/compose_app.go` | `PullAndInstall()` | Main installation coordinator |
@@ -220,9 +326,11 @@ The complete flow from API call to Docker compose execution:
220326

221327
1. **HTTP Request** → `InstallComposeApp()` endpoint
222328
2. **YAML Processing** → Parse and validate compose specification
223-
3. **Service Setup** → Create working directory and save compose file
224-
4. **Image Pull** → Download required Docker images
225-
5. **Container Creation** → Create containers via Docker Compose API
226-
6. **Service Start****Execute equivalent of `docker compose up`**
227-
228-
The actual Docker compose up operation occurs in `service/compose_app.go` where `service.Start()` is called with the compose project configuration.
329+
3. **Pre-Install Command** → Execute optional `pre-install-cmd` from x-casaos extension
330+
4. **Service Setup** → Create working directory and save compose file
331+
5. **Image Pull** → Download required Docker images
332+
6. **Container Creation** → Create containers via Docker Compose API
333+
7. **Service Start** → **Execute equivalent of `docker compose up`**
334+
8. **Post-Install Command** → Execute optional `post-install-cmd` from x-casaos extension
335+
336+
The actual Docker compose up operation occurs in `service/compose_app.go` where `service.Start()` is called with the compose project configuration. Pre-install commands run before any Docker operations, while post-install commands run after successful container startup.

0 commit comments

Comments
 (0)