-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebuild.bat
More file actions
205 lines (185 loc) · 7.59 KB
/
Copy pathrebuild.bat
File metadata and controls
205 lines (185 loc) · 7.59 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
@echo off
setlocal enabledelayedexpansion
:: Load container config from .env.container
if not exist .env.container (
echo .env.container file not found. Copy .env.container.example to .env.container and customize.
exit /b 1
)
for /f "tokens=1,2 delims==" %%a in (.env.container) do (
if "%%a"=="IMAGE_NAME" set IMAGE_NAME=%%b
if "%%a"=="CONTAINER_NAME" set CONTAINER_NAME=%%b
if "%%a"=="WORK_FOLDER" set WORK_FOLDER=%%b
if "%%a"=="EXPOSE_PORT" set EXPOSE_PORT=%%b
if "%%a"=="CLAUDE_PERSIST_FOLDER" set CLAUDE_PERSIST_FOLDER=%%b
if "%%a"=="PI_PERSIST_FOLDER" set PI_PERSIST_FOLDER=%%b
if "%%a"=="LAN_DNS" set LAN_DNS=%%b
if "%%a"=="LAN_DNS_SEARCH" set LAN_DNS_SEARCH=%%b
)
if not defined IMAGE_NAME (
echo IMAGE_NAME not set in .env.container.
exit /b 1
)
if not defined CONTAINER_NAME (
echo CONTAINER_NAME not set in .env.container.
exit /b 1
)
if not defined WORK_FOLDER (
set WORK_FOLDER=work
)
:: Load secrets from secrets.bat if it exists (check current folder and parent folder)
set SECRETS_LOADED=0
if exist secrets.bat (
echo Loading secrets from secrets.bat ^(current folder^)...
call secrets.bat
set SECRETS_LOADED=1
) else if exist ..\secrets.bat (
echo Loading secrets from secrets.bat ^(parent folder^)...
call ..\secrets.bat
set SECRETS_LOADED=1
)
if "!SECRETS_LOADED!"=="0" (
echo Note: secrets.bat not found in current or parent folder. Set environment variables manually or create secrets.bat from secrets.bat.example
)
:: Build -e flags from host environment variables
:: List of environment variable names to propagate to container (hard-coded for security)
:: Add more variable names to this list as needed, separated by spaces
set ENV_VARS=
if defined ANTHROPIC_API_KEY (
set ENV_VARS=!ENV_VARS! -e "ANTHROPIC_API_KEY=!ANTHROPIC_API_KEY!"
)
if defined GITHUB_TOKEN (
set ENV_VARS=!ENV_VARS! -e "GITHUB_TOKEN=!GITHUB_TOKEN!"
)
if defined AZURE_DEVOPS_PAT (
set ENV_VARS=!ENV_VARS! -e "AZURE_DEVOPS_PAT=!AZURE_DEVOPS_PAT!"
)
if defined AZURE_DEVOPS_ORG (
set ENV_VARS=!ENV_VARS! -e "AZURE_DEVOPS_ORG=!AZURE_DEVOPS_ORG!"
)
if defined CLAUDE_CODE_OAUTH_TOKEN (
set ENV_VARS=!ENV_VARS! -e "CLAUDE_CODE_OAUTH_TOKEN=!CLAUDE_CODE_OAUTH_TOKEN!"
)
if defined GIT_USER_NAME (
set ENV_VARS=!ENV_VARS! -e "GIT_USER_NAME=!GIT_USER_NAME!"
)
if defined GIT_USER_EMAIL (
set ENV_VARS=!ENV_VARS! -e "GIT_USER_EMAIL=!GIT_USER_EMAIL!"
)
if defined GITHUB_USERNAME (
set ENV_VARS=!ENV_VARS! -e "GITHUB_USERNAME=!GITHUB_USERNAME!"
)
if defined BS_TOKEN (
set ENV_VARS=!ENV_VARS! -e "BS_TOKEN=!BS_TOKEN!"
)
:: Auto-detect Windows timezone and convert to IANA format (unless TZ is explicitly set)
if not defined TZ (
:: Use PowerShell to get Windows timezone and convert to IANA using TimeZoneInfo
for /f "delims=" %%t in ('powershell -NoProfile -Command "$tz = [TimeZoneInfo]::Local; $tzId = $tz.Id; $mapping = @{'Eastern Standard Time'='America/New_York'; 'Central Standard Time'='America/Chicago'; 'Mountain Standard Time'='America/Denver'; 'Pacific Standard Time'='America/Los_Angeles'; 'Alaska Standard Time'='America/Anchorage'; 'Hawaiian Standard Time'='Pacific/Honolulu'; 'Atlantic Standard Time'='America/Halifax'; 'Central European Standard Time'='Europe/Berlin'; 'GMT Standard Time'='Europe/London'; 'W. Europe Standard Time'='Europe/Amsterdam'; 'Tokyo Standard Time'='Asia/Tokyo'; 'China Standard Time'='Asia/Shanghai'; 'India Standard Time'='Asia/Kolkata'; 'AUS Eastern Standard Time'='Australia/Sydney'; 'New Zealand Standard Time'='Pacific/Auckland'}; if ($mapping.ContainsKey($tzId)) { $mapping[$tzId] } else { $tzId }"') do set TZ=%%t
if not defined TZ (
echo Warning: Could not detect timezone. Set TZ environment variable manually if needed.
)
)
if defined TZ (
set ENV_VARS=!ENV_VARS! -e "TZ=!TZ!"
)
set ENV_VARS=!ENV_VARS! -e "WORK_FOLDER=!WORK_FOLDER!"
:: Add more variables here following the same pattern:
:: if defined ANOTHER_VAR (
:: set ENV_VARS=!ENV_VARS! -e "ANOTHER_VAR=!ANOTHER_VAR!"
:: )
:: Create work folder if it doesn't exist
if not exist "!WORK_FOLDER!" (
echo Creating work folder: !WORK_FOLDER!
mkdir "!WORK_FOLDER!"
)
:: Get absolute host directory and replace \ with / for Docker volume format
set HOST_DIR=%CD%
set WORK_PATH=!HOST_DIR!\!WORK_FOLDER!
set VOLUME_MOUNT=!WORK_PATH:\=/!:/home/devuser/!WORK_FOLDER!
:: ENV_VARS is built above from host environment variables
set WORKDIR=/home/devuser/!WORK_FOLDER!
set KEEP_ALIVE=tail -f /dev/null
:: Stop and remove existing container if it exists
echo Stopping and removing container %CONTAINER_NAME% if exists...
docker stop %CONTAINER_NAME% 2>nul
docker rm %CONTAINER_NAME% 2>nul
:: Remove old image if it exists
echo Removing old image %IMAGE_NAME% if exists...
docker rmi %IMAGE_NAME% 2>nul
:: Build new image
echo Building new image %IMAGE_NAME%...
set BUILD_ARGS=--build-arg WORK_FOLDER=!WORK_FOLDER!
if defined CLAUDE_CODE_OAUTH_TOKEN (
set BUILD_ARGS=!BUILD_ARGS! --build-arg INSTALL_CLAUDE=true
) else if defined CLAUDE_PERSIST_FOLDER (
set BUILD_ARGS=!BUILD_ARGS! --build-arg INSTALL_CLAUDE=true
)
if defined PI_PERSIST_FOLDER (
set BUILD_ARGS=!BUILD_ARGS! --build-arg INSTALL_PI=true
)
if defined GITHUB_USERNAME if defined GITHUB_TOKEN (
set BUILD_ARGS=!BUILD_ARGS! --build-arg INSTALL_GH=true
)
docker build !BUILD_ARGS! -t %IMAGE_NAME% .
if errorlevel 1 (
echo Build failed.
pause
exit /b 1
)
:: Build port flag if EXPOSE_PORT is set
set PORT_FLAG=
if defined EXPOSE_PORT (
set PORT_FLAG=-p !EXPOSE_PORT!
)
:: Build DNS flag if LAN_DNS is set (lets container resolve LAN hostnames)
set DNS_FLAG=
if defined LAN_DNS (
set DNS_FLAG=--dns=!LAN_DNS!
)
if defined LAN_DNS_SEARCH (
set DNS_FLAG=!DNS_FLAG! --dns-search=!LAN_DNS_SEARCH!
)
:: Build Claude persist mounts if CLAUDE_PERSIST_FOLDER is set
set CLAUDE_MOUNT=
if defined CLAUDE_PERSIST_FOLDER (
if not exist "!CLAUDE_PERSIST_FOLDER!\claude" (
echo Creating Claude persist folder: !CLAUDE_PERSIST_FOLDER!\claude
mkdir "!CLAUDE_PERSIST_FOLDER!\claude"
)
set CLAUDE_JSON_NEEDS_INIT=0
if not exist "!CLAUDE_PERSIST_FOLDER!\claude.json" (
set CLAUDE_JSON_NEEDS_INIT=1
) else (
for %%I in ("!CLAUDE_PERSIST_FOLDER!\claude.json") do if %%~zI==0 set CLAUDE_JSON_NEEDS_INIT=1
)
if "!CLAUDE_JSON_NEEDS_INIT!"=="1" (
echo Initializing claude.json with empty JSON object in !CLAUDE_PERSIST_FOLDER!
echo {}>"!CLAUDE_PERSIST_FOLDER!\claude.json"
)
set CLAUDE_PATH=!HOST_DIR!\!CLAUDE_PERSIST_FOLDER!
set CLAUDE_MOUNT=-v "!CLAUDE_PATH:\=/!/claude:/home/devuser/.claude" -v "!CLAUDE_PATH:\=/!/claude.json:/home/devuser/.claude.json"
)
:: Build Pi persist mount if PI_PERSIST_FOLDER is set
set PI_MOUNT=
if defined PI_PERSIST_FOLDER (
if not exist "!PI_PERSIST_FOLDER!\pi" (
echo Creating Pi persist folder: !PI_PERSIST_FOLDER!\pi
mkdir "!PI_PERSIST_FOLDER!\pi"
)
set PI_PATH=!HOST_DIR!\!PI_PERSIST_FOLDER!
set PI_MOUNT=-v "!PI_PATH:\=/!/pi:/home/devuser/.pi"
)
:: Derive a valid hostname from CONTAINER_NAME (RFC 1123 disallows underscores)
set HOSTNAME_VALUE=!CONTAINER_NAME:_=-!
:: Create (but don't start) new container
echo Creating new container %CONTAINER_NAME% from %IMAGE_NAME%...
docker create --init --name %CONTAINER_NAME% --hostname !HOSTNAME_VALUE! %ENV_VARS% %PORT_FLAG% %DNS_FLAG% %CLAUDE_MOUNT% %PI_MOUNT% -v "%VOLUME_MOUNT%" --workdir %WORKDIR% %IMAGE_NAME% %KEEP_ALIVE%
if errorlevel 1 (
echo Create failed.
pause
exit /b 1
)
:: Optional cleanup (uncomment if desired)
:: docker system prune -f
echo Rebuild complete. Use cbash.bat to open a shell.
endlocal