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
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
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
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
def configure_dynamic(): nowtime = datetime.datetime.now(pytz.utc) if UserCluster.CONFIGURE_TIME is None: refetch = True else: tdiff = (nowtime - UserCluster.CONFIGURE_TIME).total_seconds() refetch = tdiff > UserCluster.RECONF_SECS if refetch: cfg = JBoxDynConfig.get_user_cluster_config(CompEC2.INSTALL_ID) UserCluster.IMAGE_ID = cfg['image_id'] UserCluster.INSTANCE_TYPE = cfg['instance_type'] UserCluster.INSTANCE_CORES = cfg['instance_cores'] UserCluster.INSTANCE_COST = cfg['instance_cost'] UserCluster.KEY_NAME = cfg['key_name'] UserCluster.SEC_GRPS = cfg['sec_grps'] UserCluster.CONFIGURE_TIME = nowtime
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
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
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
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
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
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)
def test(): sprops = JBoxSessionProps(TESTCLSTR, unique_sessname('*****@*****.**')) TestDBTables.log_debug("JBoxSessionProps. user_id: %s, snapshot_id: %s, message: %s", sprops.get_user_id(), sprops.get_snapshot_id(), sprops.get_message()) JBoxDynConfig.set_cluster_leader(TESTCLSTR, 'testinstance') assert JBoxDynConfig.get_cluster_leader(TESTCLSTR) == 'testinstance' JBoxDynConfig.unset_cluster_leader(TESTCLSTR) assert JBoxDynConfig.get_cluster_leader(TESTCLSTR) is None assert JBoxDynConfig.get_allow_registration(TESTCLSTR) JBoxDynConfig.set_allow_registration(TESTCLSTR, False) assert not JBoxDynConfig.get_allow_registration(TESTCLSTR) JBoxDynConfig.set_allow_registration(TESTCLSTR, True) assert JBoxDynConfig.get_allow_registration(TESTCLSTR) assert JBoxDynConfig.get_registration_hourly_rate(TESTCLSTR) == 60 JBoxDynConfig.set_registration_hourly_rate(TESTCLSTR, 20) assert JBoxDynConfig.get_registration_hourly_rate(TESTCLSTR) == 20 JBoxDynConfig.set_registration_hourly_rate(TESTCLSTR, 60) assert JBoxDynConfig.get_registration_hourly_rate(TESTCLSTR) == 60 assert JBoxDynConfig.get_message(TESTCLSTR) is None JBoxDynConfig.set_message(TESTCLSTR, "hello world", datetime.timedelta(minutes=1)) assert JBoxDynConfig.get_message(TESTCLSTR) == "hello world" JBoxDynConfig.set_user_home_image(TESTCLSTR, "juliabox-user-home-templates", "user_home_28Nov2014.tar.gz") assert JBoxDynConfig.get_user_home_image(TESTCLSTR) == ("juliabox-user-home-templates", "user_home_28Nov2014.tar.gz") num_pending_activations = JBoxUserV2.count_pending_activations() TestDBTables.log_debug("pending activations: %d", num_pending_activations) count_created = JBoxUserV2.count_created(48) TestDBTables.log_debug("accounts created in last 1 hour: %d", count_created)
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
LoggerMixin.configure() db.configure() SessContainer.configure() VolMgr.configure() plugin = JBPluginCloud.jbox_get_plugin(JBPluginCloud.JBP_BUCKETSTORE) if plugin is None: VolMgr.log_error("No plugin found for bucketstore") exit(1) ts = JBoxVol._get_user_home_timestamp() tsstr = ts.strftime("%Y%m%d_%H%M") VolMgr.log_debug("user_home_timestamp: %s", tsstr) imgf, pkgf = copy_for_upload(tsstr) copy_for_boot() bucket = 'juliabox-user-home-templates' VolMgr.log_debug("pushing new image files to bucketstore at: %s", bucket) plugin.push(bucket, imgf) plugin.push(bucket, pkgf) # JuliaBoxTest JuliaBox clusters = sys.argv[1] if (len(sys.argv) > 1) else ['JuliaBoxTest'] for cluster in clusters: VolMgr.log_debug("setting image for cluster: %s", cluster) JBoxDynConfig.set_user_home_image(cluster, bucket, pkgf, imgf)