使用最新的 git 存储库更改更新所有 Sagemaker Jupyter Notebooks

问题描述 投票:0回答:1

我有多个为培训课程创建的 Sagemaker 笔记本。在新课程开始之前,我需要对笔记本进行更改。到目前为止,我认为执行此操作的唯一方法是使用更改更新存储库,然后单独进入每个笔记本实例并执行 git pull 来更新更改。有没有办法同时对与该存储库关联的所有笔记本执行此操作?

git jupyter-notebook amazon-sagemaker
1个回答
0
投票

我想不出一种方法可以让 git 将更改单独拉入每个实例,但为了使其更容易(无论是现在还是将来),您可以考虑使用 Lifecycle Configuration

程序看起来像这样:

  1. 创建生命周期配置。在笔记本启动时,它应该运行一个 shell 脚本来从您的存储库中提取
  2. 编写并运行脚本以将生命周期配置添加到每个笔记本。比如:
import boto3
import time

lifecycle_config_name='YOUR_CONFIG_NAME'
sagemaker = boto3.client('sagemaker', region_name='us-west-2')
notebook_names = list(map(lambda x: x['NotebookInstanceName'], sagemaker.list_notebook_instances()['NotebookInstances']))


def stop_notebook(notebook_name):
    print(f'Stopping {notebook_name}')
    sagemaker.stop_notebook_instance(NotebookInstanceName=notebook_name)
    while True:
        if sagemaker.describe_notebook_instance(NotebookInstanceName=notebook_name)['NotebookInstanceStatus'] == 'Stopped':
            break
        else:
            time.sleep(5)

def update_notebook(notebook_name):
    print(f'Updating {notebook_name}')
    sagemaker.update_notebook_instance(NotebookInstanceName=notebook_name, LifecycleConfigName=lifecycle_config_name)
    while True:
        if sagemaker.describe_notebook_instance(NotebookInstanceName=notebook_name)['NotebookInstanceStatus'] != 'Updating':
            break
        else:
            time.sleep(5)

for notebook_name in notebook_names:
    if sagemaker.describe_notebook_instance(NotebookInstanceName=notebook_name)['NotebookInstanceStatus'] == 'Pending':
        stop_notebook(notebook_name)
    update_notebook(notebook_name)
    sagemaker.start_notebook_instance(NotebookInstanceName=notebook_name)

© www.soinside.com 2019 - 2024. All rights reserved.