def NodeStageVolume(self, request, context): Utils.validate_params_exists( request, ['volume_id', 'staging_target_path', 'volume_capability']) volume_id = request.volume_id staging_target_path = request.staging_target_path volume_capability = request.volume_capability secrets = request.secrets publish_context = request.publish_context volume_context = request.volume_context reqJson = MessageToJson(request) self.logger.debug( 'NodeStageVolume called with request: {}'.format(reqJson)) access_mode = volume_capability.access_mode.mode access_type = self._get_block_or_mount_volume(request) nvmesh_volume_name = volume_id block_device_path = Utils.get_nvmesh_block_device_path( nvmesh_volume_name) # run nvmesh attach locally requested_nvmesh_access_mode = Consts.AccessMode.to_nvmesh(access_mode) Utils.nvmesh_attach_volume(nvmesh_volume_name, requested_nvmesh_access_mode) Utils.wait_for_volume_io_enabled(nvmesh_volume_name) if access_type == Consts.VolumeAccessType.MOUNT: mount_request = volume_capability.mount self.logger.info( 'Requested Mounted FileSystem Volume with fs_type={}'.format( mount_request.fs_type)) fs_type = mount_request.fs_type or 'ext4' mount_flags = [] if mount_request.mount_flags: for flag in mount_request.mount_flags.split(' '): mount_flags.append(flag) FileSystemManager.format_block_device(block_device_path, fs_type) if FileSystemManager.is_mounted(staging_target_path): self.logger.warning( 'path {} is already mounted'.format(staging_target_path)) FileSystemManager.mount(source=block_device_path, target=staging_target_path) elif access_type == Consts.VolumeAccessType.BLOCK: self.logger.info('Requested Block Volume') # We do not mount here, NodePublishVolume will mount directly from the block device to the publish_path # This is because Kubernetes automatically creates a directory in the staging_path else: self.logger.Info('Unknown AccessType {}'.format(access_type)) return NodeStageVolumeResponse()
def NodeStageVolume(self, request, context): Utils.validate_params_exists( request, ['volume_id', 'staging_target_path', 'volume_capability']) zone, nvmesh_volume_name = Utils.zone_and_vol_name_from_co_id( request.volume_id) staging_target_path = request.staging_target_path volume_capability = request.volume_capability secrets = request.secrets publish_context = request.publish_context volume_context = request.volume_context reqJson = MessageToJson(request) self.logger.debug( 'NodeStageVolume called with request: {}'.format(reqJson)) access_mode = volume_capability.access_mode.mode access_type = self._get_block_or_mount_volume(request) block_device_path = Utils.get_nvmesh_block_device_path( nvmesh_volume_name) if not Utils.is_nvmesh_volume_attached(nvmesh_volume_name): # run nvmesh attach locally requested_nvmesh_access_mode = Consts.AccessMode.to_nvmesh( access_mode) Utils.nvmesh_attach_volume(nvmesh_volume_name, requested_nvmesh_access_mode) try: Utils.wait_for_volume_io_enabled(nvmesh_volume_name) if access_type == Consts.VolumeAccessType.MOUNT: mount_request = volume_capability.mount self.logger.info( 'Requested Mounted FileSystem Volume with fs_type={}'. format(mount_request.fs_type)) fs_type = mount_request.fs_type or Consts.FSType.EXT4 mount_permissions, mount_options = self._parse_mount_options( mount_request) mkfs_options = volume_context.get('mkfsOptions', '') FileSystemManager.format_block_device(block_device_path, fs_type, mkfs_options) if FileSystemManager.is_mounted(staging_target_path): self.logger.warning('path {} is already mounted'.format( staging_target_path)) FileSystemManager.mount(source=block_device_path, target=staging_target_path, mount_options=mount_options) FileSystemManager.chmod( mount_permissions or Consts.DEFAULT_MOUNT_PERMISSIONS, staging_target_path) elif access_type == Consts.VolumeAccessType.BLOCK: self.logger.info('Requested Block Volume') # We do not mount here, NodePublishVolume will mount directly from the block device to the publish_path # This is because Kubernetes automatically creates a directory in the staging_path else: self.logger.info('Unknown AccessType {}'.format(access_type)) except Exception as staging_err: # Cleanup - un-mount and detach the volume try: if FileSystemManager.is_mounted(staging_target_path): FileSystemManager.umount(staging_target_path) Utils.nvmesh_detach_volume(nvmesh_volume_name) except Exception as cleanup_err: self.logger.warning( 'Failed to cleanup and detach device after attached and staging failed. Error: %s' % cleanup_err) # Re-raise the initial exception raise staging_err self.logger.debug( 'NodeStageVolume finished successfully for request: {}'.format( reqJson)) return NodeStageVolumeResponse()