From b6e30f43b9b960a15eeb19dcdbb6fc5907e9a0ac Mon Sep 17 00:00:00 2001 From: Harneet Virk Date: Fri, 6 Jan 2023 15:07:11 -0800 Subject: [PATCH] Refactor the script for adding identity with compute (#2014) * Refactor the script for adding identity with compute --- ...oy-mlcompute-create-with-user-identity.yml | 49 ------------------- ...loy-mlcompute-create-with-user-identity.sh | 2 - ...eploy-mlcompute-update-to-user-identity.sh | 32 +++++++++++- infra/cleanup.sh | 1 + 4 files changed, 31 insertions(+), 53 deletions(-) delete mode 100644 .github/workflows/cli-scripts-deploy-mlcompute-create-with-user-identity.yml delete mode 100644 cli/deploy-mlcompute-create-with-user-identity.sh diff --git a/.github/workflows/cli-scripts-deploy-mlcompute-create-with-user-identity.yml b/.github/workflows/cli-scripts-deploy-mlcompute-create-with-user-identity.yml deleted file mode 100644 index 3bedfd6bbde..00000000000 --- a/.github/workflows/cli-scripts-deploy-mlcompute-create-with-user-identity.yml +++ /dev/null @@ -1,49 +0,0 @@ -# This code is autogenerated. -# Code is generated by running custom script: python3 readme.py -# Any manual changes to this file may cause incorrect behavior. -# Any manual changes will be overwritten if the code is regenerated. - -name: cli-scripts-deploy-mlcompute-create-with-user-identity -on: - workflow_dispatch: - schedule: - - cron: "0 0/8 * * *" - pull_request: - branches: - - main - paths: - - cli/deploy-mlcompute-create-with-user-identity.sh - - infra/** - - .github/workflows/cli-scripts-deploy-mlcompute-create-with-user-identity.yml - - cli/setup.sh -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: check out repo - uses: actions/checkout@v2 - - name: azure login - uses: azure/login@v1 - with: - creds: ${{secrets.AZUREML_CREDENTIALS}} - - name: bootstrap resources - run: | - bash bootstrap.sh - working-directory: infra - continue-on-error: false - - name: setup-cli - run: | - source "${{ github.workspace }}/infra/sdk_helpers.sh"; - source "${{ github.workspace }}/infra/init_environment.sh"; - bash setup.sh - working-directory: cli - continue-on-error: true - - name: test script script - run: | - source "${{ github.workspace }}/infra/sdk_helpers.sh"; - source "${{ github.workspace }}/infra/init_environment.sh"; - set -e; bash -x deploy-mlcompute-create-with-user-identity.sh - working-directory: cli diff --git a/cli/deploy-mlcompute-create-with-user-identity.sh b/cli/deploy-mlcompute-create-with-user-identity.sh deleted file mode 100644 index 051f39f0a38..00000000000 --- a/cli/deploy-mlcompute-create-with-user-identity.sh +++ /dev/null @@ -1,2 +0,0 @@ -IDENTITY=$(az identity create -n my-cluster-identity --query id -o tsv) -az ml compute create --name mycluster --type amlcompute --identity-type user_assigned --user-assigned-identities $IDENTITY diff --git a/cli/deploy-mlcompute-update-to-user-identity.sh b/cli/deploy-mlcompute-update-to-user-identity.sh index 23f06a2c505..84b7410fe57 100644 --- a/cli/deploy-mlcompute-update-to-user-identity.sh +++ b/cli/deploy-mlcompute-update-to-user-identity.sh @@ -1,2 +1,30 @@ -IDENTITY=$(az identity create -n my-cluster-identity --query id -o tsv) -az ml compute update --name mycluster --identity-type user_assigned --user-assigned-identities $IDENTITY +export MSI_NAME=my-cluster-identity +export COMPUTE_NAME=mycluster-msi + +does_compute_exist() +{ + if [ -z $(az ml compute show -n $COMPUTE_NAME --query name) ]; then + echo false + else + echo true + fi +} + +echo "Creating MSI $MSI_NAME" +# Get the resource id of the identity +IDENTITY_ID=$(az identity show --name "$MSI_NAME" --query id -o tsv | tail -n1 | tr -d "[:cntrl:]" || true) +if [[ -z $IDENTITY_ID ]]; then + IDENTITY_ID=$(az identity create -n "$MSI_NAME" --query id -o tsv | tail -n1 | tr -d "[:cntrl:]") +fi +echo "MSI created: $MSI_NAME" +sleep 15 # Let the previous command finish: https://github.com/Azure/azure-cli/issues/8530 + + +echo "Checking if compute $COMPUTE_NAME already exists" +if [ "$(does_compute_exist)" == "true" ]; then + echo "Skipping, compute: $COMPUTE_NAME exists" +else + echo "Provisioning compute: $COMPUTE_NAME" + az ml compute create --name "$COMPUTE_NAME" --type amlcompute --identity-type user_assigned --user-assigned-identities "$IDENTITY_ID" +fi +az ml compute update --name "$COMPUTE_NAME" --identity-type user_assigned --user-assigned-identities "$IDENTITY_ID" diff --git a/infra/cleanup.sh b/infra/cleanup.sh index 135ac4deeef..154a83aa21f 100644 --- a/infra/cleanup.sh +++ b/infra/cleanup.sh @@ -68,6 +68,7 @@ amlcompute_to_delete=( minimal-example basic-example mycluster + mycluster-msi location-example low-pri-example ssh-example