-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove UUID from instance names (#11)
* Remove UUID from instance name * Replace print with appropriate logging * Update documentation * Fix typing and docstrings
- Loading branch information
Showing
5 changed files
with
77 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
source activate run-on-ec2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,40 @@ | ||
from contextlib import ExitStack | ||
import logging | ||
import os | ||
import sys | ||
from uuid import uuid4 | ||
|
||
from lib.ec2 import TempKeyPair, TempInstance | ||
from lib.ssh import SSH | ||
|
||
if __name__ == '__main__': | ||
logger = logging.getLogger('run-on-ec2') | ||
|
||
|
||
def main(): | ||
logger.setLevel(logging.INFO) | ||
logger.addHandler(logging.StreamHandler()) | ||
|
||
name = os.environ['NAME'] | ||
unique_name = f'{name}-{uuid4()}' | ||
key_name = f'{name}-{uuid4()}' | ||
launch_template_name = os.environ['LAUNCH_TEMPLATE_NAME'] | ||
subnet_id = os.environ['SUBNET_ID'] | ||
command = ' '.join(sys.argv[1:]) | ||
|
||
with ExitStack() as stack: | ||
private_key = stack.enter_context(TempKeyPair(unique_name)) | ||
private_key = stack.enter_context(TempKeyPair(key_name)) | ||
instance = stack.enter_context(TempInstance( | ||
unique_name, | ||
name, | ||
launch_template_name, | ||
key_name, | ||
subnet_id, | ||
)) | ||
host = instance.private_ip_address | ||
connection = stack.enter_context(SSH(host, 'ec2-user', private_key)) | ||
|
||
escaped_command = command.replace("'", "\\'") | ||
print(f'Running command "{escaped_command}" on {host}...') | ||
logger.info(f'Running command "{escaped_command}" on {host}...') | ||
connection.run(f'exec $SHELL -l -c \'{escaped_command}\'') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |