def __init__(self, name, *, instance, install_script='', image_name='',
               **extra_kwargs):
    """
   Initializes Task on top of existing AWS instance. Blocks until instance is ready to execute
   shell commands.

    Args:
      name: task name
      instance: ec2.Instance object (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#instance)
      install_script:
      image_name: AWS image name
      **extra_kwargs: unused kwargs (kept for compatibility with other backends)
    """
    self._cmd_fn = None
    self._cmd = None
    self._status_fn = None  # location of output of last status
    self.last_status = -1

    self._can_run = False  # indicates that things needed for .run were created
    self.initialize_called = False

    self.name = name
    self.instance = instance
    self.install_script = install_script
    self.extra_kwargs = extra_kwargs

    self.public_ip = u.get_public_ip(instance)
    self.ip = u.get_ip(instance)
    self.sftp = None
    self._linux_type = 'ubuntu'

    # heuristic to tell if I'm using Amazon image name
    # default image has name like 'amzn2-ami-hvm-2.0.20180622.1-x86_64-gp2'
    if 'amzn' in image_name.lower() or 'amazon' in image_name.lower():
      self.log('Detected Amazon Linux image')
      self._linux_type = 'amazon'
    self.run_counter = 0

    launch_id = util.random_id()
    self.local_scratch = f"{TMPDIR}/{name}-{launch_id}"
    self.remote_scratch = f"{TMPDIR}/{name}-{launch_id}"

    os.system('mkdir -p ' + self.local_scratch)

    self._initialized_fn = f'is_initialized'

    # _current_directory tracks current directory on task machine
    # used for uploading without specifying absolute path on target machine
    if self._linux_type == 'ubuntu':
      #      self._current_directory = '/home/ubuntu'
      self.ssh_username = 'ubuntu'  # default username on task machine
    elif self._linux_type == 'amazon':
      #      self._current_directory = '/home/ec2-user'
      self.ssh_username = 'ec2-user'
    self.homedir = '/home/' + self.ssh_username

    self.ssh_client = u.ssh_to_task(self)
    self._setup_tmux()
    self._run_raw('mkdir -p ' + self.remote_scratch)

    self._can_run = True

    if self._is_initialized_fn_present():
      self.log("reusing previous initialized state")
    else:
      self.log("running install script")

      # bin/bash needed to make self-executable or use with UserData
      self.install_script = '#!/bin/bash\n' + self.install_script
      self.install_script += f'\necho ok > {self._initialized_fn}\n'
      self.file_write('install.sh', util.shell_add_echo(self.install_script))
      self.run('bash -e install.sh')  # fail on errors
      assert self._is_initialized_fn_present(), f"Install script didn't write to {self._initialized_fn}"

    self._mount_efs()
    self.connect_instructions = f"""
    To connect to {self.name}
ssh -i {u.get_keypair_fn()} -o StrictHostKeyChecking=no {self.ssh_username}@{self.public_ip}
tmux a
""".strip()
    self.log("Initialize complete")
    self.log(self.connect_instructions)
def __init__ ( self , name , * , instance , install_script = '' , image_name = '' , ** extra_kwargs ) : self . _cmd_fn = None self . _cmd = None self . _status_fn = None self . last_status = - 1 self . _can_run = False self . initialize_called = False self . name = name self . instance = instance self . install_script = install_script self . extra_kwargs = extra_kwargs self . public_ip = u . get_public_ip ( instance ) self . ip = u . get_ip ( instance ) self . sftp = None self . _linux_type = 'ubuntu' if 'amzn' in image_name . lower ( ) or 'amazon' in image_name . lower ( ) : self . log ( 'Detected Amazon Linux image' ) self . _linux_type = 'amazon' self . run_counter = 0 launch_id = util . random_id ( ) self . local_scratch = f"{TMPDIR}/{name}-{launch_id}" self . remote_scratch = f"{TMPDIR}/{name}-{launch_id}" os . system ( 'mkdir -p ' + self . local_scratch ) self . _initialized_fn = f'is_initialized' if self . _linux_type == 'ubuntu' : self . ssh_username = 'ubuntu' elif self . _linux_type == 'amazon' : self . ssh_username = 'ec2-user' self . homedir = '/home/' + self . ssh_username self . ssh_client = u . ssh_to_task ( self ) self . _setup_tmux ( ) self . _run_raw ( 'mkdir -p ' + self . remote_scratch ) self . _can_run = True if self . _is_initialized_fn_present ( ) : self . log ( "reusing previous initialized state" ) else : self . log ( "running install script" ) self . install_script = '#!/bin/bash\n' + self . install_script self . install_script += f'\necho ok > {self._initialized_fn}\n' self . file_write ( 'install.sh' , util . shell_add_echo ( self . install_script ) ) self . run ( 'bash -e install.sh' ) assert self . _is_initialized_fn_present ( ) , f"Install script didn't write to {self._initialized_fn}" self . _mount_efs ( ) self . connect_instructions = f"""
    To connect to {self.name}
ssh -i {u.get_keypair_fn()} -o StrictHostKeyChecking=no {self.ssh_username}@{self.public_ip}
tmux a
""" . strip ( ) self . log ( "Initialize complete" ) self . log ( self . connect_instructions )
Initializes Task on top of existing AWS instance. Blocks until instance is ready to execute
   shell commands.

    Args:
      name: task name
      instance: ec2.Instance object (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#instance)
      install_script:
      image_name: AWS image name
      **extra_kwargs: unused kwargs (kept for compatibility with other backends)