code: def wait_for_cluster_state(
      conn, opts, cluster_instances, cluster_state
  ):
      """
      Wait for all the instances in the cluster to reach a designated state.
  
      cluster_instances: a list of boto.ec2.instance.Instance
      cluster_state: a string representing the desired state of all the instances in the cluster
             value can be 'ssh-ready' or a valid value from boto.ec2.instance.InstanceState such as
             'running', 'terminated', etc.
             (would be nice to replace this with a proper enum: http://stackoverflow.com/a/1695250)
      """
      sys.stdout.write(
          "Waiting for cluster to enter '{s}' state.".format(
              s=cluster_state
          )
      )
      sys.stdout.flush()
  
      start_time = datetime.now()
      num_attempts = 0
  
      while True:
          time.sleep(5 * num_attempts)  # seconds
  
          for i in cluster_instances:
              i.update()
  
          max_batch = 100
          statuses = []
          for j in xrange(0, len(cluster_instances), max_batch):
              batch = [
                  i.id for i in cluster_instances[j : j + max_batch]
              ]
              statuses.extend(
                  conn.get_all_instance_status(instance_ids=batch)
              )
  
          if cluster_state == "ssh-ready":
              if (
                  all(i.state == "running" for i in cluster_instances)
                  and all(
                      s.system_status.status == "ok" for s in statuses
                  )
                  and all(
                      s.instance_status.status == "ok" for s in statuses
                  )
                  and is_cluster_ssh_available(cluster_instances, opts)
              ):
                  break
          else:
              if all(
                  i.state == cluster_state for i in cluster_instances
              ):
                  break
  
          num_attempts += 1
  
          sys.stdout.write(".")
          sys.stdout.flush()
  
      sys.stdout.write("\n")
  
      end_time = datetime.now()
      print(
          "Cluster is now in '{s}' state. Waited {t} seconds.".format(
              s=cluster_state, t=(end_time - start_time).seconds
          )
      )
  
imports: from __future__ import division, print_function, with_statement
  import codecs
  import hashlib
  import itertools
  import logging
  import os
  import os.path
  import pipes
  import random
  import shutil
  import string
  from stat import S_IRUSR
  import subprocess
  import sys
  import tarfile
  import tempfile
  import textwrap
  import time
  import warnings
  from datetime import datetime
  from optparse import OptionParser
  from sys import stderr
  import boto
  from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType, EBSBlockDeviceType
  from boto import ec2
label_desc: Use image node_id 14 for calling a global function with example usage: wait_for_cluster_state(conn, opts, cluster_instances, cluster_state) without return types