Skip to content

Commit

Permalink
run on aws batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhett-Ying committed Nov 8, 2023
1 parent b66b185 commit 1177ead
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .github/workflow_scripts/pytest_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cd ../..

set -ex

python3 -m pytest test_main.py -v
30 changes: 30 additions & 0 deletions .github/workflows/bk_ci_githubhost.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: continuous_integration

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m venv venv
source venv/bin/activate
pip install pytest
pip install -r requirements.txt
env:
CI: true
- name: Run tests
run: |
source venv/bin/activate
python -m pytest test_main.py -v
44 changes: 29 additions & 15 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,39 @@ on:
- main
pull_request:

# All test scripts split in individual .sh files and moved to .github/workflow scripts
env:
COMMAND-PYTEST: bash ./pytest_check.sh


jobs:
test:
pytest_check:
if: ${{ github.event.label.name != 'draft' }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::801250864718:role/github-oidc-role
aws-region: us-west-2
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: '3.x'
- name: Install dependencies
shell: bash
run: |
python -m venv venv
source venv/bin/activate
pip install pytest
pip install -r requirements.txt
env:
CI: true
- name: Run tests
python3 -m pip install --upgrade pip
python3 -m pip install pytest
pip3 install boto3
- name: Submit Job
shell: bash
run: |
source venv/bin/activate
python -m pytest test_main.py -v
echo "Start submitting job - Check"
python3 ./submitJob.py --job-type CI-CPU --name hello_DGL-pytest-check-'${{ github.ref }}' \
--command "${{ env.COMMAND-PYTEST }}" \
--remote https://github.com/'${{ github.repository }}' \
--source-ref '${{ github.ref }}' \
--wait
148 changes: 148 additions & 0 deletions submitJob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# script to submit jobs to AWS Batch, queues and definitions are already existing and set up

Check failure on line 1 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L1

Line too long (92 > 79 characters) (E501)

Check failure on line 1 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L1

Trailing whitespace (W291)
import argparse
import random

Check failure on line 3 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L3

'random' imported but unused (F401)
import re
import sys
import time
from datetime import datetime

import boto3
from botocore.compat import total_seconds
from botocore.config import Config


job_type_info = {
'CI-CPU': {
'job_definition': 'hello_dgl',
'job_queue': 'hello_dgl',
},
}

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)

Check failure on line 21 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L21

Line too long (88 > 79 characters) (E501)

parser.add_argument('--profile', help='profile name of aws account.', type=str,
default=None)
parser.add_argument('--region', help='Default region when creating new connections', type=str,

Check failure on line 25 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L25

Line too long (94 > 79 characters) (E501)
default='us-west-2')
parser.add_argument('--name', help='name of the job', type=str, default='dummy')

Check failure on line 27 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L27

Line too long (80 > 79 characters) (E501)
parser.add_argument('--job-type', help='type of job to submit.', type=str,
choices=job_type_info.keys(), default='CI-CPU')
parser.add_argument('--command', help='command to run', type=str,
default='git rev-parse HEAD | tee stdout.log')
parser.add_argument('--wait', help='block wait until the job completes. '
'Non-zero exit code if job fails.', action='store_true')
parser.add_argument('--timeout', help='job timeout in seconds', default=10800, type=int)

Check failure on line 34 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L34

Line too long (88 > 79 characters) (E501)

parser.add_argument('--source-ref',
help='ref in hello_DGL main github. e.g. master, refs/pull/500/head',

Check failure on line 37 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L37

Line too long (89 > 79 characters) (E501)
type=str, default='main')
parser.add_argument('--remote',
help='git repo address. https://github.com/dglai/hello_dgl.git',

Check failure on line 40 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L40

Line too long (84 > 79 characters) (E501)
type=str, default="https://github.com/dglai/hello_dgl.git")

args = parser.parse_args()


session = boto3.Session(profile_name=args.profile, region_name=args.region)
config = Config(
retries = dict(

Check failure on line 48 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L48

Unexpected spaces around keyword / parameter equals (E251)

Check failure on line 48 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L48

Unexpected spaces around keyword / parameter equals (E251)
max_attempts = 5

Check failure on line 49 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L49

Unexpected spaces around keyword / parameter equals (E251)

Check failure on line 49 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L49

Unexpected spaces around keyword / parameter equals (E251)
)
)

batch, cloudwatch = [session.client(service_name=sn, config=config) for sn in ['batch', 'logs']]

Check failure on line 53 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L53

Line too long (96 > 79 characters) (E501)

def printLogs(logGroupName, logStreamName, startTime):

Check failure on line 55 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L55

Expected 2 blank lines, found 1 (E302)
kwargs = {'logGroupName': logGroupName,
'logStreamName': logStreamName,
'startTime': startTime,
'startFromHead': True}

lastTimestamp = startTime - 1
while True:
logEvents = cloudwatch.get_log_events(**kwargs)

for event in logEvents['events']:
lastTimestamp = event['timestamp']
timestamp = datetime.utcfromtimestamp(lastTimestamp / 1000.0).isoformat()

Check failure on line 67 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L67

Line too long (85 > 79 characters) (E501)
print('[{}] {}'.format((timestamp + '.000')[:23] + 'Z', event['message']))

Check failure on line 68 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L68

Line too long (86 > 79 characters) (E501)

nextToken = logEvents['nextForwardToken']
if nextToken and kwargs.get('nextToken') != nextToken:
kwargs['nextToken'] = nextToken
else:
break
return lastTimestamp


def nowInMillis():
endTime = int(total_seconds(datetime.utcnow() - datetime(1970, 1, 1))) * 1000

Check failure on line 79 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L79

Line too long (81 > 79 characters) (E501)
return endTime


def main():
spin = ['-', '/', '|', '\\', '-', '/', '|', '\\']
logGroupName = '/aws/batch/job' # This is the group where aws batch logs are stored in Cloudwatch

Check failure on line 85 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L85

At least two spaces before inline comment (E261)

Check failure on line 85 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L85

Line too long (101 > 79 characters) (E501)

jobName = re.sub('[^A-Za-z0-9_\-]', '', args.name)[:128] # Enforce AWS Batch jobName rules

Check failure on line 87 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L87

Invalid escape sequence '\-' (W605)

Check failure on line 87 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L87

Line too long (95 > 79 characters) (E501)
jobType = args.job_type
jobQueue = job_type_info[jobType]['job_queue']
jobDefinition = job_type_info[jobType]['job_definition']
wait = args.wait

# Printing actions parameters
print("GitHub SourceRef: ", args.source_ref)
print("GitHub Remote: ", args.remote)

parameters = {
'COMMAND': f"\"{args.command}\"", # wrap command with double quotation mark, so that batch can treat it as a single command

Check failure on line 98 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L98

Line too long (132 > 79 characters) (E501)
'SOURCE_REF': args.source_ref,
'REMOTE': args.remote,
}
kwargs = dict(
jobName=jobName,
jobQueue=jobQueue,
jobDefinition=jobDefinition,
parameters=parameters,
)
if args.timeout is not None:
kwargs['timeout'] = {'attemptDurationSeconds': args.timeout}
submitJobResponse = batch.submit_job(**kwargs)

jobId = submitJobResponse['jobId']
print('Submitted job [{} - {}] to the job queue [{}]'.format(jobName, jobId, jobQueue))

Check failure on line 113 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L113

Line too long (91 > 79 characters) (E501)

spinner = 0
running = False
status_set = set()
startTime = 0
logStreamName = None
while wait:
time.sleep(10) # Wait for 10 seconds to fetch job data from batch service

Check failure on line 121 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L121

Line too long (82 > 79 characters) (E501)
describeJobsResponse = batch.describe_jobs(jobs=[jobId])
status = describeJobsResponse['jobs'][0]['status']
if status == 'SUCCEEDED' or status == 'FAILED':
if logStreamName:
startTime = printLogs(logGroupName, logStreamName, startTime) + 1

Check failure on line 126 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L126

Line too long (81 > 79 characters) (E501)
print('=' * 80)
print('Job [{} - {}] {}'.format(jobName, jobId, status))
sys.exit(status == 'FAILED')

elif status == 'RUNNING':
logStreamName = describeJobsResponse['jobs'][0]['container']['logStreamName']

Check failure on line 132 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L132

Line too long (89 > 79 characters) (E501)
if not running:
running = True
print('\rJob [{}, {}] is RUNNING.'.format(jobName, jobId))
if logStreamName:
print('Output [{}]:\n {}'.format(logStreamName, '=' * 80))
if logStreamName:
startTime = printLogs(logGroupName, logStreamName, startTime) + 1

Check failure on line 139 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L139

Line too long (81 > 79 characters) (E501)
elif status not in status_set:
status_set.add(status)
print('\rJob [%s - %s] is %-9s... %s' % (jobName, jobId, status, spin[spinner % len(spin)]),)

Check failure on line 142 in submitJob.py

View workflow job for this annotation

GitHub Actions / Flake8

submitJob.py#L142

Line too long (105 > 79 characters) (E501)
sys.stdout.flush()
spinner += 1


if __name__ == '__main__':
main()

0 comments on commit 1177ead

Please sign in to comment.