Skip to content

Commit

Permalink
handle gcp and ec2 separately and cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
vitchyr committed Jan 28, 2019
1 parent 9803d09 commit 3704efe
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions scripts/run_experiment_from_doodad.py
@@ -1,7 +1,8 @@
import doodad as dd
from rlkit.launchers.launcher_util import run_experiment_here
import torch.multiprocessing as mp

from rlkit.launchers.launcher_util import run_experiment_here

if __name__ == "__main__":
import matplotlib
matplotlib.use('agg')
Expand All @@ -17,17 +18,33 @@
run_experiment_kwargs['variant']['slurm-job-id'] = os.environ.get(
'SLURM_JOB_ID', None
)
if run_mode and run_mode == 'ec2':
try:
import urllib.request
instance_id = urllib.request.urlopen(
'http://169.254.169.254/latest/meta-data/instance-id'
).read().decode()
run_experiment_kwargs['variant']['EC2_instance_id'] = instance_id
except Exception as e:
print("Could not get instance ID. Error was...")
print(e)
if run_mode and (run_mode == 'ec2' or run_mode == 'gcp'):
if run_mode == 'ec2':
try:
import urllib.request
instance_id = urllib.request.urlopen(
'http://169.254.169.254/latest/meta-data/instance-id'
).read().decode()
run_experiment_kwargs['variant']['EC2_instance_id'] = instance_id
except Exception as e:
print("Could not get AWS instance ID. Error was...")
print(e)
if run_mode == 'gcp':
try:
import urllib.request
request = urllib.request.Request(
"http://metadata/computeMetadata/v1/instance/name",
)
# See this URL for why we need this header:
# https://cloud.google.com/compute/docs/storing-retrieving-metadata
request.add_header("Metadata-Flavor", "Google")
instance_name = urllib.request.urlopen(request).read().decode()
run_experiment_kwargs['variant']['GCP_instance_name'] = (
instance_name
)
except Exception as e:
print("Could not get GCP instance name. Error was...")
print(e)
# Do this in case base_log_dir was already set
run_experiment_kwargs['base_log_dir'] = output_dir
run_experiment_here(
Expand All @@ -40,4 +57,4 @@
method_call,
log_dir=output_dir,
**run_experiment_kwargs
)
)

0 comments on commit 3704efe

Please sign in to comment.