Exemple #1
0
def action_user():
    if not ksclient.is_valid_user(email=options.user, domain=options.domain):
        print "%s is not a valid user. Please check your spelling or case." % options.user
        sys.exit(1)
    user = ksclient.get_user_objects(email=options.user, domain=options.domain)

    # Project counter
    count = 0

    for project in user['projects']:
        if options.admin and project.admin != options.user:
            continue
        sys.stdout.write(
            Printer.prettyprint_project_metadata(project, options, logger,
                                                 regions, options.user))
        if options.detail:
            sys.stdout.write(
                Printer.prettyprint_project_zones(project, options, logger))
            sys.stdout.write(
                Printer.prettyprint_project_volumes(project, options, logger,
                                                    regions))
            sys.stdout.write(
                Printer.prettyprint_project_images(project, options, logger,
                                                   regions))
            sys.stdout.write(
                Printer.prettyprint_project_instances(project, options, logger,
                                                      regions))

        # Print some vertical space and increase project counter
        print "\n\n"
        count += 1

    # Finally print out number of projects
    printer.output_dict({'header': 'Project list count', 'count': count})
Exemple #2
0
def action_list():
    search_filter = dict()
    if options.filter and options.filter != 'all':
        search_filter['type'] = options.filter
    projects = ksclient.get_projects(**search_filter)

    # Project counter
    count = 0

    # Loop through projects
    for project in projects:
        sys.stdout.write(
            Printer.prettyprint_project_metadata(project, options, logger,
                                                 regions))
        if options.detail:
            sys.stdout.write(
                Printer.prettyprint_project_zones(project, options, logger))
            sys.stdout.write(
                Printer.prettyprint_project_volumes(project, options, logger,
                                                    regions))
            sys.stdout.write(
                Printer.prettyprint_project_images(project, options, logger,
                                                   regions))
            sys.stdout.write(
                Printer.prettyprint_project_instances(project, options, logger,
                                                      regions))

        # Print some vertical space and increase project counter
        print "\n\n"
        count += 1

    # Finally print out number of projects
    printer.output_dict({'header': 'Project list count', 'count': count})
Exemple #3
0
def action_show():
    project = ksclient.get_project_by_name(project_name=options.project)
    if not project:
        utils.sys_error('No project found with name %s' % options.project)
    sys.stdout.write(
        Printer.prettyprint_project_metadata(project, options, logger,
                                             regions))
    if options.detail:
        sys.stdout.write(
            Printer.prettyprint_project_zones(project, options, logger))
        sys.stdout.write(
            Printer.prettyprint_project_volumes(project, options, logger,
                                                regions))
        sys.stdout.write(
            Printer.prettyprint_project_images(project, options, logger,
                                               regions))
        sys.stdout.write(
            Printer.prettyprint_project_instances(project, options, logger,
                                                  regions))
Exemple #4
0
def action_mail():
    if options.mail_user:
        if not ksclient.is_valid_user(email=options.mail_user,
                                      domain=options.domain):
            print "%s is not a valid user. Please check your spelling or case." % options.mail_user
            sys.exit(1)
        users = [options.mail_user]
    else:
        users = ksclient.list_users(domain=options.domain)

    # We want details
    options.detail = True

    # Attachment dict
    attachment = dict()

    # Admin/member dict
    admin = dict()
    member = dict()

    # Project counter
    project_counter = 0

    # Ask for confirmation
    if not options.force and not options.dry_run:
        if not utils.confirm_action(
                'Send mail to (potentially) %d users?' % len(users)):
            return

    # Loop through projects
    for user in users:
        # Ignore system users
        if not '@' in user:
            continue

        # Get user object
        this_user = ksclient.get_user_objects(email=user,
                                              domain=options.domain)
        if not this_user:
            continue

        # Ignore users who only have a DEMO project, i.e. number of
        # projects is equal or less than 1
        if len(this_user['projects']) <= 1:
            continue

        # Set common mail parameters
        mail = utils.get_client(Mail, options, logger)
        mail = Mail(options.config, debug=options.debug)
        mail.set_dry_run(options.dry_run)
        if options.fromaddr:
            fromaddr = options.fromaddr
        else:
            fromaddr = '*****@*****.**'

        # Loop through projects collecting info
        attachment_payload = ''
        admin_counter = 0
        member_counter = 0
        for project in this_user['projects']:
            attachment_payload += Printer.prettyprint_project_metadata(
                project, options, logger, regions, user)
            attachment_payload += Printer.prettyprint_project_zones(
                project, options, logger)
            attachment_payload += Printer.prettyprint_project_volumes(
                project, options, logger, regions)
            attachment_payload += Printer.prettyprint_project_images(
                project, options, logger, regions)
            attachment_payload += Printer.prettyprint_project_instances(
                project, options, logger, regions)

            # Add some vertical space
            attachment_payload += "\n\n"

            # Increase counters
            if project.admin == user:
                admin_counter += 1
            else:
                member_counter += 1

        # Construct mail content
        body_content = utils.load_template(inputfile=options.template,
                                           mapping={
                                               'admin_count': admin_counter,
                                               'member_count': member_counter
                                           },
                                           log=logger)
        msg = mail.create_mail_with_txt_attachment(options.subject,
                                                   body_content,
                                                   attachment_payload,
                                                   'resources.txt', fromaddr)
        # Send mail to user
        mail.send_mail(user, msg, fromaddr)
        if options.dry_run:
            print "Did NOT send spam to %s" % user
            print "    --> admin for %d projects, member of %d projects" % (
                admin_counter, member_counter)
        else:
            print "Spam sent to %s" % user