예제 #1
0
    def auto_activate():
        try:
            num_mails_24h, rate = CloudHost.get_email_rates()
            rate_per_second = min(JBoxd.MAX_ACTIVATIONS_PER_SEC, rate)
            num_mails = min(JBoxd.MAX_AUTO_ACTIVATIONS_PER_RUN, num_mails_24h)

            JBoxd.log_info(
                "Will activate max %d users at %d per second. AWS limits: %d mails at %d per second",
                num_mails, rate_per_second, num_mails_24h, rate)
            user_ids = JBoxUserV2.get_pending_activations(num_mails)
            JBoxd.log_info("Got %d user_ids to be activated", len(user_ids))

            for user_id in user_ids:
                JBoxd.log_info("Activating %s", user_id)

                # send email by SES
                CloudHost.send_email(user_id, JBoxd.ACTIVATION_SENDER,
                                     JBoxd.ACTIVATION_SUBJECT,
                                     JBoxd.ACTIVATION_BODY)

                # set user as activated
                user = JBoxUserV2(user_id)
                user.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO,
                                          JBoxUserV2.ACTIVATION_GRANTED)
                user.save()

                rate_per_second -= 1
                if rate_per_second <= 0:
                    time.sleep(1)
                    rate_per_second = min(JBoxd.MAX_ACTIVATIONS_PER_SEC, rate)
        finally:
            JBoxd.finish_thread()
예제 #2
0
    def auto_activate():
        plugin = JBPluginCloud.jbox_get_plugin(JBPluginCloud.JBP_SENDMAIL)
        if plugin is None:
            JBoxd.log_error("No plugin found for sending mails. Can not auto activate users.")
            return

        num_mails_24h, rate = plugin.get_email_rates()
        rate_per_second = min(JBoxd.MAX_ACTIVATIONS_PER_SEC, rate)
        num_mails = min(JBoxd.MAX_AUTO_ACTIVATIONS_PER_RUN, num_mails_24h)

        JBoxd.log_info("Will activate max %d users at %d per second. AWS limits: %d mails at %d per second",
                       num_mails, rate_per_second,
                       num_mails_24h, rate)
        user_ids = JBoxUserV2.get_pending_activations(num_mails)
        JBoxd.log_info("Got %d user_ids to be activated", len(user_ids))

        for user_id in user_ids:
            JBoxd.log_info("Activating %s", user_id)

            # send email by SES
            plugin.send_email(user_id, JBoxd.ACTIVATION_SENDER, JBoxd.ACTIVATION_SUBJECT, JBoxd.ACTIVATION_BODY)

            # set user as activated
            user = JBoxUserV2(user_id)
            user.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO, JBoxUserV2.ACTIVATION_GRANTED)
            user.save()

            rate_per_second -= 1
            if rate_per_second <= 0:
                rate_per_second = min(JBoxd.MAX_ACTIVATIONS_PER_SEC, rate)
                time.sleep(1)
예제 #3
0
파일: jbox.py 프로젝트: arshak/JuliaBox
    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()
예제 #4
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)
예제 #5
0
    def get_disk_for_user(email):
        VolMgr.log_debug("restoring disk for %s", email)
        user = JBoxUserV2(email)

        ebs = False
        if VolMgr.HAS_EBS:
            ebs = user.has_resource_profile(JBoxUserV2.RES_PROF_DISK_EBS_1G)

        custom_jimg = None
        ipython_profile = 'julia'
        # TODO: image path should be picked up from config
        if user.has_resource_profile(JBoxUserV2.RES_PROF_JULIA_PKG_PRECOMP):
            custom_jimg = '/home/juser/.juliabox/jimg/sys.ji'
            ipython_profile = 'jboxjulia'

        if ebs:
            disk = JBoxEBSVol.get_disk_for_user(email)
        else:
            disk = JBoxLoopbackVol.get_disk_for_user(email)
        try:
            disk.setup_julia_image(ipython_profile, custom_jimg)
            disk.setup_tutorial_link()
            disk.gen_ssh_key()
            disk.gen_gitconfig()
        except IOError, ioe:
            if ioe.errno == errno.ENOSPC:
                # continue login on ENOSPC to allow user to delete files
                JBoxVol.log_exception(
                    "No space left to configure JuliaBox for %s", email)
            else:
                raise
예제 #6
0
    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()
예제 #7
0
    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()
예제 #8
0
파일: jboxd.py 프로젝트: arshak/JuliaBox
    def auto_activate():
        try:
            num_mails_24h, rate = CloudHost.get_email_rates()
            rate_per_second = min(JBoxd.MAX_ACTIVATIONS_PER_SEC, rate)
            num_mails = min(JBoxd.MAX_AUTO_ACTIVATIONS_PER_RUN, num_mails_24h)

            JBoxd.log_info("Will activate max %d users at %d per second. AWS limits: %d mails at %d per second",
                           num_mails, rate_per_second,
                           num_mails_24h, rate)
            user_ids = JBoxUserV2.get_pending_activations(num_mails)
            JBoxd.log_info("Got %d user_ids to be activated", len(user_ids))

            for user_id in user_ids:
                JBoxd.log_info("Activating %s", user_id)

                # send email by SES
                CloudHost.send_email(user_id, JBoxd.ACTIVATION_SENDER, JBoxd.ACTIVATION_SUBJECT, JBoxd.ACTIVATION_BODY)

                # set user as activated
                user = JBoxUserV2(user_id)
                user.set_activation_state(JBoxUserV2.ACTIVATION_CODE_AUTO, JBoxUserV2.ACTIVATION_GRANTED)
                user.save()

                rate_per_second -= 1
                if rate_per_second <= 0:
                    time.sleep(1)
                    rate_per_second = min(JBoxd.MAX_ACTIVATIONS_PER_SEC, rate)
        finally:
            JBoxd.finish_thread()
예제 #9
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)
예제 #10
0
    def get(self):
        sessname = unquote(self.get_cookie("sessname"))
        jbox_cookie = self.get_session_cookie()

        if (None == sessname) or (len(sessname) == 0) or (None == jbox_cookie):
            self.send_error()
            return

        user_id = jbox_cookie['u']
        user = JBoxUserV2(user_id)
        is_admin = sessname in self.config("admin_sessnames", [])
        manage_containers = is_admin or user.has_role(JBoxUserV2.ROLE_MANAGE_CONTAINERS)
        show_report = is_admin or user.has_role(JBoxUserV2.ROLE_ACCESS_STATS)
        cont = JBoxContainer.get_by_name(sessname)

        if cont is None:
            self.send_error()
            return

        if self.handle_if_logout(cont):
            return
        if self.handle_if_stats(is_admin or show_report):
            return
        if self.handle_if_show_cfg(is_admin):
            return
        if self.handle_if_instance_info(is_admin):
            return
        if self.handle_switch_julia_img(user):
            return

        juliaboxver, _upgrade_available = self.get_upgrade_available(cont)

        jimg_type = 0
        if user.has_resource_profile(JBoxUserV2.RES_PROF_JULIA_PKG_PRECOMP):
            jimg_type = JBoxUserV2.RES_PROF_JULIA_PKG_PRECOMP

        d = dict(
            manage_containers=manage_containers,
            show_report=show_report,
            sessname=sessname,
            user_id=user_id,
            created=isodate.datetime_isoformat(cont.time_created()),
            started=isodate.datetime_isoformat(cont.time_started()),
            allowed_till=isodate.datetime_isoformat((cont.time_started() + timedelta(seconds=self.config('expire')))),
            mem=cont.get_memory_allocated(),
            cpu=cont.get_cpu_allocated(),
            disk=cont.get_disk_allocated(),
            expire=self.config('expire'),
            juliaboxver=juliaboxver,
            jimg_type=jimg_type
        )

        self.rendertpl("ipnbadmin.tpl", d=d, cfg=self.config())
예제 #11
0
파일: volmgr.py 프로젝트: arshak/JuliaBox
    def get_disk_for_user(email):
        VolMgr.log_debug("restoring disk for %s", email)
        ebs = False

        if VolMgr.HAS_EBS:
            user = JBoxUserV2(email)
            ebs = user.has_resource_profile(
                JBoxUserV2.RESOURCE_PROFILE_DISK_EBS_1G)

        if ebs:
            return JBoxEBSVol.get_disk_for_user(email)
        else:
            return JBoxLoopbackVol.get_disk_for_user(email)
예제 #12
0
    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()
예제 #13
0
    def calc_stats():
        VolMgr.STATS = {
            'date': '',
            'num_users': 0,
            'loopback': {
                'num_files': 0,
                'total_size': 0,
                'sizes': [],
                'min_size': 0,
                'max_size': 0,
                'avg_size': 0,
                'sizes_hist': {
                    'counts': [],
                    'bins': []
                }
            }
        }

        result_set = JBoxUserV2.table().scan(attributes=('user_id', ))
        for user in result_set:
            VolMgr.calc_stat(user['user_id'])

        sizes = VolMgr.STATS['loopback']['sizes']
        VolMgr.STATS['loopback']['num_files'] = len(sizes)
        VolMgr.STATS['loopback']['total_size'] = sum(sizes)
        VolMgr.STATS['loopback']['min_size'] = min(sizes)
        VolMgr.STATS['loopback']['max_size'] = max(sizes)
        VolMgr.STATS['loopback']['avg_size'] = sum(sizes) / len(sizes)

        bin_size = int((VolMgr.STATS['loopback']['max_size'] -
                        VolMgr.STATS['loopback']['min_size']) / 10)
        min_size = VolMgr.STATS['loopback']['min_size']
        bins = []
        for idx in range(0, 10):
            bins.append(min_size + bin_size * idx)
        bins.append(VolMgr.STATS['loopback']['max_size'])
        counts = [0] * 10

        for size in sizes:
            for idx in range(1, 11):
                if size <= bins[idx]:
                    counts[idx - 1] += 1
                    break
        VolMgr.STATS['loopback']['sizes_hist']['counts'] = counts
        VolMgr.STATS['loopback']['sizes_hist']['bins'] = bins
        del VolMgr.STATS['loopback']['sizes']
        VolMgr.STATS['date'] = datetime.datetime.now(pytz.utc).isoformat()
예제 #14
0
    def calc_stats():
        VolMgr.STATS = {
            'date': '',
            'num_users': 0,
            'loopback': {
                'num_files': 0,
                'total_size': 0,
                'sizes': [],
                'min_size': 0,
                'max_size': 0,
                'avg_size': 0,
                'sizes_hist': {
                    'counts': [],
                    'bins': []
                }
            }
        }

        result_set = JBoxUserV2.table().scan(attributes=('user_id',))
        for user in result_set:
            VolMgr.calc_stat(user['user_id'])

        sizes = VolMgr.STATS['loopback']['sizes']
        VolMgr.STATS['loopback']['num_files'] = len(sizes)
        VolMgr.STATS['loopback']['total_size'] = sum(sizes)
        VolMgr.STATS['loopback']['min_size'] = min(sizes)
        VolMgr.STATS['loopback']['max_size'] = max(sizes)
        VolMgr.STATS['loopback']['avg_size'] = sum(sizes) / len(sizes)

        bin_size = int((VolMgr.STATS['loopback']['max_size'] - VolMgr.STATS['loopback']['min_size']) / 10)
        min_size = VolMgr.STATS['loopback']['min_size']
        bins = []
        for idx in range(0, 10):
            bins.append(min_size + bin_size*idx)
        bins.append(VolMgr.STATS['loopback']['max_size'])
        counts = [0] * 10

        for size in sizes:
            for idx in range(1, 11):
                if size <= bins[idx]:
                    counts[idx-1] += 1
                    break
        VolMgr.STATS['loopback']['sizes_hist']['counts'] = counts
        VolMgr.STATS['loopback']['sizes_hist']['bins'] = bins
        del VolMgr.STATS['loopback']['sizes']
        VolMgr.STATS['date'] = datetime.datetime.now(pytz.utc).isoformat()
예제 #15
0
    def post(self):
        self.log_debug("Homework handler got POST request")
        sessname = unquote(self.get_cookie("sessname"))
        jbox_cookie = self.get_session_cookie()

        if (None == sessname) or (len(sessname) == 0) or (None == jbox_cookie):
            self.log_info(
                "Homework handler got invalid sessname[%r] or cookie[%r]",
                sessname, jbox_cookie)
            self.send_error()
            return

        user_id = jbox_cookie['u']
        user = JBoxUserV2(user_id)
        is_admin = sessname in self.config(
            "admin_sessnames", []) or user.has_role(JBoxUserV2.ROLE_SUPER)
        course_owner = is_admin or user.has_role(JBoxUserV2.ROLE_OFFER_COURSES)
        cont = JBoxContainer.get_by_name(sessname)
        self.log_info("user_id[%r], is_admin[%r], course_owner[%r]", user_id,
                      is_admin, course_owner)

        if cont is None:
            self.log_info("user_id[%r] container not found", user_id)
            self.send_error()
            return

        courses_offered = user.get_courses_offered()

        if self.handle_if_check(user_id):
            return
        if self.handle_create_course(user_id):
            return
        if self.handle_get_metadata(is_admin, courses_offered):
            return
        if course_owner and self.handle_if_report(user_id, is_admin,
                                                  courses_offered):
            return

        self.log_error("no handlers found")
        # only AJAX requests responded to
        self.send_error()
예제 #16
0
파일: volmgr.py 프로젝트: ssfrr/JuliaBox
    def get_disk_for_user(email):
        VolMgr.log_debug("restoring disk for %s", email)
        user = JBoxUserV2(email)

        ebs = False
        if VolMgr.HAS_EBS:
            ebs = user.has_resource_profile(JBoxUserV2.RES_PROF_DISK_EBS_1G)

        custom_jimg = None
        ipython_profile = 'julia'
        # TODO: image path should be picked up from config
        if user.has_resource_profile(JBoxUserV2.RES_PROF_JULIA_PKG_PRECOMP):
            custom_jimg = '/home/juser/.juliabox/jimg/sys.ji'
            ipython_profile = 'jboxjulia'

        if ebs:
            disk = JBoxEBSVol.get_disk_for_user(email)
        else:
            disk = JBoxLoopbackVol.get_disk_for_user(email)
        disk.setup_julia_image(ipython_profile, custom_jimg)
        disk.setup_tutorial_link()
        return disk
예제 #17
0
파일: admin.py 프로젝트: arshak/JuliaBox
    def get(self):
        sessname = unquote(self.get_cookie("sessname"))
        jbox_cookie = self.get_session_cookie()

        if (None == sessname) or (len(sessname) == 0) or (None == jbox_cookie):
            self.send_error()
            return

        user_id = jbox_cookie['u']
        user = JBoxUserV2(user_id)
        is_admin = sessname in self.config("admin_sessnames", [])
        manage_containers = is_admin or user.has_role(
            JBoxUserV2.ROLE_MANAGE_CONTAINERS)
        show_report = is_admin or user.has_role(JBoxUserV2.ROLE_ACCESS_STATS)
        cont = JBoxContainer.get_by_name(sessname)

        if cont is None:
            self.send_error()
            return

        if self.handle_if_logout(cont):
            return
        if self.handle_if_stats(is_admin):
            return
        if self.handle_if_show_cfg(is_admin):
            return
        if self.handle_if_instance_info(is_admin):
            return

        juliaboxver, _upgrade_available = self.get_upgrade_available(cont)

        sections = []
        report = {}
        report_span = 'day'

        if manage_containers:
            sections = self.do_containers()

        if show_report:
            today = datetime.now()
            if self.get_argument('range', 'day') == 'week':
                dates = [today - timedelta(days=i) for i in range(6, -1, -1)]
                report_span = 'week'
            else:
                dates = [today]
            report = JBoxAccountingV2.get_stats(dates)

        d = dict(manage_containers=manage_containers,
                 show_report=show_report,
                 report_span=report_span,
                 sessname=sessname,
                 user_id=user_id,
                 created=isodate.datetime_isoformat(cont.time_created()),
                 started=isodate.datetime_isoformat(cont.time_started()),
                 allowed_till=isodate.datetime_isoformat(
                     (cont.time_started() +
                      timedelta(seconds=self.config('expire')))),
                 mem=cont.get_memory_allocated(),
                 cpu=cont.get_cpu_allocated(),
                 disk=cont.get_disk_allocated(),
                 expire=self.config('expire'),
                 sections=sections,
                 report=report,
                 juliaboxver=juliaboxver)

        self.rendertpl("ipnbadmin.tpl", d=d, cfg=self.config())
예제 #18
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(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
예제 #19
0
    def get(self):
        if not self.config("gauth"):
            sessname = unquote(self.get_argument("sessname"))
            self.set_session_cookie(sessname)
            self.redirect('/')
            return

        # self_redirect_uri should be similar to  'http://<host>/hostlaunchipnb/'
        self_redirect_uri = self.request.full_url()
        idx = self_redirect_uri.index("hostlaunchipnb/")
        self_redirect_uri = self_redirect_uri[0:(idx + len("hostlaunchipnb/"))]

        # state indicates the stage of auth during multistate auth
        state = self.get_argument('state', None)

        code = self.get_argument('code', False)
        if code is not False:
            user = yield self.get_authenticated_user(redirect_uri=self_redirect_uri, code=code)

            # get user info
            http = tornado.httpclient.AsyncHTTPClient()
            auth_string = "%s %s" % (user['token_type'], user['access_token'])
            response = yield http.fetch('https://www.googleapis.com/userinfo/v2/me',
                                        headers={"Authorization": auth_string})
            user_info = json.loads(response.body)

            user_id = user_info['email']

            jbuser = JBoxUserV2(user_id, create=True)
            if state == 'store_creds':
                creds = self.make_credentials(user)
                jbuser.set_gtok(base64.b64encode(creds.to_json()))
                jbuser.save()
                #self.log_info(str(user))
                #self.log_info(creds.to_json())
                self.redirect('/')
                return
            else:
                if not AuthHandler.is_user_activated(jbuser):
                    self.redirect('/?pending_activation=' + user_id)
                    return

                self.set_session_cookie(user_id)
                if jbuser.is_new:
                    jbuser.save()

                if self.try_launch_container(user_id, max_hop=False):
                    self.set_loading_state(user_id)
                self.redirect('/')
                return
        else:
            if state == 'ask_gdrive':
                jbox_cookie = self.get_session_cookie()
                scope = ['https://www.googleapis.com/auth/drive']
                extra_params = {'approval_prompt': 'force', 'access_type': 'offline',
                                'login_hint': jbox_cookie['u'], 'include_granted_scopes': 'true',
                                'state': 'store_creds'}
            else:
                scope = ['profile', 'email']
                extra_params = {'approval_prompt': 'auto'}

            yield self.authorize_redirect(redirect_uri=self_redirect_uri,
                                          client_id=self.settings['google_oauth']['key'],
                                          scope=scope,
                                          response_type='code',
                                          extra_params=extra_params)
예제 #20
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(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