Skip to content

Commit

Permalink
Add a utility to install deepracer support for AWS CLI and boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
breadcentric committed Jan 25, 2021
1 parent e93cdaf commit 4c24b00
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# deepracer-utils
A set of utilities to take your DeepRacer experience to the next level.

# Add deepracer to AWS CLI and boto3
Make sure you have deepracer-utils >= 0.9 installed.

Then run this to install:
```
python -m deepracer install-cli
```

If you want to ensure you have the latest deepracer service definition, you can run:
```
python -m deepracer install-cli --force
```

To remove deepracer support from aws-cli and boto3, run:
```
python -m deepracer remove-cli
```

## Development

Expand Down
4 changes: 0 additions & 4 deletions deepracer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import os

DEEPRACER_UTILS_ROOT = os.path.dirname(os.path.abspath(__file__))

from . import logs, tracks, boto3_enhancer

from ._version import get_versions
Expand Down
15 changes: 15 additions & 0 deletions deepracer/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys
import deepracer.boto3_enhancer


def main():
if "install-cli" in sys.argv:
force = True if "--force" in sys.argv else False

deepracer.boto3_enhancer.install_deepracer_cli(force)
elif "remove-cli" in sys.argv:
deepracer.boto3_enhancer.remove_deepracer_cli()


if __name__ == "__main__":
main()
39 changes: 30 additions & 9 deletions deepracer/boto3_enhancer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@

import os
import boto3
import shutil

from .. import DEEPRACER_UTILS_ROOT
from deepracer.utils import DEEPRACER_UTILS_ROOT


DR_MODELS_ROOT = os.path.join(DEEPRACER_UTILS_ROOT, 'boto3_enhancer', 'models', "deepracer")


AWS_CLI_MODELS_DR_ROOT = os.path.join(os.path.expanduser("~"), ".aws", "models", "deepracer")


def add_deepracer(session=None, **kwargs):
Expand All @@ -41,10 +48,8 @@ def add_deepracer(session=None, **kwargs):
boto3.setup_default_session(**kwargs)
session = boto3.DEFAULT_SESSION

dr_path = os.path.join(DEEPRACER_UTILS_ROOT, 'boto3_enhancer', 'models')

if dr_path not in session._loader.search_paths:
session._loader.search_paths.append(dr_path)
if DR_MODELS_ROOT not in session._loader.search_paths:
session._loader.search_paths.append(DR_MODELS_ROOT)


def deepracer_client(region_name='us-east-1'):
Expand All @@ -53,7 +58,23 @@ def deepracer_client(region_name='us-east-1'):
"""
add_deepracer()

return boto3.client(
'deepracer',
region_name=region_name,
endpoint_url='https://deepracer-prod.{}.amazonaws.com'.format(region_name))
return boto3.client('deepracer', region_name=region_name)


def install_deepracer_cli(force=False):
if force:
remove_deepracer_cli()

if os.path.isdir(AWS_CLI_MODELS_DR_ROOT):
print("Configuration already found, not installing. Use --force to overwrite")
return

shutil.copytree(DR_MODELS_ROOT, AWS_CLI_MODELS_DR_ROOT)


def remove_deepracer_cli():
if not os.path.isdir(AWS_CLI_MODELS_DR_ROOT):
print("Configuration doesn't exist, nothing to clean up")
return

shutil.rmtree(AWS_CLI_MODELS_DR_ROOT)
3 changes: 3 additions & 0 deletions deepracer/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

DEEPRACER_UTILS_ROOT = os.path.dirname(os.path.abspath(__file__))

0 comments on commit 4c24b00

Please sign in to comment.