Ejemplo n.º 1
0
    def __init__(self, disk_path, user_email=None, user_name=None, sessname=None, old_sessname=None):
        self.disk_path = disk_path
        self.user_email = user_email

        if user_name is not None:
            self.user_name = user_name
        elif user_email is not None:
            self.user_name = get_user_name(user_email)
        else:
            self.user_name = None

        if sessname is not None:
            self.sessname = sessname
        elif user_email is not None:
            self.sessname = unique_sessname(user_email)
        else:
            self.sessname = None

        if old_sessname is not None:
            self.old_sessname = old_sessname
        elif user_email is not None:
            self.old_sessname = esc_sessname(user_email)
        else:
            self.old_sessname = None

        self._dbg_str = str(self.sessname) + "(" + self.disk_path + ")"
Ejemplo n.º 2
0
    def __init__(self,
                 disk_path,
                 user_email=None,
                 user_name=None,
                 sessname=None,
                 old_sessname=None):
        self.disk_path = disk_path
        self.user_email = user_email

        if user_name is not None:
            self.user_name = user_name
        elif user_email is not None:
            self.user_name = get_user_name(user_email)
        else:
            self.user_name = None

        if sessname is not None:
            self.sessname = sessname
        elif user_email is not None:
            self.sessname = unique_sessname(user_email)
        else:
            self.sessname = None

        if old_sessname is not None:
            self.old_sessname = old_sessname
        elif user_email is not None:
            self.old_sessname = esc_sessname(user_email)
        else:
            self.old_sessname = None

        self._dbg_str = str(self.sessname) + "(" + self.disk_path + ")"
Ejemplo n.º 3
0
    def restore(self):
        sessname = unique_sessname(self.user_email)
        old_sessname = esc_sessname(self.user_email)
        src = os.path.join(JBoxVol.BACKUP_LOC, sessname + ".tar.gz")
        k = JBoxVol.pull_from_s3(src)  # download from S3 if exists
        if not os.path.exists(src):
            if old_sessname is not None:
                src = os.path.join(JBoxVol.BACKUP_LOC, old_sessname + ".tar.gz")
                k = JBoxVol.pull_from_s3(src)  # download from S3 if exists

        if not os.path.exists(src):
            return

        JBoxVol.log_info("Filtering out restore info from backup " + src + " to " + self.disk_path)

        src_tar = tarfile.open(src, 'r:gz')
        try:
            for info in src_tar.getmembers():
                if not info.name.startswith('juser/'):
                    continue
                if info.name.startswith('juser/.') and (info.name.split('/')[1] in ['.juliabox', '.julia', '.ipython']):
                    continue
                info.name = info.name[6:]
                if len(info.name) == 0:
                    continue
                src_tar.extract(info, self.disk_path)
            JBoxVol.log_info("Restored backup at " + self.disk_path)
        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 restore backup for %s", sessname)
            else:
                raise
Ejemplo n.º 4
0
 def get(self):
     sessname = str(self.get_cookie("sessname")).replace('"', '')
     if self.is_valid_req(self):
         JBoxContainer.record_ping("/" + esc_sessname(sessname))
         self.set_status(status_code=204)
         self.finish()
     else:
         self.log_info("Invalid ping request for " + sessname)
         self.send_error(status_code=403)
Ejemplo n.º 5
0
    def restore(self):
        sessname = unique_sessname(self.user_email)
        old_sessname = esc_sessname(self.user_email)
        src = os.path.join(JBoxVol.BACKUP_LOC, sessname + ".tar.gz")
        k = JBoxVol.pull_from_s3(src)  # download from S3 if exists
        if not os.path.exists(src):
            if old_sessname is not None:
                src = os.path.join(JBoxVol.BACKUP_LOC,
                                   old_sessname + ".tar.gz")
                k = JBoxVol.pull_from_s3(src)  # download from S3 if exists

        if not os.path.exists(src):
            return

        JBoxVol.log_info("Filtering out restore info from backup " + src +
                         " to " + self.disk_path)

        src_tar = tarfile.open(src, 'r:gz')
        try:
            perms = {}
            for info in src_tar.getmembers():
                if not info.name.startswith('juser/'):
                    continue
                extract_name = info.name[6:]
                if info.name.startswith('juser/.'):
                    folder = info.name.split('/')[1]
                    if (
                            folder == '.julia'
                    ) or JBoxVol._is_path_user_home_essential(extract_name):
                        continue
                info.name = extract_name
                if len(info.name) == 0:
                    continue
                src_tar.extract(info, self.disk_path)
                extracted_path = os.path.join(self.disk_path, extract_name)
                if os.path.isdir(extracted_path) and not os.access(
                        extracted_path, os.W_OK):
                    st = os.stat(extracted_path)
                    perms[extracted_path] = st
                    os.chmod(extracted_path, st.st_mode | stat.S_IWRITE)
            if len(perms) > 0:
                JBoxVol.log_debug("resetting permissions on %d folders",
                                  len(perms))
                for extracted_path, perm in perms.iteritems():
                    os.chmod(extracted_path, perm)
            JBoxVol.log_info("Restored backup at " + self.disk_path)
        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 restore backup for %s",
                                      sessname)
            else:
                raise
Ejemplo n.º 6
0
    def restore(self):
        sessname = unique_sessname(self.user_email)
        old_sessname = esc_sessname(self.user_email)
        src = os.path.join(JBoxVol.BACKUP_LOC, sessname + ".tar.gz")
        k = JBoxVol.pull_from_s3(src)  # download from S3 if exists
        if not os.path.exists(src):
            if old_sessname is not None:
                src = os.path.join(JBoxVol.BACKUP_LOC, old_sessname + ".tar.gz")
                k = JBoxVol.pull_from_s3(src)  # download from S3 if exists

        if not os.path.exists(src):
            return

        JBoxVol.log_info("Filtering out restore info from backup " + src + " to " + self.disk_path)

        src_tar = tarfile.open(src, 'r:gz')
        try:
            perms = {}
            for info in src_tar.getmembers():
                if not info.name.startswith('juser/'):
                    continue
                extract_name = info.name[6:]
                if info.name.startswith('juser/.'):
                    folder = info.name.split('/')[1]
                    if (folder == '.julia') or JBoxVol._is_path_user_home_essential(extract_name):
                        continue
                info.name = extract_name
                if len(info.name) == 0:
                    continue
                src_tar.extract(info, self.disk_path)
                extracted_path = os.path.join(self.disk_path, extract_name)
                if os.path.isdir(extracted_path) and not os.access(extracted_path, os.W_OK):
                    st = os.stat(extracted_path)
                    perms[extracted_path] = st
                    os.chmod(extracted_path, st.st_mode | stat.S_IWRITE)
            if len(perms) > 0:
                JBoxVol.log_debug("resetting permissions on %d folders", len(perms))
                for extracted_path, perm in perms.iteritems():
                    os.chmod(extracted_path, perm)
            JBoxVol.log_info("Restored backup at " + self.disk_path)
        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 restore backup for %s", sessname)
            else:
                raise
Ejemplo n.º 7
0
    def restore(self):
        sessname = unique_sessname(self.user_email)
        old_sessname = esc_sessname(self.user_email)
        src = os.path.join(JBoxVol.BACKUP_LOC, sessname + ".tar.gz")
        k = JBoxVol.pull_from_s3(src)  # download from S3 if exists
        if not os.path.exists(src):
            if old_sessname is not None:
                src = os.path.join(JBoxVol.BACKUP_LOC,
                                   old_sessname + ".tar.gz")
                k = JBoxVol.pull_from_s3(src)  # download from S3 if exists

        if not os.path.exists(src):
            return

        JBoxVol.log_info("Filtering out restore info from backup " + src +
                         " to " + self.disk_path)

        src_tar = tarfile.open(src, 'r:gz')
        try:
            for info in src_tar.getmembers():
                if not info.name.startswith('juser/'):
                    continue
                extract_name = info.name[6:]
                if info.name.startswith('juser/.'):
                    folder = info.name.split('/')[1]
                    if (
                            folder == '.julia'
                    ) or JBoxVol._is_path_user_home_essential(extract_name):
                        continue
                info.name = extract_name
                if len(info.name) == 0:
                    continue
                src_tar.extract(info, self.disk_path)
            JBoxVol.log_info("Restored backup at " + self.disk_path)
        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 restore backup for %s",
                                      sessname)
            else:
                raise
Ejemplo n.º 8
0
    def start(self, email):
        self.refresh()
        JBoxContainer.log_info("Starting " + self.debug_str())
        if self.is_running() or self.is_restarting():
            JBoxContainer.log_info("Already started " + self.debug_str())
            return

        disk_id, disk_path = JBoxContainer.create_disk()
        self.restore_backup_to_disk(disk_path, old_backup=esc_sessname(email))
        JBoxContainer.gen_ssh_key(disk_path)
        JBoxContainer.gen_gitconfig(disk_path, email.split('@')[0], email)
        JBoxContainer.mark_disk_used(disk_id)

        vols = {}
        for hvol, cvol in zip(JBoxContainer.HOST_VOLUMES, JBoxContainer.VOLUMES):
            hvol = self.get_host_volume(hvol, disk_id)
            vols[hvol] = {'bind': cvol, 'ro': False}

        JBoxContainer.DCKR.start(self.dockid, port_bindings=JBoxContainer.CONTAINER_PORT_BINDINGS, binds=vols)
        self.refresh()
        JBoxContainer.log_info("Started " + self.debug_str())
        cname = self.get_name()
        if cname is not None:
            JBoxContainer.record_ping(cname)
Ejemplo n.º 9
0
    def get(self):
        jbox_cookie = AuthHandler.get_session_cookie(self)

        if self.config("invite_only"):
            if self.get_argument("invite", False):
                self.set_cookie("is_invite", "yes")
                self.redirect('/hostlaunchipnb/')
                return

        if None == jbox_cookie:
            which_msg = int(
                self.get_argument("_msg", JBoxUserV2.ACTIVATION_NONE))
            if self.get_argument("_msg", "") != "":
                self.clear_cookie("is_invite")
                if which_msg == JBoxUserV2.ACTIVATION_GRANTED:
                    msg = "Your account has already been approved"
                elif which_msg == JBoxUserV2.ACTIVATION_REQUESTED:
                    msg = "You have already registered for an invite"
                else:
                    msg = "Thank you for your interest! We will get back to you with an invitation soon."
                state = self.state(success=msg)
            else:
                state = self.state()
            self.rendertpl("index.tpl", cfg=self.config(), state=state)
        else:
            user_id = jbox_cookie['u']
            sessname = esc_sessname(user_id)

            if self.config("gauth"):
                try:
                    jbuser = JBoxUserV2(user_id)
                except:
                    # stale cookie. we don't have the user in our database anymore
                    self.log_info(
                        "stale cookie. we don't have the user in our database anymore. user: "******"invite_only"):
                    code, status = jbuser.get_activation_state()
                    if status != JBoxUserV2.ACTIVATION_GRANTED:
                        invite_code = self.get_argument("invite_code", False)
                        if invite_code is not False:
                            try:
                                invite = JBoxInvite(invite_code)
                            except:
                                invite = None

                            if (invite is not None
                                ) and invite.is_invited(user_id):
                                jbuser.set_activation_state(
                                    invite_code, JBoxUserV2.ACTIVATION_GRANTED)
                                jbuser.save()
                                self.redirect('/hostlaunchipnb/')
                                return
                            else:
                                error_msg = 'You entered an invalid invitation code. Try again or request a new invitation.'
                        else:
                            error_msg = 'Enter the invitation code'

                        self.rendertpl("index.tpl",
                                       cfg=self.config(),
                                       state=self.state(error=error_msg,
                                                        ask_invite_code=True,
                                                        user_id=user_id))
                        return

                creds = jbuser.get_gtok()
                if creds is not None:
                    try:
                        creds_json = json.loads(base64.b64decode(creds))
                        creds_json = self.renew_creds(creds_json)
                        authtok = creds_json['access_token']
                    except:
                        self.log_info(
                            "stale stored creds. will renew on next use. user: "
                            + user_id)
                        creds = None
                        authtok = None
                else:
                    authtok = None
            else:
                creds = None
                authtok = None

            self.chk_and_launch_docker(sessname, creds, authtok, user_id)
Ejemplo n.º 10
0
    def get(self):
        jbox_cookie = AuthHandler.get_session_cookie(self)

        if self.config("invite_only"):
            if self.get_argument("invite", False):
                self.set_cookie("is_invite", "yes")
                self.redirect('/hostlaunchipnb/')
                return

        if None == jbox_cookie:
            which_msg = int(self.get_argument("_msg", JBoxUserV2.ACTIVATION_NONE))
            if self.get_argument("_msg", "") != "":
                self.clear_cookie("is_invite")
                if which_msg == JBoxUserV2.ACTIVATION_GRANTED:
                    msg = "Your account has already been approved"
                elif which_msg == JBoxUserV2.ACTIVATION_REQUESTED:
                    msg = "You have already registered for an invite"
                else:
                    msg = "Thank you for your interest! We will get back to you with an invitation soon."
                state = self.state(success=msg)
            else:
                state = self.state()
            self.rendertpl("index.tpl", cfg=self.config(), state=state)
        else:
            user_id = jbox_cookie['u']
            sessname = esc_sessname(user_id)

            if self.config("gauth"):
                try:
                    jbuser = JBoxUserV2(user_id)
                except:
                    # stale cookie. we don't have the user in our database anymore
                    self.log_info("stale cookie. we don't have the user in our database anymore. user: "******"invite_only"):
                    code, status = jbuser.get_activation_state()
                    if status != JBoxUserV2.ACTIVATION_GRANTED:
                        invite_code = self.get_argument("invite_code", False)
                        if invite_code is not False:
                            try:
                                invite = JBoxInvite(invite_code)
                            except:
                                invite = None

                            if (invite is not None) and invite.is_invited(user_id):
                                jbuser.set_activation_state(invite_code, JBoxUserV2.ACTIVATION_GRANTED)
                                jbuser.save()
                                self.redirect('/hostlaunchipnb/')
                                return
                            else:
                                error_msg = 'You entered an invalid invitation code. Try again or request a new invitation.'
                        else:
                            error_msg = 'Enter the invitation code'

                        self.rendertpl("index.tpl", cfg=self.config(), state=self.state(
                            error=error_msg,
                            ask_invite_code=True, user_id=user_id))
                        return

                creds = jbuser.get_gtok()
                if creds is not None:
                    try:
                        creds_json = json.loads(base64.b64decode(creds))
                        creds_json = self.renew_creds(creds_json)
                        authtok = creds_json['access_token']
                    except:
                        self.log_info("stale stored creds. will renew on next use. user: " + user_id)
                        creds = None
                        authtok = None
                else:
                    authtok = None
            else:
                creds = None
                authtok = None

            self.chk_and_launch_docker(sessname, creds, authtok, user_id)