def wait_for_instance()

in tools/ami-creator/create_ami.py [0:0]


def wait_for_instance(instance, private_key):
    instance_id = instance.id
    current_state = instance.state
    logging.info("Waiting for instance to install software and shut down. Current state: %s", instance.state['Name'])
    windows_password = None
    last_log_size = 0
    last_install_log_size = 0
    while (current_state['Code'] != 80):
        time.sleep(20)
        i = ec2Resource.Instance(instance_id)
        if current_state['Code'] != i.state['Code']:
            current_state = i.state
            logging.info("Instance state changed to: %s", current_state['Name'])
        if current_state['Name'] == "running" and i.public_ip_address != None:
            if i.platform == "windows":
                if windows_password == None:
                    logging.debug("Attempting to get password info for instance")
                    try:
                        pwdata = i.password_data()
                        if pwdata['PasswordData'] != '':
                            logging.debug("Got password data, decrypting [%s]", pwdata['PasswordData'])
                            password = decrypt_windows_password(base64.b64decode(pwdata['PasswordData']), private_key)
                            logging.info("Public IP Address: %s", i.public_ip_address)
                            logging.info("Decrypted password: %s", password)
                            windows_password = password
                    except:
                        logging.exception("Unable to get password data for windows instance")
                # attempt to save the latest userdata execute log
                logfile = "log/userdata-{}.log".format(instance_id)
                ret = subprocess.run(["scp","-q","-T","-o","StrictHostKeyChecking=no","-o","ConnectTimeout=10","-i",private_key,"administrator@{}:\"C:\\ProgramData\Amazon\\EC2-Windows\\Launch\\Log\\UserdataExecution.log\"".format(i.public_ip_address),logfile])
                if ret.returncode == 0:
                    if os.stat(logfile).st_size != last_log_size:
                        last_log_size = os.stat(logfile).st_size
                        logging.info("Updated userdata execution log to %s (size=%d)", logfile, last_log_size)
                else:
                    logging.debug("Unable to retrieve userdata log via ssh, does this windows system have sshd installed and running?")
                    continue
                install_logfile = "log/install-{}.log".format(instance_id)
                ret = subprocess.run(["scp","-q","-T","-o","StrictHostKeyChecking=no","-o","ConnectTimeout=10","-i",private_key,"administrator@{}:\"C:\\install.log\"".format(i.public_ip_address),install_logfile])
                if ret.returncode == 0:
                    if os.stat(install_logfile).st_size != last_install_log_size:
                        last_install_log_size = os.stat(install_logfile).st_size
                        logging.info("Updated install log to %s (size=%d)", install_logfile, last_install_log_size)
            else:
                logging.info("Attempting to get cloud-init output log.")
                os.system("ssh -o StrictHostKeyChecking=no -i {} ubuntu@{} tail -n +0 -f /var/log/cloud-init-output.log 2>/dev/null".format(private_key, i.public_ip_address))
    logging.info("Instance stopped")