Beispiel #1
0
def define_clients():
    """
    define the clients (nodeACLs) to the gateway definition
    """

    # Client configurations (NodeACL's)
    for client_iqn in config.config['clients']:
        client_metadata = config.config['clients'][client_iqn]
        client_chap = CHAP(client_metadata['auth']['chap'])

        image_list = client_metadata['luns'].keys()

        chap_str = client_chap.chap_str
        if client_chap.error:
            logger.debug("Password decode issue : "
                         "{}".format(client_chap.error_msg))
            halt("Unable to decode password for "
                 "{}".format(client_iqn))

        client = GWClient(logger,
                          client_iqn,
                          image_list,
                          chap_str)

        client.manage('present')  # ensure the client exists
Beispiel #2
0
def _update_client(**kwargs):
    """
    Handler function to apply the changes to a specific client definition
    :param args:
    """
    # convert the comma separated image_list string into a list for GWClient
    if kwargs['images']:
        image_list = str(kwargs['images']).split(',')
    else:
        image_list = []

    client = GWClient(logger, kwargs['client_iqn'], image_list, kwargs['chap'])

    if client.error:
        logger.error("Invalid client request - {}".format(client.error_msg))
        return 400, "Invalid client request"

    client.manage('present', committer=kwargs['committing_host'])
    if client.error:
        logger.error("client update failed on {} : "
                     "{}".format(kwargs['client_iqn'], client.error_msg))
        return 500, "Client update failed"
    else:
        config.refresh()
        return 200, "Client configured successfully"
Beispiel #3
0
def manage_client(client_iqn):
    """
    Manage a client definition to the local gateway
    Internal Use ONLY
    :param client_iqn: iscsi name for the client
    **RESTRICTED**
    """

    if request.method == 'GET':

        if client_iqn in config.config['clients']:
            return jsonify(config.config["clients"][client_iqn]), 200
        else:
            return jsonify(message="Client does not exist"), 404

    elif request.method == 'PUT':

        try:
            valid_iqn = normalize_wwn(['iqn'], client_iqn)
        except RTSLibError:
            return jsonify(message="'{}' is not a valid name for "
                                   "iSCSI".format(client_iqn)), 400

        committing_host = request.form['committing_host']

        image_list = request.form.get('image_list', '')

        chap = request.form.get('chap', '')

        status_code, status_text = _update_client(client_iqn=client_iqn,
                                                  images=image_list,
                                                  chap=chap,
                                                  committing_host=committing_host)

        logger.debug("client create: {}".format(status_code))
        logger.debug("client create: {}".format(status_text))
        return jsonify(message=status_text), status_code

    else:
        # DELETE request
        committing_host = request.form['committing_host']

        # Make sure the delete request is for a client we have defined
        if client_iqn in config.config['clients'].keys():
            client = GWClient(logger, client_iqn, '', '')
            client.manage('absent', committer=committing_host)

            if client.error:
                logger.error("Failed to remove client : "
                             "{}".format(client.error_msg))
                return jsonify(message="Failed to remove client"), 500

            else:
                if committing_host == this_host():
                    config.refresh()

                return jsonify(message="Client deleted ok"), 200
        else:
            logger.error("Delete request for non existent client!")
            return jsonify(message="Client does not exist!"), 404
def ansible_main():

    fields = {
        "client_iqn": {
            "required": True,
            "type": "str"
        },
        "image_list": {
            "required": True,
            "type": "str"
        },
        "chap": {
            "required": True,
            "type": "str"
        },
        "state": {
            "required": True,
            "choices": ['present', 'absent'],
            "type": "str"
        },
    }

    module = AnsibleModule(argument_spec=fields, supports_check_mode=False)

    client_iqn = module.params['client_iqn']

    if module.params['image_list']:
        image_list = module.params['image_list'].split(',')
    else:
        image_list = []

    chap = module.params['chap']
    desired_state = module.params['state']

    logger.info("START - Client configuration started : {}".format(client_iqn))

    # The client is defined using the GWClient class. This class handles client attribute updates,
    # rados configuration object updates and LIO settings. Since the logic is external to this
    # custom module, clients can be created/deleted by other methods in the same manner.
    client = GWClient(logger, client_iqn, image_list, chap)
    if client.error:
        module.fail_json(msg=client.error_msg)

    client.manage(desired_state)
    if client.error:
        module.fail_json(msg=client.error_msg)

    logger.info(
        "END   - Client configuration complete - {} changes made".format(
            client.change_count))

    changes_made = True if client.change_count > 0 else False

    module.exit_json(changed=changes_made,
                     meta={
                         "msg":
                         "Client definition completed {} "
                         "changes made".format(client.change_count)
                     })
Beispiel #5
0
    def update_client(self, client_iqn, image_list):

        client = GWClient(self.logger, client_iqn, image_list, '')
        client.manage('reconfigure')

        # grab the client's metadata from the config (needed by setup_luns)
        client.metadata = self.config.config['clients'][client_iqn]
        client.setup_luns()

        if client.error:
            self._set_error(client.error_msg)
Beispiel #6
0
def ansible_main():

    fields = {
        "client_iqn": {"required": True, "type": "str"},
        "image_list": {"required": True, "type": "str"},
        "chap": {"required": True, "type": "str"},
        "state": {
            "required": True,
            "choices": ['present', 'absent'],
            "type": "str"
        },
    }

    module = AnsibleModule(argument_spec=fields,    # noqa F405
                           supports_check_mode=False)

    client_iqn = module.params['client_iqn']

    if module.params['image_list']:
        image_list = module.params['image_list'].split(',')
    else:
        image_list = []

    chap = module.params['chap']
    desired_state = module.params['state']

    logger.info("START - Client configuration started : {}".format(client_iqn))

    # The client is defined using the GWClient class. This class handles
    # client attribute updates, rados configuration object updates and LIO
    # settings. Since the logic is external to this custom module, clients
    # can be created/deleted by other methods in the same manner.
    client = GWClient(logger, client_iqn, image_list, chap)
    if client.error:
        module.fail_json(msg=client.error_msg)

    client.manage(desired_state)
    if client.error:
        module.fail_json(msg=client.error_msg)

    logger.info("END   - Client configuration complete - {} "
                "changes made".format(client.change_count))

    changes_made = True if client.change_count > 0 else False

    module.exit_json(changed=changes_made,
                     meta={"msg": "Client definition completed {} "
                                  "changes made".format(client.change_count)})
Beispiel #7
0
    def activate(self):
        disk = self.config.config['disks'].get(self.config_key, None)
        if not disk:
            raise CephiSCSIError("Image {} not found.".format(self.image))

        wwn = disk.get('wwn', None)
        if not wwn:
            raise CephiSCSIError("LUN {} missing wwn".format(self.image))

        # re-add backend storage object
        so = self.lio_stg_object()
        if not so:
            self.add_dev_to_lio(wwn)
            if self.error:
                raise CephiSCSIError("LUN activate failure - {}".format(
                    self.error_msg))

        # re-add LUN to target
        local_gw = this_host()
        targets_items = [
            item for item in self.config.config['targets'].items()
            if self.config_key in item[1]['disks']
            and local_gw in item[1]['portals']
        ]
        for target_iqn, target in targets_items:
            ip_list = target['ip_list']

            # Add the mapping for the lun to ensure the block device is
            # present on all TPG's
            gateway = GWTarget(self.logger, target_iqn, ip_list)

            gateway.manage('map')
            if gateway.error:
                raise CephiSCSIError("LUN mapping failed - {}".format(
                    gateway.error_msg))

            # re-map LUN to hosts
            client_err = ''
            for client_iqn in target['clients']:
                client_metadata = target['clients'][client_iqn]
                if client_metadata.get('group_name', ''):
                    continue

                image_list = list(client_metadata['luns'].keys())
                if self.config_key not in image_list:
                    continue

                client_chap = CHAP(client_metadata['auth']['chap'])
                chap_str = client_chap.chap_str
                if client_chap.error:
                    raise CephiSCSIError("Password decode issue : "
                                         "{}".format(client_chap.error_msg))

                client_chap_mutual = CHAP(
                    client_metadata['auth']['chap_mutual'])
                chap_mutual_str = client_chap_mutual.chap_str
                if client_chap_mutual.error:
                    raise CephiSCSIError("Password decode issue : "
                                         "{}".format(
                                             client_chap_mutual.error_msg))

                client = GWClient(self.logger, client_iqn, image_list,
                                  chap_str, chap_mutual_str, target_iqn)
                client.manage('present')
                if client.error:
                    client_err = "LUN mapping failed {} - {}".format(
                        client_iqn, client.error_msg)

            # re-map LUN to host groups
            for group_name in target['groups']:
                host_group = target['groups'][group_name]
                members = host_group.get('members')
                disks = host_group.get('disks').keys()
                if self.config_key not in disks:
                    continue

                group = Group(self.logger, target_iqn, group_name, members,
                              disks)
                group.apply()
                if group.error:
                    client_err = "LUN mapping failed {} - {}".format(
                        group_name, group.error_msg)

            if client_err:
                raise CephiSCSIError(client_err)