Skip to content

Commit

Permalink
Merge pull request Azure-Samples#16 from azure-javaee/240814_azd_enab…
Browse files Browse the repository at this point in the history
…le_gzh

Enable azd support for cargotracker-liberty-aks
  • Loading branch information
edburns authored Sep 24, 2024
2 parents 3a016a1 + ee1db28 commit 9dfe938
Show file tree
Hide file tree
Showing 19 changed files with 802 additions and 2 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/package-helm-chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: package-helm-chart

on:
push:
branches:
- main
paths:
- 'charts/**'

workflow_dispatch:

jobs:
package-helm-chart:
permissions:
contents: read
packages: write

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set environment variables
id: set-variables
run: |
echo "REPOSITORY=ghcr.io/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
echo "VERSION=$(yq -r .version ./charts/cargotracker-liberty-aks/Chart.yaml)" >> "$GITHUB_OUTPUT"
- name: Env variable output
id: test-variables
run: |
echo ${{ steps.set-variables.outputs.REPOSITORY }}
echo ${{ steps.set-variables.outputs.VERSION }}
- name: Login to GitHub Container Registry
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3.1.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Package and push helm chart
run: |
helm package ./charts/cargotracker-liberty-aks --version ${{ steps.set-variables.outputs.VERSION }}
helm push ./cargotracker-liberty-aks-chart-${{ steps.set-variables.outputs.VERSION }}.tgz oci://${{ steps.set-variables.outputs.REPOSITORY }}/charts
publish-helm-chart:
permissions:
id-token: write
packages: write
contents: write
actions: read
deployments: read
pull-requests: read

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Publish Helm chart to GitHub Pages
uses: stefanprodan/helm-gh-pages@0ad2bb377311d61ac04ad9eb6f252fb68e207260 # v1.7.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
linting: off
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
hs_err_pid*

target/*
/custom-values.yaml
/.idea/
96 changes: 94 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Cargo Tracker is a Domain-Driven Design Jakarta EE application. The application
* [Clone Cargo Tracker](#clone-cargo-tracker)
* [Prepare your variables for deployments](#prepare-your-variables-for-deployments)
* [Clone Liberty on AKS Bicep templates](#clone-liberty-on-aks-bicep-templates)
* [Build Liberty on AKS Bicep templates](#build-liberty-on-aks-bicep-templates)
* [Sign in to Azure](#sign-in-to-azure)
* [Create a resource group](#create-a-resource-group)
* [Prepare deployment parameters](#prepare-deployment-parameters)
Expand All @@ -24,6 +25,7 @@ Cargo Tracker is a Domain-Driven Design Jakarta EE application. The application
* [Start monitoring Liberty logs in Azure Log Analytics](#start-monitoring-liberty-logs-in-azure-log-analytics)
* [Start monitoring Cargo Tracker logs in Azure Log Analytics](#start-monitoring-cargo-tracker-logs-in-azure-log-analytics)
* [Unit-2 - Automate deployments using GitHub Actions](#unit-2---automate-deployments-using-github-actions)
* [Unit-3 - Automate deployments using AZD](#unit-3---automate-deployments-using-AZD)
* [Appendix 1 - Exercise Cargo Tracker Functionality](#appendix-1---exercise-cargo-tracker-functionality)
* [Appendix 2 - Learn more about Cargo Tracker](#appendix-2---learn-more-about-cargo-tracker)

Expand Down Expand Up @@ -73,7 +75,7 @@ export DIR="$PWD/cargotracker-liberty-aks"

git clone https://github.com/Azure-Samples/cargotracker-liberty-aks.git ${DIR}/cargotracker
cd ${DIR}/cargotracker
git checkout 20240808
git checkout 20240924
```

If you see a message about `detached HEAD state`, it is safe to ignore. It just means you have checked out a tag.
Expand Down Expand Up @@ -127,7 +129,6 @@ mvn install:install-file -Dfile=${DIR}/azure-javaee-iaas-parent-${VERSION}.pom \

cd ${DIR}/azure.liberty.aks
mvn clean package -DskipTests

```

### Sign in to Azure
Expand Down Expand Up @@ -717,6 +718,97 @@ This job is to build app, push it to ACR and apply it to Open Liberty server run
* Print app URL. Print the cargo tracker URL to pipeline summary page. Now you'are able to access cargo tracker with the URL from your browser.
## Unit-3 - Automate deployments using AZD
Use following steps to automate deployments using the Azure Developer CLI (azd).
### Prerequisites
1. [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd) (azd) installed.
2. Docker installed. You can install Docker by following the instructions [here](https://docs.docker.com/get-docker/).
3. Azure CLI installed. You can install the Azure CLI by following the instructions [here](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli).
4. You have executed the following steps from the preceding units:
1. [Clone Cargo Tracker](#clone-cargo-tracker)
1. [Prepare your variables for deployments](#prepare-your-variables-for-deployments)
1. [Clone Liberty on AKS Bicep templates](#clone-liberty-on-aks-bicep-templates)
1. [Build Liberty on AKS Bicep templates](#build-liberty-on-aks-bicep-templates)
5. Populate the `infra/azure.liberty.aks` directory with the built Liberty on AKS Bicep templates.
```bash
cd infra/azure.liberty.aks
envsubst < parameters_json.template > parameters.json
```

Verify the environment variables have been successfully replaced:

```bash
grep azure.liberty.aks parameters.json
```

You should see no `$` characters in this output. If you see a `$` in the output, ensure you have followed the steps in **Prepare your variables for deployments**.

6. Copy the built Liberty on AKS Bicep templates so azd can invoke them.

```bash
cp -r ${DIR}/azure.liberty.aks/target/bicep/* .
```

### How to Run

1. Change into the directory that has cargo tracker checked out.

```bash
cd ${DIR}/cargotracker
```

1. Run the following command to authenticate with Azure using the Azure CLI.
```bash
az login
```

1. Run the following command to authenticate with Azure using the Azure Developer CLI (azd).
```bash
azd auth login
```

1. Run the following command to create a new environment using the Azure Developer CLI (azd). It's a good idea to use a disambiguation prefix for your environment name, such as your initials and todays date.
```bash
azd env new gzh0919-cargotracker-liberty-aks
```
1. Run the following command to provision the required Azure resources. Input the required parameters when prompted.
* Be sure to select the correct Azure subscription when prompted.
* We observe that `westus` region has a higher likelihood of success than `eastus`.
```bash
azd provision
```
For `administratorLoginPassword` enter `Secret123456`.
When the provisioning completes, you'll see a message similar to the following:

```bash
SUCCESS: Your application was provisioned in Azure in 27 minutes 59 seconds.
```

2. Ensure Docker is running locally. Run the following command to deploy the Cargo Tracker application to Azure Kubernetes Service (AKS) using the Azure Developer CLI (azd).

```bash
azd deploy
```

3. Wait for the deployment to complete. Once the deployment is complete, you can access the Cargo Tracker application using the URL provided in the output.

You can now exercise the Cargo Tracker functionality as shown in Appendix 1.

### Clean up

The steps in this section show you how to clean up and deallocte the resources deployed in the previous section.

1. `azd down`


## Appendix 1 - Exercise Cargo Tracker Functionality

1. On the main page, inspect the date timestamp, it should reflect today's date. For example, **3.2 2024-08-06 17:48:08**.
Expand Down
8 changes: 8 additions & 0 deletions azd-hooks/postdeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export GATEWAY_PUBLICIP_ID=$(az network application-gateway list \
--resource-group ${RESOURCE_GROUP_NAME} \
--query '[0].frontendIPConfigurations[0].publicIPAddress.id' -o tsv)
export GATEWAY_HOSTNAME=$(az network public-ip show --ids ${GATEWAY_PUBLICIP_ID} --query 'dnsSettings.fqdn' -o tsv)
export CARGO_TRACKER_URL="http://${GATEWAY_HOSTNAME}/cargo-tracker/"
echo "Cargo Tracker URL: ${CARGO_TRACKER_URL}"

kubectl rollout restart deployment/cargo-tracker-cluster
84 changes: 84 additions & 0 deletions azd-hooks/postprovision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# enable Helm support
azd config set alpha.aks.helm on

echo "Create Helm repository"
HELM_REPO_URL="https://azure-javaee.github.io/cargotracker-liberty-aks"
HELM_REPO_NAME="cargotracker-liberty-aks"

# Check if the repo exists before removing
if helm repo list | grep -q "${HELM_REPO_NAME}"; then
helm repo remove ${HELM_REPO_NAME}
echo "Repo '${HELM_REPO_NAME}' removed."
else
echo "Repo '${HELM_REPO_NAME}' not found in the list."
fi

helm repo add ${HELM_REPO_NAME} ${HELM_REPO_URL}


export AKS_NAME=$(az aks list -g ${RESOURCE_GROUP_NAME} --query \[0\].name -o tsv)

az aks enable-addons \
--addons monitoring \
--name ${AKS_NAME} \
--resource-group ${RESOURCE_GROUP_NAME} \
--workspace-resource-id ${WORKSPACE_ID}

echo "Provision postgresql server"
az postgres flexible-server create \
--resource-group ${RESOURCE_GROUP_NAME} \
--name ${DB_RESOURCE_NAME} \
--location ${LOCATION} \
--admin-user ${DB_USER_NAME} \
--admin-password ${DB_USER_PASSWORD} \
--version 15 --public-access 0.0.0.0 \
--tier Burstable \
--sku-name Standard_B1ms \
--yes

echo "Provision postgresql database"
az postgres flexible-server db create \
--resource-group ${RESOURCE_GROUP_NAME} \
--server-name ${DB_RESOURCE_NAME} \
--database-name ${DB_NAME}

echo "Allow Access to Azure Services"
az postgres flexible-server firewall-rule create \
-g ${RESOURCE_GROUP_NAME} \
-n ${DB_RESOURCE_NAME} \
-r "AllowAllWindowsAzureIps" \
--start-ip-address "0.0.0.0" \
--end-ip-address "0.0.0.0"

az postgres flexible-server parameter set --name max_prepared_transactions --value 10 -g ${RESOURCE_GROUP_NAME} --server-name ${DB_RESOURCE_NAME}
az postgres flexible-server restart -g ${RESOURCE_GROUP_NAME} --name ${DB_RESOURCE_NAME}

run_maven_command() {
mvn -q -Dexec.executable=echo -Dexec.args="$1" --non-recursive exec:exec 2>/dev/null | sed -e 's/\x1b\[[0-9;]*m//g' | tr -d '\r\n'
}

IMAGE_NAME=$(run_maven_command '${project.artifactId}')
IMAGE_VERSION=$(run_maven_command '${project.version}')

##########################################################
# Create the custom-values.yaml file
##########################################################
cat << EOF > custom-values.yaml
appInsightConnectionString: ${APP_INSIGHTS_CONNECTION_STRING}
loginServer: ${AZURE_REGISTRY_NAME}
imageName: ${IMAGE_NAME}
imageTag: ${IMAGE_VERSION}
EOF

##########################################################
# DB
##########################################################
cat << EOF >> custom-values.yaml
namespace: ${AZURE_AKS_NAMESPACE}
db:
ServerName: ${DB_RESOURCE_NAME}.postgres.database.azure.com
PortNumber: 5432
Name: ${DB_NAME}
User: ${DB_USER_NAME}
Password: ${DB_USER_PASSWORD}
EOF
23 changes: 23 additions & 0 deletions azd-hooks/predeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Build and push docker image to ACR
echo "Get image name and version......"

run_maven_command() {
mvn -q -Dexec.executable=echo -Dexec.args="$1" --non-recursive exec:exec 2>/dev/null | sed -e 's/\x1b\[[0-9;]*m//g' | tr -d '\r\n'
}

IMAGE_NAME=$(run_maven_command '${project.artifactId}')
IMAGE_VERSION=$(run_maven_command '${project.version}')

echo "Docker build and push to ACR Server ${ACR_SERVER} with image name ${IMAGE_NAME} and version ${IMAGE_VERSION}"

mvn clean package -DskipTests
cd target

docker login -u ${ACR_PASSWORD} -p ${ACR_PASSWORD} ${ACR_SERVER}

export DOCKER_BUILDKIT=1
docker buildx create --use
docker buildx build --platform linux/amd64 -t ${ACR_SERVER}/${IMAGE_NAME}:${IMAGE_VERSION} --pull --file=Dockerfile . --load
docker push ${ACR_SERVER}/${IMAGE_NAME}:${IMAGE_VERSION}
1 change: 1 addition & 0 deletions azd-hooks/preprovision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
az extension add --upgrade -n application-insights
36 changes: 36 additions & 0 deletions azure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json

name: cargotracker-liberty-aks-demo
metadata:
template: [email protected]
hooks:
postprovision:
posix:
shell: sh
continueOnError: false
interactive: true
run: azd-hooks/postprovision.sh
predeploy: # This hook is executed before the deployment of the application to create the custom-values.yaml file
posix:
shell: sh
continueOnError: false
interactive: true
run: azd-hooks/predeploy.sh
postdeploy: # This hook is executed after the deployment of the application to create the custom-values.yaml file
posix:
shell: sh
continueOnError: false
interactive: true
run: azd-hooks/postdeploy.sh

services:
demo:
host: aks
k8s:
namespace: default
helm:
releases:
- name: demo
chart: cargotracker-liberty-aks/cargotracker-liberty-aks-chart
version: 1.0.5
values: custom-values.yaml # This file is created by the predeploy hook
23 changes: 23 additions & 0 deletions charts/cargotracker-liberty-aks/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
Loading

0 comments on commit 9dfe938

Please sign in to comment.