Example #1
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)
Example #2
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()
Example #3
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
Example #4
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())
Example #5
0
    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)
Example #6
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()
Example #7
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)
        disk.setup_julia_image(ipython_profile, custom_jimg)
        disk.setup_tutorial_link()
        return disk
Example #8
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
Example #9
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):
            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())
Example #10
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)