def SearchUbuntuAmiDatabase(release_name, region_name, root_store_type,
                            virtualization_type):
  """ Returns the ubuntu created ami matching the given criteria. """
  ami_list_url = 'http://cloud-images.ubuntu.com/query/%s/server/released.txt' \
   % (release_name)
  url_file = urllib2.urlopen(ami_list_url)
  # The mapping of columns names to col ids in the ubuntu release txt file.
  release_name_col = 0
  release_tag_col = 2
  release_date_col = 3
  root_store_type_col = 4
  arch_col = 5
  region_col = 6
  ami_col = 7
  matching_amis = []  # list of tuples (ami_id, tokens)
  
  for line in url_file:
    tokens = line.split()
    # lines have different number of columns (one fewer for hvm)
    if (len(tokens) == 9):
      virtualization_type_col = 8
    elif (len(tokens) == 10):
      virtualization_type_col = 9
    else:
      raise RuntimeError('invalid line format: %s' % line)
    if tokens[release_name_col] == release_name \
       and tokens[release_tag_col] == 'release' \
       and tokens[root_store_type_col] == root_store_type \
       and tokens[arch_col] == 'amd64' \
       and tokens[region_col] == region_name \
       and tokens[virtualization_type_col] == virtualization_type: 
       matching_amis.append((tokens[ami_col], tokens))
  matching_amis.sort(key=lambda (ami, tokens) : tokens[release_date_col], 
                     reverse=True)  # order newest first  
  if not matching_amis:
    params = [release_name, root_store_type, region_name,  virtualization_type]
    raise RuntimeError('Failed to find matching ubuntu ami: %s', params)
  selected_ami = matching_amis[0][0]
  return selected_ami
Returns the ubuntu created ami matching the given criteria.