Skip to content

Commit

Permalink
gcp auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarsh2001 committed Oct 6, 2023
1 parent abdc37d commit 710211f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
50 changes: 33 additions & 17 deletions .github/auth/vm_auth.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import sys
import json
import paramiko
import time
import io
import os

from google.auth import compute_engine
from googleapiclient import discovery
from google.oauth2.service_account import Credentials
from google.auth import impersonated_credentials


def authenticate_vm(path):
credentials = Credentials.from_service_account_file(path)
return discovery.build('compute', 'v1', credentials=credentials)
def start_runner(creds, path, ssh_username, passphrase, id = "gpu-insatnce", zone='us-central1-a', instance='demos-tests'):
compute = authenticate_vm(creds)
request = compute.instances().start(project=id, zone=zone, instance=instance)
request.execute()
time.sleep(60)

response = compute.instances().get(project=id, zone=zone, instance=instance).execute()
def _start_ssh_session(compute, creds, username, passphrase):
response = compute.instances().get(project="gpu-insatnce", zone='us-central1-a', instance='demos-tests').execute()
external_ip = response['networkInterfaces'][0]['accessConfigs'][0]['natIP']

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(
external_ip,
username=ssh_username, # Typically 'your-username' or 'gce-username'
key_filename = path,
username=username,
key_filename=creds,
passphrase=passphrase,
)

Expand All @@ -41,11 +36,32 @@ def start_runner(creds, path, ssh_username, passphrase, id = "gpu-insatnce", zon

return output

if __name__ == "__main__":
username, passphrase = sys.argv[1], sys.argv[2]
# Start the instance
ssh_key_path = os.path.expanduser('~/.ssh/id_rsa')
start_runner('gcp_auth.json', ssh_key_path, str(username), passphrase)
def start_runner(creds, ssh_creds, ssh_user, key_passphrase, id="gpu-insatnce", zone='us-central1-a',
instance='demos-tests'):

compute = authenticate_vm(creds)
request = compute.instances().start(project=id, zone=zone, instance=instance)
request.execute()
time.sleep(60)

_start_ssh_session(compute, ssh_creds, ssh_user, key_passphrase)


def stop_runner(creds):
compute = authenticate_vm(creds)
request = compute.instances().start(project="gpu-insatnce", zone='us-central1-a', instance='demos-tests')
request.execute()


if __name__ == "__main__":
ssh_user, key_passphrase, stop_vm = sys.argv[1], sys.argv[2], sys.argv[3]
gcp_credentials = 'gcp_auth.json'
ssh_credentials = '~/.ssh/id_rsa'

if stop_vm == "true":
# Stop the instance
stop_runner(gcp_credentials)
else:
# Start the instance
ssh_key_path = os.path.expanduser()
start_runner(gcp_credentials, ssh_credentials, ssh_user, key_passphrase)
30 changes: 19 additions & 11 deletions .github/workflows/gcp-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
echo "${{ secrets.SSH_KEY }}" > $HOME/.ssh/id_rsa
chmod 600 $HOME/.ssh/id_rsa
python3 db_auth.py ${{ secrets.DB_ENDPOINT }} ${{ secrets.DB_OBJ_ID }}
python3 vm_auth.py ${{ secrets.SSH_USERNAME }} ${{ secrets.SSH_PASSPHRASE }}
python3 vm_auth.py ${{ secrets.SSH_USERNAME }} ${{ secrets.SSH_PASSPHRASE }} "true"
run-test:
needs: activate-vm
Expand All @@ -54,17 +54,25 @@ jobs:
needs: run-test
runs-on: ubuntu-latest
steps:
- id: 'auth'
uses: 'google-github-actions/[email protected]'
with:
credentials_json: '${{ secrets.GCP_AUTH }}'
- runs-on: ubuntu-latest
steps:
- name: Checkout Demos🛎
uses: actions/checkout@v2
with:
path: demos
persist-credentials: false
submodules: "recursive"
fetch-depth: 1

- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
with:
version: '>= 363.0.0'
- name: Install clients
run: |
pip3 install pymongo google-api-python-client paramiko==2.7.1
- name: 'Stop VM instance'
run: 'gcloud compute instances stop demos-tests --zone us-central1-a'
- name: Stop GPU VM
run: |
cd demos/.github/auth
python3 db_auth.py ${{ secrets.DB_ENDPOINT }} ${{ secrets.DB_OBJ_ID }}
python3 vm_auth.py ${{ secrets.SSH_USERNAME }} ${{ secrets.SSH_PASSPHRASE }} "false"

0 comments on commit 710211f

Please sign in to comment.