Example #1
0
def process_root(service, obj, k2node):
    """Marshal k2 response (k2node) into object model"""
    if service == "web":
        k2dict = web_k2dict
        k2choice = web_k2choice
        attr_cc_to_us = web_attr_cc_to_us
        base_path = _web_base_path
    elif service == "uom":
        k2dict = uom_k2dict
        k2choice = uom_k2choice
        attr_cc_to_us = uom_attr_cc_to_us
        base_path = _uom_base_path
    else:
        msg = _("k2aclient:",
                " during k2 deserialization,"
                " unrecognized service: >%s<")
        raise ValueError(msg % service)
    if len(k2node):
        for c in k2node:
            _process_child(base_path, k2dict, k2choice, attr_cc_to_us,
                           k2node.tag, obj, 0, c)
    else:
        msg = _("k2aclient:",
                " during k2 deserialization,"
                " very, very weird, no children for k2 root"
                " element: >%s<, continuing ...")
        _logger.warning(msg % k2node.tag)
Example #2
0
def ssh_execute(ssh, cmd, process_input=None,
                addl_env=None, check_exit_code=True):
    LOG.debug(_('Running cmd (SSH): %s'), cmd)
    if addl_env:
        raise InvalidArgumentError(_('Environment not supported over SSH'))

    if process_input:
        # This is (probably) fixable if we need it...
        raise InvalidArgumentError(_('process_input not supported over SSH'))

    stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd)
    channel = stdout_stream.channel

    # NOTE(justinsb): This seems suspicious...
    # ...other SSH clients have buffering issues with this approach
    stdout = stdout_stream.read()
    stderr = stderr_stream.read()
    stdin_stream.close()

    exit_status = channel.recv_exit_status()

    # exit_status == -1 if no exit code was returned
    if exit_status != -1:
        LOG.debug(_('Result was %s') % exit_status)
        if check_exit_code and exit_status != 0:
            raise ProcessExecutionError(exit_code=exit_status,
                                        stdout=stdout,
                                        stderr=stderr,
                                        cmd=cmd)

    return (stdout, stderr)
Example #3
0
 def __init__(self, k2error, addmsg=None):
     msg = _("Timeout exception off of K2: >%s<") % k2error
     if addmsg is not None:
         msg = (_("%(msg)s, during: >%(addmsg)s<") %
                {"msg": msg,
                 "addmsg": addmsg, })
     super(K2aK2TimeoutError, self).__init__(msg)
     self.k2error = k2error
Example #4
0
 def __init__(self, e, addmsg=None):
     msg = _("Other Exception off of K2: >%s<") % e
     if addmsg is not None:
         msg = (_("%(msg)s, during: >%(addmsg)s<") %
                {"msg": msg,
                 "addmsg": addmsg, })
     super(K2aK2Other, self).__init__(msg)
     self.e = e
Example #5
0
    def emit(self, category, msg, k2response, exc=None):
        if self._broken:
            return "BROKEN"

        trcbak = traceback.format_exc()
        try:
            os.makedirs(self._logdir)
        except OSError as e:
            if e.errno == errno.EEXIST and os.path.isdir(self._logdir):
                pass
            else:
                msg = _("k2aclient:"
                        " during FFDC,"
                        " cant create k2 exception log directory:"
                        " >%s<, will disable k2 exception logging ..")
                _logger.warn(msg % self._logdir)
                self._broken = True
                return "BROKEN"

        if not os.access(self._logdir, os.W_OK):
            msg = _("k2aclient:"
                    " during FFDC,"
                    " cant write to k2 exception log directory:"
                    " >%s<, will disable k2 exception logging ..")
            _logger.warn(msg % self._logdir)
            self._broken = True
            return "BROKEN"

        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S')
        if st == self._lastst:
            self._lastseq += 1
        else:
            self._lastseq = 0
        self._lastst = st

        fn = "%s_%s-%05d" % (category, st, self._lastseq,) + ".k2r"
        diagfspec = os.path.join(self._logdir, fn)

        try:
            _dump_k2resp(msg, trcbak, exc, k2response, diagfspec)
        except Exception:
            self._broken = True
            msg = _("k2aclient:"
                    " during FFDC, "
                    " caught an exception while writing to log"
                    " directory:"
                    " >%s<, will disable k2 exception logging ..")
            _logger.exception(msg % self._logdir)
            return "BROKEN"

        self.prune()

        return diagfspec
Example #6
0
def main():
    rc = 0
    try:
        OpenStackK2Shell().main(map(strutils.safe_decode, sys.argv[1:]))
    except KeyboardInterrupt:
        msg = _("... terminating k2a client")
        print(msg, file=sys.stderr)
        rc = 130
    except Exception as e:
        logger.debug(e, exc_info=1)
        msg = _("ERROR: type: >%(type)s<, msg: >%(e)s<")
        print(msg % {"type": type(e), "e": e, }, file=sys.stderr,)
        rc = 1

    sys.exit(rc)
Example #7
0
 def __init__(self, status, k2error, addmsg=None, k2msg=None,
              diagfspec=None):
     msg = (_("K2Error off of K2: >%(msg)s<,"
              " Status: >%(status)d<, k2Error: >%(k2error)s<") %
            {"msg": self.__class__.msg,
             "status": status, "k2error": k2error, })
     if addmsg is not None:
         msg = (_("%(msg)s, during: >%(addmsg)s<") %
                {"msg": msg,
                 "addmsg": addmsg, })
     super(K2aK2Error, self).__init__(msg)
     self.status = status
     self.k2error = k2error
     self.k2msg = k2msg
     self.diagfspec = diagfspec
Example #8
0
def find_resource(manager, name_or_id):
    """Helper for the _find_* methods."""
    # first try to get entity as integer id
    try:
        if isinstance(name_or_id, int) or name_or_id.isdigit():
            return manager.get(int(name_or_id))
    except exceptions.NotFound:
        pass

    # now try to get entity as uuid
    try:
        uuid.UUID(strutils.safe_decode(name_or_id))
        return manager.get(name_or_id)
    except (ValueError, exceptions.NotFound):
        pass

    try:
        try:
            return manager.find(human_id=name_or_id)
        except exceptions.NotFound:
            pass

        # finally try to find entity by name
        try:
            return manager.find(name=name_or_id)
        except exceptions.NotFound:
            try:
                return manager.find(display_name=name_or_id)
            except (UnicodeDecodeError, exceptions.NotFound):
                try:
                    # Volumes does not have name, but display_name
                    return manager.find(display_name=name_or_id)
                except exceptions.NotFound:
                    msg = (_("k2aclient:"
                             " no %(res)s with a name or ID"
                             " of '%(name_or_id)s' exists.") %
                           {"res": manager.resource_class.__name__.lower(),
                            "name_or_id": name_or_id, })
                    raise exceptions.CommandError(msg)
    except exceptions.NoUniqueMatch:
        msg = (_("k2aclient:"
                 " multiple %(res)s matches found for '%(name_or_id)s',"
                 " use an ID to be more"
                 " specific.") %
               {"res": manager.resource_class.__name__.lower(),
                "name_or_id": name_or_id, })
        raise exceptions.CommandError(msg)
Example #9
0
    def lu_linked_clone_of_lu_bp(
        self,
        cluster,
        ssp,
        source_lu_udid,
        dest_lu_unit_name,
        dest_lu_unit_capacity=None,
        dest_lu_thin_device=None,
        dest_lu_logical_unit_type="VirtualIO_Disk",
        xa=None,
        times=None,
    ):
        """Create a new destination LU and linked_clone the source to it.
        If the underlying K2 linked_clone operation should fail, attempt to
        remove the new destination LU."""

        if times is not None:
            times.append(time.time())

        source_lu = None
        for lu in ssp.logical_units.logical_unit:
            if lu.unique_device_id == source_lu_udid:
                source_lu = lu
        if source_lu is None:
            msg = _(
                "k2aclient:"
                " during lu_linked_clone_of_lu,"
                " source_lu_udid: >%(source_lu_udid)s<,"
                " not in sharedstoragepool: >%(ssp.id)s<"
            )
            raise ValueError(msg % {"source_lu_udid": source_lu_udid, "ssp.id": ssp.id})
        if dest_lu_unit_capacity is None:
            dest_lu_unit_capacity = source_lu.unit_capacity

        if dest_lu_thin_device is None:
            dest_lu_thin = True
            if source_lu.thin_device == "false":
                dest_lu_thin = False
        elif dest_lu_thin_device:
            dest_lu_thin = True
        else:
            dest_lu_thin = False

        dest_lu, updated_ssp = ssp.update_append_lu(
            dest_lu_unit_name, dest_lu_unit_capacity, dest_lu_thin, dest_lu_logical_unit_type, xa=xa
        )

        if times is not None:
            times.append(time.time())
            times[-2] = times[-1] - times[-2]

        status, job_id = self.lu_linked_clone(cluster, source_lu_udid, dest_lu.unique_device_id, xa=xa)
        if status != "COMPLETED_OK":
            dest_lu = None

        if times is not None:
            times[-1] = time.time() - times[-1]

        return status, dest_lu, updated_ssp, job_id
def _prepdir(targetdir):
    try:
        os.makedirs(targetdir)
    except OSError as e:
        if e.errno == errno.EEXIST and os.path.isdir(targetdir):
            pass
        else:
            msg = _("during directory preparation,"
                    " cant create directory: "
                    " >%s<") % targetdir
            raise Exception(msg)

    if not os.access(targetdir, os.W_OK):
        msg = _("during directory preparation,"
                " cant write to directory: "
                " >%s<") % targetdir
        raise Exception(msg)
Example #11
0
 def readjob(self, root_id, xa=None):
     if not self._k2operator:
         self.authenticate()
     try:
         result = self._k2operator.readjob(root_id,
                                           auditmemento=xa)
     except Exception as e:
         x = exceptions.create_k2a_exception_from_k2o_exception
         addmsg = _(", during readJob")
         raise x(e, addmsg=addmsg, exclogger=self.exclogger)
     return result
    def power_on(self, logicalpartition, xa=None):
        """For specified logicalpartition, power it on

        :param logicalpartition_id: Instance of the :class:`LogicalPartition`

        """

        if self.api.client.k2operator is None:
            self.api.client.authenticate()

        jpc = k2web.JobParameter_Collection()

        jrequest = self.api.web_job.getjob(logicalpartition, "PowerOn", xa=xa)
        jrequest.job_parameters = jpc

        jresponse = self.api.web_job.runjob(logicalpartition, jrequest, xa=xa)
        k2respi = jresponse._k2resp

        while jresponse.status == "NOT_STARTED" or jresponse.status == "RUNNING":

            greenthread.sleep(1)
            jresponse = self.api.web_job.readjob(jresponse.job_id, xa=xa)

        if not jresponse.status.startswith("COMPLETED"):
            diagfspeci = self.api.exclogger.emit("JOB", "power_on", k2respi)
            diagfspec = self.api.exclogger.emit("JOB", "power_on", jresponse._k2resp)
            msg = _(
                "k2aclient:"
                " during power_on,"
                " for LogicalPartition: >%(logicalpartition.id)s<,"
                " failed to power on due to"
                " job failure,"
                " job_id: >%(jresponse.job_id)s<,"
                " status: >%(jresponse.status)s<,"
                " input K2 job diagnostics have been"
                " written to: >%(diagfspeci)s<,"
                " response k2 job diagnostics have been"
                " written to: >%(diagfspec)s<"
            )
            raise K2JobFailure(
                msg
                % {
                    "logicalpartition.id": logicalpartition.id,
                    "jresponse.job_id": jresponse.job_id,
                    "jresponse.status": jresponse.status,
                    "diagfspeci": diagfspeci,
                    "diagfspec": diagfspec,
                },
                jresponse._k2resp,
                diagfspeci=diagfspeci,
                diagfspec=diagfspec,
            )

        return jresponse.status, jresponse.job_id
Example #13
0
    def __init__(self, msg, k2resp, exclogger=None):
        diagfspec = None
        if exclogger is not None:
            diagfspec = exclogger.emit("CRUD", msg, k2resp)
            msg = (_("%(msg)s, exception diagnostics have"
                     " been written to: >%(diagfspec)s<") %
                   {"msg": msg,
                    "diagfspec": diagfspec, })

        super(K2aCrudException, self).__init__(msg)
        self.k2resp = k2resp
        self.diagfspec = diagfspec
Example #14
0
 def do_help(self, args):
     """
     Display help about this program or one of its subcommands.
     """
     if args.command:
         if args.command in self.subcommands:
             self.subcommands[args.command].print_help()
         else:
             msg = _("k2aclient:"
                     ">%s< is not a valid subcommand")
             raise exc.CommandError(msg % args.command)
     else:
         self.parser.print_help()
Example #15
0
    def authenticate(self):
        try:
            # note timeout=None is no timeout (wait forever)
            k2_conn = K2Session(self.k2_url,
                                self.k2_user,
                                self.k2_password,
                                self.k2_auditmemento,
                                timeout=self.timeout,
                                certpath=self.k2_certpath)
        except Exception as e:
            x = exceptions.create_k2a_exception_from_k2o_exception
            addmsg = _(", during authenticate")
            raise x(e, addmsg=addmsg, exclogger=self.exclogger)

        self._k2operator = K2Operator(k2_conn, use_cache=self.k2o_use_cache)
Example #16
0
def get_client_class(version):
    version_map = {
        '1': 'paxes_cinder.k2aclient.v1.client.Client',
    }
    try:
        client_path = version_map[str(version)]
    except (KeyError, ValueError):
        msg = (_("k2aclient:"
                 " while obtaining client,"
                 " invalid client version >%(version)s<."
                 " must be one of: >%(versions)s<") %
               {"version": version,
                "versions": ', '.join(version_map.keys()), })
        raise exceptions.UnsupportedVersion(msg)

    return utils.import_class(client_path)
Example #17
0
def create_k2a_exception_from_k2o_exception(e, addmsg=None, exclogger=None):
    """
    Return an instance of an K2aK2Error or subclass
    based on an requests response. Optionally add additional message.
    Optionally log details of the exception.
    """

    if not isinstance(e, K2Error):
        return K2aK2Other(e, addmsg)

    if isinstance(e, K2SSLError):
        return K2aK2SslError(e, addmsg)

    if isinstance(e, K2ConnectionError):
        return K2aK2ConnectionError(e, addmsg)

    if isinstance(e, K2TimeoutError):
        return K2aK2TimeoutError(e, addmsg)

    k2response = e.k2response
    # If non status_code then use -1
    status = -1
    if k2response is not None and k2response.status is not None:
        status = k2response.status

    # if activated place k2response in exception log
    diagfspec = None
    if (k2response is not None
            and exclogger is not None
            and status not in _EXCLUDED_EXCEPTIONS):
        category = "UNC"
        if status > -1:
            category = str(status)

        diagfspec = exclogger.emit(category, addmsg, k2response, exc=e)
        addmsg += (_(", exception diagnostics have been written to: >%s<") %
                   diagfspec)

    cls = _code_to_exception_map.get(status, K2aK2ErrorUnclassified)

    k2msg = None
    if hasattr(k2response, 'k2err'):
        m = k2response.k2err.find('./Message')
        if m is not None:
            k2msg = m.text
    return cls(status, e, addmsg, k2msg=k2msg,
               diagfspec=diagfspec)
Example #18
0
    def error(self, message):
        """error(message: string)

        Prints a usage message incorporating the message to stderr and
        exits.
        """
        self.print_usage(sys.stderr)
        #FIXME(lzyeval): if changes occur in argparse.ArgParser._check_value
        choose_from = ' (choose from'
        progparts = self.prog.partition(' ')
        msg = _("k2aclient:"
                " error: %(errmsg)s\nTry '%(mainp)s help %(subp)s'"
                " for more information.\n")
        msg = msg %\
            {'errmsg': message.split(choose_from)[0],
             'mainp': progparts[0],
             'subp': progparts[2]}
        self.exit(2, msg)
Example #19
0
def process_root(service, mode, obj):
    """Create k2node elements from object model instance"""
    if service == "web":
        ns = web_ns
        typeset = web_typeset
        k2attr = web_k2attr
    elif service == "uom":
        ns = uom_ns
        typeset = uom_typeset
        k2attr = uom_k2attr
    else:
        msg = _("k2aclient: during process_root, unrecognized service: >%s<")
        raise ValueError(msg % service)

    k2ne = _process_node(ns, typeset, k2attr, mode,
                         obj.__class__.__name__, obj,
                         "root", "co", "r", "R", "")
    return k2ne
Example #20
0
    def default(self, obj):
#         if type(obj) is types.ListType:
        if isinstance(obj, types.ListType):
            ll = []
            for li in obj:
                if li is None:
                    ll.append("Null")
                elif isinstance(li, types.StringType):
                    ll.append(li)
                elif isinstance(li, K2Resource) or \
                        isinstance(li, K2WebResource):
                    ll.append(self.default(li))
                else:
                    msg = (_("k2aclient:"
                             " during encoding"
                             " unexpected type: >%s<") %
                           li)
                    _logger.warn(msg)
#             return "["+",".join(ll)+"]"
            return ll

        elif isinstance(obj, K2Resource) or isinstance(obj, K2WebResource):
            outdct = {}
            for k, v in obj.__dict__.iteritems():
                if k[:1] == "_" and not k.startswith("_pattr_"):
                    pass
                elif k == "_pattr_metadata":
                    if hasattr(obj, "id"):
                        outdct["id"] = obj.id
                elif v is None:
#                     outdct[k] = None
                    # if not assigned, don't output
                    pass
                elif k == "group":
                    outdct[k] = v
                elif isinstance(v, types.StringType):
                    outdct[k[7:]] = v
                else:
                    outdct[k[7:]] = self.default(v)

            return outdct

        return json.JSONEncoder.default(self, obj)
Example #21
0
 def get(self, url, etag=None, xag=[], xa=None):
     if not self._k2operator:
         self.authenticate()
     # extend path w/ xag
     if len(xag) > 0:
         url += '?group=%s' % xag[0]
         if len(xag) > 1:
             for g in xag[1:len(xag)]:
                 url += ',%s' % g
     try:
         result = self._k2operator.readbypath(url,
                                              etag=etag,
                                              age=0,
                                              auditmemento=xa)
     except Exception as e:
         x = exceptions.create_k2a_exception_from_k2o_exception
         addmsg = _(", during get")
         raise x(e, addmsg=addmsg, exclogger=self.exclogger)
     return result
Example #22
0
    def delete(self,
               service,
               root_type, root_id,
               child_type=None, child_id=None,
               xa=None):

        if not self._k2operator:
            self.authenticate()
        try:
            result = self._k2operator.delete(root_type,
                                             rootId=root_id,
                                             childType=child_type,
                                             childId=child_id,
                                             service=service,
                                             auditmemento=xa)
        except Exception as e:
            x = exceptions.create_k2a_exception_from_k2o_exception
            addmsg = _(", during delete")
            raise x(e, addmsg=addmsg, exclogger=self.exclogger)
        return result
Example #23
0
def _dump_k2resp(msg, trcbak, exc, k2resp, fspec_out):

    with open(fspec_out, 'w') as fout:
        fout.write("\n********************************"
                   "***********************\n")
        if msg is not None:
            fout.write(msg)
        fout.write("\n********\nexception:\n")
        if exc is not None:
            fout.write(_("%s") % exc)
        else:
            fout.write("No exception")
        fout.write("\n********\ntraceback:\n")
        fout.write(trcbak)
        if k2resp is not None:
            fout.write("\n********\nrequest:\n")
            request = {}
            request["method"] = k2resp.reqmethod
            request["path"] = k2resp.reqpath
            request["headers"] = k2resp.reqheaders
    #                     request["body"] = k2r.reqbody
            json.dump(request, fout, sort_keys=True, indent=4)
            fout.write("\n****\nrequest.body:\n")
            try:
                try:
                    xml = minidom.parseString(k2resp.reqbody)
                    fout.write(xml.toprettyxml(indent=' ' * 4))
                except:
                    fout.write(k2resp.reqbody)
            except Exception as e:
                fout.write("Cant output request.body, because: >%s<" % e)
            fout.write("\n********\nresponse:\n")
            response = {}
            response["status_code"] = k2resp.status
            response["reason"] = k2resp.reason
            response["headers"] = dict(k2resp.headers)
    #                     response["body"] = k2r.body
            json.dump(response, fout, sort_keys=True, indent=4)
            fout.write("\n****\nresponse.body:\n")
            fout.write(k2resp.body)
Example #24
0
    def upload(self,
               wf,
               filelike,
               xa=None):
        """Upload a file to the HMC.

        filelike  a file-like object."""

        oper = self.api.client.k2operator
        fd = wf.k2resp.entry.element
        try:
            k2resp = oper.uploadfile(fd, filelike, auditmemento=xa)
        except K2Error as e:
            msg = _("k2aclient:"
                    " during file upload,"
                    " filename: >%(wf.filename)s<, id:"
                    " >%(wf.id)s<, file_uuid: >%(wf.file_uuid)s<,"
                    " file_enum_type: >%(wf.file_enum_type)s<")
            addmsg = msg % {"wf.filename": wf.filename, "wf.id": wf.id,
                            "wf.file_uuid": wf.file_uuid,
                            "wf.file_enum_type": wf.file_enum_type, }
            x = exceptions.create_k2a_exception_from_k2o_exception
            raise x(e, addmsg=addmsg, exclogger=self.api.exclogger)
        return k2resp
Example #25
0
def execute(*cmd, **kwargs):
    """Helper method to shell out and execute a command through subprocess.

    Allows optional retry.

    :param cmd:             Passed to subprocess.Popen.
    :type cmd:              string
    :param process_input:   Send to opened process.
    :type proces_input:     string
    :param check_exit_code: Single bool, int, or list of allowed exit
                            codes.  Defaults to [0].  Raise
                            :class:`ProcessExecutionError` unless
                            program exits with one of these code.
    :type check_exit_code:  boolean, int, or [int]
    :param delay_on_retry:  True | False. Defaults to True. If set to True,
                            wait a short amount of time before retrying.
    :type delay_on_retry:   boolean
    :param attempts:        How many times to retry cmd.
    :type attempts:         int
    :param run_as_root:     True | False. Defaults to False. If set to True,
                            the command is prefixed by the command specified
                            in the root_helper kwarg.
    :type run_as_root:      boolean
    :param root_helper:     command to prefix to commands called with
                            run_as_root=True
    :type root_helper:      string
    :param shell:           whether or not there should be a shell used to
                            execute this command. Defaults to false.
    :type shell:            boolean
    :returns:               (stdout, stderr) from process execution
    :raises:                :class:`UnknownArgumentError` on
                            receiving unknown arguments
    :raises:                :class:`ProcessExecutionError`
    """

    process_input = kwargs.pop('process_input', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = False
    delay_on_retry = kwargs.pop('delay_on_retry', True)
    attempts = kwargs.pop('attempts', 1)
    run_as_root = kwargs.pop('run_as_root', False)
    root_helper = kwargs.pop('root_helper', '')
    shell = kwargs.pop('shell', False)

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]

    if kwargs:
        raise UnknownArgumentError(_('Got unknown keyword args '
                                     'to utils.execute: %r') % kwargs)

    if run_as_root and hasattr(os, 'geteuid') and os.geteuid() != 0:
        if not root_helper:
            raise NoRootWrapSpecified(
                message=('Command requested root, but did not specify a root '
                         'helper.'))
        cmd = shlex.split(root_helper) + list(cmd)

    cmd = map(str, cmd)

    while attempts > 0:
        attempts -= 1
        try:
            LOG.debug(_('Running cmd (subprocess): %s'), ' '.join(cmd))
            _PIPE = subprocess.PIPE  # pylint: disable=E1101

            if os.name == 'nt':
                preexec_fn = None
                close_fds = False
            else:
                preexec_fn = _subprocess_setup
                close_fds = True

            obj = subprocess.Popen(cmd,
                                   stdin=_PIPE,
                                   stdout=_PIPE,
                                   stderr=_PIPE,
                                   close_fds=close_fds,
                                   preexec_fn=preexec_fn,
                                   shell=shell)
            result = None
            if process_input is not None:
                result = obj.communicate(process_input)
            else:
                result = obj.communicate()
            obj.stdin.close()  # pylint: disable=E1101
            _returncode = obj.returncode  # pylint: disable=E1101
            if _returncode:
                LOG.debug(_('Result was %s') % _returncode)
                if not ignore_exit_code and _returncode not in check_exit_code:
                    (stdout, stderr) = result
                    raise ProcessExecutionError(exit_code=_returncode,
                                                stdout=stdout,
                                                stderr=stderr,
                                                cmd=' '.join(cmd))
            return result
        except ProcessExecutionError:
            if not attempts:
                raise
            else:
                LOG.debug(_('%r failed. Retrying.'), cmd)
                if delay_on_retry:
                    greenthread.sleep(random.randint(20, 200) / 100.0)
        finally:
            # NOTE(termie): this appears to be necessary to let the subprocess
            #               call clean something up in between calls, without
            #               it two execute calls in a row hangs the second one
            greenthread.sleep(0)
Example #26
0
def _process_child(base_path, k2dict, k2choice, attr_cc_to_us, parent_tag,
                   parentobj, level,
                   childk2node, choicetag=None):
    """Recursively turn k2 output in object model"""

    _logger.debug("start processing child at level: >%d<, parent_tag: >%s<",
                  level, parent_tag)

    us_tag = convert_cc_to_us(attr_cc_to_us, childk2node.tag)
    us_tag_ass = "_pattr_" + us_tag

    if choicetag is not None:
        us_tag = convert_cc_to_us(attr_cc_to_us, choicetag)
        us_tag_ass = "_pattr_" + us_tag
#     _logger.debug("childk2node.tag: >%s<, us_tag: >%s<",
#                   childk2node.tag, us_tag,)

    if not hasattr(parentobj, us_tag_ass):
        msg = _("k2aclient:"
                " during k2 deserialization,"
                " for type: >%(pcname)s<, k2 gave unknown attr:"
                " >%(us_tag)s<, is there a schema mismatch?,"
                " will skip ...")
        _logger.warning(msg % {"pcname": parentobj.__class__.__name__,
                               "us_tag": us_tag, })
        return

    at = getattr(parentobj, us_tag_ass)

    # check if leaf node
    if len(childk2node) == 0:
        if not isinstance(at, types.ListType):
            s = getattr(parentobj, us_tag_ass, childk2node.text)
            if s is not None:
                msg = _("k2aclient:"
                        " during k2 deserialization,"
                        " for type: >%(pcname)s<,"
                        " k2 attr: >%(us_tag)s<: is listed"
                        " as scalar, but maybe its not?,"
                        " is there a schema mismatch?, will replace"
                        " existing scalar w/ next value ...")
                _logger.warning(msg % {"pcname": parentobj.__class__.__name__,
                                       "us_tag": us_tag, })

            # Empirical observation: if has an href it is a link
            if "href" in childk2node.attrib:
#                 _logger.debug("child is leaf scalar href node")
                setattr(parentobj, us_tag_ass, childk2node.attrib["href"])
            else:
#                 _logger.debug("child is leaf scalar text node")
                setattr(parentobj, us_tag_ass, childk2node.text)
        else:
            l = getattr(parentobj, us_tag_ass)

            # Empirical observation: if has an href it is a link
            if "href" in childk2node.attrib:
#                 _logger.debug("child is leaf list href node")
                l.append(childk2node.attrib["href"])
            else:
#                 _logger.debug("child is leaf list text node")
                l.append(childk2node.text)
#         print "return from leaf: level: >%d<" % (level,)
        return

    # instantiate object and populate w/ k2 data
    ctype = childk2node.tag

    #####
    # check for choice
    _logger.debug("not leaf")
    if parent_tag in k2choice and ctype in k2choice[parent_tag]:
#         _logger.debug("child is choice, so recurse")
        choicetype = childk2node[0].tag
        _process_child(base_path, k2dict, k2choice, attr_cc_to_us, choicetype,
                       parentobj,
                       level + 1, childk2node[0], choicetag=ctype)
        return

    if parent_tag in k2dict and ctype in k2dict[parent_tag]:
#         _logger.debug("childobject: >%s< -> >%s<", ctype,
#                       k2dict[parent_tag][ctype])
        ctype = k2dict[parent_tag][ctype]
    else:
#         _logger.debug("childobject: >%s< -> >%s<", ctype, ctype)
        pass
    C = _get_class(base_path + ctype)

    if C is None:
        msg = _("k2aclient:"
                " during k2 deserialization,"
                " while processing: k2 type: >%(parent_tag)s<,"
                " there is no definition for k2 type: >%(k2type)s<,"
                " is there a schema mismatch?,"
                " will skip ...")
        _logger.warning(msg % {"parent_tag": parent_tag,
                               "k2type": base_path + childk2node.tag, })
        return

    ####
    # instantiate child object
    childobj = C()

    # if present record XAG group
    if "group" in childk2node.attrib:
        childobj.group = childk2node.attrib["group"]

    if not isinstance(at, types.ListType):
#         msg = ("for parent attribute: >%s<, set new child object as scalar")
#         _logger.debug(msg, us_tag_ass)
        setattr(parentobj, us_tag_ass, childobj)
    else:
#         msg = ("for parent attribute: >%s<, set new child object as list")
#         _logger.debug(msg, us_tag_ass)
        l = getattr(parentobj, us_tag_ass)
        l.append(childobj)

    for c in childk2node:
#         _logger.debug("recurse on child")
        _process_child(base_path, k2dict, k2choice, attr_cc_to_us, ctype,
                       childobj, level + 1, c)
Example #27
0
 def setFailedWithException(self, exception, message, params, response):
     self._status = Status.FAILED_WITH_EXCEPTION
     self._exception = _("%s") % exception
     self._message = message
     self._params = params
     self._response = response
Example #28
0
    def setFailedWithException(self, exception, message, params, response):
        self._status = Status.FAILED_WITH_EXCEPTION
        self._exception = _("%s") % exception
        self._message = message
        self._params = params
        self._response = response

    def setFailedWithUnexpectedResult(self, message, params, response):
        self._status = Status.FAILED_WITH_UNEXPECTED_RESULT
        self._message = message
        self._params = params
        self._response = response

MESSAGES = {
    "MSG010": _("SharedStoragePool: While attempting to access Cluster: >%s<, "
    "unable to connect to HMC: >%s<"),
    "MSG020": _("Check the HMC and its configuration."),
    "MSG030": _("SharedStoragePool: Able to connect to HMC: >%s< "
    "for Cluster: >%s<"),
    "MSG040": _("SharedStoragePool: Couldn't access Cluster: >%s<"),
    "MSG050": _("Check the PowerVC SharedStoragePool configuration"),
    "MSG060": _("SharedStoragePool: Couldn't access SharedStoragePool "
    "in Cluster: >%s<"),
    "MSG070": _("Diagnose and repair Cluster"),
    "MSG080": _("SharedStoragePool: Successfully accessed SharedStoragePool "
    "in Cluster: >%s<"),
    "MSG090": _("SharedStoragePool: Unable to create LogicalUnits "
    "in Cluster: >%s<"),
    "MSG100": _("Diagnose and repair Cluster"),
    "MSG110": _("SharedStoragePool: LogicalUnit creation functional "
    "in Cluster: >%s<"),
Example #29
0
    print_k2_list(virtualswitchs, ['switch_name',
                                   'id',
                                   'switch_id',
                                   'switch_mode'])


# Cluster
def do_cluster_list(cs, args):
    """Output a list of cluster."""
    cluster_list = cs.cluster.list()
    _print_cluster_list(cluster_list)


@utils.arg('cluster',
           metavar='<cluster>',
           help=_('Id of the cluster.'))
def do_cluster_show(cs, args):
    """Output details for a specific cluster."""
    cluster = cs.cluster.get(args.cluster)
    json.dump(cluster, sys.stdout, sort_keys=True, indent=4, cls=K2Encoder)
    print ("\n")


@utils.arg('cluster',
           metavar='<cluster_id>',
           help=_('UUID of the cluster containing the LUs.'))
@utils.arg('source',
           metavar='<source_udid>',
           help=_('UDID of the source logicalunit.'))
@utils.arg('destination',
           metavar='<destination_udid>',
    def update_append_lus(self, ssp, inlus, xa=None):
        """For specified sharedstoragepool, add logicalunits
        :param ssp: The :class:`SharedStoragePool`.
        :param inlus: One or more tuples (unitname, unitcapacity, thin, type,
                                          clonedfrom)
            :unitname: The UnitName of the new :class:`LogicalUnit`
            :unitcapacity: The UnitCapicty of the new :class:`LogicalUnit`
            :thin: Boolean indicating thin or thick
            :type: String indicating LU type
                VirtualIO_Disk
                VirtualIO_Hibernation
                VirtualIO_Image
                VirtualIO_Active_Memory_Sharing
            :clonedfrom: The udid of the source disk, None if not cloned
        :rtype: (new list of :class:`LogicalUnit, u :class:`SharedStoragePool)
        """

        if not ssp.k2resp_isfor_k2entry:
            msg = _("k2aclient:"
                    " during updated_append_lus,"
                    " cannot update K2 object(s)"
                    " obtained from a list operation,"
                    " for ssp: >%s<")
            raise ValueError(msg % ssp.id)

        # no logical units specified
        if len(inlus) == 0:
            return (ssp, [])

        # check values permitting thin to default
        lus = []
        for inlu in inlus:
            leninlu = len(inlu)
            if leninlu == 2:
                l0 = inlu[0]
                l1 = str(inlu[1])
                l2 = "true"
                l3 = "VirtualIO_Disk"
                l4 = None
            elif leninlu == 3:
                l0 = inlu[0]
                l1 = str(inlu[1])
                l2 = "false"
                if inlu[2]:
                    l2 = "true"
                l3 = "VirtualIO_Disk"
                l4 = None
            elif leninlu == 4:
                l0 = inlu[0]
                l1 = str(inlu[1])
                l2 = "false"
                if inlu[2]:
                    l2 = "true"
                l3 = inlu[3]
                l4 = None
            elif leninlu == 5:
                l0 = inlu[0]
                l1 = str(inlu[1])
                l2 = "false"
                if inlu[2]:
                    l2 = "true"
                l3 = inlu[3]
                l4 = inlu[4]
            else:
                msg = _("k2aclient:"
                        " during updated_append_lus,"
                        " wrong length of lu input tuple: >%d<")
                raise ValueError(msg % len(inlu))
            lus.append((l0, l1, l2, l3, l4))

        # if  necessary, authenticate
        if self.api.client.k2operator is None:
            self.api.client.authenticate()

        # prime the update logic
        prev_ssp = ssp
#         # following line is work around for SW222953 SW239525
#         prev_ssp = self.get(ssp.id)

        attempts = 0
        tryagain = True
        while tryagain:
            tryagain = False
            # function-specific begin
            # track lus to find what was added
            lus_for_update = prev_ssp.logical_units.logical_unit
            existing_unit_names = [lu.unit_name for lu in lus_for_update]

#  SW239525
#             existing_luids = []
#             for lu in prev_ssp.logical_units.logical_unit:
#                 print "XXXX", lu.unit_name, lu.unique_device_id
#                 existing_luids.append(lu.unique_device_id)

            # add new LUs to existing LUs
            new_unit_names = []
            for unitname, unitcapacity, td, lut, cf in lus:
                if unitname in existing_unit_names:
                    msg = _("k2aclient: "
                            " during updated_append_lus,"
                            " request to add LU unit_name: >%s< failed,"
                            " name must be unique")
                    raise ValueError(msg % unitname)
                new_unit_names.append(unitname)

                new_lu = k2uom.LogicalUnit()
                new_lu.unit_name = unitname
                new_lu.unit_capacity = unitcapacity
                new_lu.thin_device = td
                new_lu.logical_unit_type = lut
                if cf is not None:
                    new_lu.cloned_from = cf
                lus_for_update.append(new_lu)

            attempts += 1
            try:
                msg = ("k2aclient:"
                       " during updated_append_lus,"
                       " adding LUs named: >%s<,"
                       " add attempt #: >%d<"
                       " max retries: >%d<")
                _logger.debug(msg,
                              ",".join([lu[0] for lu in lus]),
                              attempts,
                              self.api.retries)
                updated_ssp = self.update(prev_ssp, xa=xa)
#                 print krl.emit("OK", "RETRY", updated_ssp.k2resp)  SW239525

            except exceptions.K2aK2ErrorPreConditionFailed:
                if attempts > self.api.retries:
                    msg = _("k2aclient:"
                            " during updated_append_lus,"
                            " while adding LUs named: >%(lunames)s<, "
                            " # attempts due to HTTP 412 exceeded:"
                            " >%(self.api.retries)d<")
                    _logger.warn(msg % {"lunames": ",".join([lu[0]
                                                             for lu in lus]),
                                        "self.api.retries": self.api.retries, }
                                 )
                    raise
                # get a more recent ssp for retry
                prev_ssp = self.get(prev_ssp.id, xa=xa)
                msg = ("k2aclient:"
                       " during updated_append_lus,"
                       " while adding LUs named: >%s<, "
                       " for attempt #: >%d<, "
                       " received HTTP 412, will retry ...")
                lunames = ",".join([lu[0] for lu in lus])
                _logger.debug(msg, lunames, attempts)
#                 print "SW239525 TRYAGAIN: >%d<" %(attempts,)
                tryagain = True
            except Exception:
                raise

        # compare previous to updated
        new_lus = []
        lustoadd = len(lus)
        for lu in updated_ssp.logical_units.logical_unit:
#             print "YYYY", lu.unit_name, lu.unique_device_id  SW239525
            if lu.unit_name in new_unit_names:
                new_lus.append(lu)
                lustoadd -= 1

        if lustoadd != 0:
            lunames = ",".join(x[0] for x in lus)
            if lustoadd > 0:
                msg = _("k2aclient:"
                        " during updated_append_lus,"
                        " cant handle K2 response,"
                        " K2 reported successful"
                        " creation of >%(lenlus)d< "
                        " lus named: >%(lunames)s<, "
                        " attempts: >%(attempts)d< "
                        " but only >%(lustoadd)d< LUs were found.")
                x = exceptions.K2aCrudException
                raise x(msg
                        % {"lenlus": len(lus), "lunames": lunames,
                           "attempts": attempts,
                           "lustoadd": abs(lustoadd), },
                        updated_ssp.k2resp,
                        exclogger=self.api.exclogger)
            else:
                msg = _("k2aclient:"
                        " during updated_append_lus,"
                        " cant handle K2 response,"
                        " K2 reported successful"
                        " creation of >%(lenlus)d<"
                        " lus named: >%(lunames)s<,"
                        " attempts: >%(attempts)d<"
                        " but >%(lustoadd)d< extra LUs were returned.")
                x = exceptions.K2aCrudException
                raise x(msg %
                        {"lenlus": len(lus), "lunames": lunames,
                         "attempts": attempts, "lustoadd": abs(lustoadd), },
                        updated_ssp.k2resp,
                        exclogger=self.api.exclogger)
        msg = ("k2aclient:"
               " during updated_append_lus,"
               " for LUs named: >%s<"
               " after attempt # >%d<,"
               " success, udids: >%s<")

        _logger.debug(msg % (", ".join(x[0] for x in lus),
                             attempts,
                             ", ".join(lu.unique_device_id
                                       for lu in new_lus),))

        return (new_lus, updated_ssp)