From e98b05c530ce0b8d8beb1ebc6fe41da39550933a Mon Sep 17 00:00:00 2001 From: Stepan Bakshayev Date: Fri, 10 Feb 2023 17:49:01 +0200 Subject: [PATCH] create template for cloud image for Azure --- .github/workflows/manual_azure_stgn.yml | 30 +++++++++ runner/ami/packer/README.MD | 52 ++++++++++++++- runner/ami/packer/azure-image.json | 86 +++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/manual_azure_stgn.yml create mode 100644 runner/ami/packer/azure-image.json diff --git a/.github/workflows/manual_azure_stgn.yml b/.github/workflows/manual_azure_stgn.yml new file mode 100644 index 000000000..ba5b67a70 --- /dev/null +++ b/.github/workflows/manual_azure_stgn.yml @@ -0,0 +1,30 @@ +name: Manual Azure build for STGN + +on: [workflow_dispatch] + +jobs: + azure-image-build: + defaults: + run: + working-directory: runner + env: + AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} + AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} + AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} + AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} + strategy: + matrix: + variants: [ azure-image ] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Download packer + run: | + wget https://releases.hashicorp.com/packer/1.8.0/packer_1.8.0_linux_amd64.zip + unzip packer_1.8.0_linux_amd64.zip + chmod +x packer + cp -R ami/packer/* . + - name: Run packer + run: | + VERSION=${{ github.run_number }} + ./packer build -var build_id=$VERSION ${{ matrix.variants }}.json diff --git a/runner/ami/packer/README.MD b/runner/ami/packer/README.MD index e73922db6..095a470b6 100644 --- a/runner/ami/packer/README.MD +++ b/runner/ami/packer/README.MD @@ -3,4 +3,54 @@ To run, you need to specify AWS credentials in ENV ## Build Ubuntu AMI (with CUDA) ```shell packer build packer.json -``` \ No newline at end of file +``` + +# Azure + +## Allocate resources (make credentials) + +Follow [installation instruction](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt) +for Azure CLI `az`. [Login](https://learn.microsoft.com/en-us/cli/azure/authenticate-azure-cli) for managing resources: + +```commandline +$ az login +``` + +Steps below follows [HOWTO](https://learn.microsoft.com/en-us/azure/virtual-machines/linux/build-image-with-packer). +Create group as container for result image. Value `packer` is from property `managed_image_resource_group_name` of +`azure-arm` packer's builder. Value `eastus` is property `location` of `azure-arm` (Azure has two kind notation for +the same location). + +```commandline +$ az group create -n packer -l eastus +``` + +Packer allocates resources on its own. It requires access to subscription. Obtain id. + +```commandline +$ az account show --query "{ subscription_id: id }" +{ + "subscription_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" +} +``` + +Create credentials for packer. + +```commandline +$ az ad sp create-for-rbac --role Contributor --scopes /subscriptions/ --query "{ client_id: appId, client_secret: password, tenant_id: tenant }" +{ + "client_id": "f5b6a5cf-fbdf-4a9f-b3b8-3c2cd00225a4", + "client_secret": "0e760437-bf34-4aad-9f8d-870be799c55d", + "tenant_id": "72f988bf-86f1-41af-91ab-2d7cd011db47" +} +``` + +Set environment variables. + +| Env | Azure | +|-----|-------| +| AZURE_CLIENT_ID | client_id | +| AZURE_CLIENT_SECRET | client_secret | +| AZURE_TENANT_ID | tenant_id | +| AZURE_SUBSCRIPTION_ID | subscription_id | + diff --git a/runner/ami/packer/azure-image.json b/runner/ami/packer/azure-image.json new file mode 100644 index 000000000..da375d129 --- /dev/null +++ b/runner/ami/packer/azure-image.json @@ -0,0 +1,86 @@ +{ + "min_packer_version": "1.8", + "variables": { + "azure_client_id": "{{env `AZURE_CLIENT_ID`}}", + "azure_client_secret": "{{env `AZURE_CLIENT_SECRET`}}", + "azure_tenant_id": "{{env `AZURE_TENANT_ID`}}", + "azure_subscription_id": "{{env `AZURE_SUBSCRIPTION_ID`}}", + "azure_location": "East US", + "azure_vm_size": "Standard_DS2_v2", + "build_id": "{{timestamp}}", + "build_prefix": "stgn-", + "docker_version": "20.10.17", + "dstack_stage": "STGN" + }, + "builders": [{ + "type": "azure-arm", + "client_id": "{{user `azure_client_id`}}", + "client_secret": "{{user `azure_client_secret`}}", + "tenant_id": "{{user `azure_tenant_id`}}", + "subscription_id": "{{user `azure_subscription_id`}}", + "managed_image_resource_group_name": "packer", + "managed_image_name": "{{user `build_prefix`}}dstack-{{user `build_id`}}", + "os_type": "Linux", + "image_publisher": "Canonical", + "image_offer": "UbuntuServer", + "image_sku": "18.04-DAILY-LTS", + "azure_tags": { + "Name": "DSTACK" + }, + "location": "{{user `azure_location`}}", + "vm_size": "{{user `azure_vm_size`}}" + }], + "provisioners": [ + { + "type": "shell", + "inline": ["cloud-init status --long --wait"] + }, + { + "type": "shell", + "scripts": [ + "provisioners/kernel/apt-upgrade.sh", + "provisioners/kernel/apt-daily.sh", + "provisioners/kernel/apt-packages.sh", + "provisioners/kernel/kernel-tuning.sh" + ] + }, + { + "type": "shell", + "environment_vars": ["DSTACK_STAGE={{user `dstack_stage`}}"], + "script": "provisioners/get-dstack-runner.sh" + }, + { + "type": "file", + "source": "provisioners/install-docker.sh", + "destination": "/tmp/install-docker.sh" + }, + { + "type": "file", + "source": "provisioners/run-docker", + "destination": "/tmp/run-docker" + }, + { + "type": "shell", + "inline_shebang": "/bin/sh -x", + "inline": [ + "cd /tmp", + "chmod +x install-docker.sh", + "./install-docker.sh --version {{user `docker_version`}}" + ] + }, + { + "type": "shell", + "script": "provisioners/docker-image-without-cuda.sh" + }, + { + "type": "shell", + "inline_shebang": "/bin/sh -x", + "inline": ["/usr/local/bin/dstack-runner --version"] + }, + { + "type": "shell", + "inline_shebang": "/bin/sh -x", + "inline": ["/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"] + } + ] +} \ No newline at end of file