Esempio n. 1
0
def action_resources():
    project = kc.get_project_by_name(options.project)
    start = himutils.get_date(options.start, date.today() - timedelta(days=1))
    stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)
    output = dict({'vcpu': 0, 'ram':0})
    for region in regions:
        # instances
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        gc = Gnocchi(options.config, debug=options.debug, log=logger, region=region)
        deleted = nc.get_project_instances(project_id=project.id, deleted=True)
        running = nc.get_project_instances(project_id=project.id)
        for i in deleted + running:
            resource = gc.get_resource(resource_type='instance', resource_id=i.id)
            if not resource:
                continue
            metrics = dict()
            metrics['vcpu'] = gc.get_client().metric.get('vcpus', i.id)
            metrics['ram'] = gc.get_client().metric.get('memory', i.id)
            for key, value in metrics.iteritems():
                measurement = gc.get_client().metric.get_measures(metric=value['id'],
                                                                  aggregation='max',
                                                                  start=start,
                                                                  stop=stop)
                if measurement:
                    output[key] += measurement[0][2]
    printer.output_dict({'header': 'resources used by %s in all regions' % project.name})
    printer.output_dict(output)
Esempio n. 2
0
def action_delete():
    q = "Delete these instances? (yes|no) "
    answer = raw_input(q)
    if answer.lower() != 'yes':
        print "Abort delete!"
        return
    search_filter = dict()
    if options.type:
        search_filter['type'] = options.type
    projects = ksclient.get_projects(domain=options.domain, **search_filter)
    count = 0
    for region in regions:
        #print "==============\n REGION=%s\n==============" % region
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        for project in projects:
            if not options.filter or options.filter in project.name:
                #print "found %s" % project.name
                #printer.output_dict(project.to_dict())
                instances = novaclient.get_project_instances(project.id)
                for i in instances:
                    logger.debug('=> DELETE %s (%s)' % (i.name, project.name))
                    if not options.dry_run:
                        i.delete()
                        count += 1
                        time.sleep(2)
    print "\nTotal number of instances deleted: %s" % count
Esempio n. 3
0
def action_notify():
    q = "Send mail to all users of these instances about termination? (yes|no) "
    answer = raw_input(q)
    if answer.lower() != 'yes':
        print "Abort sending mail!"
        return

    search_filter = dict()
    if options.type:
        search_filter['type'] = options.type
    projects = ksclient.get_projects(domain=options.domain, **search_filter)
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        for project in projects:
            if not options.filter or options.filter in project.name:
                instances = novaclient.get_project_instances(project.id)
                mapping = dict(region=region.upper(), project=project.name)
                body_content = himutils.load_template(
                    inputfile=options.template, mapping=mapping, log=logger)
                subject = ('UH-IaaS: Your instances will be terminated (%s)' %
                           (region))

                notify = Notify(options.config, debug=False, log=logger)
                notify.set_keystone_client(ksclient)
                notify.set_dry_run(options.dry_run)
                users = notify.mail_instance_owner(instances, body_content,
                                                   subject)
                notify.close()
                print users
Esempio n. 4
0
def action_delete():
    q = "Delete these instances? (yes|no) "
    answer = raw_input(q)
    if answer.lower() != 'yes':
        print "Abort delete!"
        return
    search_filter = dict()
    if options.type:
        search_filter['type'] = options.type
    projects = ksclient.get_projects(domain=options.domain, **search_filter)
    count = 0
    for region in regions:
        #print "==============\n REGION=%s\n==============" % region
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        for project in projects:
            if not options.filter or options.filter in project.name:
                #print "found %s" % project.name
                #printer.output_dict(project.to_dict())
                instances = novaclient.get_project_instances(project.id)
                for i in instances:
                    network = i.addresses.keys()[0] if len(i.addresses.keys()) > 0 else 'unknown'
                    if options.network and options.network != network:
                        continue
                    logger.debug('=> DELETE %s (%s)' % (i.name, project.name))
                    if not options.dry_run:
                        i.delete()
                        count += 1
                        time.sleep(2)
    print "\nTotal number of instances deleted: %s" % count
Esempio n. 5
0
def action_list():
    search_filter = dict()
    if options.type:
        search_filter['type'] = options.type
    projects = ksclient.get_projects(domain=options.domain, **search_filter)
    #regions = ksclient.get_regions()
    count = 0
    for region in regions:
        #print "==============\n REGION=%s\n==============" % region
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        for project in projects:
            if not options.filter or options.filter in project.name:
                #print "found %s" % project.name
                #printer.output_dict(project.to_dict())
                instances = novaclient.get_project_instances(project.id)
                for i in instances:
                    count += 1
                    output = dict()
                    output['_region'] = region
                    output['id'] = i.id
                    output['status'] = i.status
                    output['name'] = unicode(i.name)
                    output['project'] = project.name
                    printer.output_dict(objects=output, one_line=True)
                    #print '%s %s %s (%s)' % (i.id, i.status, unicode(i.name), project.name)
    print "\nTotal number of instances for cleanup: %s" % count
Esempio n. 6
0
def action_notify():
    q = "Send mail to all users of these instances about termination? (yes|no) "
    answer = raw_input(q)
    if answer.lower() != 'yes':
        print "Abort sending mail!"
        return

    search_filter = dict()
    if options.type:
        search_filter['type'] = options.type
    projects = ksclient.get_projects(domain=options.domain, **search_filter)
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        for project in projects:
            if not options.filter or options.filter in project.name:
                instances = novaclient.get_project_instances(project.id)
                verified_instances = list()
                for i in instances:
                    network = i.addresses.keys()[0] if len(i.addresses.keys()) > 0 else 'unknown'
                    if options.network and options.network != network:
                        continue
                    verified_instances.append(i)
                mapping = dict(region=region.upper(), project=project.name)
                body_content = himutils.load_template(inputfile=options.template,
                                                      mapping=mapping,
                                                      log=logger)
                subject = ('UH-IaaS: Your instances will be terminated (%s)' % (region))

                notify = Notify(options.config, debug=False, log=logger)
                notify.set_keystone_client(ksclient)
                notify.set_dry_run(options.dry_run)
                users = notify.mail_instance_owner(verified_instances, body_content, subject)
                notify.close()
                if users:
                    print users
Esempio n. 7
0
def action_list():
    search_filter = dict()
    if options.type:
        search_filter['type'] = options.type
    projects = ksclient.get_projects(domain=options.domain, **search_filter)
    #regions = ksclient.get_regions()
    count = 0
    for region in regions:
        #print "==============\n REGION=%s\n==============" % region
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        for project in projects:
            if not options.filter or options.filter in project.name:
                #print "found %s" % project.name
                #printer.output_dict(project.to_dict())
                instances = novaclient.get_project_instances(project.id)
                for i in instances:
                    network = i.addresses.keys()[0] if len(i.addresses.keys()) > 0 else 'unknown'
                    if options.network and options.network != network:
                        continue
                    count += 1
                    output = dict()
                    output['_region'] = region
                    output['id'] = i.id
                    output['status'] = i.status
                    output['name'] = unicode(i.name)
                    output['project'] = project.name
                    printer.output_dict(objects=output, one_line=True)
                    #print '%s %s %s (%s)' % (i.id, i.status, unicode(i.name), project.name)
    print "\nTotal number of instances for cleanup: %s" % count
Esempio n. 8
0
def action_disable():
    projects = ksclient.get_projects(domain=options.domain)
    project_list = list()
    for project in projects:
        found = False
        if hasattr(project, 'notify') and project.notify == 'converted':
            logger.debug('=> keep personal project. %s converted',
                         project.name)
            continue
        if not project.enabled:
            logger.debug('=> personal project %s already disabled',
                         project.name)
            continue

        if (hasattr(project, 'type') and project.type == 'personal'
                and 'PRIVATE' not in project.name):
            print "%s (new old personal project)" % project.name
            found = True
        elif '@' in project.name and not hasattr(project, 'type'):
            print "%s (old old personal project)" % project.name
            found = True
        #else:
        #    logger.debug('=> project %s not old personal', project.name)
        if found:
            project_list.append(project)
    if len(project_list) == 0:
        print 'No project to disable'
        return
    question = 'Disable all personal project in list above'
    if options.dry_run:
        question = 'DRY-RUN: %s' % question
    if not himutils.confirm_action(question):
        return
    for project in project_list:
        # stop instances
        for region in regions:
            novaclient = Nova(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
            instances = novaclient.get_project_instances(project.id)
            for instance in instances:
                if not options.dry_run:
                    if instance.status == 'ACTIVE':
                        instance.stop()
                        logger.debug('=> stop instance %s' % instance.name)
                        time.sleep(2)
                else:
                    logger.debug('=> DRY-RUN: stop instance %s' %
                                 instance.name)
        # disable project
        if not options.dry_run:
            ksclient.update_project(project_id=project.id, enabled=False)
            print 'Disable project %s' % project.name
        else:
            print 'DRY-RUN: disable project %s' % project.name
Esempio n. 9
0
def action_deactivate():
    if options.org == 'all':
        active, deactive, unknown = get_valid_users()
    else:
        active, deactive, unknown = get_valid_users(options.org)

    q = 'This will deactivate %s users (total active users %s)' \
        % (len(deactive), active['total'])
    if not himutils.confirm_action(q):
        return
    subject = '[UH-IaaS] Your account have been disabled'
    regions = ksclient.find_regions()
    count = 0
    users_deactivated = list()
    for email in deactive:
        user = ksclient.get_user_by_email(email, 'api')
        # Disable user and notify user
        if user.enabled:
            # notify user
            mail_user(email, 'notify/notify_deactivate.txt', subject)
            # Disable api user
            date = datetime.today().strftime('%Y-%m-%d')
            ksclient.update_user(user_id=user.id, enabled=False, disabled=date)
        else:
            continue
        projects = ksclient.get_user_projects(email)
        # Shutoff instances in demo and personal project.
        for project in projects:
            if not hasattr(project, 'type'):
                continue
            if project.type == 'demo' or project.type == 'personal':
                for region in regions:
                    nc = Nova(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
                    nc.set_dry_run(options.dry_run)
                    instances = nc.get_project_instances(project.id)
                    for i in instances:
                        if not options.dry_run:
                            count += 1
                            if i.status == 'ACTIVE':
                                i.stop()
                        nc.debug_log('stop instance %s' % i.id)
        users_deactivated.append(email)
    output = dict()
    output['header'] = 'Deactivated users:'
    output['users'] = users_deactivated
    printer.output_dict(output)
    output = dict()
    output['header'] = 'Stopped instances:'
    output['servers'] = count
    printer.output_dict(output)
Esempio n. 10
0
def action_resources():
    project = kc.get_project_by_name(options.project)
    start = himutils.get_date(options.start, date.today() - timedelta(days=1))
    stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)
    output = dict({'vcpu': 0, 'ram': 0})
    for region in regions:
        # instances
        nc = Nova(options.config,
                  debug=options.debug,
                  log=logger,
                  region=region)
        gc = Gnocchi(options.config,
                     debug=options.debug,
                     log=logger,
                     region=region)
        deleted = nc.get_project_instances(project_id=project.id, deleted=True)
        running = nc.get_project_instances(project_id=project.id)
        for i in deleted + running:
            resource = gc.get_resource(resource_type='instance',
                                       resource_id=i.id)
            if not resource:
                continue
            metrics = dict()
            metrics['vcpu'] = gc.get_client().metric.get('vcpus', i.id)
            metrics['ram'] = gc.get_client().metric.get('memory', i.id)
            for key, value in metrics.iteritems():
                measurement = gc.get_client().metric.get_measures(
                    metric=value['id'],
                    aggregation='max',
                    start=start,
                    stop=stop)
                if measurement:
                    output[key] += measurement[0][2]
    printer.output_dict(
        {'header': 'resources used by %s in all regions' % project.name})
    printer.output_dict(output)
Esempio n. 11
0
def action_deactivate():
    active, deactive, unknown = get_valid_users()
    q = 'This will deactivate %s users (total active users %s)' \
        % (len(deactive), active['total'])
    if not himutils.confirm_action(q):
        return
    subject = '[UH-IaaS] Your account have been disabled'
    regions = ksclient.find_regions()
    count = 0
    users_deactivated = list()
    for email in deactive:
        user = ksclient.get_user_by_email(email, 'api')
        # Disable user and notify user
        if user.enabled:
            # notify user
            mail_user(email, 'notify/notify_deactivate.txt', subject)
            # Disable api user
            date = datetime.today().strftime('%Y-%m-%d')
            ksclient.update_user(user_id=user.id, enabled=False, disabled=date)
        else:
            continue
        projects = ksclient.get_user_projects(email)
        # Shutoff instances in demo and personal project.
        for project in projects:
            if not hasattr(project, 'type'):
                continue
            if project.type == 'demo' or project.type == 'personal':
                for region in regions:
                    nc = Nova(options.config, debug=options.debug, log=logger, region=region)
                    nc.set_dry_run(options.dry_run)
                    instances = nc.get_project_instances(project.id)
                    for i in instances:
                        if not options.dry_run:
                            count += 1
                            if i.status == 'ACTIVE':
                                i.stop()
                        nc.debug_log('stop instance %s' % i.id)
        users_deactivated.append(email)
    output = dict()
    output['header'] = 'Deactivated users:'
    output['users'] = users_deactivated
    printer.output_dict(output)
    output = dict()
    output['header'] = 'Stopped instances:'
    output['servers'] = count
    printer.output_dict(output)
Esempio n. 12
0
def user():
    if not ksclient.is_valid_user(email=options.email):
        print "%s is not a valid user. Please check your spelling or case." % options.email
        sys.exit(1)
    obj = ksclient.get_user_objects(email=options.email, domain='Dataporten')
    projects = obj['projects']
    total = 0
    for project in projects:
        project_type = project.type if hasattr(project, 'type') else 'unknown'
        print "\n%s (type=%s):" % (project.name, project_type)
        for name in sorted(regions['regions'].iterkeys()):
            logger.debug('=> count region %s' % name)
            novaclient = Nova(options.config, debug=options.debug, log=logger, region=name)
            instances = novaclient.get_project_instances(project.id)
            total += len(instances)
            for i in instances:
                print "* %s" % i.name
    print "\nTotal number of instances for %s: %s" % (options.email, total)
Esempio n. 13
0
def action_instances():
    project = ksclient.get_project_by_name(project_name=options.project)
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger)
        instances = novaclient.get_project_instances(project_id=project.id)
        if not instances:
            himutils.sys_error('No instances found for the project %s' % options.project)
        printer.output_dict({'header': 'Instances list (id, name, region)'})
        count = 0
        for i in instances:
            output = {
                'id': i.id,
                'name': i.name,
                'region': region,
            }
            count += 1
            printer.output_dict(output, sort=True, one_line=True)
        printer.output_dict({'header': 'Total instances in this project', 'count': count})
Esempio n. 14
0
def action_cleanup():
    projects = ksclient.get_projects(domain=options.domain)
    project_list = list()
    for project in projects:
        # Only list disabled projects
        if project.enabled:
            continue
        if hasattr(project, 'notify'):
            print "%s (notified=%s)" % (project.name, project.notify)
        else:
            print "%s (disabled project)" % project.name
        project_list.append(project)
    if len(project_list) == 0:
        print 'No project to cleanup'
        return
    question = 'Cleanup all personal project in list above'
    if options.dry_run:
        question = 'DRY-RUN: %s' % question
    if not himutils.confirm_action(question):
        return
    count = 0
    for project in project_list:
        # stop instances
        for region in regions:
            novaclient = Nova(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
            instances = novaclient.get_project_instances(project.id)
            for instance in instances:
                if instance.status == 'SHUTOFF':
                    count += 1
                    if not options.dry_run:
                        logger.debug('=> delete instance %s (%s)' %
                                     (instance.name, instance.id))
                        instance.delete()
                        time.sleep(5)
                else:
                    himutils.sys_error(
                        'instance %s not deleted! (%s)' %
                        (instance.name, instance.id), 0)
        ksclient.delete_project(project_name=project.name,
                                domain=options.domain)
    print "Deleted %s instances from %s projects" % (count, len(project_list))
Esempio n. 15
0
def user():
    if not ksclient.is_valid_user(email=options.email):
        print "%s is not a valid user. Please check your spelling or case." % options.email
        sys.exit(1)
    obj = ksclient.get_user_objects(email=options.email, domain='Dataporten')
    projects = obj['projects']
    total = 0
    for project in projects:
        project_type = project.type if hasattr(project, 'type') else 'unknown'
        print "\n%s (type=%s):" % (project.name, project_type)
        for name in sorted(regions['regions'].iterkeys()):
            logger.debug('=> count region %s' % name)
            novaclient = Nova(options.config,
                              debug=options.debug,
                              log=logger,
                              region=name)
            instances = novaclient.get_project_instances(project.id)
            total += len(instances)
            for i in instances:
                print "* %s" % i.name
    print "\nTotal number of instances for %s: %s" % (options.email, total)