Exemple #1
0
def main(argv=None):
    script = 'clean-old-aggregates'
    slack = Slackbot(args.slack, script_name=script) if args.slack else None

    try:
        term_leases = [
            lease for lease in leases.values() if is_terminated(lease)
        ]
        old_aggregates = [
            aggs for aggs in (aggregates_for_lease(lease)
                              for lease in term_leases) if aggs != None
        ]
        aggregate_list = list(itertools.chain(*old_aggregates))
        errors, reports = clear_aggregates(aggregate_list)
        orphan_list = orphan_find(aggregates)

        for orphan in orphan_list:
            destiny = has_active_allocation(orphan)
            host = osrest.blazar.host(auth, orphan)
            if destiny is None:
                reports.append(
                    "Error identifying allocation for orphan host {}.".format(
                        orphan))
            elif destiny is False:
                reports.append(
                    "Returning orphan host {} to freepool.".format(orphan) +
                    "\n")
                osrest.nova.aggregate_add_host(auth, 1,
                                               host['hypervisor_hostname'])
            else:
                destination_agg = [
                    aggr['id'] for aggr in aggregates.values()
                    if aggr['name'] == destiny
                ][0]
                reports.append(
                    "Moving orphan host {} to destined aggregate {}.".format(
                        orphan, destination_agg))
                osrest.nova.aggregate_add_host(auth, destination_agg,
                                               host['hypervisor_hostname'])

        old_allocations = query.blazar_find_old_host_alloc(conn)
        for alloc in old_allocations:
            hostname, lease_id = del_expired_alloc(conn, alloc)
            reports.append(
                "Deleted host_allocation for host {} matching expired lease {}."
                .format(hostname, lease_id))
        conn.db.commit()

        if reports:
            str_report = '\n'.join(reports)
            if slack:
                if errors:
                    slack.error(str_report)
                else:
                    slack.message(str_report)
    except:
        if slack:
            slack.exception()
        raise
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = base_parser('Detects orphan leases and remove them.')

    mysqlargs = MySqlArgs({
        'user': '******',
        'password': '',
        'host': 'localhost',
        'port': 3306,
    })
    mysqlargs.inject(parser)

    parser.add_argument(
        '-d',
        '--dbversion',
        type=str,
        help=
        'Version of the database. Schemas differ, pick the appropriate one.',
        choices=[query.LIBERTY, query.ROCKY],
        default=query.ROCKY)
    parser.add_argument('--kvm', help='Run at KVM site', action='store_true')
    osapi.add_arguments(parser)

    args = parser.parse_args(argv[1:])
    mysqlargs.extract(args)

    kvm = args.kvm

    slack = Slackbot(args.slack,
                     script_name='orphan-detector') if args.slack else None

    try:
        db = mysqlargs.connect()
        db.version = args.dbversion

        if kvm:
            # at kvm site
            os_vars = {
                k: os.environ[k]
                for k in os.environ if k.startswith('OS_')
            }
            if args.osrc:
                os_vars.update(osapi.load_osrc(args.osrc))

            auth = v2.Password(username=os_vars['OS_USERNAME'],
                               password=os_vars['OS_PASSWORD'],
                               tenant_name=os_vars['OS_TENANT_NAME'],
                               auth_url=os_vars['OS_AUTH_URL'])
            sess = session.Session(auth=auth)
            keystone = client.Client(session=sess)

            orphan_instances = get_orphan_instances_kvm(db, keystone)
        else:
            orphan_instances = get_orphan_instances(db)

        orphan_instances_report = generate_report(
            orphan_instances, "-" * 45 + "ORPHAN INSTANCES" + "-" * 45)

        if orphan_instances_report:
            print(orphan_instances_report)

            if slack:
                slack.error(orphan_instances_report)
        else:
            print('No orphan instances detected')

        # Additionally perform lease report for CHI
        if not kvm:
            orphan_leases_report = generate_report(
                get_orphan_leases(db), "-" * 45 + "ORPHAN LEASES" + "-" * 45)

            if orphan_leases_report:
                print(orphan_leases_report)

                if slack:
                    slack.error(orphan_leases_report)
            else:
                print('No orphan leases detected')
    except:
        if slack:
            slack.exception()
        raise