-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathstart.cmd
More file actions
335 lines (297 loc) · 13.6 KB
/
start.cmd
File metadata and controls
335 lines (297 loc) · 13.6 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
@echo off
setlocal enabledelayedexpansion
echo Starting the application setup...
REM Set root and config paths
set ROOT_DIR=%cd%\..
set AZURE_FOLDER=%ROOT_DIR%\.azure
set CONFIG_FILE=%AZURE_FOLDER%\config.json
set API_ENV_FILE=%ROOT_DIR%\src\api\.env
set WORKSHOP_ENV_FILE=%ROOT_DIR%\docs\workshop\docs\workshop\.env
REM Check if .azure folder exists first
if not exist "%AZURE_FOLDER%" (
echo .azure folder not found. This is normal if Azure deployment hasn't been run yet.
goto :check_local_env
)
REM Check if config.json exists
if not exist "%CONFIG_FILE%" (
echo config.json not found in .azure folder. This is normal if Azure deployment hasn't been run yet.
goto :check_local_env
)
REM Extract default environment name
for /f "delims=" %%i in ('powershell -command "try { (Get-Content '%CONFIG_FILE%' | ConvertFrom-Json).defaultEnvironment } catch { '' }"') do set DEFAULT_ENV=%%i
if not defined DEFAULT_ENV (
echo Failed to extract defaultEnvironment from config.json or config.json is invalid.
goto :check_local_env
)
REM Load .env file from Azure deployment
set ENV_FILE=%AZURE_FOLDER%\%DEFAULT_ENV%\.env
REM Check if .env file exists in .azure folder
if exist "%ENV_FILE%" (
echo Found .env file in Azure deployment folder: %ENV_FILE%
REM Check if API .env also exists and ask for overwrite
if exist "%API_ENV_FILE%" (
echo Found existing .env file in src/api
set /p OVERWRITE_ENV="Do you want to overwrite it with the Azure deployment .env? (y/N): "
if /i "!OVERWRITE_ENV!"=="y" (
echo Overwriting existing .env file with Azure deployment configuration...
copy /Y "%ENV_FILE%" "%API_ENV_FILE%"
if errorlevel 1 (
echo Failed to copy .env to src/api
exit /b 1
)
echo Azure deployment .env copied to src/api
set ENV_FILE_FOR_ROLES=%ENV_FILE%
) else (
echo Preserving existing .env file in src/api
echo Reading environment variables from src/api/.env for role assignments...
set ENV_FILE_FOR_ROLES=%API_ENV_FILE%
)
) else (
echo No .env file found in src/api, copying from Azure deployment...
copy /Y "%ENV_FILE%" "%API_ENV_FILE%"
if errorlevel 1 (
echo Failed to copy .env to src/api
exit /b 1
)
echo Copied .env to src/api
set ENV_FILE_FOR_ROLES=%ENV_FILE%
)
copy /Y "%ENV_FILE%" "%WORKSHOP_ENV_FILE%"
if errorlevel 1 (
echo Warning: Failed to copy .env to workshop directory
) else (
echo Azure deployment .env copied to workshop/docs/workshop
)
goto :setup_environment
)
:check_local_env
echo Checking for local .env file in src/api...
REM Try to use src/api .env file as fallback
if exist "%API_ENV_FILE%" (
echo Using existing .env file from src/api for configuration...
set ENV_FILE_FOR_ROLES=%API_ENV_FILE%
echo Warning: No Azure deployment found, using local src/api/.env
copy /Y "%API_ENV_FILE%" "%WORKSHOP_ENV_FILE%"
if errorlevel 1 (
echo Warning: Failed to copy .env to workshop directory
) else (
echo Local .env copied to workshop/docs/workshop
)
goto :setup_environment
) else (
echo ERROR: No .env files found in any location.
echo.
echo The following files/folders are missing:
if not exist "%AZURE_FOLDER%" echo - .azure folder (created by 'azd up')
if exist "%AZURE_FOLDER%" if not exist "%CONFIG_FILE%" echo - .azure/config.json (created by 'azd up')
if exist "%CONFIG_FILE%" if not defined DEFAULT_ENV echo - Valid defaultEnvironment in config.json
if defined DEFAULT_ENV if not exist "%ENV_FILE%" echo - .env file in Azure deployment folder: %ENV_FILE%
echo - Local .env file: %API_ENV_FILE%
echo.
echo Please choose one of the following options:
echo 1. Run 'azd up' to deploy Azure resources and generate .env files
echo 2. Manually create %API_ENV_FILE% with required environment variables
echo 3. Copy an existing .env file to %API_ENV_FILE%
echo.
echo For more information, see: documents/LocalDevelopmentSetup.md
exit /b 1
)
:setup_environment
REM Parse required variables for role assignments from the appropriate env file
echo Reading environment variables for role assignments from: %ENV_FILE_FOR_ROLES%
for /f "tokens=1,* delims==" %%A in ('type "%ENV_FILE_FOR_ROLES%"') do (
if "%%A"=="RESOURCE_GROUP_NAME" set AZURE_RESOURCE_GROUP=%%~B
if "%%A"=="AZURE_COSMOSDB_ACCOUNT" set AZURE_COSMOSDB_ACCOUNT=%%~B
if "%%A"=="AZURE_AI_FOUNDRY_NAME" set "AI_FOUNDRY_NAME=%%~B"
if "%%A"=="AZURE_AI_SEARCH_NAME" set "SEARCH_SERVICE_NAME=%%~B"
if "%%A"=="AZURE_EXISTING_AIPROJECT_RESOURCE_ID" set "EXISTING_AI_PROJECT_RESOURCE_ID=%%~B"
if "%%A"=="SQLDB_SERVER" (
set SQLDB_SERVER=%%~B
for /f "tokens=1 delims=." %%C in ("%%~B") do set SQLDB_SERVER_NAME=%%C
)
)
REM Write API base URL to frontend .env
set APP_ENV_FILE=%ROOT_DIR%\src\App\.env
(
echo REACT_APP_API_BASE_URL=http://127.0.0.1:8000
) > "%APP_ENV_FILE%"
echo Updated src/App/.env with REACT_APP_API_BASE_URL
REM Add or update APP_ENV="dev" in API .env file
echo Checking for existing APP_ENV in src/api/.env...
findstr /i "^APP_ENV=" "%API_ENV_FILE%" >nul 2>&1
if %ERRORLEVEL%==0 (
echo APP_ENV already exists, updating to "dev"...
powershell -command "(Get-Content '%API_ENV_FILE%') -replace '^APP_ENV=.*', 'APP_ENV=\"dev\"' | Set-Content '%API_ENV_FILE%'"
) else (
echo APP_ENV not found, adding APP_ENV="dev"...
echo APP_ENV="dev" >> "%API_ENV_FILE%"
)
echo APP_ENV="dev" configured in src/api/.env
REM Authenticate with Azure
echo Checking Azure login status...
call az account show --query id --output tsv >nul 2>&1
if %ERRORLEVEL%==0 (
echo Already authenticated with Azure.
) else (
echo Not authenticated. Attempting Azure login...
call az login --use-device-code --output none
call az account show --query "[name, id]" --output tsv
echo Logged in successfully.
)
REM Get signed-in user ID and subscription ID
FOR /F "delims=" %%i IN ('az ad signed-in-user show --query id -o tsv') DO set "signed_user_id=%%i"
FOR /F "delims=" %%s IN ('az account show --query id -o tsv') DO set "subscription_id=%%s"
REM Check if user has Cosmos DB role
FOR /F "delims=" %%i IN ('az cosmosdb sql role assignment list --resource-group %AZURE_RESOURCE_GROUP% --account-name %AZURE_COSMOSDB_ACCOUNT% --query "[?roleDefinitionId.ends_with(@, '00000000-0000-0000-0000-000000000002') && principalId == '%signed_user_id%']" -o tsv') DO set "roleExists=%%i"
if defined roleExists (
echo User already has the Cosmos DB Built-in Data Contributor role.
) else (
echo Assigning Cosmos DB Built-in Data Contributor role...
set MSYS_NO_PATHCONV=1
call az cosmosdb sql role assignment create ^
--resource-group %AZURE_RESOURCE_GROUP% ^
--account-name %AZURE_COSMOSDB_ACCOUNT% ^
--role-definition-id 00000000-0000-0000-0000-000000000002 ^
--principal-id %signed_user_id% ^
--scope "/" ^
--output none
echo Cosmos DB Built-in Data Contributor role assigned successfully.
)
REM Assign Azure SQL Server AAD admin
FOR /F "delims=" %%i IN ('az account show --query user.name --output tsv') DO set "SQLADMIN_USERNAME=%%i"
echo Assigning Azure SQL Server AAD admin role to %SQLADMIN_USERNAME%...
call az sql server ad-admin create ^
--display-name %SQLADMIN_USERNAME% ^
--object-id "%signed_user_id%" ^
--resource-group %AZURE_RESOURCE_GROUP% ^
--server %SQLDB_SERVER_NAME% ^
--output tsv >nul 2>&1
echo Azure SQL Server AAD admin role assigned successfully.
REM Assign Azure AI User role
echo Checking Azure AI User role assignment...
if not defined EXISTING_AI_PROJECT_RESOURCE_ID (
echo Using AI Foundry account scope...
FOR /F "delims=" %%i IN ('az role assignment list --assignee %signed_user_id% --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "/subscriptions/%subscription_id%/resourceGroups/%AZURE_RESOURCE_GROUP%/providers/Microsoft.CognitiveServices/accounts/%AI_FOUNDRY_NAME%" --query "[0].id" -o tsv') DO set "aiUserRoleExists=%%i"
if defined aiUserRoleExists (
echo User already has the Azure AI User role.
) else (
echo Assigning Azure AI User role to AI Foundry account...
call az role assignment create ^
--assignee %signed_user_id% ^
--role "53ca6127-db72-4b80-b1b0-d745d6d5456d" ^
--scope "/subscriptions/%subscription_id%/resourceGroups/%AZURE_RESOURCE_GROUP%/providers/Microsoft.CognitiveServices/accounts/%AI_FOUNDRY_NAME%" ^
--output none
echo Azure AI User role assigned successfully.
)
) else (
echo Extracting foundry scope from existing AI project resource ID...
for /f "tokens=1,2,3,4,5,6,7,8 delims=/" %%a in ("%EXISTING_AI_PROJECT_RESOURCE_ID%") do (
set "FOUNDRY_SCOPE=/%%a/%%b/%%c/%%d/%%e/%%f/%%g/%%h"
)
echo Using foundry scope from existing project: !FOUNDRY_SCOPE!
FOR /F "delims=" %%i IN ('az role assignment list --assignee %signed_user_id% --role "53ca6127-db72-4b80-b1b0-d745d6d5456d" --scope "!FOUNDRY_SCOPE!" --query "[0].id" -o tsv') DO set "aiUserRoleExists=%%i"
if defined aiUserRoleExists (
echo User already has the Azure AI User role.
) else (
echo Assigning Azure AI User role to foundry account...
call az role assignment create ^
--assignee %signed_user_id% ^
--role "53ca6127-db72-4b80-b1b0-d745d6d5456d" ^
--scope "!FOUNDRY_SCOPE!" ^
--output none
echo Azure AI User role assigned successfully.
)
)
REM Assign Search Index Data Reader role
echo Checking Search Index Data Reader role assignment...
FOR /F "delims=" %%i IN ('az role assignment list --assignee %signed_user_id% --role "1407120a-92aa-4202-b7e9-c0e197c71c8f" --scope "/subscriptions/%subscription_id%/resourceGroups/%AZURE_RESOURCE_GROUP%/providers/Microsoft.Search/searchServices/%SEARCH_SERVICE_NAME%" --query "[0].id" -o tsv') DO set "searchReaderRoleExists=%%i"
if defined searchReaderRoleExists (
echo User already has the Search Index Data Reader role.
) else (
echo Assigning Search Index Data Reader role to AI Search service...
call az role assignment create ^
--assignee %signed_user_id% ^
--role "1407120a-92aa-4202-b7e9-c0e197c71c8f" ^
--scope "/subscriptions/%subscription_id%/resourceGroups/%AZURE_RESOURCE_GROUP%/providers/Microsoft.Search/searchServices/%SEARCH_SERVICE_NAME%" ^
--output none
echo Search Index Data Reader role assigned successfully.
)
echo Proceeding to create virtual environment and restore backend Python packages...
REM Create and activate virtual environment in root folder
cd %ROOT_DIR%
REM Check if virtual environment already exists
if not exist ".venv" (
echo Creating Python virtual environment in root folder...
call python -m venv .venv
if errorlevel 1 (
echo Failed to create virtual environment
exit /b 1
)
echo Virtual environment created successfully.
) else (
echo Virtual environment already exists.
)
REM Activate virtual environment and install backend packages
echo Activating virtual environment and installing backend packages...
call .venv\Scripts\activate.bat
call python -m pip install --upgrade pip
cd src\api
call python -m pip install -r requirements.txt
if errorlevel 1 (
echo Failed to restore backend Python packages
call deactivate
exit /b 1
)
echo Backend Python packages installed successfully in virtual environment.
call deactivate
cd %ROOT_DIR%
REM Restore frontend packages
cd %ROOT_DIR%\src\App
call npm install --force
if errorlevel 1 (
echo Failed to restore frontend npm packages
exit /b 1
)
cd %ROOT_DIR%
REM Close any processes using ports 8000 and 3000
echo Checking for processes using ports 8000 and 3000...
REM Kill any existing processes on ports 8000 and 3000 before starting
for %%P in (8000 3000) do (
for /f "tokens=5" %%A in ('netstat -ano ^| findstr "LISTENING" ^| findstr ":%%P "') do (
if "%%A" neq "0" (
echo Port %%P is already in use by PID %%A. Stopping it...
taskkill /F /PID %%A /T >nul 2>&1
)
)
)
REM Start backend and frontend
echo Starting backend server...
cd %ROOT_DIR%
call .venv\Scripts\activate.bat
cd src\api
start /b python app.py --port=8000
echo Backend started at http://127.0.0.1:8000
echo Waiting for backend to initialize...
timeout /t 30 /nobreak >nul
echo Starting frontend server...
cd %ROOT_DIR%\src\App
REM Show server information before starting
echo.
echo ========================================
echo Both servers are now running:
echo Backend: http://127.0.0.1:8000
echo Frontend: http://localhost:3000
echo ========================================
echo Press Ctrl+C to stop all servers
echo.
REM Start npm with PowerShell wrapper for automatic cleanup on Ctrl+C (single line for reliability)
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { npm start } finally { Write-Host ''; Write-Host 'Stopping all processes...'; Start-Sleep -Milliseconds 500; @(8000, 3000) | ForEach-Object { $port = $_; Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue | ForEach-Object { $pid_ = $_.OwningProcess; if ($pid_ -and $pid_ -ne 0) { taskkill /F /PID $pid_ /T 2>$null } } }; Write-Host 'Cleanup complete.'; Write-Host ''; Write-Host 'All servers stopped.' -ForegroundColor Yellow }"
REM Fallback cleanup in case PowerShell finally block was interrupted
for %%P in (8000 3000) do (
for /f "tokens=5" %%A in ('netstat -ano ^| findstr "LISTENING" ^| findstr ":%%P "') do (
if "%%A" neq "0" (
taskkill /F /PID %%A /T >nul 2>&1
)
)
)
endlocal