-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathrun.ps1
More file actions
143 lines (125 loc) · 4.04 KB
/
run.ps1
File metadata and controls
143 lines (125 loc) · 4.04 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
#!/usr/bin/env pwsh
param(
[string]$Image,
[string]$Profile,
[string]$Output = "./bin",
[string]$CustomModules = "./custom_modules",
[string]$Mirror = "mirrors.pku.edu.cn",
[switch]$WithPull,
[switch]$RmFirst,
[switch]$UseMirror,
[switch]$Help
)
function Show-Usage {
Write-Host "--image: specify imagebuilder docker image, find it in https://hub.docker.com/r/openwrt/imagebuilder/tags or https://hub.docker.com/r/immortalwrt/imagebuilder/tags"
Write-Host "--profile: specify profile"
Write-Host "--output: specify output directory for build results (default: ./bin)"
Write-Host "--custom-modules: specify custom modules directory (default: ./custom_modules)"
Write-Host "--with-pull: pull image before build"
Write-Host "--rm-first: remove container before build"
Write-Host "--use-mirror: use mirror"
Write-Host "--mirror: specify mirror url, like mirrors.jlu.edu.cn, do not add http:// or https://"
Write-Host "-h|--help: print this help"
exit 1
}
function Invoke-DockerCompose {
# Check if docker-compose is available, otherwise use docker compose
if (Get-Command docker-compose -ErrorAction SilentlyContinue) {
& docker-compose @args
} else {
& docker compose @args
}
}
# Handle help parameter
if ($Help) {
Show-Usage
}
# Validate required parameters
if (-not $Image) {
Write-Error "ERROR: no image specified"
Show-Usage
}
# Set default values
if (-not $UseMirror) {
$UseMirror = $true
}
Write-Host "IMAGEBUILDER_IMAGE: $Image PROFILE: $Profile"
Write-Host "CUSTOM_MODULES_PATH: $CustomModules"
# Determine build directory based on image type
if ($Image -match "immortalwrt") {
$BuildDir = "/home/build/immortalwrt"
} else {
$BuildDir = "/builder"
}
# Verify Docker is available
if (!(Get-Command docker -ErrorAction SilentlyContinue)) {
Write-Error "错误: 未找到 Docker,请确保 Docker Desktop 已安装并运行"
Write-Host "请从以下地址下载并安装 Docker Desktop for Windows:"
Write-Host " - https://www.docker.com/products/docker-desktop"
exit 1
}
# Generate docker-compose.yml content
$dockerComposeContent = @"
version: "3.5"
services:
imagebuilder:
image: "$Image"
container_name: imagebuilder
environment:
- PROFILE=$Profile
- USE_MIRROR=$([int]$UseMirror)
- MIRROR=$Mirror
- IMAGEBUILDER_IMAGE=$Image
- ENABLE_MODULES=$env:ENABLE_MODULES
- CONFIG_TARGET_ROOTFS_PARTSIZE=$env:CONFIG_TARGET_ROOTFS_PARTSIZE
env_file:
- ./.env
volumes:
- ${Output}:${BuildDir}/bin
- ./build.sh:${BuildDir}/build.sh
- ./setup:${BuildDir}/setup
- ./modules:${BuildDir}/modules_in_container
- ${CustomModules}:${BuildDir}/custom_modules_in_container
- ./.env:${BuildDir}/.env
command: "./build.sh"
"@
# Write docker-compose.yml file
$dockerComposeContent | Out-File -FilePath "docker-compose.yml" -Encoding UTF8
# Pull image if requested
if ($WithPull) {
Write-Host "Pulling Docker image..."
Invoke-DockerCompose pull
}
# Remove existing container if requested
if ($RmFirst) {
Write-Host "Removing existing container..."
try {
& docker stop imagebuilder 2>$null
& docker rm imagebuilder 2>$null
} catch {
# Container might not exist, ignore errors
}
}
# Create output directory
if (!(Test-Path $Output)) {
New-Item -ItemType Directory -Path $Output -Force | Out-Null
}
# Check if .env file exists
if (!(Test-Path ".env")) {
Write-Warning "WARNING: .env file not found, using default values"
"" | Out-File -FilePath ".env" -Encoding UTF8
}
# Run the build
Write-Host "Starting build process..."
Invoke-DockerCompose up --exit-code-from imagebuilder --remove-orphans
$buildStatus = $LASTEXITCODE
# Cleanup
Invoke-DockerCompose rm -f
Remove-Item "docker-compose.yml" -Force
if ($buildStatus -ne 0) {
Write-Error "build failed with exit code $buildStatus"
exit 1
} else {
Write-Host "Build completed successfully!"
Get-ChildItem $Output -Recurse | Format-Table Name, Length, LastWriteTime
}