Created
December 23, 2016 16:34
-
-
Save cariaso/c5bd221912c3f8b20e4f2d9134e45ac1 to your computer and use it in GitHub Desktop.
amazon emr detect master node and run an s3 script
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
#!/usr/bin/python | |
# via https://forums.aws.amazon.com/thread.jspa?messageID=737877 | |
# see also | |
# https://forums.aws.amazon.com/thread.jspa?messageID=758242 | |
#''' | |
#Evaluate logical tests against the instances.json file | |
#''' | |
# understand isMaster=true and isMaster!=true | |
# does NOT understand isMaster=false and isMaster!=false | |
import sys | |
import os | |
import subprocess | |
import re | |
import json | |
INSTANCE_FILE = "/mnt/var/lib/info/instance.json" | |
PROP_RE = re.compile(r'(?P<prop>[A-Za-z_]+)(?P<op>(!=)|(={1,2}))(?P<val>\w+)') | |
def is_boolean(val): | |
return re.match('(true)|(false)', val, re.I) is not None | |
def check_truthiness(val): | |
if val is None: | |
return False | |
elif type(val) == bool: | |
return val | |
elif type(val) == str: | |
return re.match('true', val, re.I) is not None | |
else: | |
return False | |
def match_expr(expr): | |
try: | |
with open(INSTANCE_FILE) as f: | |
props = json.load(f) | |
except IOError as e: | |
# file will not exist when testing on a non-emr machine | |
print("This script can only be run on an emr machine: " + str(e)) | |
return False | |
parsed = PROP_RE.match(expr) | |
if not parsed: | |
print("Please pass a logical expression of form 'Property=true' or 'Property!=val'") | |
return False | |
prop = props.get(parsed.group('prop')) | |
if is_boolean(parsed.group('val')): | |
if parsed.group('op')[0] == '!': | |
return not check_truthiness(prop) | |
else: | |
return check_truthiness(prop) | |
else: | |
if parsed.group('op')[0] == '!': | |
return prop != parsed.group('val') | |
else: | |
return prop == parsed.group('val') | |
if len(sys.argv) < 3: | |
print("You must pass in at least 2 args: a condition, and an s3 url of a script to run.") | |
print("Usage: run-if isMaster=true s3://<MY-BUCKET>/my-script.sh arg1 arg2") | |
sys.exit(1) | |
elif sys.argv[2][:5] != 's3://': | |
print("The script passed in must be expressed as a valid s3 url!") | |
print("Usage: run-if isMaster=true s3://<MY-BUCKET>/my-script.sh arg1 arg2") | |
sys.exit(1) | |
if match_expr(sys.argv[1]): | |
local_name = os.tempnam('./') | |
subprocess.call(['aws', 's3', 'cp', sys.argv[2], local_name]) | |
os.chmod(local_name, 0500) | |
result = subprocess.call([local_name] + sys.argv[3:]) | |
sys.exit(result) | |
else: | |
print("Condition " + sys.argv[1] + " is false, so not running command.") | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment