Beispiel #1
0
    def database_insert_server(self, instance_id, status=None,
                               launched_at=None, life_time=None,
                               image_key=None, token=None):
        logging.debug('Insert instance %s', instance_id)

        query = self.database.query(Instance).filter(
            Instance.provider_id == instance_id
        )

        if query.count() > 0:
            data_instance = query.first()
        else:
            data_instance = Instance()
            data_instance.launched_at = datetime.datetime.now()

        data_instance.provider_id = instance_id
        data_instance.status = status

        if image_key:
            data_instance.image_key = image_key

        # Overwrite launched
        if launched_at is not None:
            data_instance.launched_at = launched_at

        if life_time is not None:
            data_instance.life_time = life_time

        if token is not None:
            data_instance.token = token

        self.database.merge(data_instance)
        self.database.commit()
        return data_instance
Beispiel #2
0
    def registerattendee(groupId):
        if not groupId is None:
            group = Groups.get_or_none((Groups.group_id == groupId))
            if group is None:
                # group-id not in database -> abort
                abort(404)
            else:
                event = Event.get_or_none(Event.event_id == group.event_id)
                if event is None:
                    # event_id in group is not registered
                    abort(500)
                else:
                    instance = Instance.get_or_none(
                        Instance.name == event.instance_id)
                    if instance is None:
                        abort(500)
        else:
            abort(400)
        # PLSS WHY DIDN'T I CHOOSE A JOIN

        registersuccess = request.args.get('registersuccess')
        if registersuccess is None:
            registersuccess = False

        username = request.args.get('username')

        return dict(group=group,
                    event=event,
                    instance=instance,
                    registersuccess=registersuccess,
                    username=username)
Beispiel #3
0
 def index():
     instance_list = list(
         Instance.select(Instance.hostname, Instance.name).namedtuples())
     manager_list = list(
         EventManager.select(EventManager.username, EventManager.email,
                             EventManager.site_admin).namedtuples())
     return dict(instances=instance_list, managers=manager_list)
Beispiel #4
0
    def groupsyncstate(event_id):
        req = json.loads(request.data)
        group_id = req['group_id']

        try:
            state = not req['state'] == "True"

        except:
            return redirect(url_for(".admin", event_id=event_id))

        hostname, api_key = list(
            Instance.select(Instance.hostname, Instance.api_key).join(
                Event, on=Event.instance_id == Instance.name).join(
                    Groups, on=Groups.event_id == Event.event_id).where(
                        Event.event_id == event_id).namedtuples())[0]

        with RdyApi(hostname, api_key) as api:
            api.set_sync_npcs(group_id, state)

        print(state)
        print(group_id)

        Groups.update(has_synced_npcs=state).where(
            group_id == group_id).execute()

        return redirect(url_for(".admin", event_id=event_id))
Beispiel #5
0
    def add_event_group(event_id):
        instance = list(
            Instance.select(Instance).join(
                Event, on=(Event.instance_id == Instance.name)).where(
                    Event.event_id == event_id).namedtuples())

        groupname = request.form.get('groupname')

        register_event_group(instance[0].hostname, instance[0].api_key,
                             event_id, groupname, False)

        return redirect(url_for(".admin", event_id=event_id))
Beispiel #6
0
 def instance_details(instance_id):
     managers = defaultdict(list)
     i = Instance.get_or_none(Instance.name == instance_id)
     if i is None:
         return abort(404)  # todo 404 handler
     event_list = list(
         Event.select(Event, EventManagerRelation.manager).join(
             EventManagerRelation,
             on=EventManagerRelation.event == Event.event_id,
             join_type=JOIN.LEFT_OUTER).where(
                 Event.instance == instance_id).group_by(
                     Event.event_id).namedtuples())
     for ev in event_list:
         managers[ev.event_id].append(ev.manager)
     return dict(events=event_list,
                 managers=managers,
                 name=instance_id,
                 hostname=i.hostname,
                 api_key=i.api_key)
Beispiel #7
0
    def create_instance(self, image_key, time, token):
        logging.info("Create Instance")
        image = self.config.images[image_key]
        active_instance = self.database_count_active_instance(image_key)

        if self.check_user_own_instance_type(token, image_key):
            raise DemoExceptionUserAlreadyHaveInstanceImage()
        elif 'max_instance' in image\
                and active_instance >= image['max_instance']:
            raise DemoExceptionToMuchInstanceImage()
        else:
            # Add a demand
            database_instance = Instance()
            database_instance.status = 'ASK'
            database_instance.launched_at = datetime.datetime.now()
            database_instance.life_time = 10
            database_instance.image_key = image_key
            database_instance.token = '0'
            self.database.add(database_instance)
            self.database.commit()

            pool = self.get_pooled_instance_database(image_key)
            if len(pool) > 0:
                new_instance_id = pool.pop(0)['id']
            else:
                new_instance_id = self.provider.create_instance(image)

            # If more than max go to max
            life_time = image['time_default']
            if time is not None and 'time_max' in image and\
                    time <= image['time_max']:
                life_time = time

            self.database_insert_server(
                new_instance_id, status='CREATED',
                life_time=life_time,
                image_key=image_key, token=token,
                launched_at=datetime.datetime.now()
            )

            # Remove the demand
            self.database.delete(database_instance)
            self.database.commit()

            return new_instance_id
Beispiel #8
0
    def admin(event_id: int):
        e: Event = Event.get_or_none(event_id)
        if e is None:
            abort(404)

        try:
            i: Instance = Instance.get(Instance.name == e.instance)

            api = RdyApi(i.hostname, i.api_key)
            groups = list(Groups.select().where(
                Groups.event_id == event_id).namedtuples())

        except json.decoder.JSONDecodeError:
            abort(500, 'API Key invalid!')

        except DoesNotExist:
            abort(500, 'Instance does not Exist!')

        return dict(name=e.name,
                    event_id=e.event_id,
                    instance=i.name,
                    instance_host=i.hostname,
                    groups=groups)
Beispiel #9
0
    def register_instance():
        instance_name = request.form.get('name')
        if not instance_name:
            flash('Missing Name')
            return redirect(url_for('.index'))

        api_key = request.form.get('api_key').rstrip().lstrip()
        if not api_key:
            flash("No API KEY")
            return redirect(url_for('.index'))

        hostname = request.form.get('hostname').rstrip().lstrip()
        if not hostname:
            flash('Missing hostname')
            return redirect(url_for('.index'))

        i, created = Instance.get_or_create(name=instance_name,
                                            hostname=hostname,
                                            api_key=api_key)
        if not created:
            flash('This instance name is already in use.')
            return redirect(url_for('.index'))
        return redirect(url_for('.instance_details',
                                instance_id=instance_name))
Beispiel #10
0
    def add_event_attendee(event_id):
        username = request.form.get('username')
        groupID = request.form.get('group-id')
        email = request.form.get('email')

        if username is None:
            flash("please enter a username")
            return redirect(url_for('.admin', event_id=event_id))

        if groupID is None:
            flash("please choose a Group")
            return redirect(url_for('.admin', event_id=event_id))

        instance = list(
            Instance.select(Instance).join(
                Event, on=(Event.instance_id == Instance.name)).where(
                    Event.event_id == event_id).namedtuples())

        if email is None:
            flash("please enter a email for sending user credentials")
            return redirect(url_for('.admin', event_id=event_id))
#            emails = list(Event.select()
#                               .join(EventManagerRelation, on=Event.event_id == EventManagerRelation.event_id)
#                               .join(EventManager, on=EventManager.username == EventManagerRelation.manager_id)
#                               .namedtuples())

        register_event_attendee(instance[0].hostname,
                                instance[0].api_key,
                                event_id,
                                username,
                                pw_gen(8),
                                groupID,
                                sendmail=True,
                                email=email)

        return redirect(url_for(".admin", event_id=event_id))