From 08d5840c7196569cf49b074b5c8c1d011183b436 Mon Sep 17 00:00:00 2001 From: jaehwang Date: Tue, 23 Sep 2025 23:19:31 +0000 Subject: [PATCH] add remove self instance --- README.md | 15 +++++++++++---- kill_myself.py | 30 ++++++++++++++++++++++++++++++ main.py | 10 ++++++++-- requirements.txt | 5 ++++- 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 kill_myself.py diff --git a/README.md b/README.md index 2dd8c5a..15c4930 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,18 @@ + +sudo su + apt-get update -apt-get install python3-dev python3-venv +add-apt-repository ppa:deadsnakes/ppa -python3 -m venv .venv +apt-get install python3.13-dev python3.13-venv -source ./venv/bin/activate +cd /ADO2VMCrawler -pip install -r requirements +python3.13 -m venv .venv + +source ./.venv/bin/activate + +pip install -r requirements.txt playwright install-deps playwright install \ No newline at end of file diff --git a/kill_myself.py b/kill_myself.py new file mode 100644 index 0000000..712e56a --- /dev/null +++ b/kill_myself.py @@ -0,0 +1,30 @@ +from azure.identity import DefaultAzureCredential +from azure.mgmt.compute import ComputeManagementClient +from pprint import pprint + +import azure.mgmt.compute as compute +import uuid +import requests +import json + +#subscription_id = instance_metadata['compute']['subscription_id'] + + +def delete_self_instance(): + instance_metadata = requests.get("http://169.254.169.254/metadata/instance?api-version=2025-04-07&format=json", headers={"Metadata":"True"}).json() + instance_resource_id = instance_metadata['compute']['resourceId'] + parts = instance_resource_id.split('/') + subscription_id = parts[2] + resource_group = parts[4] + vm_name = parts[8] + if vm_name == "ado2-crawler-dev": + print("This is dev, don't die") + return False + + credential = DefaultAzureCredential() + compute_client = ComputeManagementClient(credential, subscription_id) + compute_client.virtual_machines.begin_delete(resource_group, vm_name) + print("Kill this instance") + return True + +delete_self_instance() \ No newline at end of file diff --git a/main.py b/main.py index 5e80152..8a2d531 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,10 @@ +import os, json +import asyncio import playwright from playwright.async_api import async_playwright import asyncio - +HOME_PATH = "/home/azureuser" async def playwright_test(): async with async_playwright() as p: browser = await p.chromium.launch() @@ -13,4 +15,8 @@ async def playwright_test(): pass -asyncio.run(playwright_test()) \ No newline at end of file +asyncio.run(playwright_test()) + +if __name__=="__main__": + with open(os.path.join(HOME_PATH, "success.txt"), "w") as fp: + json.dump("", fp) diff --git a/requirements.txt b/requirements.txt index 3f3391f..4f3e4ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,5 @@ playwright -dotenv \ No newline at end of file +dotenv +azure-identity +azure-mgmt-resource +azure-mgmt-compute \ No newline at end of file