跳转到主要内容

Batch Apps Python客户端

项目描述

此包旨在使Azure Batch Apps客户能够使用Python与Management API进行交互。

此客户端模块旨在与现有Batch Apps服务内设置的应用程序一起工作。您可以通过Batch Apps门户上传您的应用程序映像和云组件。有关设置此内容的更多信息,请参阅此篇文章

许可

此项目采用MIT许可。有关详细信息,请参阅LICENSE.txt或访问https://open-source.org.cn/licenses/MIT

安装

此包已与Python 2.6、2.7、3.2、3.3和3.4进行了测试

>> pip install azure-batch-apps

所需包

文档

该文档由Sphinx生成,可以在项目根目录中找到压缩包。它也托管在这里

发布历史

有关更改的完整摘要,请参阅CHANGES.txt

  • 2015-07-15 - 0.5.2
    • 修复了下载流中的错误

  • 2015-07-09 - 0.5.1
    • 添加了对无人值守令牌的自动刷新

    • 暴露了块大小配置,以改变上传和下载回调频率

  • 2015-07-01 - 0.5.0
    • 为文件上传和下载添加了进度回调

    • 移除了创建池的3个实例最小限制

  • 2015-05-11 - 0.4.0
    • 添加了可选的认证配置验证

    • 添加了JobSubmission.settings属性

  • 2015-01-13 - 0.3.0
    • 添加了对Batch Apps池管理的初步支持

  • 2014-11-26 - 0.2.0
    • 更改了文件上传格式

    • 更改了认证配置格式

    • 更改术语:应用程序到作业类型

    • 更改术语:服务主体到无人值守帐户

    • 添加了FileCollection.index方法

    • 改进了对Configuration中缺失认证值的处理

  • 2014-11-03 - 0.1.1
    • 身份验证错误修复

  • 2014-10-28 - 0.1.0
    • beta版本发布

使用说明

应用程序配置

为了与Batch Apps账户中设置的服务交互,您需要配置Python客户端。

第一次实例化Configuration对象时,配置文件将默认创建为

$HOME/BatchAppsData/batch_apps.ini

单个配置对象代表Batch Apps账户中的单个服务。这意味着每个配置都需要一个端点和客户端ID。

要设置新的作业类型引用,您可以将其添加到配置文件中,并附带您希望与之关联的任何自定义参数。

您可以直接编辑该文件,或者通过Configuration类编辑

from batchapps import Configuration

# These can be retrieved when creating an unattended account in the Batch Apps portal.
# See the authentication section below for more details.
endpoint = 'myservice.batchapps.core.windows.net'
account_details = 'ClientID=xxxxxxxx;TenantID=abcdefg'
account_key = '12345'

cfg = Configuration(log_level='debug', default=True)
cfg.aad_config(account=account_details, key=account_key,
        endpoint=endpoint, unattended=True)

cfg.add_jobtype('my_job_type')

# Set this job type as the current job type
cfg.current_jobtype('my_job_type')

# Set the current job type as the default job type for future jobs
cfg.set_default_jobtype()

# Set up some default parameter values for the current job type
cfg.set('quality', 10)
cfg.set('timeout', 500)

# Save updated configuration to file
cfg.save_config()

身份验证

该模块通过Azure Active Directory(OAuth2的一种实现)进行身份验证。Batchapps模块提供了一个辅助类,用于使用Requests-OAuthlib检索AAD令牌。但是,如果您有首选的OAuth实现,也可以使用此模块进行身份验证。

您可以在您的Batch Apps账户中创建一组“无人值守账户”凭据。这些凭据的格式为

Account Id = ClientId=abc;TenantId=xyz
Account Key = ***********************

获取这些凭据后,您可以通过将它们添加到batch_apps.ini配置文件中(使用Python,如上所述,或直接编辑文件)来通过Python客户端进行身份验证

[Authentication]
endpoint = myservice.batchapps.core.windows.net
unattended_account = ClientID=abc;TenantID=xyz
unattended_key = ***********************

然后您可以使用这些凭据进行身份验证

from batchapps import AzureOAuth

creds = AzureOAuth.get_unattended_session()

或者,如果您使用不同的AAD实现来获取令牌

from batchapps import Credentials, Configuration
import my_oauth

client_id = "abc"
cfg = Configuration()

aad_token = my_oauth.get_token(client_id)
creds = Credentials(cfg, client_id, token=aad_token)

将通过Web UI登录进行身份验证的功能将很快支持。

作业管理

作业管理,包括提交、监控和访问输出,是通过JobManager类完成的

from batchapps import AzureOAuth, JobManager
import time

creds = AzureOAuth.get_unattended_session()
mgr = JobManager(creds)

my_job = mgr.create_job("First Job")

# Apply any custom parameters and source files here
my_job.example_parameter = "test123"

# Then submit the job
new_job = my_job.submit()

job_progress = mgr.get_job(url=new_job['link'])

# Let's allow up to 30 minutes for the job to complete
timeout = time.time() + 1800

while time.time() < timeout:

        if job_progress.status is 'Complete':
                job_progress.get_output('c:\\my_download_dir')
                break

        if job_progress.status is 'Error':
                break

        time.sleep(30)
        job_progress.update()

else:
        job_progress.cancel()

文件管理

文件管理,包括同步作业源文件和依赖项到云端,可以使用FileManager类完成

from batchapps import AzureOAuth, FileManager

creds = AzureOAuth.get_unattended_session()
mgr = FileManager(creds)

file_collection = mgr.files_from_dir('c:\\my_job_assets')
job_source = mgr.file_from_path('C:\\start_job.bat')
file_collection.add(job_source)

file_collection.upload()

# Check files previously uploaded matching a certain name
mgr.find_files('start_job.bat')

# Retrieve a list of all uploaded files
mgr.list_files()

池管理

池管理,包括创建、调整大小和删除池,可以使用PoolManager类完成。

一旦创建了池,就可以向其提交作业。默认情况下,当作业提交时没有引用现有池,它将使用为作业运行而创建的自动池,并在完成时删除。

from batchapps import AzureOAuth, PoolManager

creds = AzureOAuth.get_unattended_session()
mgr = PoolManager(creds)

new_pool = mgr.create_pool(target_size=5)

# Create new job submission, then submit to pool
my_job.pool = new_pool
my_job.submit()

# After job has completed, and we no longer need the pool
pool.delete()

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

azure-batch-apps-0.5.2.tar.gz (319.3 kB 查看哈希值)

上传时间

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面