From 5038644f45bc90b01c7b3973bd4cd03906aca94b Mon Sep 17 00:00:00 2001 From: Aarsh2001 Date: Wed, 4 Oct 2023 19:33:23 +0100 Subject: [PATCH] gcp auth --- .github/auth/vm_auth.py | 20 +++++++++++--------- .github/workflows/gcp-test.yaml | 2 +- jiggle.py | 21 ++------------------- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/.github/auth/vm_auth.py b/.github/auth/vm_auth.py index 52756c97..066f568b 100644 --- a/.github/auth/vm_auth.py +++ b/.github/auth/vm_auth.py @@ -1,6 +1,7 @@ import sys import json import paramiko +import time import io from google.auth import compute_engine @@ -10,20 +11,21 @@ def authenticate_vm(path): credentials = Credentials.from_service_account_file(path) - return discovery.build('compute', 'v1', credentials=credentials), credentials -def start_runner(creds, key, id = "gpu-insatnce", zone='us-central1-a', instance='demos-tests'): - compute, credentials = authenticate_vm(creds) + return discovery.build('compute', 'v1', credentials=credentials) +def start_runner(creds, key, ssh_username, id = "gpu-insatnce", zone='us-central1-a', instance='demos-tests'): + compute = authenticate_vm(creds) compute.instances().start(project=id, zone=zone, instance=instance).execute() + time.sleep(60) # Get the SSH username (assuming it's stored in the credentials) - - ssh_username = credentials.service_account_email + response = compute.instances().get(project=id, zone=zone, instance=instance).execute() + external_ip = response['networkInterfaces'][0]['accessConfigs'][0]['natIP'] ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( - hostname=f'{instance}.{zone}.compute.internal', + external_ip, username=ssh_username, # Typically 'your-username' or 'gce-username' - pkey = paramiko.RSAKey(file_obj=io.StringIO(key)), + pkey=paramiko.RSAKey(file_obj=io.StringIO(key)), ) @@ -39,9 +41,9 @@ def start_runner(creds, key, id = "gpu-insatnce", zone='us-central1-a', instance return output if __name__ == "__main__": - key = sys.argv[1] + key, username = sys.argv[1] # Start the instance - start_runner('gcp_auth.json', key) + start_runner('gcp_auth.json', key, username) diff --git a/.github/workflows/gcp-test.yaml b/.github/workflows/gcp-test.yaml index 619eb09b..b7be20b7 100644 --- a/.github/workflows/gcp-test.yaml +++ b/.github/workflows/gcp-test.yaml @@ -28,7 +28,7 @@ jobs: run: | cd demos/.github/auth python3 db_auth.py ${{ secrets.DB_ENDPOINT }} ${{ secrets.DB_OBJ_ID }} - python3 vm_auth.py ${{ secrets.SSH_KEY}} + python3 vm_auth.py ${{ secrets.SSH_KEY}} ${{ secrets.SSH_USERNAME }} run-test: needs: activate-vm diff --git a/jiggle.py b/jiggle.py index 48233d28..d9ec0bd5 100644 --- a/jiggle.py +++ b/jiggle.py @@ -1,19 +1,2 @@ -import json -string = \ -""" -{ - "type": "service_account", - "project_id": "gpu-insatnce", - "private_key_id": "a693769da2d4afbee63d3c37476d776d69edcd69", - "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCn6GqJg8ONfS1h\ntgTSw1R/tE/2Hlr2HwOpXtL5Au+KYCHwVMRw6a48liAKrmsNt5ny3u90ilg97czv\n8ZBfrl6p+0d40YGzJ+IXJ/IBdjIKioQdgi8zegnhhxZb7GsGdcF9yovzfSSNI2a/\nJAO7fOAHvFQQvCBhUmPAp0li0IS5ot/eCjyqZPxLLwZHRpgWqGhyfZqroyYi0XWq\nWybPegUJ7dfXxHyaqQEO1+LZCV2im3Oe8lUlCj6nNYl6YttyGI33PEuaLDdCL/cC\ndnz0umNMC/6OvGQGHbRfnr/WaZEXE3H+ffCvaduf7hZN0nqFUuBZ9y4JeRIf0rKL\nfmfmJIUlAgMBAAECggEACaDMKyKBHiXlakrzr9o/iCsgwq4u7RoOLbIgSuMeNUMZ\n7xwNP6RGt3asU7B4twqmK0UZWgds0/BE5iVl7/ahuwGLsaPh2hIZZtaFjAvpBq22\nsbJ7XmpLEsGaSJ8f6/jopXvt1oKAjA9RnvhDtoaMmiT0dRk0iiAdVDjDfoUSd0qF\n2FztO0t/IwLxMfwx51+Qbsr83uzuCp9/LCQdbz6q2VV6M3gSt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@gpu-insatnce.iam.gserviceaccount.com", - "client_id": "107342639027122981894", - "auth_uri": "https://accounts.google.com/o/oauth2/auth", - "token_uri": "https://oauth2.googleapis.com/token", - "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", - "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/demos-testing-ivy%40gpu-insatnce.iam.gserviceaccount.com", - "universe_domain": "googleapis.com" -} -""" -string = string.strip(' \n\t') -credentials = json.loads(string) \ No newline at end of file +import paramiko +