|
1 |
| -import os |
2 |
| -import json |
3 |
| -from azure.identity import DefaultAzureCredential |
4 |
| -from azure.mgmt.resource import ResourceManagementClient |
5 |
| -from azure.mgmt.storage import StorageManagementClient |
6 |
| -from azure.mgmt.web import WebSiteManagementClient |
7 |
| -import shutil |
8 |
| - |
9 |
| - |
10 |
| -def deploy_to_azure_functions(azure_credentials_json, azure_function_path): |
11 |
| - """ |
12 |
| - This function deploys a Python function app to Azure Functions. |
13 |
| -
|
14 |
| - Parameters: |
15 |
| - - azure_credentials_json (str): Path to the Azure credentials |
16 |
| - JSON file. |
17 |
| - - azure_function_path (str): Path to the Python function |
18 |
| - app directory. |
19 |
| -
|
20 |
| - Returns: |
21 |
| - None |
22 |
| - """ |
23 |
| - |
24 |
| - # Load Azure credentials |
25 |
| - with open('azure_credentials.json') as f: |
26 |
| - credentials = json.load(f) |
27 |
| - |
28 |
| - SUBSCRIPTION_ID = credentials['subscriptionId'] |
29 |
| - RESOURCE_GROUP_NAME = "myResourceGroup" |
30 |
| - LOCATION = "eastus" |
31 |
| - STORAGE_ACCOUNT_NAME = "mystorageaccount123" |
32 |
| - FUNCTION_APP_NAME = "my-python-function-app" |
33 |
| - |
34 |
| - # Authenticate using DefaultAzureCredential |
35 |
| - credential = DefaultAzureCredential() |
36 |
| - |
37 |
| - # Clients |
38 |
| - resource_client = ResourceManagementClient(credential, SUBSCRIPTION_ID) |
39 |
| - storage_client = StorageManagementClient(credential, SUBSCRIPTION_ID) |
40 |
| - web_client = WebSiteManagementClient(credential, SUBSCRIPTION_ID) |
41 |
| - |
42 |
| - # Create Resource Group |
43 |
| - resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, |
44 |
| - {"location": LOCATION}) |
45 |
| - |
46 |
| - # Create Storage Account |
47 |
| - storage_client.storage_accounts.begin_create( |
48 |
| - RESOURCE_GROUP_NAME, |
49 |
| - STORAGE_ACCOUNT_NAME, |
50 |
| - { |
51 |
| - "sku": {"name": "Standard_LRS"}, |
52 |
| - "kind": "StorageV2", |
53 |
| - "location": LOCATION |
54 |
| - } |
55 |
| - ).result() |
56 |
| - |
57 |
| - # Create Function App (with a Consumption Plan) |
58 |
| - site_config = { |
59 |
| - "location": LOCATION, |
60 |
| - "server_farm_id": f"/subscriptions/{SUBSCRIPTION_ID}" + |
61 |
| - "/resourceGroups" + |
62 |
| - "/{RESOURCE_GROUP_NAME}/providers/Microsoft.Web/" + |
63 |
| - "serverfarms/{APP_SERVICE_PLAN_NAME}", |
64 |
| - "reserved": True, # This is necessary for Linux-based function apps |
65 |
| - "site_config": { |
66 |
| - "app_settings": [ |
67 |
| - { |
68 |
| - "name": "FUNCTIONS_WORKER_RUNTIME", "value": "python" |
69 |
| - }, |
70 |
| - { |
71 |
| - "name": "AzureWebJobsStorage", |
72 |
| - "value": "DefaultEndpointsProtocol=https;" + |
73 |
| - f"AccountName={STORAGE_ACCOUNT_NAME}" + |
74 |
| - ";AccountKey=account_key>", |
75 |
| - } |
76 |
| - ] |
77 |
| - }, |
78 |
| - "kind": "functionapp", |
79 |
| - } |
80 |
| - |
81 |
| - web_client.web_apps.begin_create_or_update(RESOURCE_GROUP_NAME, |
82 |
| - FUNCTION_APP_NAME, |
83 |
| - site_config).result() |
84 |
| - |
85 |
| - # Zip Function Code |
86 |
| - def zipdir(path, zipfile): |
87 |
| - for root, dirs, files in os.walk(path): |
88 |
| - for file in files: |
89 |
| - zipfile.write(os.path.join(root, file), |
90 |
| - os.path.relpath(os.path.join(root, file), path)) |
91 |
| - |
92 |
| - shutil.make_archive('myfunctionapp', 'zip', 'myfunctionapp/') |
93 |
| - |
94 |
| - # Deploy Function Code |
95 |
| - def deploy_function(): |
96 |
| - with open("myfunctionapp.zip", "rb") as z: |
97 |
| - web_client.web_apps.begin_create_zip_deployment( |
98 |
| - RESOURCE_GROUP_NAME, FUNCTION_APP_NAME, z).result() |
99 |
| - |
100 |
| - deploy_function() |
101 |
| - |
102 |
| - print(f"Function app '{FUNCTION_APP_NAME}' deployed to Azure.") |
| 1 | +# import os |
| 2 | +# import json |
| 3 | +# from azure.identity import DefaultAzureCredential |
| 4 | +# from azure.mgmt.resource import ResourceManagementClient |
| 5 | +# from azure.mgmt.storage import StorageManagementClient |
| 6 | +# from azure.mgmt.web import WebSiteManagementClient |
| 7 | +# import shutil |
| 8 | + |
| 9 | + |
| 10 | +# def deploy_to_azure_functions(azure_credentials_json, azure_function_path): |
| 11 | +# """ |
| 12 | +# This function deploys a Python function app to Azure Functions. |
| 13 | + |
| 14 | +# Parameters: |
| 15 | +# - azure_credentials_json (str): Path to the Azure credentials |
| 16 | +# JSON file. |
| 17 | +# - azure_function_path (str): Path to the Python function |
| 18 | +# app directory. |
| 19 | + |
| 20 | +# Returns: |
| 21 | +# None |
| 22 | +# """ |
| 23 | + |
| 24 | +# # Load Azure credentials |
| 25 | +# with open('azure_credentials.json') as f: |
| 26 | +# credentials = json.load(f) |
| 27 | + |
| 28 | +# SUBSCRIPTION_ID = credentials['subscriptionId'] |
| 29 | +# RESOURCE_GROUP_NAME = "myResourceGroup" |
| 30 | +# LOCATION = "eastus" |
| 31 | +# STORAGE_ACCOUNT_NAME = "mystorageaccount123" |
| 32 | +# FUNCTION_APP_NAME = "my-python-function-app" |
| 33 | + |
| 34 | +# # Authenticate using DefaultAzureCredential |
| 35 | +# credential = DefaultAzureCredential() |
| 36 | + |
| 37 | +# # Clients |
| 38 | +# resource_client = ResourceManagementClient(credential, SUBSCRIPTION_ID) |
| 39 | +# storage_client = StorageManagementClient(credential, SUBSCRIPTION_ID) |
| 40 | +# web_client = WebSiteManagementClient(credential, SUBSCRIPTION_ID) |
| 41 | + |
| 42 | +# # Create Resource Group |
| 43 | +# resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, |
| 44 | +# {"location": LOCATION}) |
| 45 | + |
| 46 | +# # Create Storage Account |
| 47 | +# storage_client.storage_accounts.begin_create( |
| 48 | +# RESOURCE_GROUP_NAME, |
| 49 | +# STORAGE_ACCOUNT_NAME, |
| 50 | +# { |
| 51 | +# "sku": {"name": "Standard_LRS"}, |
| 52 | +# "kind": "StorageV2", |
| 53 | +# "location": LOCATION |
| 54 | +# } |
| 55 | +# ).result() |
| 56 | + |
| 57 | +# # Create Function App (with a Consumption Plan) |
| 58 | +# site_config = { |
| 59 | +# "location": LOCATION, |
| 60 | +# "server_farm_id": f"/subscriptions/{SUBSCRIPTION_ID}" + |
| 61 | +# "/resourceGroups" + |
| 62 | +# "/{RESOURCE_GROUP_NAME}/providers/Microsoft.Web/" + |
| 63 | +# "serverfarms/{APP_SERVICE_PLAN_NAME}", |
| 64 | +# "reserved": True, # This is necessary for Linux-based function apps |
| 65 | +# "site_config": { |
| 66 | +# "app_settings": [ |
| 67 | +# { |
| 68 | +# "name": "FUNCTIONS_WORKER_RUNTIME", "value": "python" |
| 69 | +# }, |
| 70 | +# { |
| 71 | +# "name": "AzureWebJobsStorage", |
| 72 | +# "value": "DefaultEndpointsProtocol=https;" + |
| 73 | +# f"AccountName={STORAGE_ACCOUNT_NAME}" + |
| 74 | +# ";AccountKey=account_key>", |
| 75 | +# } |
| 76 | +# ] |
| 77 | +# }, |
| 78 | +# "kind": "functionapp", |
| 79 | +# } |
| 80 | + |
| 81 | +# web_client.web_apps.begin_create_or_update(RESOURCE_GROUP_NAME, |
| 82 | +# FUNCTION_APP_NAME, |
| 83 | +# site_config).result() |
| 84 | + |
| 85 | +# # Zip Function Code |
| 86 | +# def zipdir(path, zipfile): |
| 87 | +# for root, dirs, files in os.walk(path): |
| 88 | +# for file in files: |
| 89 | +# zipfile.write(os.path.join(root, file), |
| 90 | +# os.path.relpath(os.path.join(root, file), path)) |
| 91 | + |
| 92 | +# shutil.make_archive('myfunctionapp', 'zip', 'myfunctionapp/') |
| 93 | + |
| 94 | +# # Deploy Function Code |
| 95 | +# def deploy_function(): |
| 96 | +# with open("myfunctionapp.zip", "rb") as z: |
| 97 | +# web_client.web_apps.begin_create_zip_deployment( |
| 98 | +# RESOURCE_GROUP_NAME, FUNCTION_APP_NAME, z).result() |
| 99 | + |
| 100 | +# deploy_function() |
| 101 | + |
| 102 | +# print(f"Function app '{FUNCTION_APP_NAME}' deployed to Azure.") |
0 commit comments