Пример #1
0
    def get(self):
        self.log_debug("APIInfo handler got GET request")
        key = self.get_argument("key", None)
        sign = self.get_argument("sign", None)

        if key is None or sign is None:
            self.send_error()
            return
        sign2 = signstr(key, JBoxCfg.get('sesskey'))

        if sign != sign2:
            self.log_info("signature mismatch. key:%r sign:%r expected:%r", key, sign, sign2)
            self.send_error()
            return

        api_status = JBoxInstanceProps.get_instance_status(Compute.get_install_id())
        self.log_info("cluster api status: %r", api_status)

        # filter out instances that should not accept more load
        filtered_api_status = {k: v for (k, v) in api_status.iteritems() if v['accept']}
        preferred_instances = filtered_api_status.keys()

        # flip the dict
        per_api_instances = dict()
        for (inst, status) in filtered_api_status.iteritems():
            api_names = status['api_status'].keys()
            for api_name in api_names:
                v = per_api_instances.get(api_name, [])
                v.append(inst)

        per_api_instances[" preferred "] = preferred_instances
        self.log_info("per api instances: %r", per_api_instances)
        self.write(per_api_instances)
        return
Пример #2
0
    def is_user_activated(jbuser):
        reg_allowed = JBoxDynConfig.get_allow_registration(
            Compute.get_install_id())
        if jbuser.is_new:
            if not reg_allowed:
                activation_state = JBoxUserV2.ACTIVATION_REQUESTED
            else:
                activation_state = JBoxUserV2.ACTIVATION_GRANTED
            jbuser.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO,
                                        activation_state)
            jbuser.save()
        else:
            activation_code, activation_state = jbuser.get_activation_state()
            if reg_allowed and (activation_state !=
                                JBoxUserV2.ACTIVATION_GRANTED):
                activation_state = JBoxUserV2.ACTIVATION_GRANTED
                jbuser.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO,
                                            activation_state)
                jbuser.save()
            elif activation_state != JBoxUserV2.ACTIVATION_GRANTED:
                if not ((activation_state == JBoxUserV2.ACTIVATION_REQUESTED)
                        and
                        (activation_code == JBoxUserV2.ACTIVATION_CODE_AUTO)):
                    activation_state = JBoxUserV2.ACTIVATION_REQUESTED
                    jbuser.set_activation_state(
                        JBoxUserV2.ACTIVATION_CODE_AUTO, activation_state)
                    jbuser.save()

        return activation_state == JBoxUserV2.ACTIVATION_GRANTED
Пример #3
0
    def update_user_home_image(fetch=True):
        plugin = JBPluginCloud.jbox_get_plugin(JBPluginCloud.JBP_BUCKETSTORE)
        if plugin is None:
            VolMgr.log_info("No plugin provided for bucketstore. Can not update packages and user home images")
            return

        home_img_dir, curr_home_img = os.path.split(JBoxVol.USER_HOME_IMG)
        pkg_img_dir, curr_pkg_img = os.path.split(JBoxVol.PKG_IMG)

        bucket, new_pkg_img, new_home_img = JBoxDynConfig.get_user_home_image(Compute.get_install_id())

        new_home_img_path = os.path.join(home_img_dir, new_home_img)
        new_pkg_img_path = os.path.join(pkg_img_dir, new_pkg_img)
        updated = False
        for img_path in (new_home_img_path, new_pkg_img_path):
            if not os.path.exists(img_path):
                if fetch:
                    VolMgr.log_debug("fetching new image to %s", img_path)
                    k = plugin.pull(bucket, img_path)
                    if k is not None:
                        VolMgr.log_debug("fetched new image")

        if os.path.exists(new_home_img_path):
            VolMgr.log_debug("set new home image to %s", new_home_img_path)
            JBoxVol.USER_HOME_IMG = new_home_img_path
            updated = True

        if os.path.exists(new_pkg_img_path):
            VolMgr.log_debug("set new pkg image to %s", new_pkg_img_path)
            JBoxVol.PKG_IMG = new_pkg_img_path
            updated = True

        return updated
Пример #4
0
def is_cluster_leader():
    self_id = Compute.get_instance_id()
    cluster = Compute.get_install_id()
    instances = Compute.get_all_instances()
    leader = JBoxDynConfig.get_cluster_leader(cluster)
    img_recentness = Compute.get_image_recentness()
    JBoxDB.log_debug("cluster: %s. instances: %s. leader: %s. image recentness: %d",
                     cluster, repr(instances), repr(leader), img_recentness)

    # if none set, or set instance is dead elect self as leader, but wait till next cycle to prevent conflicts
    if (leader is None) or (leader not in instances) and (img_recentness >= 0):
        JBoxDB.log_info("setting self (%s) as cluster leader", self_id)
        try:
            JBoxDynConfig.set_cluster_leader(cluster, self_id)
        except:
            JBoxDB.log_info("error setting self (%s) as cluster leader, will retry", self_id)
        return False

    is_leader = (leader == self_id)

    # if running an older ami, step down from cluster leader
    if (img_recentness < 0) and is_leader:
        JBoxDB.log_info("unmarking self (%s) as cluster leader", self_id)
        JBoxDynConfig.unset_cluster_leader(cluster)
        return False

    return is_leader
Пример #5
0
    def has_update_for_user_home_image():
        home_img_dir, curr_home_img = os.path.split(JBoxVol.USER_HOME_IMG)
        pkg_img_dir, curr_pkg_img = os.path.split(JBoxVol.PKG_IMG)

        # VolMgr.log_debug("checking for updates to user home image %s/%s", img_dir, curr_img)
        bucket, new_pkg_img, new_home_img = JBoxDynConfig.get_user_home_image(
            Compute.get_install_id())

        if bucket is None:
            VolMgr.log_info("Home: none configured. current: %s/%s",
                            home_img_dir, curr_home_img)
            return False

        if new_home_img == curr_home_img and new_pkg_img == curr_pkg_img:
            VolMgr.log_info("Home: no updates. current: %s/%s", home_img_dir,
                            curr_home_img)
            VolMgr.log_info("Packages: no updates. current: %s/%s",
                            pkg_img_dir, curr_pkg_img)
            return False
        else:
            VolMgr.log_info("Home: update: %s/%s. current: %s/%s", bucket,
                            new_home_img, home_img_dir, curr_home_img)
            VolMgr.log_info("Packages: update: %s/%s. current: %s/%s", bucket,
                            new_pkg_img, home_img_dir, curr_pkg_img)

        return True
Пример #6
0
 def do_cluster_housekeeping():
     JBoxEBSHousekeep.log_debug("starting cluster housekeeping")
     detached_disks = JBoxDiskState.get_detached_disks()
     time_now = datetime.datetime.now(pytz.utc)
     for disk_key in detached_disks:
         disk_info = JBoxDiskState(disk_key=disk_key)
         user_id = disk_info.get_user_id()
         sess_props = JBoxSessionProps(Compute.get_install_id(), unique_sessname(user_id))
         incomplete_snapshots = []
         modified = False
         for snap_id in disk_info.get_snapshot_ids():
             if not EBSVol.is_snapshot_complete(snap_id):
                 incomplete_snapshots.append(snap_id)
                 continue
             JBoxEBSHousekeep.log_debug("updating latest snapshot of user %s to %s", user_id, snap_id)
             old_snap_id = sess_props.get_snapshot_id()
             sess_props.set_snapshot_id(snap_id)
             modified = True
             if old_snap_id is not None:
                 EBSVol.delete_snapshot(old_snap_id)
         if modified:
             sess_props.save()
             disk_info.set_snapshot_ids(incomplete_snapshots)
             disk_info.save()
         if len(incomplete_snapshots) == 0:
             if (time_now - disk_info.get_detach_time()).total_seconds() > 24 * 60 * 60:
                 vol_id = disk_info.get_volume_id()
                 JBoxEBSHousekeep.log_debug("volume %s for user %s unused for too long", vol_id, user_id)
                 disk_info.delete()
                 EBSVol.detach_volume(vol_id, delete=True)
         else:
             JBoxEBSHousekeep.log_debug("ongoing snapshots of user %s: %r", user_id, incomplete_snapshots)
     JBoxEBSHousekeep.log_debug("finished cluster housekeeping")
Пример #7
0
def is_cluster_leader():
    self_id = Compute.get_instance_id()
    cluster = Compute.get_install_id()
    instances = Compute.get_all_instances()
    leader = JBoxDynConfig.get_cluster_leader(cluster)
    img_recentness = Compute.get_image_recentness()
    JBoxDB.log_debug(
        "cluster: %s. instances: %s. leader: %s. image recentness: %d",
        cluster, repr(instances), repr(leader), img_recentness)

    # if none set, or set instance is dead elect self as leader, but wait till next cycle to prevent conflicts
    if (leader is None) or (leader not in instances) and (img_recentness >= 0):
        JBoxDB.log_info("setting self (%s) as cluster leader", self_id)
        try:
            JBoxDynConfig.set_cluster_leader(cluster, self_id)
        except:
            JBoxDB.log_info(
                "error setting self (%s) as cluster leader, will retry",
                self_id)
        return False

    is_leader = (leader == self_id)

    # if running an older ami, step down from cluster leader
    if (img_recentness < 0) and is_leader:
        JBoxDB.log_info("unmarking self (%s) as cluster leader", self_id)
        JBoxDynConfig.unset_cluster_leader(cluster)
        return False

    return is_leader
Пример #8
0
    def handle_get_metadata(self, is_admin, courses_offered):
        mode = self.get_argument('mode', None)
        if (mode is None) or (mode != "metadata"):
            return False

        self.log_debug("handling answers")
        params = self.get_argument('params', None)
        params = json.loads(params)
        course_id = params['course']
        problemset_id = params['problemset']
        question_ids = params['questions'] if 'questions' in params else None
        send_answers = True

        if (not is_admin) and (course_id not in courses_offered):
            send_answers = False

        err = None
        course = JBoxDynConfig.get_course(Compute.get_install_id(), course_id)
        self.log_debug("got course %r", course)
        if problemset_id not in course['problemsets']:
            err = "Problem set %s not found!" % (problemset_id,)
        if question_ids is None:
            question_ids = course['questions'][problemset_id]

        if err is None:
            report = JBoxCourseHomework.get_problemset_metadata(course_id, problemset_id, question_ids, send_answers)
            code = 0
        else:
            report = err
            code = -1

        response = {'code': code, 'data': report}
        self.write(response)
        return True
Пример #9
0
 def find_logged_in_instance(user_id):
     sessname = unique_sessname(user_id)
     try:
         sess_props = JBoxSessionProps(Compute.get_install_id(), sessname)
         return sess_props.get_instance_id()
     except JBoxDBItemNotFound:
         return None
Пример #10
0
    def update_user_home_image(fetch=True):
        plugin = JBPluginCloud.jbox_get_plugin(JBPluginCloud.JBP_BUCKETSTORE)
        if plugin is None:
            VolMgr.log_info("No plugin provided for bucketstore. Can not update packages and user home images")
            return

        home_img_dir, curr_home_img = os.path.split(JBoxVol.USER_HOME_IMG)
        pkg_img_dir, curr_pkg_img = os.path.split(JBoxVol.PKG_IMG)

        bucket, new_pkg_img, new_home_img = JBoxDynConfig.get_user_home_image(Compute.get_install_id())

        new_home_img_path = os.path.join(home_img_dir, new_home_img)
        new_pkg_img_path = os.path.join(pkg_img_dir, new_pkg_img)
        updated = False
        for img_path in (new_home_img_path, new_pkg_img_path):
            if not os.path.exists(img_path):
                if fetch:
                    VolMgr.log_debug("fetching new image to %s", img_path)
                    k = plugin.pull(bucket, img_path)
                    if k is not None:
                        VolMgr.log_debug("fetched new image")

        if os.path.exists(new_home_img_path):
            VolMgr.log_debug("set new home image to %s", new_home_img_path)
            JBoxVol.USER_HOME_IMG = new_home_img_path
            updated = True

        if os.path.exists(new_pkg_img_path):
            VolMgr.log_debug("set new pkg image to %s", new_pkg_img_path)
            JBoxVol.PKG_IMG = new_pkg_img_path
            updated = True

        return updated
Пример #11
0
 def do_cluster_housekeeping():
     JBoxEBSHousekeep.log_debug("starting cluster housekeeping")
     detached_disks = JBoxDiskState.get_detached_disks()
     time_now = datetime.datetime.now(pytz.utc)
     for disk_key in detached_disks:
         disk_info = JBoxDiskState(disk_key=disk_key)
         user_id = disk_info.get_user_id()
         sess_props = JBoxSessionProps(Compute.get_install_id(), unique_sessname(user_id))
         incomplete_snapshots = []
         modified = False
         for snap_id in disk_info.get_snapshot_ids():
             if not EBSVol.is_snapshot_complete(snap_id):
                 incomplete_snapshots.append(snap_id)
                 continue
             JBoxEBSHousekeep.log_debug("updating latest snapshot of user %s to %s", user_id, snap_id)
             old_snap_id = sess_props.get_snapshot_id()
             sess_props.set_snapshot_id(snap_id)
             modified = True
             if old_snap_id is not None:
                 EBSVol.delete_snapshot(old_snap_id)
         if modified:
             sess_props.save()
             disk_info.set_snapshot_ids(incomplete_snapshots)
             disk_info.save()
         if len(incomplete_snapshots) == 0:
             if (time_now - disk_info.get_detach_time()).total_seconds() > 24*60*60:
                 vol_id = disk_info.get_volume_id()
                 JBoxEBSHousekeep.log_debug("volume %s for user %s unused for too long", vol_id, user_id)
                 disk_info.delete()
                 EBSVol.detach_volume(vol_id, delete=True)
         else:
             JBoxEBSHousekeep.log_debug("ongoing snapshots of user %s: %r", user_id, incomplete_snapshots)
     JBoxEBSHousekeep.log_debug("finished cluster housekeeping")
Пример #12
0
 def _backup(self, clear_volume=False):
     sess_props = JBoxSessionProps(Compute.get_install_id(), self.sessname)
     desc = sess_props.get_user_id() + " JuliaBox Backup"
     disk_id = self.disk_path.split('/')[-1]
     snap_id = EBSVol.snapshot_volume(dev_id=disk_id,
                                      tag=self.sessname,
                                      description=desc,
                                      wait_till_complete=False)
     return snap_id
Пример #13
0
    def get_disk_for_user(user_email):
        JBoxEBSVol.log_debug("creating EBS volume for %s", user_email)

        disk_id = JBoxEBSVol._reserve_disk_id()
        if disk_id is None:
            raise Exception("No free disk available")

        try:
            existing_disk = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID,
                                          region_id=CompEC2.REGION,
                                          user_id=user_email)
        except Exception as ex:
            JBoxEBSVol.log_debug("No existing disk for %s. Exception %r",
                                 user_email, ex)
            existing_disk = None

        if existing_disk is None:
            sess_id = unique_sessname(user_email)
            sess_props = JBoxSessionProps(Compute.get_install_id(),
                                          sess_id,
                                          create=True,
                                          user_id=user_email)
            if sess_props.is_new:
                sess_props.save()
            snap_id = sess_props.get_snapshot_id()
            if snap_id is None:
                snap_id = JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT

            JBoxEBSVol.log_debug("will use snapshot id %s for %s", snap_id,
                                 user_email)

            dev_path, vol_id = EBSVol.create_new_volume(
                snap_id,
                disk_id,
                tag=user_email,
                disk_sz_gb=JBoxEBSVol.DISK_LIMIT)
            existing_disk = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID,
                                          region_id=CompEC2.REGION,
                                          user_id=user_email,
                                          volume_id=vol_id,
                                          attach_time=None,
                                          create=True)
        else:
            dev_path = EBSVol.attach_volume(existing_disk.get_volume_id(),
                                            disk_id)

        existing_disk.set_state(JBoxDiskState.STATE_ATTACHING)
        existing_disk.save()

        return JBoxEBSVol(dev_path, user_email=user_email)
Пример #14
0
    def handle_if_instance_info(self, is_allowed):
        stats = self.get_argument('instance_info', None)
        if stats is None:
            return False

        if not is_allowed:
            AdminHandler.log_error("Show instance info not allowed for user")
            response = {'code': -1, 'data': 'You do not have permissions to view these stats'}
        else:
            try:
                if stats == 'load':
                    result = {}
                    # get cluster loads
                    average_load = Compute.get_cluster_average_stats('Load')
                    if average_load is not None:
                        result['Average Load'] = average_load

                    machine_loads = Compute.get_cluster_stats('Load')
                    if machine_loads is not None:
                        for n, v in machine_loads.iteritems():
                            result['Instance ' + n] = v
                elif stats == 'sessions':
                    result = JBoxSessionProps.get_active_sessions(Compute.get_install_id())
                elif stats == 'apis':
                    result = JBoxInstanceProps.get_instance_status(Compute.get_install_id())
                else:
                    raise Exception("unknown command %s" % (stats,))

                response = {'code': 0, 'data': result}
            except:
                AdminHandler.log_error("exception while getting stats")
                AdminHandler._get_logger().exception("exception while getting stats")
                response = {'code': -1, 'data': 'error getting stats'}

        self.write(response)
        return True
Пример #15
0
 def do_periodic_task(_mode):
     active_clusters = UserCluster.list_all_groupids()
     ParallelHousekeep.log_info("%d active clusters", len(active_clusters))
     if len(active_clusters) == 0:
         return
     for cluster_id in active_clusters:
         sessname = UserCluster.sessname_for_cluster(cluster_id)
         try:
             sess_props = JBoxSessionProps(Compute.get_install_id(), sessname)
             if not sess_props.get_instance_id():
                 ParallelHousekeep.log_info(
                     "Session (%s) corresponding to cluster (%s) not found. Terminating cluster.",
                     sessname, cluster_id)
                 ParallelHousekeep.terminate_or_delete_cluster(cluster_id)
         except JBoxDBItemNotFound:
             pass
Пример #16
0
 def do_periodic_task(_mode):
     active_clusters = UserCluster.list_all_groupids()
     ParallelHousekeep.log_info("%d active clusters", len(active_clusters))
     if len(active_clusters) == 0:
         return
     for cluster_id in active_clusters:
         sessname = UserCluster.sessname_for_cluster(cluster_id)
         try:
             sess_props = JBoxSessionProps(Compute.get_install_id(),
                                           sessname)
             if not sess_props.get_instance_id():
                 ParallelHousekeep.log_info(
                     "Session (%s) corresponding to cluster (%s) not found. Terminating cluster.",
                     sessname, cluster_id)
                 ParallelHousekeep.terminate_or_delete_cluster(cluster_id)
         except JBoxDBItemNotFound:
             pass
Пример #17
0
    def handle_if_report(self, user_id, is_admin, courses_offered):
        mode = self.get_argument('mode', None)
        if (mode is None) or ((mode != "report") and (mode != "myreport")):
            return False

        self.log_debug("handling report")
        params = self.get_argument('params', None)
        params = json.loads(params)
        course_id = params['course']
        problemset_id = params['problemset']
        question_ids = params['questions'] if 'questions' in params else None

        if mode == "myreport":
            student_id = user_id
        else:
            student_id = params['student'] if 'student' in params else None

        err = None
        if (not is_admin) and (course_id not in courses_offered):
            if student_id is None:
                student_id = user_id
            elif student_id != user_id:
                err = "Course %s not found!" % (course_id, )

        if err is None:
            course = JBoxDynConfig.get_course(Compute.get_install_id(),
                                              course_id)
            if problemset_id not in course['problemsets']:
                err = "Problem set %s not found!" % (problemset_id, )
            elif question_ids is None:
                question_ids = course['questions'][problemset_id]

        if err is None:
            report = JBoxCourseHomework.get_report(course_id,
                                                   problemset_id,
                                                   question_ids,
                                                   student_id=student_id)
            code = 0
        else:
            report = err
            code = -1

        response = {'code': code, 'data': report}
        self.write(response)
        return True
Пример #18
0
    def has_update_for_user_home_image():
        home_img_dir, curr_home_img = os.path.split(JBoxVol.USER_HOME_IMG)
        pkg_img_dir, curr_pkg_img = os.path.split(JBoxVol.PKG_IMG)

        # VolMgr.log_debug("checking for updates to user home image %s/%s", img_dir, curr_img)
        bucket, new_pkg_img, new_home_img = JBoxDynConfig.get_user_home_image(Compute.get_install_id())

        if bucket is None:
            VolMgr.log_info("Home: none configured. current: %s/%s", home_img_dir, curr_home_img)
            return False

        if new_home_img == curr_home_img and new_pkg_img == curr_pkg_img:
            VolMgr.log_info("Home: no updates. current: %s/%s", home_img_dir, curr_home_img)
            VolMgr.log_info("Packages: no updates. current: %s/%s", pkg_img_dir, curr_pkg_img)
            return False
        else:
            VolMgr.log_info("Home: update: %s/%s. current: %s/%s", bucket, new_home_img, home_img_dir, curr_home_img)
            VolMgr.log_info("Packages: update: %s/%s. current: %s/%s", bucket, new_pkg_img, home_img_dir, curr_pkg_img)

        return True
Пример #19
0
    def release(self, backup=False):
        sess_props = JBoxSessionProps(Compute.get_install_id(), self.sessname)
        existing_disk = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION,
                                      user_id=sess_props.get_user_id())
        existing_disk.set_state(JBoxDiskState.STATE_DETACHING)
        existing_disk.save()

        disk_id = self.disk_path.split('/')[-1]
        if backup:
            snap_id = self._backup()
        else:
            snap_id = None
        vol_id = EBSVol.get_volume_id_from_device(disk_id)
        EBSVol.detach_volume(vol_id, delete=False)

        if snap_id is not None:
            existing_disk.add_snapshot_id(snap_id)

        existing_disk.set_state(JBoxDiskState.STATE_DETACHED)
        existing_disk.save()
Пример #20
0
    def handle_if_report(self, user_id, is_admin, courses_offered):
        mode = self.get_argument('mode', None)
        if (mode is None) or ((mode != "report") and (mode != "myreport")):
            return False

        self.log_debug("handling report")
        params = self.get_argument('params', None)
        params = json.loads(params)
        course_id = params['course']
        problemset_id = params['problemset']
        question_ids = params['questions'] if 'questions' in params else None

        if mode == "myreport":
            student_id = user_id
        else:
            student_id = params['student'] if 'student' in params else None

        err = None
        if (not is_admin) and (course_id not in courses_offered):
            if student_id is None:
                student_id = user_id
            elif student_id != user_id:
                err = "Course %s not found!" % (course_id,)

        if err is None:
            course = JBoxDynConfig.get_course(Compute.get_install_id(), course_id)
            if problemset_id not in course['problemsets']:
                err = "Problem set %s not found!" % (problemset_id,)
            elif question_ids is None:
                question_ids = course['questions'][problemset_id]

        if err is None:
            report = JBoxCourseHomework.get_report(course_id, problemset_id, question_ids, student_id=student_id)
            code = 0
        else:
            report = err
            code = -1

        response = {'code': code, 'data': report}
        self.write(response)
        return True
Пример #21
0
    def release(self, backup=False):
        sess_props = JBoxSessionProps(Compute.get_install_id(), self.sessname)
        existing_disk = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID,
                                      region_id=CompEC2.REGION,
                                      user_id=sess_props.get_user_id())
        existing_disk.set_state(JBoxDiskState.STATE_DETACHING)
        existing_disk.save()

        disk_id = self.disk_path.split('/')[-1]
        if backup:
            snap_id = self._backup()
        else:
            snap_id = None
        vol_id = EBSVol.get_volume_id_from_device(disk_id)
        EBSVol.detach_volume(vol_id, delete=False)

        if snap_id is not None:
            existing_disk.add_snapshot_id(snap_id)

        existing_disk.set_state(JBoxDiskState.STATE_DETACHED)
        existing_disk.save()
Пример #22
0
    def get(self):
        self.log_debug("APIInfo handler got GET request")
        key = self.get_argument("key", None)
        sign = self.get_argument("sign", None)

        if key is None or sign is None:
            self.send_error()
            return
        sign2 = signstr(key, JBoxCfg.get('sesskey'))

        if sign != sign2:
            self.log_info("signature mismatch. key:%r sign:%r expected:%r",
                          key, sign, sign2)
            self.send_error()
            return

        api_status = JBoxInstanceProps.get_instance_status(
            Compute.get_install_id())
        self.log_info("cluster api status: %r", api_status)

        # filter out instances that should not accept more load
        filtered_api_status = {
            k: v
            for (k, v) in api_status.iteritems() if v['accept']
        }
        preferred_instances = filtered_api_status.keys()

        # flip the dict
        per_api_instances = dict()
        for (inst, status) in filtered_api_status.iteritems():
            api_names = status['api_status'].keys()
            for api_name in api_names:
                v = per_api_instances.get(api_name, [])
                v.append(inst)

        per_api_instances[" preferred "] = preferred_instances
        self.log_info("per api instances: %r", per_api_instances)
        self.write(per_api_instances)
        return
Пример #23
0
    def handle_if_stats(self, is_allowed):
        stats = self.get_argument('stats', None)
        if stats is None:
            return False

        if not is_allowed:
            AdminHandler.log_error("Show stats not allowed for user")
            response = {'code': -1, 'data': 'You do not have permissions to view these stats'}
        else:
            try:
                if stats == 'stat_sessions':
                    stats = self.get_session_stats()
                else:
                    stats = JBoxDynConfig.get_stat(Compute.get_install_id(), stats)
                response = {'code': 0, 'data': stats} if stats is not None else {'code': 1, 'data': {}}
            except:
                AdminHandler.log_error("exception while getting stats")
                AdminHandler._get_logger().exception("exception while getting stats")
                response = {'code': -1, 'data': 'error getting stats'}

        self.write(response)
        return True
Пример #24
0
    def get_disk_for_user(user_email):
        JBoxEBSVol.log_debug("creating EBS volume for %s", user_email)

        disk_id = JBoxEBSVol._reserve_disk_id()
        if disk_id is None:
            raise Exception("No free disk available")

        try:
            existing_disk = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION,
                                          user_id=user_email)
        except Exception as ex:
            JBoxEBSVol.log_debug("No existing disk for %s. Exception %r", user_email, ex)
            existing_disk = None

        if existing_disk is None:
            sess_id = unique_sessname(user_email)
            sess_props = JBoxSessionProps(Compute.get_install_id(), sess_id, create=True, user_id=user_email)
            if sess_props.is_new:
                sess_props.save()
            snap_id = sess_props.get_snapshot_id()
            if snap_id is None:
                snap_id = JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT

            JBoxEBSVol.log_debug("will use snapshot id %s for %s", snap_id, user_email)

            dev_path, vol_id = EBSVol.create_new_volume(snap_id, disk_id, tag=user_email,
                                                        disk_sz_gb=JBoxEBSVol.DISK_LIMIT)
            existing_disk = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION,
                                          user_id=user_email,
                                          volume_id=vol_id,
                                          attach_time=None,
                                          create=True)
        else:
            dev_path = EBSVol.attach_volume(existing_disk.get_volume_id(), disk_id)

        existing_disk.set_state(JBoxDiskState.STATE_ATTACHING)
        existing_disk.save()

        return JBoxEBSVol(dev_path, user_email=user_email)
Пример #25
0
    def is_user_activated(jbuser):
        reg_allowed = JBoxDynConfig.get_allow_registration(Compute.get_install_id())
        if jbuser.is_new:
            if not reg_allowed:
                activation_state = JBoxUserV2.ACTIVATION_REQUESTED
            else:
                activation_state = JBoxUserV2.ACTIVATION_GRANTED
            jbuser.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO, activation_state)
            jbuser.save()
        else:
            activation_code, activation_state = jbuser.get_activation_state()
            if reg_allowed and (activation_state != JBoxUserV2.ACTIVATION_GRANTED):
                activation_state = JBoxUserV2.ACTIVATION_GRANTED
                jbuser.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO, activation_state)
                jbuser.save()
            elif activation_state != JBoxUserV2.ACTIVATION_GRANTED:
                if not ((activation_state == JBoxUserV2.ACTIVATION_REQUESTED) and
                        (activation_code == JBoxUserV2.ACTIVATION_CODE_AUTO)):
                    activation_state = JBoxUserV2.ACTIVATION_REQUESTED
                    jbuser.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO, activation_state)
                    jbuser.save()

        return activation_state == JBoxUserV2.ACTIVATION_GRANTED
Пример #26
0
 def publish_stats():
     VolMgr.calc_stats()
     VolMgr.log_debug("stats: %r", VolMgr.STATS)
     JBoxDynConfig.set_stat(Compute.get_install_id(), VolMgr.STAT_NAME, VolMgr.STATS)
Пример #27
0
 def publish_stats():
     VolMgr.calc_stats()
     VolMgr.log_debug("stats: %r", VolMgr.STATS)
     JBoxDynConfig.set_stat(Compute.get_install_id(), VolMgr.STAT_NAME, VolMgr.STATS)
Пример #28
0
def is_proposed_cluster_leader():
    cluster = Compute.get_install_id()
    leader = JBoxDynConfig.get_cluster_leader(cluster)
    return leader == Compute.get_instance_id()
Пример #29
0
def publish_stats():
    JBoxUserV2.calc_stats()
    JBoxDynConfig.set_stat(Compute.get_install_id(), JBoxUserV2.STAT_NAME,
                           JBoxUserV2.STATS)
Пример #30
0
    def upload_course(user_id, course):
        course_id = course['id']

        if (user_id is not None) and (user_id not in course['admins']):
            course['admins'].append(user_id)

        existing_course = JBoxDynConfig.get_course(Compute.get_install_id(), course_id)
        existing_admins = existing_course['admins'] if existing_course is not None else []
        existing_psets = existing_course['problemsets'] if existing_course is not None else []

        question_list = {}
        if (existing_course is not None) and ('questions' in existing_course):
            question_list = existing_course['questions']

        if (existing_course is not None) and (user_id is not None) and (user_id not in existing_admins):
            return -1

        for pset in course['problemsets']:
            pset_id = pset['id']
            if pset_id not in existing_psets:
                existing_psets.append(pset_id)
            question_ids = [q['id'] for q in pset['questions']]
            question_list[pset_id] = question_ids

        dt = datetime.datetime.now(pytz.utc)
        JBoxDynConfig.set_course(Compute.get_install_id(), course_id, {
            'admins': course['admins'],
            'problemsets': existing_psets,
            'questions': question_list,
            'create_time': JBoxUserV2.datetime_to_yyyymmdd(dt)
        })

        for problemset in course['problemsets']:
            problemset_id = problemset['id']
            questions = problemset['questions']
            for question in questions:
                question_id = question['id']
                answer = question['ans']
                score = question['score'] if 'score' in question else 0
                attempts = question['attempts'] if 'attempts' in question else 0
                # nscore = question['nscore'] if 'nscore' in question else 0
                try:
                    ans = JBoxCourseHomework(course_id, problemset_id, question_id, JBoxCourseHomework.ANSWER_KEY,
                                             answer=answer, state=JBoxCourseHomework.STATE_CORRECT, create=True)
                except:
                    ans = JBoxCourseHomework(course_id, problemset_id, question_id, JBoxCourseHomework.ANSWER_KEY)
                    ans.set_answer(answer, JBoxCourseHomework.STATE_CORRECT)
                ans.set_score(score)
                ans.set_attempts(attempts)
                ans.save()

        for uid in course['admins']:
            user = JBoxUserV2(uid)
            courses_offered = user.get_courses_offered()
            if course['id'] not in courses_offered:
                courses_offered.append(course['id'])
            user.set_courses_offered(courses_offered)
            user.set_role(JBoxUserV2.ROLE_OFFER_COURSES)
            user.save()

        for uid in existing_admins:
            if uid in course['admins']:
                continue
            user = JBoxUserV2(uid)
            courses_offered = user.get_courses_offered()
            if course['id'] in courses_offered:
                courses_offered.remove(course['id'])
            user.set_courses_offered(courses_offered)
            user.set_role(JBoxUserV2.ROLE_OFFER_COURSES)
            user.save()

        return 0
Пример #31
0
def is_proposed_cluster_leader():
    cluster = Compute.get_install_id()
    leader = JBoxDynConfig.get_cluster_leader(cluster)
    return leader == Compute.get_instance_id()
Пример #32
0
def publish_stats():
    JBoxUserV2.calc_stats()
    JBoxDynConfig.set_stat(Compute.get_install_id(), JBoxUserV2.STAT_NAME, JBoxUserV2.STATS)
Пример #33
0
 def _backup(self, clear_volume=False):
     sess_props = JBoxSessionProps(Compute.get_install_id(), self.sessname)
     desc = sess_props.get_user_id() + " JuliaBox Backup"
     disk_id = self.disk_path.split('/')[-1]
     snap_id = EBSVol.snapshot_volume(dev_id=disk_id, tag=self.sessname, description=desc, wait_till_complete=False)
     return snap_id