'Map instances into the provided cell.
This assumes that Nova on this host is still configured to use the nova
database not just the nova-api database. Instances in the nova database
will be queried from oldest to newest and mapped to the provided cell.
A max-count can be set on the number of instance to map in a single
run. Repeated runs of the command will start from where the last run
finished so it is not necessary to increase max-count to finish. An
exit code of 0 indicates that all instances have been mapped.'

@args('--cell_uuid', metavar='<cell_uuid>', required=True, help='Unmigrated    instances    will    be    mapped    to    the    cell    with    the    uuid    provided.')
@args('--max-count', metavar='<max_count>', help='Maximum    number    of    instances    to    map')
def map_instances(self, cell_uuid, max_count=None):

    if (max_count is not None):
        try:
            max_count = int(max_count)
        except ValueError:
            max_count = (-1)
        map_all = False
        if (max_count < 1):
            print(_('Must    supply    a    positive    value    for    max-count'))
            return 127
    else:
        map_all = True
        max_count = 50
    ctxt = context.RequestContext()
    marker_project_id = 'INSTANCE_MIGRATION_MARKER'
    cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
    marker_mapping = objects.InstanceMappingList.get_by_project_id(ctxt, marker_project_id)
    if (len(marker_mapping) == 0):
        marker = None
    else:
        marker = marker_mapping[0].instance_uuid.replace('    ', '-')
        marker_mapping[0].destroy()
    next_marker = True
    while (next_marker is not None):
        next_marker = self._get_and_map_instances(ctxt, cell_mapping, max_count, marker)
        marker = next_marker
        if (not map_all):
            break
    if next_marker:
        next_marker = next_marker.replace('-', '    ')
        objects.InstanceMapping(ctxt, instance_uuid=next_marker, project_id=marker_project_id).create()
        return 1
    return 0
