def get_disk_from_container(cid): props = JBoxVol.dckr().inspect_container(cid) vols = props['Volumes'] for _cpath, hpath in vols.iteritems(): if hpath.startswith(JBoxLoopbackVol.FS_LOC): return JBoxLoopbackVol.get_disk_from_container(cid) elif VolMgr.HAS_EBS and hpath.startswith(JBoxEBSVol.FS_LOC): return JBoxEBSVol.get_disk_from_container(cid) return None
def do_task(plugin_type, data): if plugin_type != JBPluginTask.JBP_CMD_ASYNC: return mode = data['action'] user_id = data['user_id'] sessname = data['sessname'] user = JBoxUserV2(user_id) is_allowed = user.has_resource_profile( JBoxUserV2.RES_PROF_DISK_EBS_10G) if not is_allowed: JBoxEBSVolAsyncTask.log_error( "Data volume access not allowed for user") return cont = SessContainer.get_by_name(sessname) if cont is None: return vol = JBoxEBSVol.get_disk_from_container(sessname) disk_state = None try: disk_state = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION, user_id=user_id) except: pass JBoxEBSVolAsyncTask.log_debug("Data volume request %s for %s", mode, cont.debug_str()) if mode == 'attach': if vol is None: vol = JBoxEBSVol.get_disk_for_user(user_id) JBoxEBSVol.mount_host_device(vol.disk_path, cont.dockid, JBoxVol.DATA_MOUNT_POINT) disk_state = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION, user_id=user_id) if disk_state.get_state() != JBoxDiskState.STATE_ATTACHED: disk_state.set_state(JBoxDiskState.STATE_ATTACHED) disk_state.save() elif mode == 'detach': if cont is not None and cont.is_running(): if vol is not None: # unmount from container first JBoxEBSVol.unmount_host_device(vol.disk_path, cont.dockid) elif disk_state is not None: # no volume attached. ensure disk state is updated if disk_state.get_state() != JBoxDiskState.STATE_DETACHED: disk_state.set_state(JBoxDiskState.STATE_DETACHED) disk_state.save() if vol is not None: vol.release(backup=True) JBoxEBSVolAsyncTask.log_debug( "Data volume request %s completed for %s", mode, cont.debug_str())
def get_disk_from_container(cid): props = JBoxVol.dckr().inspect_container(cid) try: vols = props['Volumes'] for _cpath, hpath in vols.iteritems(): if hpath.startswith(JBoxLoopbackVol.FS_LOC): return JBoxLoopbackVol.get_disk_from_container(cid) elif VolMgr.HAS_EBS and hpath.startswith(JBoxEBSVol.FS_LOC): return JBoxEBSVol.get_disk_from_container(cid) except: VolMgr.log_error("error finding disk ids used in " + cid) return None
def do_task(plugin_type, data): if plugin_type != JBPluginTask.JBP_CMD_ASYNC: return mode = data["action"] user_id = data["user_id"] sessname = data["sessname"] user = JBoxUserV2(user_id) is_allowed = user.has_resource_profile(JBoxUserV2.RES_PROF_DISK_EBS_10G) if not is_allowed: JBoxEBSVolAsyncTask.log_error("Data volume access not allowed for user") return cont = SessContainer.get_by_name(sessname) if cont is None: return vol = JBoxEBSVol.get_disk_from_container(sessname) disk_state = None try: disk_state = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION, user_id=user_id) except: pass JBoxEBSVolAsyncTask.log_debug("Data volume request %s for %s", mode, cont.debug_str()) if mode == "attach": if vol is None: vol = JBoxEBSVol.get_disk_for_user(user_id) JBoxEBSVol.mount_host_device(vol.disk_path, cont.dockid, JBoxVol.DATA_MOUNT_POINT) disk_state = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION, user_id=user_id) if disk_state.get_state() != JBoxDiskState.STATE_ATTACHED: disk_state.set_state(JBoxDiskState.STATE_ATTACHED) disk_state.save() elif mode == "detach": if cont is not None and cont.is_running(): if vol is not None: # unmount from container first JBoxEBSVol.unmount_host_device(vol.disk_path, cont.dockid) elif disk_state is not None: # no volume attached. ensure disk state is updated if disk_state.get_state() != JBoxDiskState.STATE_DETACHED: disk_state.set_state(JBoxDiskState.STATE_DETACHED) disk_state.save() if vol is not None: vol.release(backup=True) JBoxEBSVolAsyncTask.log_debug("Data volume request %s completed for %s", mode, cont.debug_str())
def _get_state(self, sessname, user_id): vol = JBoxEBSVol.get_disk_from_container(sessname) state_code = JBoxDiskState.STATE_DETACHED try: disk_state = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION, user_id=user_id) state_code = disk_state.get_state() except: pass if ((state_code == JBoxDiskState.STATE_ATTACHED) and (vol is None)) or ( (state_code == JBoxDiskState.STATE_DETACHED) and (vol is not None) ): state_code = -1 self.log_debug("EBS disk state: %r", state_code) return {"disk_size": "10 GB", "state": state_code}
def _get_state(self, sessname, user_id): vol = JBoxEBSVol.get_disk_from_container(sessname) state_code = JBoxDiskState.STATE_DETACHED try: disk_state = JBoxDiskState(cluster_id=CompEC2.INSTALL_ID, region_id=CompEC2.REGION, user_id=user_id) state_code = disk_state.get_state() except: pass if ((state_code == JBoxDiskState.STATE_ATTACHED) and (vol is None)) or \ ((state_code == JBoxDiskState.STATE_DETACHED) and (vol is not None)): state_code = -1 self.log_debug("EBS disk state: %r", state_code) return {'disk_size': '10 GB', 'state': state_code}