可以将Flask部署到Azure并使用外部目录吗?

huangapple go评论76阅读模式
英文:

Is it possible to deploy Flask to Azure with external directories?

问题

以下是已翻译的部分:

让我们假设我有以下的目录结构

root/
- app/
  - flask_app.py
  - requirements.txt
- shared_libs/
  - lib.py
- others/

共享文件夹用于 flask_app.py 和其他可以引用它的处理。由于它存储在 flask_app 外部,只能通过将父工作目录(root)添加到 sys.path 中在本地运行

import sys
sys.path.append('..')
import os
from shared_libs.lib import ExampleLib

通常,仅使用 app/ 部署到 Azure 很容易实现,但在这种情况下,我想将这些 app/shared_libs/ 压缩成一个 zip 文件,随后将默认运行程序指向 app/flask_app.py

我不确定如何做到这一点,如果您能给我一些想法,我将不胜感激。

英文:

Let's say I have following structure

root/
- app/
  - flask_app.py
  - requirements.txt
- shared_libs/
  - lib.py
- others/

The shared folder is used for either flask_app.py and other processing that can reference to. As it is stored outside of the flask_app, it could only run in local by adding parent working directory (root) to sys.path

import sys
sys.path.append('..')
import os
from shared_libs.lib import ExampleLib

Typically, the deployment to Azure with only app/ is easy to achieve, but in this case I would like to pick these app/ and shared_libs/ to compress to a zip file, subsequently point default runner to app/flask_app.py

I am not sure which technique to do it, thank if you could give me some ideas.

答案1

得分: 0

以下是翻译好的部分:

"Seems quite specific for my case but I believe my answer would be helpful for someone would like to achieve it."

"对于我的情况来说似乎相当具体,但我相信我的答案对于想要实现这个目标的人会有帮助。"

"Following above structure is fine for local running but for Azure webapp this is not enough, particularly in requirements.txt should be put under root folder"

"遵循上述结构在本地运行是可以的,但对于 Azure webapp 来说还不够,特别是 requirements.txt 应该放在根目录下。"

"Could not find setup.py or requirements.txt; Not running pip install.": The Oryx build process failed to find your requirements.txt file.

"找不到 setup.py 或 requirements.txt;不运行 pip 安装。":Oryx 构建过程无法找到您的 requirements.txt 文件。

"Connect to the web app's container via SSH and verify that requirements.txt is named correctly and exists directly under site/wwwroot. If it doesn't exist, make site the file exists in your repository and is included in your deployment. If it exists in a separate folder, move it to the root."

"通过 SSH 连接到 Web 应用程序的容器,验证 requirements.txt 的命名是否正确,是否直接存在于 site/wwwroot 目录下。如果不存在,请确保文件存在于您的存储库中并包含在部署中。如果它存在于一个单独的文件夹中,请将其移动到根目录下。"

"So I was going to create .deploy folder and move these elements"

"所以我打算创建一个 .deploy 文件夹并移动这些元素"

"Then compress it to zip file, noticed that zip content is including child of .deploy, not folder .deploy"

"然后将其压缩为 zip 文件,注意到 zip 内容包括 .deploy 的子文件夹,而不是 .deploy 文件夹本身"

"Next, if your Azure is using gunicorn, it is good to start testing by python or you can assume it is gunicorn as default."

"接下来,如果您的 Azure 使用 gunicorn,最好通过 python 开始测试,或者您可以假设它默认使用 gunicorn。"

"os.environ.get("SERVER_SOFTWARE", "")"

"os.environ.get("SERVER_SOFTWARE", "")"

"I was using this gunicorn to point to the app folder"

"我使用了这个 gunicorn 来指向 app 文件夹"

"My final Powershell script"

"我的最终 Powershell 脚本"

英文:

Seems quite specific for my case but I believe my answer would be helpful for someone would like to achieve it.

root/
 - app/
   - flask_app.py
   - requirements.txt
 - libs/
   - lib.py
 - others/

Following above structure is fine for local running but for Azure webapp this is not enough, particularly in requirements.txt should be put under root folder

https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#could-not-find-setuppy-or-requirementstxt

> "Could not find setup.py or requirements.txt; Not running pip install.": The Oryx build process failed to find your requirements.txt file.
>
> Connect to the web app's container via SSH and verify that requirements.txt is named correctly and exists directly under site/wwwroot. If it doesn't exist, make site the file exists in your repository and is included in your deployment. If it exists in a separate folder, move it to the root.

So I was going to create .deploy folder and move these elements

root/
 - .deploy/
   - app/*
   - libs/*
   - requirements.txt
 - app/
   - flask_app.py
   - requirements.txt
 - libs/
   - lib.py
 - others/root/

Then compress it to zip file, noticed that zip content is including child of .deploy, not folder .deploy

Next, if your Azure is using gunicorn, it is good to start testing by python or you can assume it is gunicorn as default.

os.environ.get("SERVER_SOFTWARE", "")
# gunicorn/20.1.0

I was using this gunicorn to point to the app folder

gunicorn --bind=0.0.0.0:8000 --chdir app flask_app:app

My final Powershell script

# Azure deployment variables
$resourceGroupName = "YOUR_RESOURCE_GROUP_NAME"
$appName = "YOUR_APP_NAME"
$zipFilePath = "app.zip"

$mainDir = "app"
$libDir = "libs"

$deploymentDir = ".deploy"

# Step 1: Create the deployment directory
# Remove previous
if (Test-Path -Path $deploymentDir) {
    Remove-Item -Path $deploymentDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $deploymentDir | Out-Null

# Step 2: Copy the necessary files
Copy-Item -Path $mainDir -Destination $deploymentDir -Recurse -Force
Copy-Item -Path $libDir -Destination "$deploymentDir/$libDir" -Recurse -Force
Copy-Item -Path "$mainDir/requirements.txt" -Destination "$deploymentDir" -Force

# Step 3: Create the zip file
Compress-Archive -Path $deploymentDir/* -DestinationPath $zipFilePath -Force

# Step 4: Deploy the zip file
az webapp config appsettings set --name "$appName" --resource-group "$resourceGroupName" --settings SCM_DO_BUILD_DURING_DEPLOYMENT=true
az webapp deploy --name $appName --resource-group $resourceGroupName --src-path $zipFilePath --type zip
az webapp config set --name "$appName" --resource-group "$resourceGroupName" --startup-file "gunicorn --bind=0.0.0.0:8000 --chdir $mainDir flask_app:app"

# Step 5: Clean up the deployment files
Remove-Item -Path $zipFilePath -Force
Remove-Item -Path $deploymentDir -Recurse

huangapple
  • 本文由 发表于 2023年5月26日 16:44:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定