Example #1
0
 def __delete_instances(self, project, dry_run=False):
     """ Use novaclient to delete all instances for a project """
     novaclient = Nova(config_path=self.config_path,
                       debug=self.debug,
                       log=self.logger,
                       region=self.region)
     novaclient.delete_project_instances(project, dry_run)
Example #2
0
def action_compute():
    stats = dict()
    for region in regions:
        metrics = ['vcpus', 'vcpus_used', 'running_vms', 'memory_mb_used',
                   'memory_mb', 'local_gb', 'local_gb_used']
        nc = Nova(options.config, debug=options.debug, region=region, log=logger)
        azs = nc.get_availability_zones()
        zone_hosts = dict()
        # Availablity zones
        for az in azs:
            if ('iaas-team-only' in az.zoneName or
                    region not in az.zoneName or not az.hosts):
                continue
            for host in az.hosts.iterkeys():
                zone_hosts[host] = az.zoneName
            stats[az.zoneName] = dict()
            for metric in metrics:
                stats[az.zoneName][metric] = 0
        # Hypervisor hosts
        hosts = nc.get_hosts()
        for host in hosts:
            if not host.hypervisor_hostname in zone_hosts:
                himutils.sys_error('host %s not enabled or in valid az'
                                   % host.hypervisor_hostname, 0)
                continue
            az = zone_hosts[host.hypervisor_hostname]
            for metric in metrics:
                logger.debug('=> %s %s=%s', host.hypervisor_hostname, metric, getattr(host, metric))
                stats[az][metric] = int(getattr(host, metric) + stats[az][metric])
    statsd.gauge_dict('compute', stats)
    if not options.quiet:
        for name, stat in stats.iteritems():
            printer.output_dict({'header': name})
            printer.output_dict(stat)
Example #3
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
Example #4
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
Example #5
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)
Example #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)
                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
Example #7
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
Example #8
0
def action_revoke():
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        nc.set_dry_run(options.dry_run)
        update_access(nc, 'revoke')
        print "Revoke access to %s for %s in %s" % (options.flavor,
            options.project, region)
Example #9
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
Example #10
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
Example #11
0
def users():
    stats = dict()
    stats['total'] = 0
    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_instances()
        stats['total'] += len(instances)
        for i in instances:
            user = ksclient.get_by_id('user', i.user_id)
            if not user:
                org = 'unknown'
                logger.debug('=> unknown user for %s (id=%s)' % (i.name, i.id))
            elif '@' not in user.name:
                org = 'sysuser'
            else:
                org = user.name.split("@")[1]
            if org in stats:
                stats[org] += 1
            else:
                stats[org] = 1
    if options.output == 'count':
        stats['header'] = 'Usage grouped by user email domain:'
        printer.output_dict(stats)
    else:
        percent = dict()
        if stats['total'] > 0:
            for s in sorted(stats):
                percent[s] = (float(stats[s]) / float(stats['total'])) * 100
        percent[
            'header'] = 'Percentage of instances grouped by users email domain:'
        printer.output_dict(percent)
Example #12
0
def action_nodiscard():
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        glclient = Glance(options.config, debug=options.debug, log=logger, region=region)
        instances = novaclient.get_instances()
        printer.output_dict({'header': '%s: Instances without discard (image, name, visibility)'
                                       % region})
        count = 0
        for i in instances:
            image = glclient.get_image_by_id(i.image['id'])
            # Remove instances in disabled project
            project = ksclient.get_by_id('project', i.tenant_id)
            if project.enabled:
                continue
            # Assume that hw_disk_bus == discard
            if image and 'hw_disk_bus' in image:
                continue
            if not image:
                image = dict()
                image['name'] = 'unknown'
                image['visibility'] = 'unknown'
            output = {'name': i.name, 'image': image['name'], 'status': image['visibility']}
            printer.output_dict(output, sort=True, one_line=True)
            count += 1
        printer.output_dict({'header': 'Instance count', 'count': count})
Example #13
0
def org():
    stats = dict()
    stats['total'] = 0
    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_instances()
        stats['total'] += len(instances)
        for i in instances:
            user = ksclient.get_by_id('user', i.user_id)
            if not user:
                org = 'unknown'
                logger.debug('=> unknown user for %s (id=%s)' % (i.name, i.id))
            elif '@' not in user.name:
                org = 'sysuser'
            else:
                domain = user.name.split("@")[1]
                if len(domain.split(".")) > 1:
                    org = domain.split(".")[-2]
                else:
                    org = 'unknown'
            if org in stats:
                stats[org] += 1
            else:
                stats[org] = 1
    if options.output == 'count':
        stats['header'] = 'Usage grouped by user organization:'
        printer.output_dict(stats)
    else:
        percent = dict()
        if stats['total'] > 0:
            for s in sorted(stats):
                percent[s] = (float(stats[s])/float(stats['total']))*100
        percent['header'] = 'Percentage of instances grouped by users organization:'
        printer.output_dict(percent)
Example #14
0
def action_disabled():
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        instances = novaclient.get_instances()
        printer.output_dict({'header': '%s: Instances in disabled project (host, name, project)'
                                       % region})
        groups = {'group1': 0, 'group2': 0, 'group3': 0}
        count = 0
        for i in instances:
            project = ksclient.get_by_id('project', i.tenant_id)
            if project.enabled:
                continue
            host = getattr(i, 'OS-EXT-SRV-ATTR:host').split('.')[0]
            output = {'name': i.name, 'project': project.name, 'status': host}
            printer.output_dict(output, sort=True, one_line=True)
            if '01' in host or '04' in host:
                groups['group1'] += 1
            elif '02' in host or '05' in host:
                groups['group2'] += 1
            elif '03' in host or '06' in host:
                groups['group3'] += 1
            count += 1
        output = {'header': 'Instance count', 'total': count}
        output.update(groups)
        printer.output_dict(output)
Example #15
0
def action_whales():
    start = himutils.get_date(options.start, date.today() - timedelta(days=1))
    stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
    if start > stop:
        himutils.sys_error('start %s must be fore stop %s' % (start, stop))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)

    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        cc = Cinder(options.config, debug=options.debug, log=logger, region=region)
        project_usage = nc.get_usage(start=start, end=stop)
        logger.debug('=> threshold for whales filter %s', options.threshold)
        print_header = True
        for usage in project_usage:
            project = kc.get_by_id(obj_type='project', obj_id=usage.tenant_id)
            if not project:
                logger.debug('=> project with id %s not found',usage.tenant_id)
                continue
            if len(usage.server_usages) < options.threshold:
                continue
            cinderusage = cc.get_usage(usage.tenant_id)
            admin = project.admin if hasattr(project, 'admin') else 'unknown!'
            output = OrderedDict()
            output['instances'] = len(usage.server_usages)
            output['volume_gb'] =  cinderusage.gigabytes['in_use']
            output['name'] = project.name
            output['admin'] = admin
            if print_header:
                output['header'] = 'project usage %s (instances, volume (GB), name, id)' % region
                print_header = False
            printer.output_dict(objects=output, sort=False, one_line=True)
Example #16
0
def action_compute():
    stats = dict()
    for region in regions:
        drop_az = ['iaas-team-only', '%s-legacy-1' % region]
        metrics = ['vcpus', 'vcpus_used', 'running_vms', 'memory_mb_used',
                   'memory_mb', 'local_gb']
        nc = Nova(options.config, debug=options.debug, region=region, log=logger)
        aggregates = nc.get_aggregates(False)
        for a in aggregates:
            a_name = "%s-%s" % (region, a.name)
            stats[a_name] = dict()
            if a.availability_zone in drop_az:
                continue
            hosts = nc.get_aggregate_hosts(a.name, True)
            for host in hosts:
                #print host.to_dict()
                for metric in metrics:
                    logger.debug('=> %s %s=%s', host.hypervisor_hostname, metric, getattr(host, metric))
                    stats[a_name][metric] = int(getattr(host, metric) +
                                                stats[a_name].get(metric, 0))
    statsd.gauge_dict('compute', stats)
    if not options.quiet:
        for name, stat in stats.iteritems():
            printer.output_dict({'header': name})
            printer.output_dict(stat)
Example #17
0
def action_disabled():
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        instances = novaclient.get_instances()
        printer.output_dict({
            'header':
            '%s: Instances in disabled project (host, name, project)' % region
        })
        groups = {'group1': 0, 'group2': 0, 'group3': 0}
        count = 0
        for i in instances:
            project = ksclient.get_by_id('project', i.tenant_id)
            if project.enabled:
                continue
            host = getattr(i, 'OS-EXT-SRV-ATTR:host').split('.')[0]
            output = {'name': i.name, 'project': project.name, 'status': host}
            printer.output_dict(output, sort=True, one_line=True)
            if '01' in host or '04' in host:
                groups['group1'] += 1
            elif '02' in host or '05' in host:
                groups['group2'] += 1
            elif '03' in host or '06' in host:
                groups['group3'] += 1
            count += 1
        output = {'header': 'Instance count', 'total': count}
        output.update(groups)
        printer.output_dict(output)
Example #18
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
Example #19
0
def action_list():
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        flavors = nc.get_flavors(filters=options.flavor)
        outputs = ['name', 'vcpus', 'ram', 'disk']
        header = 'flavors in %s (%s)' % (region, ', '.join(outputs))
        printer.output_dict({'header': header})
        for flavor in flavors:
            output = OrderedDict()
            for out in outputs:
                output[out] = getattr(flavor, out)
            printer.output_dict(objects=output, one_line=True, sort=False)
Example #20
0
def action_sync():
    engine = create_engine(kc.get_config('db', 'database_uri'))
    Base.metadata.bind = engine
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        net = Neutron(options.config, debug=options.debug, log=logger, region=region)
        network_list = list()
        for network in net.list_networks():
            network_list.append(network['name'])
        instances = nc.get_all_instances()
        for i in instances:
            owner = dict()
            # Get IP address
            if not i.addresses:
                continue
            for network in network_list:
                if str(network) not in i.addresses:
                    continue
                for addr in i.addresses[str(network)]:
                    if addr['version'] == 4:
                        owner['ip'] = addr['addr']
            # Get project, admin and org
            project = kc.get_by_id('project', i.tenant_id)
            if project:
                owner['project_name'] = project.name
                owner['admin'] = project.admin if hasattr(project, 'admin') else None
                domain = project.admin.split("@")[1] if hasattr(project, 'admin') else ''
                if len(domain.split(".")) > 1:
                    org = domain.split(".")[-2]
                else:
                    org = 'unknown'
                owner['organization'] = org
            else:
                owner['project_name'] = 'unknown'
                owner['organization'] = 'unknown'
            # Get user and instance_id
            user = kc.get_by_id('user', i.user_id)
            owner['user'] = user.name.lower() if user else None
            owner['instance_id'] = i.id
            # Update owner
            old = session.query(Owner).filter(Owner.ip == owner['ip']).first()
            if old is not None:
                logger.debug('=> update owner for ip %s', owner['ip'])
                old.update(owner)
            else:
                logger.debug('=> create owner for ip %s', owner['ip'])
                session.add(Owner(**owner))
            session.commit()
    session.close()
Example #21
0
def action_update():
    public = flavors['public'] if 'public' in flavors else False
    properties = flavors['properties'] if 'properties' in flavors else None
    if not properties.get('aggregate_instance_extra_specs:type', None):
        properties['aggregate_instance_extra_specs:type'] = 's== standard'
    if (options.flavor not in flavors or
            not isinstance(flavors[options.flavor], dict)):
        himutils.sys_error('%s hash not found in config' % options.flavor)

    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        nc.set_dry_run(options.dry_run)
        for name, spec in sorted(flavors[options.flavor].iteritems()):
            nc.update_flavor(name=name, spec=spec,
                             properties=properties, public=public)
        # Update access
        access = nc.get_flavor_access(filters=options.flavor)
        all_projects = set()
        for name, projects in access.iteritems():
            for project_id in projects:
                all_projects.add(project_id.tenant_id)
        for project in all_projects:
            nc.update_flavor_access(filters=options.flavor,
                                    project_id=project,
                                    action='grant')
Example #22
0
def action_purge():
    engine = create_engine(kc.get_config('db', 'database_uri'))
    Base.metadata.bind = engine
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        instances = nc.get_all_instances({'all_tenants': 1, 'deleted': 1})
        for i in instances:
            old = session.query(Owner).filter(Owner.instance_id == i.id).first()
            if old is not None:
                logger.debug('=> purge owner for instance %s', old.ip)
                session.delete(old)
                session.commit()
    session.close()
Example #23
0
def project():
    stats = {
        'demo': 0,
        'personal': 0,
        'research': 0,
        'education': 0,
        'admin': 0,
        'test': 0,
        'hpc': 0,
        'total': 0
    }
    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_instances()
        stats['total'] += len(instances)
        for i in instances:
            project = ksclient.get_by_id('project', i.tenant_id)
            if not project:
                sys.stderr.write("MISSING PROJECT! id=%s for instance %s\n" %
                                 (i.tenant_id, i.name))
                continue
            if hasattr(project, 'course'):
                stats['education'] += 1
            elif '@' in project.name:
                stats['personal'] += 1
            else:
                if hasattr(project, 'type'):
                    if project.type not in stats:
                        print "unknown project type %s for %s" % (project.type,
                                                                  project.name)
                    else:
                        stats[project.type] += 1
                else:
                    stats['admin'] += 1
    if options.output == 'count':
        stats['header'] = 'Number of instances grouped by instance type:'
        printer.output_dict(stats)
    else:
        percent = dict()
        if stats['total'] > 0:
            for s in sorted(stats):
                percent[s] = (float(stats[s]) / float(stats['total'])) * 100
        percent['header'] = 'Percent of instances grouped by instance type:'
        printer.output_dict(percent)
Example #24
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)
Example #25
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})
Example #26
0
def action_list_access():
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        nc.set_dry_run(options.dry_run)
        access = nc.get_flavor_access(filters=options.flavor)
        header = 'access to %s flavor in %s' % (options.flavor, region)
        printer.output_dict({'header': header})
        output = dict()
        for name, projects in access.iteritems():
            output[name] = list()
            for project_id in projects:
                project = kc.get_by_id('project', project_id.tenant_id)
                if project:
                    output[name].append(project.name)
                else:
                    himutils.sys_error('project not found %s' % project_id.tenant_id, 0)
                    continue
        printer.output_dict(output)
Example #27
0
def action_legacy():
    projects_count = kc.get_project_count('dataporten')
    users_count = kc.get_user_count('dataporten')

    stats = dict()
    stats['users'] = {}
    stats['projects'] = {}
    stats['instances'] = {}
    stats['instances']['total'] = {'count': 0, 'error': 0}

    for region in regions:
        logger.debug('=> count region %s' % region)

        # Projects and users (this will be the same for all regions)
        stats['projects'][region] = {}
        stats['projects']['total'] = {}
        stats['projects'][region]['count'] = projects_count
        stats['projects']['total']['count'] = projects_count
        stats['users'][region] = {}
        stats['users']['total'] = {}
        stats['users'][region]['count'] = users_count
        stats['users']['total']['count'] = users_count

        # Instances
        novaclient = Nova(options.config, debug=options.debug, region=region, log=logger)
        novastats = novaclient.get_stats()
        stats['instances'][region] = {}
        stats['instances'][region]['count'] = novastats['count']
        stats['instances'][region]['error'] = novastats['error']
        stats['instances']['total']['count'] += novastats['count']
        stats['instances']['total']['error'] += novastats['error']

    for t, s in stats.iteritems():
        for r, d in s.iteritems():
            name = '%s.%s' % (r, t)
            count = d['count']
            if not options.quiet:
                print '%s = %s' % (name, count)
            statsd.gauge(name, count)
            if 'error' in d:
                name = '%s.instance_errors' % (r)
                if not options.quiet:
                    print '%s = %s' % (name, d['error'])
                statsd.gauge(name, d['error'])
Example #28
0
def action_legacy():
    projects_count = kc.get_project_count('dataporten')
    users_count = kc.get_user_count('dataporten')

    stats = dict()
    stats['users'] = {}
    stats['projects'] = {}
    stats['instances'] = {}
    stats['instances']['total'] = {'count': 0, 'error': 0}

    for region in regions:
        logger.debug('=> count region %s' % region)

        # Projects and users (this will be the same for all regions)
        stats['projects'][region] = {}
        stats['projects']['total'] = {}
        stats['projects'][region]['count'] = projects_count
        stats['projects']['total']['count'] = projects_count
        stats['users'][region] = {}
        stats['users']['total'] = {}
        stats['users'][region]['count'] = users_count
        stats['users']['total']['count'] = users_count

        # Instances
        novaclient = Nova(options.config, debug=options.debug, region=region, log=logger)
        novastats = novaclient.get_stats()
        stats['instances'][region] = {}
        stats['instances'][region]['count'] = novastats['count']
        stats['instances'][region]['error'] = novastats['error']
        stats['instances']['total']['count'] += novastats['count']
        stats['instances']['total']['error'] += novastats['error']

    for t, s in stats.iteritems():
        for r, d in s.iteritems():
            name = '%s.%s' % (r, t)
            count = d['count']
            if not options.quiet:
                print '%s = %s' % (name, count)
            statsd.gauge(name, count)
            if 'error' in d:
                name = '%s.instance_errors' % (r)
                if not options.quiet:
                    print '%s = %s' % (name, d['error'])
                statsd.gauge(name, d['error'])
Example #29
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))
Example #30
0
def action_flavors():
    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))
    if start > stop:
        himutils.sys_error('start %s must be fore stop %s' % (start, stop))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)

    flavors = dict()
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        usage = nc.get_usage(project_id=project.id, start=start, end=stop)
        if not hasattr(usage, 'server_usages'):
            continue
        for server in usage.server_usages:
            flavors[server['flavor']] = flavors.get(server['flavor'], 0) + 1
    flavors['header'] = 'flavor usage for %s in all regions' % project.name
    printer.output_dict(flavors)
Example #31
0
def image_usage(detailed=False):
    status = 'deactivated' if options.deactive else 'active'
    novaclient = Nova(options.config, debug=options.debug, log=logger, region=options.region)
    filters = {'status': status, 'visibility': options.visibility, 'tag': tags}
    logger.debug('=> filter: %s' % filters)
    image_usage = dict()
    images = glclient.get_images(limit=1000, page_size=999, filters=filters)
    for image in images:
        image_usage[image.id] = image
        image_usage[image.id]['count'] = 0
        search_opts = {'image': image.id}
        instances = novaclient.get_all_instances(search_opts=search_opts)
        if detailed:
            image_usage[image.id]['instances'] = list()
        for i in instances:
            image_usage[image.id]['count'] += 1
            if detailed:
                image_usage[image.id]['instances'].append(i.id)
    return image_usage
Example #32
0
def action_instance():
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        instances = novaclient.get_instances()
        mapping = dict(region=region.upper())
        body_content = himutils.load_template(inputfile=options.template,
                                              mapping=mapping,
                                              log=logger)
        subject = options.subject
        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=instances,
                                           body=body_content,
                                           subject=subject,
                                           admin=True,
                                           options=['project', 'az'])
        notify.close()
        printer.output_dict(users)
Example #33
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)
Example #34
0
def project():
    stats = {'demo': 0,
             'personal': 0,
             'research': 0,
             'education': 0,
             'admin': 0,
             'test': 0,
             'total': 0}
    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_instances()
        stats['total'] += len(instances)
        for i in instances:
            project = ksclient.get_by_id('project', i.tenant_id)
            if not project:
                sys.stderr.write("MISSING PROJECT! id=%s for instance %s\n" % (i.tenant_id, i.name))
                continue
            if hasattr(project, 'course'):
                stats['education'] += 1
            elif '@' in project.name:
                stats['personal'] += 1
            else:
                if hasattr(project, 'type'):
                    if project.type not in stats:
                        print "unknown project type %s for %s" % (project.type, project.name)
                    else:
                        stats[project.type] += 1
                else:
                    stats['admin'] += 1
    if options.output == 'count':
        stats['header'] = 'Number of instances grouped by instance type:'
        printer.output_dict(stats)
    else:
        percent = dict()
        if stats['total'] > 0:
            for s in sorted(stats):
                percent[s] = (float(stats[s])/float(stats['total']))*100
        percent['header'] = 'Percent of instances grouped by instance type:'
        printer.output_dict(percent)
Example #35
0
def action_update():
    question = (
        "This will delete the flavor and recreate it for all other "
        "changes than properties. Check project access after. Continue?")
    if not himutils.confirm_action(question):
        return

    for region in regions:
        flavors = get_flavor_config(region)
        public = flavors['public'] if 'public' in flavors else False
        properties = flavors[
            'properties'] if 'properties' in flavors else dict()
        if not properties or not properties.get(
                'aggregate_instance_extra_specs:type', None):
            properties['aggregate_instance_extra_specs:type'] = 's== standard'
        if (options.flavor not in flavors
                or not isinstance(flavors[options.flavor], dict)):
            himutils.sys_error('%s hash not found in config' % options.flavor)

        nc = Nova(options.config,
                  debug=options.debug,
                  log=logger,
                  region=region)
        nc.set_dry_run(options.dry_run)
        for name, spec in sorted(flavors[options.flavor].iteritems()):
            # Hack to override properties per flavor
            flavor_properties = properties.copy()
            if 'properties' in spec:
                for p_name, prop in spec['properties'].iteritems():
                    flavor_properties[p_name] = prop
                del spec['properties']
            nc.update_flavor(name=name,
                             spec=spec,
                             properties=flavor_properties,
                             public=public)
        # Update access
        access = nc.get_flavor_access(filters=options.flavor)
        all_projects = set()
        for name, projects in access.iteritems():
            for project_id in projects:
                all_projects.add(project_id.tenant_id)
        for project in all_projects:
            nc.update_flavor_access(class_filter=options.flavor,
                                    project_id=project,
                                    action='grant')
Example #36
0
def action_whales():
    start = himutils.get_date(options.start, date.today() - timedelta(days=1))
    stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
    if start > stop:
        himutils.sys_error('start %s must be fore stop %s' % (start, stop))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)

    for region in regions:
        nc = Nova(options.config,
                  debug=options.debug,
                  log=logger,
                  region=region)
        cc = Cinder(options.config,
                    debug=options.debug,
                    log=logger,
                    region=region)
        project_usage = nc.get_usage(start=start, end=stop)
        logger.debug('=> threshold for whales filter %s', options.threshold)
        print_header = True
        for usage in project_usage:
            project = kc.get_by_id(obj_type='project', obj_id=usage.tenant_id)
            if not project:
                logger.debug('=> project with id %s not found',
                             usage.tenant_id)
                continue
            if len(usage.server_usages) < options.threshold:
                continue
            cinderusage = cc.get_quota(usage.tenant_id, True)
            admin = project.admin if hasattr(project, 'admin') else 'unknown!'
            output = OrderedDict()
            output['instances'] = len(usage.server_usages)
            output['volume_gb'] = cinderusage['gigabytes']['in_use']
            output['name'] = project.name
            output['admin'] = admin
            if print_header:
                output[
                    'header'] = 'project usage %s (instances, volume (GB), name, id)' % region
                print_header = False
            printer.output_dict(objects=output, sort=False, one_line=True)
Example #37
0
def image_usage(detailed=False):
    status = 'deactivated' if options.deactive else 'active'
    novaclient = Nova(options.config,
                      debug=options.debug,
                      log=logger,
                      region=options.region)
    filters = {'status': status, 'visibility': options.visibility, 'tag': tags}
    logger.debug('=> filter: %s' % filters)
    image_usage = dict()
    images = glclient.get_images(limit=1000, page_size=999, filters=filters)
    for image in images:
        image_usage[image.id] = image
        image_usage[image.id]['count'] = 0
        search_opts = {'image': image.id}
        instances = novaclient.get_all_instances(search_opts=search_opts)
        if detailed:
            image_usage[image.id]['instances'] = list()
        for i in instances:
            image_usage[image.id]['count'] += 1
            if detailed:
                image_usage[image.id]['instances'].append(i.id)
    return image_usage
Example #38
0
def action_instance():
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        instances = novaclient.get_instances()
        mapping = dict(region=region.upper())
        body_content = himutils.load_template(inputfile=options.template,
                                              mapping=mapping,
                                              log=logger)
        subject = options.subject
        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=instances,
                                           body=body_content,
                                           subject=subject,
                                           admin=True,
                                           options=['project', 'az'])
        notify.close()
        printer.output_dict(users)
Example #39
0
def action_flavors():
    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))
    if start > stop:
        himutils.sys_error('start %s must be fore stop %s' % (start, stop))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)

    flavors = dict()
    for region in regions:
        nc = Nova(options.config,
                  debug=options.debug,
                  log=logger,
                  region=region)
        usage = nc.get_usage(project_id=project.id, start=start, end=stop)
        if not hasattr(usage, 'server_usages'):
            continue
        for server in usage.server_usages:
            flavors[server['flavor']] = flavors.get(server['flavor'], 0) + 1
    flavors['header'] = 'flavor usage for %s in all regions' % project.name
    printer.output_dict(flavors)
Example #40
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)
Example #41
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)
Example #42
0
def action_nodiscard():
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        glclient = Glance(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        instances = novaclient.get_instances()
        printer.output_dict({
            'header':
            '%s: Instances without discard (image, name, visibility)' % region
        })
        count = 0
        for i in instances:
            image = glclient.get_image_by_id(i.image['id'])
            # Remove instances in disabled project
            project = ksclient.get_by_id('project', i.tenant_id)
            if project.enabled:
                continue
            # Assume that hw_disk_bus == discard
            if image and 'hw_disk_bus' in image:
                continue
            if not image:
                image = dict()
                image['name'] = 'unknown'
                image['visibility'] = 'unknown'
            output = {
                'name': i.name,
                'image': image['name'],
                'status': image['visibility']
            }
            printer.output_dict(output, sort=True, one_line=True)
            count += 1
        printer.output_dict({'header': 'Instance count', 'count': count})
Example #43
0
def action_instance():
    #ToDo
    for region in regions:
        flavors = dict()
        cores = ram = 0
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        instances = novaclient.get_instances()
        total = 0
        for i in instances:
            # Filter demo
            project = ksclient.get_by_id('project', i.tenant_id)
            if not options.demo and project and 'DEMO' in project.name:
                continue
            flavor_name = i.flavor.get('original_name', 'unknown')
            flavors[flavor_name] = flavors.get(flavor_name, 0) + 1
            cores += i.flavor.get('vcpus', 0)
            ram += i.flavor.get('ram', 0)
            total += 1
        printer.output_dict({'header': '%s instances' % region})
        printer.output_dict(flavors)
        printer.output_dict({'header': '%s resources' % region})
        printer.output_dict({'cores': cores,
                             'ram': '%.1f MB' % int(ram),
                             'instances': total})
Example #44
0
def action_instance():
    #ToDo
    for region in regions:
        flavors = dict()
        cores = ram = 0
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        instances = novaclient.get_instances()
        total = 0
        for i in instances:
            # Filter demo
            project = ksclient.get_by_id('project', i.tenant_id)
            if not options.demo and project and 'DEMO' in project.name:
                continue
            flavor_name = i.flavor.get('original_name', 'unknown')
            flavors[flavor_name] = flavors.get(flavor_name, 0) + 1
            cores += i.flavor.get('vcpus', 0)
            ram += i.flavor.get('ram', 0)
            total += 1
        printer.output_dict({'header': '%s instances' % region})
        printer.output_dict(flavors)
        printer.output_dict({'header': '%s resources' % region})
        printer.output_dict({'cores': cores,
                             'ram': '%.1f MB' % int(ram),
                             'instances': total})
Example #45
0
def action_show():
    project = ksclient.get_project_by_name(project_name=options.project,
                                           domain=options.domain)
    if not project:
        himutils.sys_error('No project found with name %s' % options.project)
    output_project = project.to_dict()
    output_project['header'] = "Show information for %s" % project.name
    printer.output_dict(output_project)
    if not options.detailed:
        return
    roles = ksclient.list_roles(project_name=options.project)
    printer.output_dict({'header': 'Roles in project %s' % options.project})
    for role in roles:
        printer.output_dict(role, sort=True, one_line=True)
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        cinderclient = Cinder(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
        neutronclient = Neutron(options.config,
                                debug=options.debug,
                                log=logger,
                                region=region)
        components = {
            'nova': novaclient,
            'cinder': cinderclient,
            'neutron': neutronclient
        }
        for comp, client in components.iteritems():
            quota = dict()
            if hasattr(client, 'get_quota_class'):
                quota = getattr(client, 'list_quota')(project.id)
            else:
                logger.debug('=> function get_quota_class not found for %s' %
                             comp)
                continue
            if quota:
                quota.update({
                    'header': '%s quota in %s' % (comp, region),
                    'region': region
                })
                #printer.output_dict({'header': 'Roles in project %s' % options.project})
                printer.output_dict(quota)
Example #46
0
def action_update():
    dry_run_txt = 'DRY-RUN: ' if options.dry_run else ''
    defaults = himutils.load_config('config/quotas/%s' % options.quota_config,
                                    logger)
    if not defaults:
        himutils.sys_error('No default quotas found in config/quota/%s' %
                           options.quota_config)
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        cinderclient = Cinder(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
        components = {'nova': novaclient, 'cinder': cinderclient}
        for comp, client in components.iteritems():
            if options.service != 'all' and comp != options.service:
                continue
            if comp not in defaults:
                logger.debug('=> could not find quota for %s in config' % comp)
                continue
            if hasattr(client, 'get_quota_class'):
                current = getattr(client, 'get_quota_class')()
            else:
                logger.debug('=> function get_quota_class not found for %s' %
                             comp)
                continue
            if not isinstance(current, dict):
                current = current.to_dict()
            updates = dict()
            for k, v in defaults[comp].iteritems():
                if k in current and current[k] != v:
                    logger.debug("=> %sUpdated %s: from %s to %s in %s" %
                                 (dry_run_txt, k, current[k], v, region))
                    updates[k] = v
            if updates and not options.dry_run:
                result = getattr(client, 'update_quota_class')(updates=updates)
                logger.debug('=> %s' % result)
            elif not updates:
                logger.debug(
                    '=> no need to update default quota for %s in %s' %
                    (comp, region))
Example #47
0
def action_show_quota():
    project = ksclient.get_project_by_name(project_name=options.project)
    if not project:
        himutils.sys_error('could not find project {}'.format(options.project))
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        cinderclient = Cinder(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
        neutronclient = Neutron(options.config,
                                debug=options.debug,
                                log=logger,
                                region=region)
        components = {
            'nova': novaclient,
            'cinder': cinderclient,
            'neutron': neutronclient
        }
        for comp, client in components.iteritems():
            if options.service != 'all' and comp != options.service:
                continue
            quota = dict()
            if hasattr(client, 'get_quota'):
                quota = getattr(client, 'get_quota')(project.id)
            else:
                logger.debug('=> function get_quota_class not found for %s' %
                             comp)
                continue
            if quota:
                quota.update({
                    'header': '%s quota in %s' % (comp, region),
                    'region': region
                })
                #printer.output_dict({'header': 'Roles in project %s' % options.project})
                printer.output_dict(quota)
Example #48
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)
Example #49
0
def action_show():
    for region in regions:
        novaclient = Nova(options.config,
                          debug=options.debug,
                          log=logger,
                          region=region)
        cinderclient = Cinder(options.config,
                              debug=options.debug,
                              log=logger,
                              region=region)
        components = {'nova': novaclient, 'cinder': cinderclient}
        for comp, client in components.iteritems():
            if options.service != 'all' and comp != options.service:
                continue
            if hasattr(client, 'get_quota_class'):
                current = getattr(client, 'get_quota_class')()
            else:
                logger.debug('=> function get_quota_class not found for %s' %
                             comp)
                continue
            if not isinstance(current, dict):
                current = current.to_dict()
            current['header'] = 'Quota for %s in %s' % (comp, region)
            printer.output_dict(current)
Example #50
0
def action_purge():
    for region in regions:
        nc = Nova(options.config, debug=options.debug, log=logger, region=region)
        nc.set_dry_run(options.dry_run)
        print 'Purge %s flavors in %s' % (options.flavor, region)
        nc.purge_flavors(options.flavor, flavors)
Example #51
0
def action_create():
    if not ksclient.is_valid_user(options.admin, options.domain) and options.type == 'personal':
        himutils.sys_error('not valid user', 1)
    quota = himutils.load_config('config/quotas/%s.yaml' % options.quota)
    if options.quota and not quota:
        himutils.sys_error('Could not find quota in config/quotas/%s.yaml' % options.quota)
    test = 1 if options.type == 'test' else 0
    if options.enddate:
        try:
            enddate = datetime.strptime(options.enddate, '%d.%m.%Y').date()
        except ValueError:
            himutils.sys_error('date format DD.MM.YYYY not valid for %s' % options.enddate, 1)
    else:
        enddate = None
    createdate = datetime.today()
    if not options.force:
        print 'Project name: %s\nAdmin: %s\nType: %s\nEnd date: %s\nQuota: %s\nRT: %s' \
                % (options.project,
                   options.admin.lower(),
                   options.type,
                   str(enddate),
                   options.quota,
                   options.rt)
        if not himutils.confirm_action('Are you sure you want to create this project?'):
            himutils.sys_error('Aborted', 1)
    project = ksclient.create_project(project_name=options.project,
                                      admin=options.admin.lower(),
                                      test=test,
                                      type=options.type,
                                      description=options.desc,
                                      enddate=str(enddate),
                                      createdate=createdate.isoformat(),
                                      quota=options.quota,
                                      rt=options.rt)
    if not ksclient.is_valid_user(options.admin, options.domain):
        himutils.sys_error('WARNING: "%s" is not a valid user.' % options.admin, 0)
    if not project:
        himutils.sys_error('Failed creating %s' % options.project, 1)
    else:
        output = Keystone.get_dict(project)
        output['header'] = "Show information for %s" % options.project
        printer.output_dict(output)

    # Quotas
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        cinderclient = Cinder(options.config, debug=options.debug, log=logger, region=region)
        neutronclient = Neutron(options.config, debug=options.debug, log=logger, region=region)
        cinderclient.set_dry_run(options.dry_run)
        novaclient.set_dry_run(options.dry_run)
        neutronclient.set_dry_run(options.dry_run)
        project_id = Keystone.get_attr(project, 'id')
        if quota and 'cinder' in quota and project:
            cinderclient.update_quota(project_id=project_id, updates=quota['cinder'])
        if quota and 'nova' in quota and project:
            novaclient.update_quota(project_id=project_id, updates=quota['nova'])
        if quota and 'neutron' in quota and project:
            neutronclient.update_quota(project_id=project_id, updates=quota['neutron'])

    if options.mail:
        mail = Mail(options.config, debug=options.debug)
        mail.set_dry_run(options.dry_run)

        if options.rt is None:
            himutils.sys_error('--rt parameter is missing.')
        else:
            mapping = dict(project_name=options.project,
                           admin=options.admin.lower(),
                           quota=options.quota,
                           end_date=str(enddate))
            subject = 'UH-IaaS: Project %s has been created' % options.project
            body_content = himutils.load_template(inputfile=project_msg_file,
                                                  mapping=mapping)
        if not body_content:
            himutils.sys_error('ERROR! Could not find and parse mail body in \
                               %s' % options.msg)

        mime = mail.rt_mail(options.rt, subject, body_content)
        mail.send_mail('*****@*****.**', mime)
Example #52
0
def action_test():
    novaclient = Nova(options.config, debug=options.debug, log=logger, region=options.region)
    neutronclient = Neutron(options.config, debug=options.debug, log=logger, region=options.region)
    filters = {'status': 'active', 'visibility': options.visibility, 'tag': tags}
    logger.debug('=> filter: %s' % filters)
    images = glclient.get_images(filters=filters)
    flavors = novaclient.get_flavors('m1')
    networks = neutronclient.list_networks()
    secgroup_name = 'image_test-' + str(int(time.time()))
    secgroup = neutronclient.create_security_port_group(secgroup_name, 22)
    tests = dict({'passed': 0, 'failed': 0, 'dropped': 0})
    for image in images:
        if options.name and options.name not in image.tags:
            logger.debug('=> dropped: image name %s not in tags %s', options.name,
                         ', '.join(image.tags))
            continue
        for network in networks:
            if network['name'] == 'imagebuilder':
                continue
            try:
                starttime = int(time.time())
                print '* Create instance from %s with network %s' % (image.name, network['name'])
                flavor = glclient.find_optimal_flavor(image, flavors)
                if not flavor:
                    print '* Could not optimal find flavor for %s' % image.name
                    print '-------------------------------------------------------------'
                    continue
                logger.debug('=> use %s flavor' % flavor.name)
                nics = list()
                nics.append({'net-id': network['id']})
                server = novaclient.create_server(name='image_test'+ str(int(time.time())),
                                                  flavor=flavor,
                                                  image_id=image.id,
                                                  security_groups=[secgroup['id']],
                                                  nics=nics)
                timeout = 300 # 5 min timeout
                if not server:
                    print '-------------------------------------------------------------'
                    continue
                server = novaclient.get_instance(server.id)
                while timeout > 0 and server.status == 'BUILD':
                    time.sleep(2)
                    timeout -= 2
                    server = novaclient.get_instance(server.id)
                if timeout <= 0:
                    print ('* Could not start instance from image %s in %s seconds' %
                           (image.name, timeout))
                if server.status == 'ERROR':
                    print '* Instance started with error'
                    print server.fault
                else:
                    used_time = int(time.time()) - starttime
                    print '* Instance started after %s sec' % used_time
                if server.addresses:
                    for net in server.addresses[network['name']]:
                        starttime = int(time.time())
                        ip = IP(net['addr'])
                        print ('* Instance started with IPv%s %s (%s)' %
                               (net['version'], ip, ip.iptype()))
                        if ip.iptype() == 'ALLOCATED RIPE NCC':
                            print '* Drop connection check for IPv6 for now'
                            tests['dropped'] += 1
                            continue
                        elif ip.iptype() == 'PRIVATE':
                            print '* Drop connection check for rfc1918 address for now'
                            tests['dropped'] += 1
                            continue
                        timeout = 90
                        port = False
                        while timeout > 0 and not port:
                            start = int(time.time())
                            port = himutils.check_port(address=str(ip), port=22, timeout=2, log=logger)
                            time.sleep(3)
                            timeout -= (int(time.time()) - start)
                        used_time = int(time.time()) - starttime
                        if port:
                            print '* Port 22 open on %s (%s)' % (ip, ip.iptype())
                            tests['passed'] += 1
                        else:
                            print ('* Unable to reach port 22 on %s after %s sec (%s)'
                                   % (ip, used_time, ip.iptype()))
                            tests['failed'] += 1
                else:
                    print '* No IP found for instances %s' % server.name
                try:
                    server.delete()
                    time.sleep(3)
                    print '* Instance deleted'
                except:
                    himutils.sys_error('error!!!')
                print '-------------------------------------------------------------'
            except KeyboardInterrupt:
                if server:
                    server.delete()
                    time.sleep(5)
                    print '* Instance deleted'
    print '* Delete security group'
    neutronclient.delete_security_group(secgroup['id'])
    printer.output_dict({'header': 'Result'})
    printer.output_dict(tests)
Example #53
0
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
import time

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

kc = Keystone(options.config, debug=options.debug)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
nc = Nova(options.config, debug=options.debug, log=logger)
nc.set_dry_run(options.dry_run)

source = nc.get_fqdn(options.source)
search_opts = dict(all_tenants=1, host=source)

if not nc.get_host(source):
    himutils.sys_error('Could not find source host %s' % source)

def action_list():
    instances = nc.get_all_instances(search_opts=search_opts)
    printer.output_dict({'header': 'Instance list (id, name, state, task)'})
    for i in instances:
        output = {
            'id': i.id,
            'name': i.name,
Example #54
0
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.neutron import Neutron
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
import time

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

kc = Keystone(options.config, debug=options.debug)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
nc = Nova(options.config, debug=options.debug, log=logger)
nc.set_dry_run(options.dry_run)

# Region
if hasattr(options, 'region'):
    regions = kc.find_regions(region_name=options.region)
else:
    regions = kc.find_regions()


def action_list():
    for region in regions:
        nc = himutils.get_client(Neutron, options, logger, region)
        networks = nc.list_networks()
        printer.output_dict(
            {'header': 'networks (name, shared, IPv4 addresses)'})
Example #55
0
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
import time

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

kc = Keystone(options.config, debug=options.debug)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
nc = Nova(options.config, debug=options.debug, log=logger)
nc.set_dry_run(options.dry_run)


def action_instances():
    host = nc.get_host(nc.get_fqdn(options.host))
    if not host:
        himutils.sys_error('Could not find valid host %s' % options.host)
    search_opts = dict(all_tenants=1, host=host.hypervisor_hostname)
    instances = nc.get_all_instances(search_opts=search_opts)
    printer.output_dict({'header': 'Instance list (id, name, status, updated)'})
    status = dict({'total': 0})
    for i in instances:
        # Filter for project type
        if options.type:
            project = kc.get_by_id('project', i.tenant_id)
Example #56
0
import sys

himutils.is_virtual_env()

# Default value for date: today + 5 days at 14:00
today = datetime.today()
date = datetime(today.year, today.month, today.day, 14, 0) + timedelta(days=5)

# Load parser config from config/parser/*
parser = Parser()
parser.update_default('-m', date.strftime('%Y-%m-%d around %H:00'))
options = parser.parse_args()

ksclient = Keystone(options.config, debug=options.debug)
logger = ksclient.get_logger()
novaclient = Nova(options.config, debug=options.debug, log=logger)
domain = 'Dataporten'
zone = '%s-default-1' % ksclient.region
msg_file = 'misc/notify_reboot.txt'

if 'host' in options and options.host:
    if '.' in options.host:
        host = options.host
    else:
        domain = ksclient.get_config('openstack', 'domain')
        host = options.host + '.' + domain
else:
    host = None


def action_show():
Example #57
0
 def __set_compute_quota(self, project, quota):
     self.novaclient = Nova(config_path=self.config_path,
                            debug=self.debug,
                            log=self.logger,
                            region=self.region)
     return self.novaclient.set_quota(project.id, quota)