def get_disk_for_user(user_email): JBoxEBSVol.log_debug("creating EBS volume for %s", user_email) if not JBoxEBSVol.HAS_EBS: raise Exception("EBS disks not enabled") disk_id = JBoxEBSVol._reserve_disk_id() if disk_id is None: raise Exception("No free disk available") sess_id = unique_sessname(user_email) sess_props = JBoxSessionProps(sess_id, create=True, user_id=user_email) if sess_props.is_new: sess_props.save() snap_id = sess_props.get_snapshot_id() if snap_id is None: snap_id = JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT JBoxEBSVol.log_debug("will use snapshot id %s for %s", snap_id, user_email) _dev_path, mnt_path = CloudHost.create_new_volume(snap_id, disk_id, JBoxEBSVol.FS_LOC, tag=user_email) ebsvol = JBoxEBSVol(mnt_path, user_email=user_email) if snap_id == JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT: JBoxEBSVol.log_debug("creating home folder on blank volume for %s", user_email) ebsvol.restore_user_home() ebsvol.restore() #else: # snap_age_days = CloudHost.get_snapshot_age(snap_id).total_seconds()/(60*60*24) # if snap_age_days > 7: # ebsvol.restore_user_home() JBoxEBSVol.log_debug("setting up instance configuration on disk for %s", user_email) ebsvol.setup_instance_config() return ebsvol
def _backup(self, clear_volume=False): sess_props = JBoxSessionProps(self.sessname) desc = sess_props.get_user_id() + " JuliaBox Backup" disk_id = self.disk_path.split('/')[-1] snap_id = CloudHost.snapshot_volume(dev_id=disk_id, tag=self.sessname, description=desc) old_snap_id = sess_props.get_snapshot_id() sess_props.set_snapshot_id(snap_id) sess_props.save() if old_snap_id is not None: CloudHost.delete_snapshot(old_snap_id)
def _backup(self, clear_volume=False): sess_props = JBoxSessionProps(self.sessname) desc = sess_props.get_user_id() + " JuliaBox Backup" disk_id = self.disk_path.split('/')[-1] snap_id = CloudHost.snapshot_volume(dev_id=disk_id, tag=self.sessname, description=desc) old_snap_id = sess_props.get_snapshot_id() sess_props.set_snapshot_id(snap_id) sess_props.save() if old_snap_id is not None: CloudHost.delete_snapshot(old_snap_id)
def get_disk_for_user(user_email): JBoxEBSVol.log_debug("creating EBS volume for %s", user_email) if not JBoxEBSVol.HAS_EBS: raise Exception("EBS disks not enabled") disk_id = JBoxEBSVol._reserve_disk_id() if disk_id is None: raise Exception("No free disk available") sess_id = unique_sessname(user_email) sess_props = JBoxSessionProps(sess_id, create=True, user_id=user_email) if sess_props.is_new: sess_props.save() snap_id = sess_props.get_snapshot_id() if snap_id is None: snap_id = JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT JBoxEBSVol.log_debug("will use snapshot id %s for %s", snap_id, user_email) _dev_path, mnt_path = CloudHost.create_new_volume(snap_id, disk_id, JBoxEBSVol.FS_LOC, tag=user_email) ebsvol = JBoxEBSVol(mnt_path, user_email=user_email) if snap_id == JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT: JBoxEBSVol.log_debug("creating home folder on blank volume for %s", user_email) ebsvol.restore_user_home(True) ebsvol.restore() else: JBoxEBSVol.log_debug( "updating home folder on existing volume for %s", user_email) ebsvol.restore_user_home(False) # snap_age_days = CloudHost.get_snapshot_age(snap_id).total_seconds()/(60*60*24) # if snap_age_days > 7: # ebsvol.restore_user_home() JBoxEBSVol.log_debug( "setting up instance configuration on disk for %s", user_email) ebsvol.setup_instance_config() return ebsvol
def update_disk_states(): detached_disks = JBoxDiskState.get_detached_disks() time_now = datetime.datetime.now(pytz.utc) for disk_key in detached_disks: disk_info = JBoxDiskState(disk_key=disk_key) user_id = disk_info.get_user_id() sess_props = JBoxSessionProps(unique_sessname(user_id)) incomplete_snapshots = [] modified = False for snap_id in disk_info.get_snapshot_ids(): if not CloudHost.is_snapshot_complete(snap_id): incomplete_snapshots.append(snap_id) continue JBoxd.log_debug("updating latest snapshot of user %s to %s", user_id, snap_id) old_snap_id = sess_props.get_snapshot_id() sess_props.set_snapshot_id(snap_id) modified = True if old_snap_id is not None: CloudHost.delete_snapshot(old_snap_id) if modified: sess_props.save() disk_info.set_snapshot_ids(incomplete_snapshots) disk_info.save() if len(incomplete_snapshots) == 0: if (time_now - disk_info.get_detach_time() ).total_seconds() > 24 * 60 * 60: vol_id = disk_info.get_volume_id() JBoxd.log_debug( "volume %s for user %s unused for too long", vol_id, user_id) disk_info.delete() CloudHost.detach_volume(vol_id, delete=True) else: JBoxd.log_debug("ongoing snapshots of user %s: %r", user_id, incomplete_snapshots)
def get_disk_for_user(user_email): JBoxEBSVol.log_debug("creating EBS volume for %s", user_email) if not JBoxEBSVol.HAS_EBS: raise Exception("EBS disks not enabled") disk_id = JBoxEBSVol._reserve_disk_id() if disk_id is None: raise Exception("No free disk available") try: existing_disk = JBoxDiskState(cluster_id=CloudHost.INSTALL_ID, region_id=CloudHost.REGION, user_id=user_email) except: existing_disk = None if existing_disk is None: sess_id = unique_sessname(user_email) sess_props = JBoxSessionProps(sess_id, create=True, user_id=user_email) if sess_props.is_new: sess_props.save() snap_id = sess_props.get_snapshot_id() if snap_id is None: snap_id = JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT JBoxEBSVol.log_debug("will use snapshot id %s for %s", snap_id, user_email) _dev_path, mnt_path, vol_id = CloudHost.create_new_volume( snap_id, disk_id, JBoxEBSVol.FS_LOC, tag=user_email, disk_sz_gb=JBoxEBSVol.DISK_LIMIT) existing_disk = JBoxDiskState(cluster_id=CloudHost.INSTALL_ID, region_id=CloudHost.REGION, user_id=user_email, volume_id=vol_id, attach_time=None, create=True) else: _dev_path, mnt_path = CloudHost.attach_volume( existing_disk.get_volume_id(), disk_id, JBoxEBSVol.FS_LOC) existing_disk.set_attach_time() snap_id = None existing_disk.set_state(JBoxDiskState.STATE_ATTACHED) existing_disk.save() ebsvol = JBoxEBSVol(mnt_path, user_email=user_email) if snap_id == JBoxEBSVol.DISK_TEMPLATE_SNAPSHOT: JBoxEBSVol.log_debug("creating home folder on blank volume for %s", user_email) ebsvol.restore_user_home(True) ebsvol.restore() else: JBoxEBSVol.log_debug( "updating home folder on existing volume for %s", user_email) ebsvol.restore_user_home(False) # snap_age_days = CloudHost.get_snapshot_age(snap_id).total_seconds()/(60*60*24) # if snap_age_days > 7: # ebsvol.restore_user_home() JBoxEBSVol.log_debug( "setting up instance configuration on disk for %s", user_email) ebsvol.setup_instance_config() return ebsvol