Skip to content

Commit

Permalink
Init container debug
Browse files Browse the repository at this point in the history
  • Loading branch information
lrakai committed Oct 13, 2023
1 parent f26a7f0 commit 989d1ed
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 21 deletions.
27 changes: 27 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build and Push Docker Image

on:
push

env:
IMAGE_NAME: ghcr.io/cloudacademy/azure-vcf

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set outputs
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ env.IMAGE_NAME }}:${{ github.ref_name }},${{ env.IMAGE_NAME }}:{{ github.ref_name }}-${{ steps.vars.outputs.sha_short }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
venv/
__pycache__
.DS_Store
.DS_Store
.config.env
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
"AZURE_TENANT_ID": "INSERT_AZURE_TENANT_ID",
"AZURE_RESOURCE_GROUP": "INSERT_AZURE_RESOURCE_GROUP"
}
},
{
"name": "Docker: Python - General",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "general"
}
}
]
}
26 changes: 26 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "azurevcfenv:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"file": "src/entry.py"
}
}
]
}
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.9-slim

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install dependencies for pip install and clean up
RUN apt-get install debian-archive-keyring && \
apt-get update --allow-insecure-repositories && \
apt-get install -y build-essential libssl-dev libffi-dev python-dev-is-python3 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Install dev dependencies
RUN python -m pip install --upgrade python-dotenv==1.0.0

# Install pip requirements
COPY requirements.txt prune_azure_mgmt_libs.sh /
RUN python -m pip install -r requirements.txt
# Prune libs like in prod to keep lambda layer size down
RUN bash prune_azure_mgmt_libs.sh

WORKDIR /app
COPY . /app
# move config.env to .env if .config.env exists
RUN test -f .config.env && mv .config.env .env

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "src/entry.py"]
22 changes: 2 additions & 20 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -xe
set -e

BITBUCKET_USER=YOUR_BITBUCKET_USER
VCF_BITBUCKET_CLONE_URL=https://$BITBUCKET_USER@bitbucket.org/cloudacademy/labs-vcf-boilerplates.git
Expand All @@ -25,22 +25,4 @@ pip install --upgrade pip
pip install pylint autopep8 # dev dependencies
pip install -r requirements.txt # prod dependencies

# Trim Azure mgmt packages included api versions
keep_api_versions=1
mgmt_client_dir=venv/lib/python*/site-packages/azure/mgmt
skip_clients=( "eventhub" "monitor" "keyvault") # skip clients requiring more than latest version of the API
for client_dir in $mgmt_client_dir/*; do
if [[ " ${skip_clients[*]} " =~ " $(basename ${client_dir}) " ]]; then
echo "skipping pruning $client_dir"
continue
fi
old_IFS=$IFS; IFS=$'\n'
api_dirs=($(find $client_dir -maxdepth 1 -type d -regex "$client_dir/v[0-9][0-9][0-9][0-9].*" | sort))
unset IFS; IFS=$old_IFS
if [[ ${#api_dirs[@]} -le $keep_api_versions ]]; then
continue
fi
for i in $(seq 0 1 $(( ${#api_dirs[@]} - $(( $keep_api_versions + 1 )) )) ); do
rm -rf ${api_dirs[i]}
done
done
source prune_azure_mgmt_libs.sh
22 changes: 22 additions & 0 deletions prune_azure_mgmt_libs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -e

# Trim Azure mgmt packages included api versions
keep_api_versions=1
mgmt_client_dir=venv/lib/python*/site-packages/azure/mgmt
skip_clients=( "eventhub" "monitor" "keyvault") # skip clients requiring more than latest version of the API
for client_dir in $mgmt_client_dir/*; do
if [[ " ${skip_clients[*]} " =~ " $(basename ${client_dir}) " ]]; then
echo "skipping pruning $client_dir"
continue
fi
old_IFS=$IFS; IFS=$'\n'
api_dirs=($(find $client_dir -maxdepth 1 -type d -regex "$client_dir/v[0-9][0-9][0-9][0-9].*" | sort))
unset IFS; IFS=$old_IFS
if [[ ${#api_dirs[@]} -le $keep_api_versions ]]; then
continue
fi
for i in $(seq 0 1 $(( ${#api_dirs[@]} - $(( $keep_api_versions + 1 )) )) ); do
rm -rf ${api_dirs[i]}
done
done
21 changes: 21 additions & 0 deletions src/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from dotenv import load_dotenv
load_dotenv()

import time
from config import EVENT_CONFIG
from vcf import handler


def timed_handler(event, context):
start = time.time()
result = handler(event, context)
end = time.time()
print(end - start)
return result

def entry():
result = timed_handler(EVENT_CONFIG, None)
print(result)

if __name__ == "__main__":
entry()

0 comments on commit 989d1ed

Please sign in to comment.