def main():
    try:
        args = parse_cmdline_args()
    except Exception as e:
        log.error('Check command line arguments (%s)', e.strerror)
    conn = makeOsConnection()
    db_url = get_db_url(args.config)
    (cinder_session, cinder_metadata, cinder_Base) = makeConnection(db_url)
    orphan_volume_attachments = get_orphan_volume_attachments(cinder_metadata)
    nova_instances = get_nova_instances(conn)
    wrong_orphan_volume_attachments = get_wrong_orphan_volume_attachments(nova_instances, orphan_volume_attachments)
    if (len(wrong_orphan_volume_attachments) != 0):
        log.info('- orphan volume attachments found:')
        for orphan_volume_attachment_id in wrong_orphan_volume_attachments:
            log.info('-- orphan volume attachment (id in cinder db: %s) for non existent instance in nova: %s', orphan_volume_attachment_id, orphan_volume_attachments[orphan_volume_attachment_id])
        if (not args.dry_run):
            log.info('- deleting orphan volume attachment inconsistencies found')
            fix_wrong_orphan_volume_attachments(cinder_metadata, wrong_orphan_volume_attachments, args.fix_limit)
    else:
        log.info('- no orphan volume attachments found')
    error_deleting_volumes = get_error_deleting_volumes(cinder_metadata)
    if (len(error_deleting_volumes) != 0):
        log.info('- volumes in state error_deleting found')
        for error_deleting_volumes_id in error_deleting_volumes:
            log.info('-- volume id: %s', error_deleting_volumes_id)
        if (not args.dry_run):
            log.info('- deleting volumes in state error_deleting')
            fix_error_deleting_volumes(cinder_metadata, error_deleting_volumes)
    else:
        log.info('- no volumes in state error_deleting found')
    error_deleting_snapshots = get_error_deleting_snapshots(cinder_metadata)
    if (len(error_deleting_snapshots) != 0):
        log.info('- snapshots in state error_deleting found')
        for error_deleting_snapshots_id in error_deleting_snapshots:
            log.info('-- snapshot id: %s', error_deleting_snapshots_id)
        if (not args.dry_run):
            log.info('- deleting snapshots in state error_deleting')
            fix_error_deleting_snapshots(cinder_metadata, error_deleting_snapshots)
    else:
        log.info('- no snapshots in state error_deleting found')
    wrong_admin_metadata = get_wrong_volume_admin_metadata(cinder_metadata)
    if (len(wrong_admin_metadata) != 0):
        log.info('- volume_admin_metadata inconsistencies found')
        for volume_admin_metadata_id in wrong_admin_metadata:
            log.info('-- volume_admin_metadata id: %s - deleted volume id: %s', volume_admin_metadata_id, wrong_admin_metadata[volume_admin_metadata_id])
        if (not args.dry_run):
            log.info('- removing volume_admin_metadata inconsistencies found')
            fix_wrong_volume_admin_metadata(cinder_metadata, wrong_admin_metadata)
    else:
        log.info('- volume_admin_metadata entries are consistent')
    wrong_glance_metadata = get_wrong_volume_glance_metadata(cinder_metadata)
    if (len(wrong_glance_metadata) != 0):
        log.info('- volume_glance_metadata inconsistencies found')
        for volume_glance_metadata_id in wrong_glance_metadata:
            log.info('-- volume_glance_metadata id: %s - deleted volume id: %s', volume_glance_metadata_id, wrong_glance_metadata[volume_glance_metadata_id])
        if (not args.dry_run):
            log.info('- removing volume_glance_metadata inconsistencies found')
            fix_wrong_volume_glance_metadata(cinder_metadata, wrong_glance_metadata)
    else:
        log.info('- volume_glance_metadata entries are consistent')
    wrong_metadata = get_wrong_volume_metadata(cinder_metadata)
    if (len(wrong_metadata) != 0):
        log.info('- volume_metadata inconsistencies found')
        for volume_metadata_id in wrong_metadata:
            log.info('-- volume_metadata id: %s - deleted volume id: %s', volume_metadata_id, wrong_metadata[volume_metadata_id])
        if (not args.dry_run):
            log.info('- removing volume_metadata inconsistencies found')
            fix_wrong_volume_metadata(cinder_metadata, wrong_metadata)
    else:
        log.info('- volume_metadata entries are consistent')
    wrong_attachments = get_wrong_volume_attachments(cinder_metadata)
    if (len(wrong_attachments) != 0):
        log.info('- volume attachment inconsistencies found')
        for volume_attachment_id in wrong_attachments:
            log.info('-- volume attachment id: %s - deleted volume id: %s', volume_attachment_id, wrong_attachments[volume_attachment_id])
        if (not args.dry_run):
            log.info('- removing volume attachment inconsistencies found')
            fix_wrong_volume_attachments(cinder_metadata, wrong_attachments, args.fix_limit)
    else:
        log.info('- volume attachments are consistent')
    table_names = ['snapshots', 'volume_attachment']
    missing_deleted_at = get_missing_deleted_at(cinder_metadata, table_names)
    if (len(missing_deleted_at) != 0):
        log.info('- missing deleted_at values found:')
        for missing_deleted_at_id in missing_deleted_at:
            log.info('--- id %s of the %s table is missing deleted_at time', missing_deleted_at_id, missing_deleted_at[missing_deleted_at_id])
        if (not args.dry_run):
            log.info('- setting missing deleted_at values')
            fix_missing_deleted_at(cinder_metadata, table_names)
    else:
        log.info('- no missing deleted_at values')
    deleted_services_still_used_in_volumes = get_deleted_services_still_used_in_volumes(cinder_metadata)
    if (len(deleted_services_still_used_in_volumes) != 0):
        log.info('- deleted services still used in volumes found:')
        for deleted_services_still_used_in_volumes_id in deleted_services_still_used_in_volumes:
            log.info('--- deleted service uuid %s still used in volumes table entry %s', deleted_services_still_used_in_volumes_id, deleted_services_still_used_in_volumes[deleted_services_still_used_in_volumes_id])
        if (not args.dry_run):
            log.info('- undeleting service uuid still used in volumes table')
            fix_deleted_services_still_used_in_volumes(cinder_metadata, deleted_services_still_used_in_volumes)
    else:
        log.info('- deleted services still used in volumes')
def main():
    try:
        args = parse_cmdline_args()
    except Exception as e:
        log.error('Check command line arguments (%s)', e.strerror)
    conn = makeOsConnection()
    db_url = get_db_url(args.config)
    (cinder_session, cinder_metadata, cinder_Base) = makeConnection(db_url)
    orphan_volume_attachments = get_orphan_volume_attachments(cinder_metadata)
    nova_instances = get_nova_instances(conn)
    wrong_orphan_volume_attachments = get_wrong_orphan_volume_attachments(nova_instances, orphan_volume_attachments)
    if (len(wrong_orphan_volume_attachments) != 0):
        log.info('- orphan volume attachments found:')
        for orphan_volume_attachment_id in wrong_orphan_volume_attachments:
            log.info('-- orphan volume attachment (id in cinder db: %s) for non existent instance in nova: %s', orphan_volume_attachment_id, orphan_volume_attachments[orphan_volume_attachment_id])
        if (not args.dry_run):
            log.info('- deleting orphan volume attachment inconsistencies found')
            fix_wrong_orphan_volume_attachments(cinder_metadata, wrong_orphan_volume_attachments, args.fix_limit)
    else:
        log.info('- no orphan volume attachments found')
    error_deleting_volumes = get_error_deleting_volumes(cinder_metadata)
    if (len(error_deleting_volumes) != 0):
        log.info('- volumes in state error_deleting found')
        for error_deleting_volumes_id in error_deleting_volumes:
            log.info('-- volume id: %s', error_deleting_volumes_id)
        if (not args.dry_run):
            log.info('- deleting volumes in state error_deleting')
            fix_error_deleting_volumes(cinder_metadata, error_deleting_volumes)
    else:
        log.info('- no volumes in state error_deleting found')
    error_deleting_snapshots = get_error_deleting_snapshots(cinder_metadata)
    if (len(error_deleting_snapshots) != 0):
        log.info('- snapshots in state error_deleting found')
        for error_deleting_snapshots_id in error_deleting_snapshots:
            log.info('-- snapshot id: %s', error_deleting_snapshots_id)
        if (not args.dry_run):
            log.info('- deleting snapshots in state error_deleting')
            fix_error_deleting_snapshots(cinder_metadata, error_deleting_snapshots)
    else:
        log.info('- no snapshots in state error_deleting found')
    wrong_admin_metadata = get_wrong_volume_admin_metadata(cinder_metadata)
    if (len(wrong_admin_metadata) != 0):
        log.info('- volume_admin_metadata inconsistencies found')
        for volume_admin_metadata_id in wrong_admin_metadata:
            log.info('-- volume_admin_metadata id: %s - deleted volume id: %s', volume_admin_metadata_id, wrong_admin_metadata[volume_admin_metadata_id])
        if (not args.dry_run):
            log.info('- removing volume_admin_metadata inconsistencies found')
            fix_wrong_volume_admin_metadata(cinder_metadata, wrong_admin_metadata)
    else:
        log.info('- volume_admin_metadata entries are consistent')
    wrong_glance_metadata = get_wrong_volume_glance_metadata(cinder_metadata)
    if (len(wrong_glance_metadata) != 0):
        log.info('- volume_glance_metadata inconsistencies found')
        for volume_glance_metadata_id in wrong_glance_metadata:
            log.info('-- volume_glance_metadata id: %s - deleted volume id: %s', volume_glance_metadata_id, wrong_glance_metadata[volume_glance_metadata_id])
        if (not args.dry_run):
            log.info('- removing volume_glance_metadata inconsistencies found')
            fix_wrong_volume_glance_metadata(cinder_metadata, wrong_glance_metadata)
    else:
        log.info('- volume_glance_metadata entries are consistent')
    wrong_metadata = get_wrong_volume_metadata(cinder_metadata)
    if (len(wrong_metadata) != 0):
        log.info('- volume_metadata inconsistencies found')
        for volume_metadata_id in wrong_metadata:
            log.info('-- volume_metadata id: %s - deleted volume id: %s', volume_metadata_id, wrong_metadata[volume_metadata_id])
        if (not args.dry_run):
            log.info('- removing volume_metadata inconsistencies found')
            fix_wrong_volume_metadata(cinder_metadata, wrong_metadata)
    else:
        log.info('- volume_metadata entries are consistent')
    wrong_attachments = get_wrong_volume_attachments(cinder_metadata)
    if (len(wrong_attachments) != 0):
        log.info('- volume attachment inconsistencies found')
        for volume_attachment_id in wrong_attachments:
            log.info('-- volume attachment id: %s - deleted volume id: %s', volume_attachment_id, wrong_attachments[volume_attachment_id])
        if (not args.dry_run):
            log.info('- removing volume attachment inconsistencies found')
            fix_wrong_volume_attachments(cinder_metadata, wrong_attachments, args.fix_limit)
    else:
        log.info('- volume attachments are consistent')
    table_names = ['snapshots', 'volume_attachment']
    missing_deleted_at = get_missing_deleted_at(cinder_metadata, table_names)
    if (len(missing_deleted_at) != 0):
        log.info('- missing deleted_at values found:')
        for missing_deleted_at_id in missing_deleted_at:
            log.info('--- id %s of the %s table is missing deleted_at time', missing_deleted_at_id, missing_deleted_at[missing_deleted_at_id])
        if (not args.dry_run):
            log.info('- setting missing deleted_at values')
            fix_missing_deleted_at(cinder_metadata, table_names)
    else:
        log.info('- no missing deleted_at values')
    deleted_services_still_used_in_volumes = get_deleted_services_still_used_in_volumes(cinder_metadata)
    if (len(deleted_services_still_used_in_volumes) != 0):
        log.info('- deleted services still used in volumes found:')
        for deleted_services_still_used_in_volumes_id in deleted_services_still_used_in_volumes:
            log.info('--- deleted service uuid %s still used in volumes table entry %s', deleted_services_still_used_in_volumes_id, deleted_services_still_used_in_volumes[deleted_services_still_used_in_volumes_id])
        if (not args.dry_run):
            log.info('- undeleting service uuid still used in volumes table')
            fix_deleted_services_still_used_in_volumes(cinder_metadata, deleted_services_still_used_in_volumes)
    else:
        log.info('- deleted services still used in volumes')