def get_allow_registration(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, "allow_registration")) except JBoxDBItemNotFound: return True return record.get_value() == "True"
def set_course(cluster, course_id, course_details): val = json.dumps(course_details) course_key = "|".join(["course", course_id]) record = JBoxDynConfig(JBoxDB.qual(cluster, course_key), create=True, value=val) if not record.is_new: record.set_value(val) record.save()
def set_user_home_image(cluster, bucket, pkg_file, home_file): img = {"bucket": bucket, "pkg_file": pkg_file, "home_file": home_file} img = json.dumps(img) record = JBoxDynConfig(JBoxDB.qual(cluster, "user_home_image"), create=True, value=img) if not record.is_new: record.set_value(img) record.save()
def set_course(cluster, course_id, course_details): val = json.dumps(course_details) course_key = '|'.join(['course', course_id]) record = JBoxDynConfig(JBoxDB.qual(cluster, course_key), create=True, value=val) if not record.is_new: record.set_value(val) record.save()
def get_allow_registration(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, 'allow_registration')) except JBoxDBItemNotFound: return True return record.get_value() == 'True'
def get_course(cluster, course_id): try: course_key = "|".join(["course", course_id]) record = JBoxDynConfig(JBoxDB.qual(cluster, course_key)) except JBoxDBItemNotFound: return None return json.loads(record.get_value())
def get_course(cluster, course_id): try: course_key = '|'.join(['course', course_id]) record = JBoxDynConfig(JBoxDB.qual(cluster, course_key)) except JBoxDBItemNotFound: return None return json.loads(record.get_value())
def get_user_home_image(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, 'user_home_image')) except JBoxDBItemNotFound: return None, None, None img = json.loads(record.get_value()) pkg_file = img['pkg_file'] if 'pkg_file' in img else None home_file = img['home_file'] if 'home_file' in img else None return img['bucket'], pkg_file, home_file
def get_user_home_image(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, "user_home_image")) except JBoxDBItemNotFound: return None, None, None img = json.loads(record.get_value()) pkg_file = img["pkg_file"] if "pkg_file" in img else None home_file = img["home_file"] if "home_file" in img else None return img["bucket"], pkg_file, home_file
def get_stale_instances(cluster): now = datetime.datetime.now(pytz.utc) nowsecs = JBoxInstanceProps.datetime_to_epoch_secs(now) valid_time = nowsecs - JBoxInstanceProps.SESS_UPDATE_INTERVAL stale = [] for record in JBoxInstanceProps.scan(instance_id__beginswith=JBoxDB.qual(cluster, ''), publish_time__lt=valid_time): stale.append(record.get('instance_id').split('.', 1)[1]) return stale
def set_message(cluster, message, valid_delta): tnow = datetime.datetime.now(pytz.utc) tvalid = tnow + valid_delta msg = {"msg": message, "valid_till": isodate.datetime_isoformat(tvalid)} msg = json.dumps(msg) record = JBoxDynConfig(JBoxDB.qual(cluster, "message"), create=True, value=msg) if not record.is_new: record.set_value(msg) record.save()
def get_stale_instances(cluster): now = datetime.datetime.now(pytz.utc) nowsecs = JBoxInstanceProps.datetime_to_epoch_secs(now) valid_time = nowsecs - JBoxInstanceProps.SESS_UPDATE_INTERVAL stale = [] for record in JBoxInstanceProps.scan( instance_id__beginswith=JBoxDB.qual(cluster, ''), publish_time__lt=valid_time): stale.append(record.get('instance_id').split('.', 1)[1]) return stale
def get_available_instances(cluster): now = datetime.datetime.now(pytz.utc) nowsecs = JBoxInstanceProps.datetime_to_epoch_secs(now) valid_time = nowsecs - JBoxInstanceProps.SESS_UPDATE_INTERVAL result = list() for record in JBoxInstanceProps.scan(instance_id__beginswith=JBoxDB.qual(cluster, ''), publish_time__gte=valid_time, accept__eq=1): result.append(record.get('instance_id').split('.', 1)[1]) return result
def get_available_instances(cluster): now = datetime.datetime.now(pytz.utc) nowsecs = JBoxInstanceProps.datetime_to_epoch_secs(now) valid_time = nowsecs - JBoxInstanceProps.SESS_UPDATE_INTERVAL result = list() for record in JBoxInstanceProps.scan( instance_id__beginswith=JBoxDB.qual(cluster, ''), publish_time__gte=valid_time, accept__eq=1): result.append(record.get('instance_id').split('.', 1)[1]) return result
def set_user_home_image(cluster, bucket, pkg_file, home_file): img = { 'bucket': bucket, 'pkg_file': pkg_file, 'home_file': home_file } img = json.dumps(img) record = JBoxDynConfig(JBoxDB.qual(cluster, 'user_home_image'), create=True, value=img) if not record.is_new: record.set_value(img) record.save()
def __init__(self, cluster, instance_id, create=False): qinstance_id = JBoxDB.qual(cluster, instance_id) try: self.item = self.fetch(instance_id=qinstance_id) self.is_new = False except JBoxDBItemNotFound: if create: data = {'instance_id': qinstance_id} self.create(data) self.item = self.fetch(instance_id=qinstance_id) self.is_new = True else: raise
def set_message(cluster, message, valid_delta): tnow = datetime.datetime.now(pytz.utc) tvalid = tnow + valid_delta msg = { 'msg': message, 'valid_till': isodate.datetime_isoformat(tvalid) } msg = json.dumps(msg) record = JBoxDynConfig(JBoxDB.qual(cluster, 'message'), create=True, value=msg) if not record.is_new: record.set_value(msg) record.save()
def get_instance_status(cluster): now = datetime.datetime.now(pytz.utc) nowsecs = JBoxInstanceProps.datetime_to_epoch_secs(now) valid_time = nowsecs - JBoxInstanceProps.SESS_UPDATE_INTERVAL result = dict() for record in JBoxInstanceProps.scan(instance_id__beginswith=JBoxDB.qual(cluster, ''), publish_time__gte=valid_time): iid = record.get('instance_id').split('.', 1)[1] props = { 'load': float(record.get('load', '0.0')), 'accept': bool(record.get('accept', 0)), 'api_status': json.loads(record.get('api_status', '{}')) } result[iid] = props return result
def __init__(self, cluster, instance_id, create=False): qinstance_id = JBoxDB.qual(cluster, instance_id) try: self.item = self.fetch(instance_id=qinstance_id) self.is_new = False except JBoxDBItemNotFound: if create: data = { 'instance_id': qinstance_id } self.create(data) self.item = self.fetch(instance_id=qinstance_id) self.is_new = True else: raise
def get_instance_status(cluster): now = datetime.datetime.now(pytz.utc) nowsecs = JBoxInstanceProps.datetime_to_epoch_secs(now) valid_time = nowsecs - JBoxInstanceProps.SESS_UPDATE_INTERVAL result = dict() for record in JBoxInstanceProps.scan( instance_id__beginswith=JBoxDB.qual(cluster, ''), publish_time__gte=valid_time): iid = record.get('instance_id').split('.', 1)[1] props = { 'load': float(record.get('load', '0.0')), 'accept': bool(record.get('accept', 0)), 'api_status': json.loads(record.get('api_status', '{}')) } result[iid] = props return result
def __init__(self, cluster, session_id, create=False, user_id=None): if session_id.startswith("/"): session_id = session_id[1:] qsession_id = JBoxDB.qual(cluster, session_id) try: self.item = self.fetch(session_id=qsession_id) self.is_new = False except JBoxDBItemNotFound: if create: data = {"session_id": qsession_id} if user_id is not None: data["user_id"] = user_id self.create(data) self.item = self.fetch(session_id=qsession_id) self.is_new = True else: raise
def __init__(self, cluster, session_id, create=False, user_id=None): if session_id.startswith("/"): session_id = session_id[1:] qsession_id = JBoxDB.qual(cluster, session_id) try: self.item = self.fetch(session_id=qsession_id) self.is_new = False except JBoxDBItemNotFound: if create: data = { 'session_id': qsession_id } if user_id is not None: data['user_id'] = user_id self.create(data) self.item = self.fetch(session_id=qsession_id) self.is_new = True else: raise
def get_message(cluster, del_expired=True): try: record = JBoxDynConfig(JBoxDB.qual(cluster, 'message')) except JBoxDBItemNotFound: return None msg = record.get_value() if msg is None: return None msg = json.loads(msg) tnow = datetime.datetime.now(pytz.utc) tvalid = parse_iso_time(msg['valid_till']) # JBoxDynConfig.log_debug("tnow: %s, tvalid: %s", str(tnow), str(tvalid)) if tvalid >= tnow: return msg['msg'] if del_expired: record.delete() return None
def get_message(cluster, del_expired=True): try: record = JBoxDynConfig(JBoxDB.qual(cluster, "message")) except JBoxDBItemNotFound: return None msg = record.get_value() if msg is None: return None msg = json.loads(msg) tnow = datetime.datetime.now(pytz.utc) tvalid = parse_iso_time(msg["valid_till"]) # JBoxDynConfig.log_debug("tnow: %s, tvalid: %s", str(tnow), str(tvalid)) if tvalid >= tnow: return msg["msg"] if del_expired: record.delete() return None
def set_stat_collected_date(cluster): dt = datetime.datetime.now(pytz.utc).isoformat() record = JBoxDynConfig(JBoxDB.qual(cluster, "stat_date"), create=True, value=dt) if not record.is_new: record.set_value(dt) record.save()
def get_stat_collected_date(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, "stat_date")) except JBoxDBItemNotFound: return None return parse_iso_time(record.get_value())
def get_stat(cluster, stat_name): try: record = JBoxDynConfig(JBoxDB.qual(cluster, stat_name)) except JBoxDBItemNotFound: return None return json.loads(record.get_value())
def get_registration_hourly_rate(cluster): try: return int(JBoxDynConfig(JBoxDB.qual(cluster, "registrations_hourly_rate")).get_value()) except JBoxDBItemNotFound: return JBoxDynConfig.DEFAULT_REGISTRATION_RATE
def set_registration_hourly_rate(cluster, rate): record = JBoxDynConfig(JBoxDB.qual(cluster, "registrations_hourly_rate"), create=True, value=str(rate)) if not record.is_new: record.set_value(str(rate)) record.save()
def unset_cluster_leader(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, 'leader')) record.delete() except JBoxDBItemNotFound: return
def get_cluster_leader(cluster): try: return JBoxDynConfig(JBoxDB.qual(cluster, 'leader')).get_value() except JBoxDBItemNotFound: return None
def get_user_cluster_config(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, "user_cluster")) except JBoxDBItemNotFound: return None return json.loads(record.get_value())
def get_user_cluster_config(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, 'user_cluster')) except JBoxDBItemNotFound: return None return json.loads(record.get_value())
def set_registration_hourly_rate(cluster, rate): record = JBoxDynConfig(JBoxDB.qual(cluster, 'registrations_hourly_rate'), create=True, value=str(rate)) if not record.is_new: record.set_value(str(rate)) record.save()
def set_user_cluster_config(cluster, cfg): val = json.dumps(cfg) record = JBoxDynConfig(JBoxDB.qual(cluster, "user_cluster"), create=True, value=val) if not record.is_new: record.set_value(val) record.save()
def unset_cluster_leader(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, "leader")) record.delete() except JBoxDBItemNotFound: return
def set_cluster_leader(cluster, instance): record = JBoxDynConfig(JBoxDB.qual(cluster, "leader"), create=True, value=instance) if not record.is_new: record.set_value(instance) record.save()
def get_cluster_leader(cluster): try: return JBoxDynConfig(JBoxDB.qual(cluster, "leader")).get_value() except JBoxDBItemNotFound: return None
def set_user_cluster_config(cluster, cfg): val = json.dumps(cfg) record = JBoxDynConfig(JBoxDB.qual(cluster, 'user_cluster'), create=True, value=val) if not record.is_new: record.set_value(val) record.save()
def set_cluster_leader(cluster, instance): record = JBoxDynConfig(JBoxDB.qual(cluster, 'leader'), create=True, value=instance) if not record.is_new: record.set_value(instance) record.save()
def set_stat(cluster, stat_name, stat): val = json.dumps(stat) record = JBoxDynConfig(JBoxDB.qual(cluster, stat_name), create=True, value=val) if not record.is_new: record.set_value(val) record.save()
def set_allow_registration(cluster, allow): record = JBoxDynConfig(JBoxDB.qual(cluster, 'allow_registration'), create=True, value=str(allow)) if not record.is_new: record.set_value(str(allow)) record.save()
def set_stat_collected_date(cluster): dt = datetime.datetime.now(pytz.utc).isoformat() record = JBoxDynConfig(JBoxDB.qual(cluster, 'stat_date'), create=True, value=dt) if not record.is_new: record.set_value(dt) record.save()
def get_registration_hourly_rate(cluster): try: return int(JBoxDynConfig(JBoxDB.qual(cluster, 'registrations_hourly_rate')).get_value()) except JBoxDBItemNotFound: return JBoxDynConfig.DEFAULT_REGISTRATION_RATE
def get_stat_collected_date(cluster): try: record = JBoxDynConfig(JBoxDB.qual(cluster, 'stat_date')) except JBoxDBItemNotFound: return None return parse_iso_time(record.get_value())
def set_allow_registration(cluster, allow): record = JBoxDynConfig(JBoxDB.qual(cluster, "allow_registration"), create=True, value=str(allow)) if not record.is_new: record.set_value(str(allow)) record.save()