def collect_stats(): try: VolMgr.publish_stats() db.publish_stats() JBoxDynConfig.set_stat_collected_date(CloudHost.INSTALL_ID) finally: JBoxd.finish_thread()
def monitor_registrations(): max_rate = JBoxDynConfig.get_registration_hourly_rate( CloudHost.INSTALL_ID) rate = JBoxUserV2.count_created(1) reg_allowed = JBoxDynConfig.get_allow_registration( CloudHost.INSTALL_ID) CloudHost.log_debug( "registration allowed: %r, rate: %d, max allowed: %d", reg_allowed, rate, max_rate) if (reg_allowed and (rate > max_rate * 1.1)) or ((not reg_allowed) and (rate < max_rate * 0.9)): reg_allowed = not reg_allowed CloudHost.log_info("Changing registration allowed to %r", reg_allowed) JBoxDynConfig.set_allow_registration(CloudHost.INSTALL_ID, reg_allowed) if reg_allowed: num_pending_activations = JBoxUserV2.count_pending_activations() if num_pending_activations > 0: CloudHost.log_info( "scheduling activations for %d pending activations", num_pending_activations) JBoxContainer.async_schedule_activations()
def monitor_registrations(): max_rate = JBoxDynConfig.get_registration_hourly_rate(Compute.get_install_id()) rate = JBoxUserV2.count_created(1) reg_allowed = JBoxDynConfig.get_allow_registration(Compute.get_install_id()) JBox.log_debug("registration allowed: %r, rate: %d, max allowed: %d", reg_allowed, rate, max_rate) if (reg_allowed and (rate > max_rate*1.1)) or ((not reg_allowed) and (rate < max_rate*0.9)): reg_allowed = not reg_allowed JBox.log_warn("Changing registration allowed to %r", reg_allowed) JBoxDynConfig.set_allow_registration(Compute.get_install_id(), reg_allowed) if reg_allowed: num_pending_activations = JBoxUserV2.count_pending_activations() if num_pending_activations > 0: JBox.log_info("scheduling activations for %d pending activations", num_pending_activations) JBoxAsyncJob.async_schedule_activations()
def monitor_registrations(): max_rate = JBoxDynConfig.get_registration_hourly_rate(CloudHost.INSTALL_ID) rate = JBoxUserV2.count_created(1) reg_allowed = JBoxDynConfig.get_allow_registration(CloudHost.INSTALL_ID) CloudHost.log_debug("registration allowed: %r, rate: %d, max allowed: %d", reg_allowed, rate, max_rate) if (reg_allowed and (rate > max_rate*1.1)) or ((not reg_allowed) and (rate < max_rate*0.9)): reg_allowed = not reg_allowed CloudHost.log_warn("Changing registration allowed to %r", reg_allowed) JBoxDynConfig.set_allow_registration(CloudHost.INSTALL_ID, reg_allowed) if reg_allowed: num_pending_activations = JBoxUserV2.count_pending_activations() if num_pending_activations > 0: CloudHost.log_info("scheduling activations for %d pending activations", num_pending_activations) JBoxContainer.async_schedule_activations()
def do_housekeeping(): terminating = False server_delete_timeout = JBoxCfg.get('interactive.expire') inactive_timeout = JBoxCfg.get('interactive.inactivity_timeout') SessContainer.maintain(max_timeout=server_delete_timeout, inactive_timeout=inactive_timeout) is_leader = is_cluster_leader() if is_leader: terminating = False else: try: terminating = JBoxAsyncJob.sync_is_terminating() if terminating['code'] == 0: terminating = terminating['data'] else: JBox.log_error("Error checking if instance is terminating. Assuming False.") terminating = False except: JBox.log_error("Exception checking if instance is terminating. Assuming False.") terminating = False if is_leader: JBox.log_info("I am the cluster leader") JBox.update_juliabox_status() JBox.monitor_registrations() if not JBoxDynConfig.is_stat_collected_within(Compute.get_install_id(), 1): JBoxAsyncJob.async_collect_stats() if terminating: JBox.log_warn("terminating to scale down") else: JBox.do_update_user_home_image() JBoxAsyncJob.async_plugin_maintenance(is_leader)
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(CloudHost.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 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(CloudHost.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 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: stats = JBoxDynConfig.get_stat(CloudHost.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") response = {'code': -1, 'data': 'error getting stats'} self.write(response) return True
def has_update_for_user_home_image(): img_dir, curr_img = os.path.split(JBoxVol.USER_HOME_IMG) VolMgr.log_debug("checking for updates to user home image %s/%s", img_dir, curr_img) bucket, new_img = JBoxDynConfig.get_user_home_image(CloudHost.INSTALL_ID) if bucket is None: VolMgr.log_debug("no images configured") return False VolMgr.log_debug("latest user home image %s/%s", bucket, new_img) if new_img == curr_img: VolMgr.log_debug("already on latest image") return False return True
def has_update_for_user_home_image(): img_dir, curr_img = os.path.split(JBoxVol.USER_HOME_IMG) #VolMgr.log_debug("checking for updates to user home image %s/%s", img_dir, curr_img) bucket, new_img = JBoxDynConfig.get_user_home_image(CloudHost.INSTALL_ID) if bucket is None: VolMgr.log_info("User home image: none configured. current: %s/%s", img_dir, curr_img) return False if new_img == curr_img: VolMgr.log_info("User home image: no updates. current: %s/%s", img_dir, curr_img) return False else: VolMgr.log_info("User home image: update: %s/%s. current: %s/%s", bucket, new_img, img_dir, curr_img) return True
def monitor_registrations(): max_rate = JBoxDynConfig.get_registration_hourly_rate( Compute.get_install_id()) rate = JBoxUserV2.count_created(1) reg_allowed = JBoxDynConfig.get_allow_registration( Compute.get_install_id()) JBox.log_debug("registration allowed: %r, rate: %d, max allowed: %d", reg_allowed, rate, max_rate) if (reg_allowed and (rate > max_rate * 1.1)) or ((not reg_allowed) and (rate < max_rate * 0.9)): reg_allowed = not reg_allowed JBox.log_warn("Changing registration allowed to %r", reg_allowed) JBoxDynConfig.set_allow_registration(Compute.get_install_id(), reg_allowed) if reg_allowed: num_pending_activations = JBoxUserV2.count_pending_activations() if num_pending_activations > 0: JBox.log_info( "scheduling activations for %d pending activations", num_pending_activations) JBoxAsyncJob.async_schedule_activations()
def update_user_home_image(fetch=True): img_dir, curr_img = os.path.split(JBoxVol.USER_HOME_IMG) bucket, new_img = JBoxDynConfig.get_user_home_image(CloudHost.INSTALL_ID) new_img_path = os.path.join(img_dir, new_img) if fetch and (not os.path.exists(new_img_path)): VolMgr.log_debug("fetching new image to %s", new_img_path) k = CloudHost.pull_file_from_s3(bucket, new_img_path) if k is not None: VolMgr.log_debug("fetched new user home image") if os.path.exists(new_img_path): VolMgr.log_debug("set new image to %s", new_img_path) JBoxVol.USER_HOME_IMG = new_img_path return True return False
def has_update_for_user_home_image(): img_dir, curr_img = os.path.split(JBoxVol.USER_HOME_IMG) #VolMgr.log_debug("checking for updates to user home image %s/%s", img_dir, curr_img) bucket, new_img = JBoxDynConfig.get_user_home_image( CloudHost.INSTALL_ID) if bucket is None: VolMgr.log_info("User home image: none configured. current: %s/%s", img_dir, curr_img) return False if new_img == curr_img: VolMgr.log_info("User home image: no updates. current: %s/%s", img_dir, curr_img) return False else: VolMgr.log_info("User home image: update: %s/%s. current: %s/%s", bucket, new_img, img_dir, curr_img) return True
def update_user_home_image(fetch=True): img_dir, curr_img = os.path.split(JBoxVol.USER_HOME_IMG) bucket, new_img = JBoxDynConfig.get_user_home_image( CloudHost.INSTALL_ID) new_img_path = os.path.join(img_dir, new_img) if fetch and (not os.path.exists(new_img_path)): VolMgr.log_debug("fetching new image to %s", new_img_path) k = CloudHost.pull_file_from_s3(bucket, new_img_path) if k is not None: VolMgr.log_debug("fetched new user home image") if os.path.exists(new_img_path): VolMgr.log_debug("set new image to %s", new_img_path) JBoxVol.USER_HOME_IMG = new_img_path return True return False
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: stats = JBoxDynConfig.get_stat(CloudHost.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") response = {'code': -1, 'data': 'error getting stats'} self.write(response) 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(CloudHost.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 do_housekeeping(): JBox.do_update_user_home_image() server_delete_timeout = JBox.cfg['expire'] JBoxContainer.maintain(max_timeout=server_delete_timeout, inactive_timeout=JBox.cfg['inactivity_timeout'], protected_names=JBox.cfg['protected_docknames']) if JBox.cfg['cloud_host']['scale_down'] and (JBoxContainer.num_active() == 0) and \ (JBoxContainer.num_stopped() == 0) and CloudHost.should_terminate(): JBox.log_info("terminating to scale down") try: CloudHost.deregister_instance_dns() except: CloudHost.log_error("Error deregistering instance dns") CloudHost.terminate_instance() elif is_cluster_leader(): CloudHost.log_info("I am the cluster leader") JBox.monitor_registrations() if not JBoxDynConfig.is_stat_collected_within(CloudHost.INSTALL_ID, 7): JBoxContainer.async_collect_stats()
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(CloudHost.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 do_housekeeping(): terminating = False server_delete_timeout = JBox.cfg['expire'] JBoxContainer.maintain(max_timeout=server_delete_timeout, inactive_timeout=JBox.cfg['inactivity_timeout'], protected_names=JBox.cfg['protected_docknames']) if is_cluster_leader(): CloudHost.log_info("I am the cluster leader") JBox.monitor_registrations() if not JBoxDynConfig.is_stat_collected_within(CloudHost.INSTALL_ID, 1): JBoxContainer.async_collect_stats() JBoxContainer.async_update_disk_state() elif JBox.is_ready_to_terminate(): terminating = True JBox.log_warn("terminating to scale down") try: CloudHost.deregister_instance_dns() except: CloudHost.log_error("Error deregistering instance dns") CloudHost.terminate_instance() if not terminating: JBox.do_update_user_home_image()
def do_housekeeping(): terminating = False server_delete_timeout = JBoxCfg.get('interactive.expire') inactive_timeout = JBoxCfg.get('interactive.inactivity_timeout') SessContainer.maintain(max_timeout=server_delete_timeout, inactive_timeout=inactive_timeout) is_leader = is_cluster_leader() if is_leader: terminating = False else: try: terminating = JBoxAsyncJob.sync_is_terminating() if terminating['code'] == 0: terminating = terminating['data'] else: JBox.log_error( "Error checking if instance is terminating. Assuming False." ) terminating = False except: JBox.log_error( "Exception checking if instance is terminating. Assuming False." ) terminating = False if is_leader: JBox.log_info("I am the cluster leader") JBox.update_juliabox_status() JBox.monitor_registrations() if not JBoxDynConfig.is_stat_collected_within( Compute.get_install_id(), 1): JBoxAsyncJob.async_collect_stats() if terminating: JBox.log_warn("terminating to scale down") else: JBox.do_update_user_home_image() JBoxAsyncJob.async_plugin_maintenance(is_leader)
def do_housekeeping(): terminating = False server_delete_timeout = JBox.cfg['expire'] JBoxContainer.maintain(max_timeout=server_delete_timeout, inactive_timeout=JBox.cfg['inactivity_timeout'], protected_names=JBox.cfg['protected_docknames']) if is_cluster_leader(): CloudHost.log_info("I am the cluster leader") JBox.monitor_registrations() if not JBoxDynConfig.is_stat_collected_within( CloudHost.INSTALL_ID, 7): JBoxContainer.async_collect_stats() elif JBox.is_ready_to_terminate(): terminating = True JBox.log_info("terminating to scale down") try: CloudHost.deregister_instance_dns() except: CloudHost.log_error("Error deregistering instance dns") CloudHost.terminate_instance() if not terminating: JBox.do_update_user_home_image()
def is_user_activated(jbuser): reg_allowed = JBoxDynConfig.get_allow_registration(CloudHost.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 test(): yday = datetime.datetime.now() - datetime.timedelta(hours=24) stats = JBoxAccountingV2.get_stats(dates=(yday,)) TestDBTables.log_debug("stats for yesterday: %s", repr(stats)) stats = JBoxAccountingV2.get_stats() TestDBTables.log_debug("stats for today: %s", repr(stats)) sprops = JBoxSessionProps(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) resultset = JBoxInvite.table().scan() result_arr = [obj for obj in resultset] TestDBTables.log_debug("got array: %r", result_arr) count_created = JBoxUserV2.count_created(48) TestDBTables.log_debug("accounts created in last 1 hour: %d", count_created)
has_autoscale=cloud_cfg['autoscale'], has_route53=cloud_cfg['route53'], has_ebs=cloud_cfg['ebs'], has_ses=cloud_cfg['ses'], scale_up_at_load=cloud_cfg['scale_up_at_load'], scale_up_policy=cloud_cfg['scale_up_policy'], autoscale_group=cloud_cfg['autoscale_group'], route53_domain=cloud_cfg['route53_domain'], region=cloud_cfg['region'], install_id=cloud_cfg['install_id']) VolMgr.configure(dckr, cfg) ts = JBoxVol._get_user_home_timestamp() VolMgr.log_debug("user_home_timestamp: %s", ts.strftime("%Y%m%d_%H%M")) img_dir, img_file = os.path.split(JBoxVol.USER_HOME_IMG) new_img_file_name = 'user_home_' + ts.strftime("%Y%m%d_%H%M") + '.tar.gz' new_img_file = os.path.join(img_dir, new_img_file_name) shutil.copyfile(JBoxVol.USER_HOME_IMG, new_img_file) VolMgr.log_debug("new image file is at : %s", new_img_file) bucket = 'juliabox-user-home-templates' VolMgr.log_debug("pushing new image file to s3 at: %s", bucket) CloudHost.push_file_to_s3(bucket, new_img_file) for cluster in ['JuliaBoxTest', 'JuliaBox']: VolMgr.log_debug("setting image for cluster: %s", cluster) JBoxDynConfig.set_user_home_image(cluster, bucket, new_img_file_name)
def publish_container_stats(): VolMgr.publish_stats() db.publish_stats() JBoxDynConfig.set_stat_collected_date(Compute.get_install_id())
def publish_stats(): VolMgr.calc_stats() VolMgr.log_debug("stats: %r", VolMgr.STATS) JBoxDynConfig.set_stat(CloudHost.INSTALL_ID, VolMgr.STAT_NAME, VolMgr.STATS)
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(CloudHost.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(CloudHost.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 #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.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
def collect_stats(): VolMgr.publish_stats() db.publish_stats() JBoxDynConfig.set_stat_collected_date(CloudHost.INSTALL_ID)
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(CloudHost.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( CloudHost.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
def test(): yday = datetime.datetime.now() - datetime.timedelta(hours=24) stats = JBoxAccountingV2.get_stats(dates=(yday, )) TestDBTables.log_debug("stats for yesterday: %s", repr(stats)) stats = JBoxAccountingV2.get_stats() TestDBTables.log_debug("stats for today: %s", repr(stats)) sprops = JBoxSessionProps(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) resultset = JBoxInvite.table().scan() result_arr = [obj for obj in resultset] TestDBTables.log_debug("got array: %r", result_arr) count_created = JBoxUserV2.count_created(48) TestDBTables.log_debug("accounts created in last 1 hour: %d", count_created)