def new_vm():
    request_time = time.time()
    fields = validate_vm_request_form(request.vars)
    approver_mail_required = False
    mail2 = True
    if len(fields):
        raise HTTP(400,body=jsonify(status='fail', fields=fields))
    try:
        if (ldap.user_is_faculty(session.username)) or \
                (user_is_project_admin):
            owner_id = session.username
            vm_state = 1
        else:
            owner_id = request.vars.faculty
            approver_mail_required = True
            vm_state = 0

        public_ip_required = 1 if request.vars.public_ip == 'yes' else 0
        db.vm_requests.insert(vm_name=request.vars.vm_name,
                              flavor=request.vars.config,
                              sec_domain=request.vars.sec_domain,
                              image=request.vars.template,
                              owner=owner_id,
                              requester=session.username,
                              purpose=request.vars.purpose,
                              public_ip_required=public_ip_required,
                              extra_storage=request.vars.storage,
                              collaborators=request.vars.collaborators,
                              request_time=request_time,
                              state=vm_state
                              )
        context = Storage()
        user_info = ldap.fetch_user_info(session.username)
        context.username = user_info['user_name']
        user_email = user_info['user_email']
        context.vm_name = request.vars.vm_name
        context.mail_support = mail_support

        mail1 = mailer.send(mailer.MailTypes.VMRequest, user_email, context)
        if approver_mail_required:
            user_info = ldap.fetch_user_info(request.vars.faculty)
            context.approver = user_info['user_name']
            user_email = user_info['user_email']
            context.request_type = 'New VM'
            context.request_time = seconds_to_localtime(request_time)
            mail2 = mailer.send(mailer.MailTypes.ApprovalReminder, user_email,
                                context)
        if mail1 and mail2:
            db.commit()
            return jsonify()
        else:
            db.rollback()
            raise Exception('Email sending failed')
    except Exception as e:
        logger.exception(e.message or str(e.__class__))
        return jsonify(status='fail', message=e.message or str(e.__class__))
示例#2
0
def new_vm():
    request_time = time.time()
    fields = validate_vm_request_form(request.vars)
    approver_mail_required = False
    mail2 = True
    if len(fields):
        raise HTTP(400, body=jsonify(status='fail', fields=fields))
    try:
        if (ldap.user_is_faculty(session.username)) or \
                (user_is_project_admin):
            owner_id = session.username
            vm_state = 1
        else:
            owner_id = request.vars.faculty
            approver_mail_required = True
            vm_state = 0

        public_ip_required = 1 if request.vars.public_ip == 'yes' else 0
        db.vm_requests.insert(vm_name=request.vars.vm_name,
                              flavor=request.vars.config,
                              sec_domain=request.vars.sec_domain,
                              image=request.vars.template,
                              owner=owner_id,
                              requester=session.username,
                              purpose=request.vars.purpose,
                              public_ip_required=public_ip_required,
                              extra_storage=request.vars.storage,
                              collaborators=request.vars.collaborators,
                              request_time=request_time,
                              state=vm_state)
        context = Storage()
        user_info = ldap.fetch_user_info(session.username)
        context.username = user_info['user_name']
        user_email = user_info['user_email']
        context.vm_name = request.vars.vm_name
        context.mail_support = mail_support

        mail1 = mailer.send(mailer.MailTypes.VMRequest, user_email, context)
        if approver_mail_required:
            user_info = ldap.fetch_user_info(request.vars.faculty)
            context.approver = user_info['user_name']
            user_email = user_info['user_email']
            context.request_type = 'New VM'
            context.request_time = seconds_to_localtime(request_time)
            mail2 = mailer.send(mailer.MailTypes.ApprovalReminder, user_email,
                                context)
        if mail1 and mail2:
            db.commit()
            return jsonify()
        else:
            db.rollback()
            raise Exception('Email sending failed')
    except Exception as e:
        logger.exception(e.message or str(e.__class__))
        return jsonify(status='fail', message=e.message or str(e.__class__))
示例#3
0
def task_create_vm(reqid, auth):
    logger.debug('inside scheduler')
    try:
        auth = Storage(loads(b64decode(auth)))
        req = db(db.vm_requests.id == reqid).select()[0]
        conn = Baadal.Connection(_authurl, _tenant, auth.u, auth.p)
        name = req.vm_name
        img = req.image
        owner = req.owner
        collaborators = req.collaborators
        requester = req.requester
        flavor = req.flavor
        nics = [{'net-id': req.sec_domain}]
        kp = default_keypair
        pub_ip = req.public_ip_required
        vdisk = req.extra_storage
        vm = conn.create_baadal_vm(name,
                                   img,
                                   flavor,
                                   nics,
                                   key_name=kp,
                                   requester=requester,
                                   owner=owner,
                                   collaborators=collaborators)
        status = vm.get_status()
        while status not in ('Running', 'Error'):
            logger.info('VM %s in creation, current status %s' % \
                        (name, status))
            sleep(5)
            status = vm.get_status()
        if status == 'Running':
            logger.info('VM created')
            try:
                req.update_record(state=REQUEST_STATUS_APPROVED)
                context = Storage()
                user_info = ldap.fetch_user_info(req.requester)
                context.username = user_info['user_name']
                context.user_email = user_info['user_email']
                context.vm_name = name
                context.mail_support = mail_support
                context.gateway_server = gateway_server
                context.request_time = seconds_to_localtime(req.request_time)
                logger.info('sending mail')
                mailer.send(mailer.MailTypes.VMCreated, context.user_email,
                            context)
                logger.info('mail sent')
                if pub_ip == 1 or vdisk:
                    if pub_ip:
                        vm.attach_floating_ip()

                    if vdisk:
                        disk = conn.create_volume(vdisk)
                        while disk.status != 'available':
                            disk = conn.get_disk_by_id(disk.id)
                        num_disks = vm.metadata().get('disks', 0)
                        disk_path = '/dev/vd' + chr(97 + num_disks)
                        vm.attach_disk(disk, disk_path)
                        vm.update(disks=num_disks + 1)
            except Exception as e:
                logger.exception(e)
        else:
            # VM state Error
            req.update_record(state=REQUEST_STATUS_POSTED)
            raise Exception('VM build failed')
    except Exception as e:
        req.update_record(state=REQUEST_STATUS_POSTED)
        logger.exception(e)
    finally:
        db.commit()
示例#4
0
def task_create_vm(reqid, auth):
    logger.debug('inside scheduler')
    try:
        auth = Storage(loads(b64decode(auth)))
        req = db(db.vm_requests.id == reqid).select()[0]
        conn = Baadal.Connection(_authurl, _tenant, auth.u, auth.p)
        name = req.vm_name
        img = req.image
        owner = req.owner
        collaborators = req.collaborators
        requester = req.requester
        flavor = req.flavor
        nics = [{'net-id': req.sec_domain}]
        kp = default_keypair
        pub_ip = req.public_ip_required
        vdisk = req.extra_storage
        vm = conn.create_baadal_vm(name, img, flavor, nics, key_name=kp,
                                   requester=requester, owner=owner,
                                   collaborators=collaborators)
        status = vm.get_status()
        while status not in ('Running', 'Error'):
            logger.info('VM %s in creation, current status %s' % \
                        (name, status))
            sleep(5)
            status = vm.get_status()
        if status == 'Running':
            logger.info('VM created')
            try:
                req.update_record(state=REQUEST_STATUS_APPROVED)
                context = Storage()
                user_info = ldap.fetch_user_info(req.requester)
                context.username = user_info['user_name']
                context.user_email = user_info['user_email']
                context.vm_name = name
                context.mail_support = mail_support
                context.gateway_server = gateway_server
                context.request_time = seconds_to_localtime(req.request_time)
                logger.info('sending mail')
                mailer.send(mailer.MailTypes.VMCreated, context.user_email,
                            context)
                logger.info('mail sent')
                if pub_ip == 1 or vdisk:
                    if pub_ip:
                        vm.attach_floating_ip()

                    if vdisk:
                        disk = conn.create_volume(vdisk)
                        while disk.status != 'available':
                            disk = conn.get_disk_by_id(disk.id)
                        num_disks = vm.metadata().get('disks', 0)
                        disk_path = '/dev/vd' + chr(97 + num_disks)
                        vm.attach_disk(disk, disk_path)
                        vm.update(disks=num_disks + 1)
            except Exception as e:
                logger.exception(e)
        else:
            # VM state Error
            req.update_record(state=REQUEST_STATUS_POSTED)
            raise Exception('VM build failed')
    except Exception as e:
        req.update_record(state=REQUEST_STATUS_POSTED)
        logger.exception(e)
    finally:
        db.commit()