Skip to content

Commit 483e40c

Browse files
committed
adding support for running the solution using docker compose
1 parent 20ffbec commit 483e40c

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

app/backend/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
1919
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
2020
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
2121

22+
2223
RUN curl -LJ -o /app/applicationinsights-agent-3.4.19.jar https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.4.19/applicationinsights-agent-3.4.19.jar
2324
COPY applicationinsights.json /app
2425

app/backend/src/main/java/com/microsoft/openai/samples/rag/config/AzureAuthenticationConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.azure.core.credential.TokenCredential;
55
import com.azure.identity.AzureCliCredentialBuilder;
6+
import com.azure.identity.EnvironmentCredentialBuilder;
67
import com.azure.identity.ManagedIdentityCredentialBuilder;
78
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.context.annotation.Bean;
@@ -22,6 +23,12 @@ public TokenCredential localTokenCredential() {
2223
return new AzureCliCredentialBuilder().build();
2324
}
2425

26+
@Profile("docker")
27+
@Bean
28+
public TokenCredential servicePrincipalTokenCredential() {
29+
return new EnvironmentCredentialBuilder().build();
30+
}
31+
2532
@Bean
2633
@Profile("default")
2734
public TokenCredential managedIdentityTokenCredential() {

app/compose.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
services:
2+
frontend:
3+
image: ai-chat-reference-java/frontend
4+
build: ./frontend
5+
environment:
6+
REACT_APP_API_BASE_URL: "http://backend:8080"
7+
ports:
8+
- "80:80"
9+
backend:
10+
image: ai-chat-reference-java/backend
11+
build: ./backend
12+
environment:
13+
- AZURE_STORAGE_ACCOUNT=${AZURE_STORAGE_ACCOUNT}
14+
- AZURE_STORAGE_CONTAINER=${AZURE_STORAGE_CONTAINER}
15+
- AZURE_SEARCH_INDEX=${AZURE_SEARCH_INDEX}
16+
- AZURE_SEARCH_SERVICE=${AZURE_SEARCH_SERVICE}
17+
- AZURE_SEARCH_QUERY_LANGUAGE=${AZURE_SEARCH_QUERY_LANGUAGE}
18+
- AZURE_SEARCH_QUERY_SPELLER=${AZURE_SEARCH_QUERY_SPELLER}
19+
- AZURE_OPENAI_EMB_MODEL_NAME=${AZURE_OPENAI_EMB_MODEL_NAME}
20+
- AZURE_OPENAI_EMB_DEPLOYMENT=${AZURE_OPENAI_EMB_DEPLOYMENT}
21+
- AZURE_OPENAI_CHATGPT_MODEL=${AZURE_OPENAI_CHATGPT_MODEL}
22+
- AZURE_OPENAI_SERVICE=${AZURE_OPENAI_SERVICE}
23+
- AZURE_OPENAI_CHATGPT_DEPLOYMENT=${AZURE_OPENAI_CHATGPT_DEPLOYMENT}
24+
- spring_profiles_active=docker
25+
- AZURE_CLIENT_ID=${servicePrincipal}
26+
- AZURE_CLIENT_SECRET=${servicePrincipalPassword}
27+
- AZURE_TENANT_ID=${servicePrincipalTenant}
28+
ports:
29+
- "8080:8080"
30+
indexer:
31+
image: ai-chat-reference-java/indexer
32+
build:
33+
context: ./indexer
34+
dockerfile: microservice/Dockerfile
35+
environment:
36+
- AZURE_STORAGE_ACCOUNT=${AZURE_STORAGE_ACCOUNT}
37+
- AZURE_STORAGE_CONTAINER=${AZURE_STORAGE_CONTAINER}
38+
- AZURE_SEARCH_INDEX=${AZURE_SEARCH_INDEX}
39+
- AZURE_SEARCH_SERVICE=${AZURE_SEARCH_SERVICE}
40+
- AZURE_FORMRECOGNIZER_SERVICE=${AZURE_FORMRECOGNIZER_SERVICE}
41+
- AZURE_SEARCH_QUERY_SPELLER=${AZURE_SEARCH_QUERY_SPELLER}
42+
- AZURE_OPENAI_EMB_MODEL_NAME=${AZURE_OPENAI_EMB_MODEL_NAME}
43+
- AZURE_OPENAI_EMB_DEPLOYMENT=${AZURE_OPENAI_EMB_DEPLOYMENT}
44+
- AZURE_OPENAI_SERVICE=${AZURE_OPENAI_SERVICE}
45+
- AZURE_SERVICEBUS_NAMESPACE=${AZURE_SERVICEBUS_NAMESPACE}
46+
- spring_profiles_active=docker
47+
- SPRING_CONFIG_LOCATION=classpath:/local-dev.properties
48+
- AZURE_CLIENT_ID=${servicePrincipal}
49+
- AZURE_CLIENT_SECRET=${servicePrincipalPassword}
50+
- AZURE_TENANT_ID=${servicePrincipalTenant}
51+

app/indexer/microservice/src/main/java/com/microsoft/openai/samples/indexer/service/config/AzureAuthenticationConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.azure.core.credential.TokenCredential;
55
import com.azure.identity.AzureCliCredentialBuilder;
6+
import com.azure.identity.EnvironmentCredentialBuilder;
67
import com.azure.identity.ManagedIdentityCredentialBuilder;
78
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.context.annotation.Bean;
@@ -23,6 +24,12 @@ public TokenCredential localTokenCredential() {
2324
return new AzureCliCredentialBuilder().build();
2425
}
2526

27+
@Profile("docker")
28+
@Bean
29+
@Primary
30+
public TokenCredential servicePrincipalTokenCredential() {
31+
return new EnvironmentCredentialBuilder().build();
32+
}
2633
@Bean
2734
@Profile("default")
2835
@Primary

deploy/aca/scripts/start-compose.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
$output = azd env get-values
2+
3+
foreach ($line in $output) {
4+
$name, $value = $line.Split("=")
5+
$value = $value -replace '^\"|\"$'
6+
[Environment]::SetEnvironmentVariable($name, $value)
7+
}
8+
9+
Write-Host "Environment variables set."
10+
$roles = @(
11+
"5e0bd9bd-7b93-4f28-af87-19fc36ad61bd",
12+
"a97b65f3-24c7-4388-baec-2e87135dc908",
13+
"2a2b9908-6ea1-4ae2-8e65-a410df84e7d1",
14+
"ba92f5b4-2d11-453d-a403-e96b0029c9fe",
15+
"1407120a-92aa-4202-b7e9-c0e197c71c8f",
16+
"8ebe5a00-799e-43f5-93ac-243d3dce84a7",
17+
"7ca78c08-252a-4471-8644-bb5ff32d4ba0",
18+
"4f6d3b9b-027b-4f4c-9142-0e5a2a2247e0"
19+
)
20+
21+
# Check if service principal exists
22+
$servicePrincipal = $(az ad sp list --display-name "azure-ai-chat-java-spi" --query [].appId --output tsv)
23+
24+
if ([string]::IsNullOrEmpty($servicePrincipal)) {
25+
Write-Host "Service principal not found. Creating service principal"
26+
$servicePrincipal = $(az ad sp create-for-rbac --name "azure-ai-chat-java-spi" --role reader --scopes "/subscriptions/$($env:AZURE_SUBSCRIPTION_ID)/resourceGroups/$($env:AZURE_RESOURCE_GROUP)" --query appId --output tsv)
27+
if ($LASTEXITCODE -ne 0) {
28+
Write-Host "Failed to create service principal"
29+
exit $LASTEXITCODE
30+
}
31+
$servicePrincipalObjectId = $(az ad sp show --id $servicePrincipal --query id --output tsv)
32+
Write-Host "Assigning Roles to service principal azure-ai-chat-java-spi with principal id: $servicePrincipal and object id[$servicePrincipalObjectId]"
33+
foreach ($role in $roles) {
34+
Write-Host "Assigning Role[$role] to principal id[$servicePrincipal] for resource[/subscriptions/$($env:AZURE_SUBSCRIPTION_ID)/resourceGroups/$($env:AZURE_RESOURCE_GROUP)]"
35+
az role assignment create `
36+
--role $role `
37+
--assignee-object-id $servicePrincipalObjectId `
38+
--scope "/subscriptions/$($env:AZURE_SUBSCRIPTION_ID)/resourceGroups/$($env:AZURE_RESOURCE_GROUP)" `
39+
--assignee-principal-type ServicePrincipal
40+
}
41+
}
42+
43+
$servicePrincipalPassword = $(az ad sp credential reset --id $servicePrincipal --query password --output tsv)
44+
$servicePrincipalTenant = $(az ad sp show --id $servicePrincipal --query appOwnerOrganizationId --output tsv)
45+
46+
# Set environment variables
47+
[Environment]::SetEnvironmentVariable("servicePrincipal", $servicePrincipal)
48+
[Environment]::SetEnvironmentVariable("servicePrincipalPassword", $servicePrincipalPassword)
49+
[Environment]::SetEnvironmentVariable("servicePrincipalTenant", $servicePrincipalTenant)
50+
51+
52+
Write-Host ""
53+
Write-Host "Starting solution locally using docker compose."
54+
Write-Host ""
55+
56+
docker compose -f ../../../app/compose.yaml up

deploy/aca/scripts/start-compose.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
echo ""
2+
echo "Loading azd .env file from current environment"
3+
echo ""
4+
5+
while IFS='=' read -r key value; do
6+
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//')
7+
export "$key=$value"
8+
echo "export $key=$value"
9+
done <<EOF
10+
$(azd env get-values)
11+
EOF
12+
13+
if [ $? -ne 0 ]; then
14+
echo "Failed to load environment variables from azd environment"
15+
exit $?
16+
fi
17+
18+
echo ""
19+
echo "Checking Service Principal and get password"
20+
echo ""
21+
22+
roles=(
23+
"5e0bd9bd-7b93-4f28-af87-19fc36ad61bd"
24+
"a97b65f3-24c7-4388-baec-2e87135dc908"
25+
"2a2b9908-6ea1-4ae2-8e65-a410df84e7d1"
26+
"ba92f5b4-2d11-453d-a403-e96b0029c9fe"
27+
"1407120a-92aa-4202-b7e9-c0e197c71c8f"
28+
"8ebe5a00-799e-43f5-93ac-243d3dce84a7"
29+
"7ca78c08-252a-4471-8644-bb5ff32d4ba0"
30+
"4f6d3b9b-027b-4f4c-9142-0e5a2a2247e0"
31+
)
32+
33+
#Check if service principal exists
34+
export servicePrincipal=$(az ad sp list --display-name "azure-ai-chat-java-spi" --query [].appId --output tsv)
35+
36+
if [ -z "$servicePrincipal" ]; then
37+
echo "Service principal not found. Creating service principal"
38+
export servicePrincipal=$(az ad sp create-for-rbac --name "azure-ai-chat-java-spi" --role reader --scopes /subscriptions/"$AZURE_SUBSCRIPTION_ID"/resourceGroups/"$AZURE_RESOURCE_GROUP" --query appId --output tsv)
39+
if [ $? -ne 0 ]; then
40+
echo "Failed to create service principal"
41+
exit $?
42+
fi
43+
export servicePrincipalObjectId=$(az ad sp show --id "$servicePrincipal" --query id --output tsv)
44+
echo "Assigning Roles to service principal azure-ai-chat-java-spi with principal id:$servicePrincipal and object id[$servicePrincipalObjectId]"
45+
for role in "${roles[@]}"; do
46+
47+
echo "Assigning Role[$role] to principal id[$servicePrincipal] for resource[/subscriptions/"$AZURE_SUBSCRIPTION_ID"/resourceGroups/"$AZURE_RESOURCE_GROUP"] "
48+
az role assignment create \
49+
--role "$role" \
50+
--assignee-object-id "$servicePrincipalObjectId" \
51+
--scope /subscriptions/"$AZURE_SUBSCRIPTION_ID"/resourceGroups/"$AZURE_RESOURCE_GROUP" \
52+
--assignee-principal-type ServicePrincipal
53+
done
54+
fi
55+
56+
export servicePrincipalPassword=$(az ad sp credential reset --id "$servicePrincipal" --query password --output tsv)
57+
export servicePrincipalTenant=$(az ad sp show --id "$servicePrincipal" --query appOwnerOrganizationId --output tsv)
58+
59+
echo ""
60+
echo "Starting solution locally using docker compose. "
61+
echo ""
62+
63+
docker compose -f ../../../app/compose.yaml up

0 commit comments

Comments
 (0)