def run(self, target): normalize(target, { 'enabled': True, 'user': None, 'secret': None, 'mutual_user': None, 'mutual_secret': None }) # if iscsid service is not enabled, enable it now. service_state = self.dispatcher.call_sync('service.query', [('name', '=', 'iscsid')], {'single': True}) if service_state['state'] != 'RUNNING': config = service_state['config'] config['enable'] = True self.run_subtask_sync('service.update', service_state['id'], {'config': config}) id = self.datastore.insert('iscsi_initiator.targets', target) ctx = iscsi.ISCSIInitiator() session = iscsi_convert_session(target) ctx.add_session(session) self.dispatcher.emit_event('disk.iscsi.target.changed', { 'operation': 'create', 'ids': [id] }) return id
def run(self, id): target = self.datastore.get_by_id('iscsi_initiator.targets', id) if not target: raise TaskException(errno.ENOENT, 'Target {0} doesn\'t exist'.format(id)) self.datastore.delete('iscsi_initiator.targets', id) ctx = iscsi.ISCSIInitiator() session = first_or_default(lambda s: s.config.target == target['name'], ctx.sessions) if session: ctx.remove_session(session) self.dispatcher.emit_event('disk.iscsi.target.changed', { 'operation': 'delete', 'ids': [id] })
def _init(dispatcher, plugin): plugin.register_schema_definition('IscsiTarget', { 'type': 'object', 'additionalProperties': False, 'properties': { 'id': {'type': 'string'}, 'address': {'type': 'string'}, 'name': {'type': 'string'}, 'enabled': {'type': 'boolean'}, 'user': {'type': ['string', 'null']}, 'secret': {'type': ['string', 'null']}, 'mutual_user': {'type': ['string', 'null']}, 'mutual_secret': {'type': ['string', 'null']}, 'status': {'$ref': 'IscsiTargetStatus'} } }) plugin.register_schema_definition('IscsiTargetStatus', { 'type': 'object', 'additionalProperties': False, 'readOnly': True, 'properties': { 'connected': {'type': 'boolean'}, 'status': {'type': 'string'}, } }) def on_session_update(args): target = dispatcher.datastore.query('iscsi_initiator.targets', ('name', '=', args['target']), single=True) if target: dispatcher.emit_event('disk.iscsi.target.changed', { 'operation': 'update', 'ids': [target['id']] }) plugin.register_provider('disk.iscsi.target', ISCSITargetProvider) plugin.register_event_type('disk.iscsi.target.changed') plugin.register_task_handler('disk.iscsi.target.create', ISCSITargetCreateTask) plugin.register_task_handler('disk.iscsi.target.update', ISCSITargetUpdateTask) plugin.register_task_handler('disk.iscsi.target.delete', ISCSITargetDeleteTask) plugin.register_event_handler('iscsi.session.update', on_session_update) ctx = iscsi.ISCSIInitiator() for i in dispatcher.datastore.query_stream('iscsi_initiator.targets'): session = iscsi_convert_session(i) ctx.add_session(session)
def query(self, filter=None, params=None): ctx = iscsi.ISCSIInitiator() sessions = self.dispatcher.threaded(lambda: list(ctx.sessions)) def extend(obj): ses = first_or_default(lambda s: s.config.target == obj['name'], sessions) obj['status'] = { 'connected': ses.connected if ses else False, 'status': ses.reason if ses else 'Unknown' } return obj return self.datastore.query_stream('iscsi_initiator.targets', *(filter or []), callback=extend, **(params or {}))
def run(self, id, updated_params): target = self.datastore.get_by_id('iscsi_initiator.targets', id) if not target: raise TaskException(errno.ENOENT, 'Target {0} doesn\'t exist'.format(id)) target.update(updated_params) self.datastore.update('iscsi_initiator.targets', id, target) ctx = iscsi.ISCSIInitiator() session = first_or_default(lambda s: s.config.target == target['name'], ctx.sessions) session = iscsi_convert_session(target, session) if session.id: ctx.modify_session(session) else: ctx.add_session(session) self.dispatcher.emit_event('disk.iscsi.target.changed', { 'operation': 'update', 'ids': [id] })
def _cleanup(dispatcher, plugin): ctx = iscsi.ISCSIInitiator() for i in ctx.sessions: ctx.remove_session(i)