Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.markdown("""
|
3 |
+
|
4 |
+
Create a Terraform script that spins up an Azure Container Registry, a Dockerfile that pulls ubuntu latest python and then pulls a github repository, creates a github action for it when it updates to create a new web app inside the azure container registry. Show python and terraform code to automate this.
|
5 |
+
|
6 |
+
To accomplish this task, you'll need to create the following components:
|
7 |
+
|
8 |
+
An Azure Container Registry to store the Docker images.
|
9 |
+
A Dockerfile that pulls the latest Ubuntu and Python images and then pulls the code from the GitHub repository.
|
10 |
+
A GitHub Action that triggers the creation of a new web app inside the Azure Container Registry when the code in the GitHub repository is updated.
|
11 |
+
A Terraform script to automate the deployment of these components.
|
12 |
+
Here is the Terraform script that you can use to deploy an Azure Container Registry:
|
13 |
+
|
14 |
+
terraform
|
15 |
+
Copy code
|
16 |
+
provider "azurerm" {
|
17 |
+
features {}
|
18 |
+
}
|
19 |
+
|
20 |
+
resource "azurerm_resource_group" "example" {
|
21 |
+
name = "example-resource-group"
|
22 |
+
location = "East US"
|
23 |
+
}
|
24 |
+
|
25 |
+
resource "azurerm_container_registry" "example" {
|
26 |
+
name = "example-container-registry"
|
27 |
+
resource_group_name = azurerm_resource_group.example.name
|
28 |
+
location = azurerm_resource_group.example.location
|
29 |
+
sku = "Premium"
|
30 |
+
admin_enabled = true
|
31 |
+
georeplication_locations = ["East US 2"]
|
32 |
+
}
|
33 |
+
This script creates an Azure resource group and an Azure Container Registry.
|
34 |
+
|
35 |
+
Next, here is the Dockerfile that pulls the latest Ubuntu and Python images and then pulls the code from the GitHub repository:
|
36 |
+
|
37 |
+
dockerfile
|
38 |
+
Copy code
|
39 |
+
FROM ubuntu:latest
|
40 |
+
|
41 |
+
RUN apt-get update && \
|
42 |
+
apt-get install -y python3 python3-pip && \
|
43 |
+
rm -rf /var/lib/apt/lists/*
|
44 |
+
|
45 |
+
RUN mkdir /app
|
46 |
+
WORKDIR /app
|
47 |
+
|
48 |
+
COPY requirements.txt /app
|
49 |
+
RUN pip3 install -r requirements.txt
|
50 |
+
|
51 |
+
COPY . /app
|
52 |
+
This Dockerfile installs Python 3 and its dependencies, creates a directory for the application, copies the requirements file and installs the dependencies. It then copies the entire project into the container.
|
53 |
+
|
54 |
+
Finally, here is the Python code to deploy a new web app inside the Azure Container Registry:
|
55 |
+
|
56 |
+
python
|
57 |
+
Copy code
|
58 |
+
import os
|
59 |
+
import subprocess
|
60 |
+
|
61 |
+
az_acr_name = os.environ.get("AZ_ACR_NAME")
|
62 |
+
|
63 |
+
def build_and_push_docker_image():
|
64 |
+
subprocess.call(f"docker build -t {az_acr_name}.azurecr.io/myapp:latest .", shell=True)
|
65 |
+
subprocess.call(f"docker push {az_acr_name}.azurecr.io/myapp:latest", shell=True)
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
68 |
+
build_and_push_docker_image()
|
69 |
+
This Python code builds a new Docker image for the app and then pushes it to the Azure Container Registry.
|
70 |
+
|
71 |
+
Finally, here is the GitHub Action that triggers the creation of a new web app inside the Azure Container Registry:
|
72 |
+
|
73 |
+
yaml
|
74 |
+
Copy code
|
75 |
+
name: Build and Push Docker Image
|
76 |
+
|
77 |
+
on:
|
78 |
+
push:
|
79 |
+
branches:
|
80 |
+
- main
|
81 |
+
|
82 |
+
env:
|
83 |
+
AZ_ACR_NAME: myacrname
|
84 |
+
|
85 |
+
jobs:
|
86 |
+
build-and-push:
|
87 |
+
runs-on: ubuntu-latest
|
88 |
+
steps:
|
89 |
+
- name: Checkout code
|
90 |
+
uses: actions/checkout@v2
|
91 |
+
|
92 |
+
- name: Login to Azure
|
93 |
+
uses: azure/login@v1
|
94 |
+
with:
|
95 |
+
creds: ${{ secrets.AZURE_CREDENTIALS }}
|
96 |
+
|
97 |
+
- name: Build and Push Docker Image
|
98 |
+
run: python3 build_and_push.py
|
99 |
+
env:
|
100 |
+
AZ_ACR_NAME: ${{ env.AZ_ACR_NAME }}
|
101 |
+
This GitHub Action triggers when code is pushed to the main branch. It checks out the code, logs into Azure using the provided credentials, and then runs the Python script to build and push the Docker image to the Azure Container Registry.
|
102 |
+
|
103 |
+
Finally, here is the Terraform script that automates the deployment of the components:
|
104 |
+
|
105 |
+
terraform
|
106 |
+
Copy code
|
107 |
+
provider "azurerm" {
|
108 |
+
features {}
|
109 |
+
}
|
110 |
+
|
111 |
+
resource "azurerm_resource_group" "
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
""")
|