src/modules/client/history.py
author Yiteng Zhang <yiteng.zhang@oracle.com>
Wed, 01 Jul 2015 16:20:01 -0700
changeset 3234 3a90dc0b66c9
parent 3171 525f5bdb3f62
child 3245 d04bb3ca0128
permissions -rw-r--r--
21188662 use six library for python 3 migration
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
     1
#!/usr/bin/python
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     2
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     3
# CDDL HEADER START
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     4
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     5
# The contents of this file are subject to the terms of the
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     6
# Common Development and Distribution License (the "License").
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     7
# You may not use this file except in compliance with the License.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     8
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     9
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    10
# or http://www.opensolaris.org/os/licensing.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    11
# See the License for the specific language governing permissions
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    12
# and limitations under the License.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    13
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    14
# When distributing Covered Code, include this CDDL HEADER in each
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    15
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    16
# If applicable, add the following below this CDDL HEADER, with the
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    17
# fields enclosed by brackets "[]" replaced with your own identifying
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    18
# information: Portions Copyright [yyyy] [name of copyright owner]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    19
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    20
# CDDL HEADER END
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    21
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    22
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    23
#
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
    24
# Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    25
#
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    26
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
    27
import copy
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    28
import errno
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    29
import os
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    30
import shutil
3234
3a90dc0b66c9 21188662 use six library for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3171
diff changeset
    31
import six
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    32
import sys
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
    33
import traceback
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    34
import xml.dom.minidom as xmini
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    35
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    36
import pkg
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    37
import pkg.client.api_errors as apx
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    38
import pkg.client.bootenv as bootenv
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    39
import pkg.fmri as fmri
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    40
import pkg.misc as misc
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    41
import pkg.portable as portable
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    42
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
    43
# Constants for the (outcome, reason) combination for operation result
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
    44
# and reason.  The first field, 'outcome' should be a single word to allow easy
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
    45
# extraction from 'pkg history' but 'reason' may be a phrase.
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    46
# Indicates that the user canceled the operation.
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
    47
RESULT_CANCELED = ["Canceled", "None"]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    48
# Indicates that the operation had no work to perform or didn't need to make
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    49
# any changes to the image.
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
    50
RESULT_NOTHING_TO_DO = ["Ignored", "Nothing to do"]
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    51
# Indicates that the operation succeeded.
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
    52
RESULT_SUCCEEDED = ["Succeeded", "None"]
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    53
# Indicates that the user or client provided bad information which resulted in
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    54
# operation failure.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    55
RESULT_FAILED_BAD_REQUEST = ["Failed", "Bad Request"]
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    56
# Indicates that the operation failed due to a configuration error (such as an
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    57
# invalid SSL Certificate, etc.).
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    58
RESULT_FAILED_CONFIGURATION = ["Failed", "Configuration"]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    59
# Indicates that the operation failed due to package constraints or because of
1829
e581a205ec02 14907 references to old-style package names must be purged
Danek Duvall <danek.duvall@sun.com>
parents: 1710
diff changeset
    60
# a restriction enforced by the client (e.g. pkg(5) out of date).
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    61
RESULT_FAILED_CONSTRAINED = ["Failed", "Constrained"]
1710
139720e2e756 1668 image locking needed to prevent race conditions
Shawn Walker <srw@sun.com>
parents: 1658
diff changeset
    62
# Indicates an operation failed because the image was already in use.
139720e2e756 1668 image locking needed to prevent race conditions
Shawn Walker <srw@sun.com>
parents: 1658
diff changeset
    63
RESULT_FAILED_LOCKED = ["Failed", "Locked"]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    64
# Indicates that a search operation failed.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    65
RESULT_FAILED_SEARCH = ["Failed", "Search"]
1658
49b8cc06eecb 13360 client can traceback if corrupt catalogs exist
Shawn Walker <srw@sun.com>
parents: 1540
diff changeset
    66
# Indicates that there was a problem reading, writing, or accessing a file.
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    67
RESULT_FAILED_STORAGE = ["Failed", "Storage"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    68
# Indicates that a transport error caused the operation to fail.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    69
RESULT_FAILED_TRANSPORT = ["Failed", "Transport"]
1019
e61c57c724c9 7663 disable_fmri should be synchronous
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 926
diff changeset
    70
# Indicates that the operation failed due to an actuator problem
e61c57c724c9 7663 disable_fmri should be synchronous
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 926
diff changeset
    71
RESULT_FAILED_ACTUATOR = ["Failed", "Actuator"]
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1032
diff changeset
    72
# Indicates that the operation failed due to not enough memory
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1032
diff changeset
    73
RESULT_FAILED_OUTOFMEMORY = ["Failed", "Out of Memory"]
2205
53d0be594162 3822 image plans should prevent conflicting actions from being installed
Danek Duvall <danek.duvall@oracle.com>
parents: 2141
diff changeset
    74
# Indicates that the operation failed because of conflicting actions
53d0be594162 3822 image plans should prevent conflicting actions from being installed
Danek Duvall <danek.duvall@oracle.com>
parents: 2141
diff changeset
    75
RESULT_CONFLICTING_ACTIONS = ["Failed", "Conflicting Actions"]
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    76
# Indicates that the operation failed for an unknown reason.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    77
RESULT_FAILED_UNKNOWN = ["Failed", "Unknown"]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    78
556
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
    79
# Operations that are discarded, not saved, when recorded by history.
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
    80
DISCARDED_OPERATIONS = ["contents", "info", "list"]
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
    81
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    82
# Cross-reference table for errors and results.  Entries should be ordered
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    83
# most-specific to least-specific.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
    84
error_results = {
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    85
    apx.ImageLockedError: RESULT_FAILED_LOCKED,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    86
    apx.InvalidCatalogFile: RESULT_FAILED_STORAGE,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    87
    apx.BENamingNotSupported: RESULT_FAILED_BAD_REQUEST,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    88
    apx.InvalidBENameException: RESULT_FAILED_BAD_REQUEST,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    89
    apx.CertificateError: RESULT_FAILED_CONFIGURATION,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    90
    apx.PublisherError: RESULT_FAILED_BAD_REQUEST,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    91
    apx.CanceledException: RESULT_CANCELED,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    92
    apx.ImageUpdateOnLiveImageException: RESULT_FAILED_BAD_REQUEST,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    93
    apx.ProblematicPermissionsIndexException: RESULT_FAILED_STORAGE,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    94
    apx.PermissionsException: RESULT_FAILED_STORAGE,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    95
    apx.SearchException: RESULT_FAILED_SEARCH,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    96
    apx.PlanCreationException: RESULT_FAILED_CONSTRAINED,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    97
    apx.NonLeafPackageException: RESULT_FAILED_CONSTRAINED,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    98
    apx.IpkgOutOfDateException: RESULT_FAILED_CONSTRAINED,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
    99
    apx.InvalidDepotResponseException: RESULT_FAILED_TRANSPORT,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   100
    apx.ConflictingActionErrors: RESULT_CONFLICTING_ACTIONS,
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   101
    fmri.IllegalFmri: RESULT_FAILED_BAD_REQUEST,
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   102
    KeyboardInterrupt: RESULT_CANCELED,
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1032
diff changeset
   103
    MemoryError: RESULT_FAILED_OUTOFMEMORY,
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   104
}
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   105
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   106
class _HistoryOperation(object):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   107
        """A _HistoryOperation object is a representation of data about an
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   108
        operation that a pkg(5) client has performed.  This class is private
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   109
        and not intended for use by classes other than History.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   110
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   111
        This class provides an abstraction layer between the stack of
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   112
        operations that History manages should these values need to be
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   113
        manipulated as they are set or retrieved.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   114
        """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   115
2728
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   116
        result_l10n = {}    # Static variable for dictionary
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   117
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   118
        def __copy__(self):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   119
                h = _HistoryOperation()
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   120
                for attr in ("name", "start_time", "end_time", "start_state",
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   121
                    "end_state", "username", "userid", "be", "be_exists",
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   122
                    "be_uuid", "current_be", "current_new_be", "new_be",
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   123
                    "new_be_exists", "new_be_uuid", "result", "release_notes",
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   124
                    "snapshot"):
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   125
                        setattr(h, attr, getattr(self, attr))
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   126
                h.errors = [copy.copy(e) for e in self.errors]
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   127
                return h
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   128
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   129
        def __setattr__(self, name, value):
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   130
                if name not in ("result", "errors", "be", "be_uuid",
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   131
                    "current_be", "current_new_be", "new_be", "new_be_exists",
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   132
                    "new_be_uuid", "snapshot"):
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   133
                        # Force all other attribute values to be a string
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   134
                        # to avoid issues with minidom.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   135
                        value = str(value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   136
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   137
                return object.__setattr__(self, name, value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   138
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   139
        def __str__(self):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   140
                return """\
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   141
Operation Name: {0}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   142
Operation Result: {1}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   143
Operation Start Time: {2}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   144
Operation End Time: {3}
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   145
Operation Start State:
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   146
{4}
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   147
Operation End State:
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   148
{5}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   149
Operation User: {6} ({7})
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   150
Operation Boot Env.: {8}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   151
Operation Boot Env. Currrent: {9}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   152
Operation Boot Env. UUID: {10}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   153
Operation New Boot Env.: {11}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   154
Operation New Boot Env. Current: {12}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   155
Operation New Boot Env. UUID: {13}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   156
Operation Snapshot: {14}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   157
Operation Release Notes: {15}
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   158
Operation Errors:
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   159
{16}
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   160
""".format(self.name, self.result, self.start_time, self.end_time,
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   161
    self.start_state, self.end_state, self.username, self.userid,
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   162
    self.be, self.current_be, self.be_uuid, self.new_be, self.current_new_be,
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   163
    self.new_be_uuid, self.snapshot, self.release_notes, self.errors)
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   164
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   165
        # All "time" values should be in UTC, using ISO 8601 as the format.
2089
c8b9d6341530 2775 pkg should provide a way to downgrade (reinstall) packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2069
diff changeset
   166
        # Name of the operation performed (e.g. install, update, etc.).
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   167
        name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   168
        # When the operation started.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   169
        start_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   170
        # When the operation ended.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   171
        end_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   172
        # The starting state of the operation (e.g. image plan pre-evaluation).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   173
        start_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   174
        # The ending state of the operation (e.g. image plan post-evaluation).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   175
        end_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   176
        # Errors encountered during an operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   177
        errors = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   178
        # username of the user that performed the operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   179
        username = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   180
        # id of the user that performed the operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   181
        userid = None
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   182
        # The boot environment on which the user performed the operation
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   183
        be = None
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   184
        # The current name of the boot environment.
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   185
        current_be = None
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   186
        # The uuid of the BE on which the user performed the operation
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   187
        be_uuid = None
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   188
        # The new boot environment that was created as a result of the operation
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   189
        new_be = None
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   190
        # The current name of the new boot environment.
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   191
        current_new_be = None
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   192
        # The uuid of the boot environment that was created as a result of the
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   193
        # operation
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   194
        new_be_uuid = None
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   195
        # The name of the file containing the release notes, or None.
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   196
        release_notes = None
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   197
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   198
        # The snapshot that was created while running this operation
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   199
        # set to None if no snapshot was taken, or destroyed after successful
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   200
        # completion
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   201
        snapshot = None
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   202
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   203
        # The result of the operation (must be a list indicating (outcome,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   204
        # reason)).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   205
        result = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   206
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   207
        def __init__(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   208
                self.errors = []
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   209
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   210
        @property
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   211
        def result_text(self):
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   212
                """Returns a tuple containing the translated text for the
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   213
                operation result of the form (outcome, reason)."""
2728
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   214
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   215
                if not _HistoryOperation.result_l10n:
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   216
                        # since we store english text in our XML files, we
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   217
                        # need a way for clients obtain a translated version
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   218
                        # of these messages.
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   219
                        _HistoryOperation.result_l10n = {
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   220
                            "Canceled": _("Canceled"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   221
                            "Failed": _("Failed"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   222
                            "Ignored": _("Ignored"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   223
                            "Nothing to do": _("Nothing to do"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   224
                            "Succeeded": _("Succeeded"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   225
                            "Bad Request": _("Bad Request"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   226
                            "Configuration": _("Configuration"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   227
                            "Constrained": _("Constrained"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   228
                            "Locked": _("Locked"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   229
                            "Search": _("Search"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   230
                            "Storage": _("Storage"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   231
                            "Transport": _("Transport"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   232
                            "Actuator": _("Actuator"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   233
                            "Out of Memory": _("Out of Memory"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   234
                            "Conflicting Actions": _("Conflicting Actions"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   235
                            "Unknown": _("Unknown"),
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   236
                            "None": _("None")
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   237
                        }
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   238
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   239
                if not self.start_time or not self.result:
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   240
                        return ("", "")
2728
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   241
                return (_HistoryOperation.result_l10n[self.result[0]],
69903f3b722b 7166082 pkg command does not handle Japanese character encoding on Solaris 11
Abhinandan Ekande <abhinandan.ekande@oracle.com>
parents: 2708
diff changeset
   242
                    _HistoryOperation.result_l10n[self.result[1]])
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   243
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   244
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   245
class History(object):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   246
        """A History object is a representation of data about a pkg(5) client
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   247
        and about operations that the client is executing or has executed.  It
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   248
        uses the _HistoryOperation class to represent the data about an
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   249
        operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   250
        """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   251
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   252
        # The directory where the history directory can be found (or
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   253
        # created if it doesn't exist).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   254
        root_dir = None
2991
75e616731cc3 16635525 EOL of packagemanager and related components
Shawn Walker <shawn.walker@oracle.com>
parents: 2728
diff changeset
   255
        # The name of the client (e.g. pkg, etc.)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   256
        client_name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   257
        # The version of the client (e.g. 093ca22da67c).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   258
        client_version = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   259
        # How the client was invoked (e.g. 'pkg install -n foo').
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   260
        client_args = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   261
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   262
        # A stack where operation data will actually be stored.
1271
036d87b0bd44 6831 need 'pkg change-variant' command
Edward Pilatowicz <Edward.Pilatowicz@Sun.COM>
parents: 1235
diff changeset
   263
        __operations = []
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   264
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   265
        # A private property used by preserve() and restore() to store snapshots
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   266
        # of history and operation state information.
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   267
        __snapshot = None
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   268
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   269
        # These attributes exist to fake access to the operations stack.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   270
        operation_name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   271
        operation_username = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   272
        operation_userid = None
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   273
        operation_current_be = None
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   274
        operation_be = None
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   275
        operation_be_uuid = None
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   276
        operation_current_new_be = None
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   277
        operation_new_be = None
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   278
        operation_new_be_uuid = None
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   279
        operation_start_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   280
        operation_end_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   281
        operation_start_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   282
        operation_end_state = None
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   283
        operation_snapshot = None
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   284
        operation_errors = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   285
        operation_result = None
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   286
        operation_release_notes = None
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   287
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   288
        def __copy__(self):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   289
                h = History()
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   290
                for attr in ("root_dir", "client_name", "client_version"):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   291
                        setattr(h, attr, getattr(self, attr))
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   292
                object.__setattr__(self, "client_args",
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   293
                    [copy.copy(a) for a in self.client_args])
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   294
                # A deepcopy has to be performed here since this a list of dicts
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   295
                # and not just History operation objects.
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   296
                h.__operations = [copy.deepcopy(o) for o in self.__operations]
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   297
                return h
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   298
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   299
        def __getattribute__(self, name):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   300
                if name == "client_args":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   301
                        return object.__getattribute__(self, name)[:]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   302
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   303
                if not name.startswith("operation_"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   304
                        return object.__getattribute__(self, name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   305
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   306
                ops = object.__getattribute__(self, "_History__operations")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   307
                if not ops:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   308
                        return None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   309
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   310
                return getattr(ops[-1]["operation"], name[len("operation_"):])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   311
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   312
        def __setattr__(self, name, value):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   313
                if name == "client_args":
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   314
                        raise AttributeError("'history' object attribute '{0}' "
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   315
                            "is read-only.".format(name))
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   316
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   317
                if not name.startswith("operation_"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   318
                        return object.__setattr__(self, name, value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   319
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   320
                ops = object.__getattribute__(self, "_History__operations")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   321
                if name == "operation_name":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   322
                        if not ops:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   323
                                ops = []
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   324
                                object.__setattr__(self,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   325
                                    "_History__operations", ops)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   326
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   327
                        ops.append({
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   328
                            "pathname": None,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   329
                            "operation": _HistoryOperation()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   330
                        })
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   331
                elif not ops:
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   332
                        raise AttributeError("'history' object attribute '{0}' "
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   333
                            "cannot be set before 'operation_name'.".format(
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   334
                            name))
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   335
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   336
                op = ops[-1]["operation"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   337
                setattr(op, name[len("operation_"):], value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   338
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   339
                # Access to the class attributes is done through object instead
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   340
                # of just referencing self to avoid any of the special logic in
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   341
                # place interfering with logic here.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   342
                if name == "operation_name":
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   343
                        # Before a new operation starts, clear exception state
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   344
                        # for the current one so that when this one ends, the
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   345
                        # last operation's exception won't be recorded to this
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   346
                        # one.  If the error hasn't been recorded by now, it
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   347
                        # doesn't matter anyway, so should be safe to clear.
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   348
                        sys.exc_clear()
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   349
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   350
                        # Mark the operation as having started and record
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   351
                        # other, relevant information.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   352
                        op.start_time = misc.time_to_timestamp(None)
2459
2dfb12aa98e8 18496 history needs to cope with get_username failing
Tim Foster <tim.s.foster@oracle.com>
parents: 2205
diff changeset
   353
                        try:
2dfb12aa98e8 18496 history needs to cope with get_username failing
Tim Foster <tim.s.foster@oracle.com>
parents: 2205
diff changeset
   354
                                op.username = portable.get_username()
2dfb12aa98e8 18496 history needs to cope with get_username failing
Tim Foster <tim.s.foster@oracle.com>
parents: 2205
diff changeset
   355
                        except KeyError:
2dfb12aa98e8 18496 history needs to cope with get_username failing
Tim Foster <tim.s.foster@oracle.com>
parents: 2205
diff changeset
   356
                                op.username = "unknown"
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   357
                        op.userid = portable.get_userid()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   358
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   359
                        ca = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   360
                        if sys.argv[0]:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   361
                                ca = [sys.argv[0]]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   362
                        else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   363
                                # Fallback for clients that provide no value.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   364
                                ca = [self.client_name]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   365
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   366
                        ca.extend(sys.argv[1:])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   367
                        object.__setattr__(self, "client_args", ca)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   368
                        object.__setattr__(self, "client_version", pkg.VERSION)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   369
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   370
                elif name == "operation_result":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   371
                        # Record when the operation ended.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   372
                        op.end_time = misc.time_to_timestamp(None)
556
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   373
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   374
                        # Some operations shouldn't be saved -- they're merely
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   375
                        # included in the stack for completeness or to support
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   376
                        # client functionality.
2069
207f168361ba 10148 successful image operations with no effect should be elided from history
Shawn Walker <shawn.walker@oracle.com>
parents: 2028
diff changeset
   377
                        if op.name not in DISCARDED_OPERATIONS and \
207f168361ba 10148 successful image operations with no effect should be elided from history
Shawn Walker <shawn.walker@oracle.com>
parents: 2028
diff changeset
   378
                            value != RESULT_NOTHING_TO_DO:
556
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   379
                                # Write current history and last operation to a
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   380
                                # file.
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   381
                                self.__save()
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   382
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   383
                        # Discard it now that it is no longer needed.
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   384
                        del ops[-1]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   385
2648
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   386
        def __init__(self, root_dir=".", filename=None, uuid_be_dic=None):
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   387
                """'root_dir' should be the path of the directory where the
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   388
                history directory can be found (or created if it doesn't
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   389
                exist).  'filename' should be the name of an XML file
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   390
                containing serialized history information to load.
2648
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   391
                'uuid_be_dic', if supplied, should be a dictionary of BE uuid
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   392
                information, as produced by
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   393
                pkg.client.bootenv.BootEnv.get_uuid_be_dic(), otherwise that
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   394
                method is called each time a History object is created.
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   395
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   396
                # Since this is a read-only attribute normally, we have to
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   397
                # bypass our setattr override by calling object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   398
                object.__setattr__(self, "client_args", [])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   399
696
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   400
                # Initialize client_name to what the client thinks it is.  This
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   401
                # will be overridden if we load history entries off disk.
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   402
                self.client_name = pkg.client.global_settings.client_name
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   403
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   404
                self.root_dir = root_dir
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   405
                if filename:
2648
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   406
                        self.__load(filename, uuid_be_dic=uuid_be_dic)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   407
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   408
        def __str__(self):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   409
                ops = self.__operations
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   410
                return "\n".join([str(op["operation"]) for op in ops])
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   411
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   412
        @property
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   413
        def path(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   414
                """The directory where history files will be written to or
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   415
                read from.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   416
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   417
                return os.path.join(self.root_dir, "history")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   418
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   419
        @property
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   420
        def pathname(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   421
                """Returns the pathname that the history information was read
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   422
                from or will attempted to be written to.  Returns None if no
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   423
                operation has started yet or if no operation has been loaded.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   424
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   425
                if not self.operation_start_time:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   426
                        return None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   427
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   428
                ops = self.__operations
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   429
                pathname = ops[-1]["pathname"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   430
                if not pathname:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   431
                        return os.path.join(self.path,
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   432
                            "{0}-01.xml".format(ops[-1]["operation"].start_time))
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   433
                return pathname
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   434
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   435
        @property
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   436
        def notes(self):
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   437
                """Generates the lines of release notes for this operation.
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   438
                If no release notes are present, no output occurs."""
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   439
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   440
                if not self.operation_release_notes:
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   441
                        return
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   442
                try:
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   443
                        rpath = os.path.join(self.root_dir, 
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   444
                            "notes", 
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   445
                            self.operation_release_notes)
3234
3a90dc0b66c9 21188662 use six library for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3171
diff changeset
   446
                        for a in open(rpath, "r"):
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   447
                                yield a.rstrip()
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   448
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   449
                except Exception as e:
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   450
                        raise apx.HistoryLoadException(e)
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   451
                        
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   452
        def clear(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   453
                """Discards all information related to the current history
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   454
                object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   455
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   456
                self.client_name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   457
                self.client_version = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   458
                object.__setattr__(self, "client_args", [])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   459
                self.__operations = []
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   460
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   461
        def __load_client_data(self, node):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   462
                """Internal function to load the client data from the given XML
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   463
                'node' object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   464
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   465
                self.client_name = node.getAttribute("name")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   466
                self.client_version = node.getAttribute("version")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   467
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   468
                        args = node.getElementsByTagName("args")[0]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   469
                except IndexError:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   470
                        # There might not be any.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   471
                        pass
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   472
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   473
                        ca = object.__getattribute__(self, "client_args")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   474
                        for cnode in args.getElementsByTagName("arg"):
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   475
                                try:
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   476
                                        ca.append(cnode.childNodes[0].wholeText)
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   477
                                except (AttributeError, IndexError):
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   478
                                        # There may be no childNodes, or
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   479
                                        # wholeText may not be defined.
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   480
                                        pass
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   481
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   482
        @staticmethod
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   483
        def __load_operation_data(node, uuid_be_dic):
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   484
                """Internal function to load the operation data from the given
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   485
                XML 'node' object and return a _HistoryOperation object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   486
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   487
                op = _HistoryOperation()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   488
                op.name = node.getAttribute("name")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   489
                op.start_time = node.getAttribute("start_time")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   490
                op.end_time = node.getAttribute("end_time")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   491
                op.username = node.getAttribute("username")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   492
                op.userid = node.getAttribute("userid")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   493
                op.result = node.getAttribute("result").split(", ")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   494
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   495
                if len(op.result) == 1:
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   496
                        op.result.append("None")
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   497
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   498
                # older clients simply wrote "Nothing to do" instead of
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   499
                # "Ignored, Nothing to do", so work around that
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   500
                if op.result[0] == "Nothing to do":
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   501
                        op.result = RESULT_NOTHING_TO_DO
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   502
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   503
                if node.hasAttribute("be_uuid"):
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   504
                        op.be_uuid = node.getAttribute("be_uuid")
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   505
                if node.hasAttribute("new_be_uuid"):
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   506
                        op.new_be_uuid = node.getAttribute("new_be_uuid")
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   507
                if node.hasAttribute("be"):
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   508
                        op.be = node.getAttribute("be")
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   509
                        if op.be_uuid:
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   510
                                op.current_be = uuid_be_dic.get(op.be_uuid,
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   511
                                    op.be)
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   512
                if node.hasAttribute("new_be"):
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   513
                        op.new_be = node.getAttribute("new_be")
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   514
                        if op.new_be_uuid:
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   515
                                op.current_new_be = uuid_be_dic.get(
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   516
                                    op.new_be_uuid, op.new_be)
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   517
                if node.hasAttribute("release-notes"):
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   518
                        op.release_notes = node.getAttribute("release-notes")
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   519
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   520
                def get_node_values(parent_name, child_name=None):
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   521
                        try:
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   522
                                parent = node.getElementsByTagName(parent_name)[0]
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   523
                                if child_name:
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   524
                                        cnodes = parent.getElementsByTagName(
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   525
                                            child_name)
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   526
                                        return [
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   527
                                            cnode.childNodes[0].wholeText
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   528
                                            for cnode in cnodes
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   529
                                        ]
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   530
                                return parent.childNodes[0].wholeText
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   531
                        except (AttributeError, IndexError):
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   532
                                # Assume no values are present for the node.
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   533
                                pass
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   534
                        if child_name:
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   535
                                return []
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   536
                        return
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   537
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   538
                op.start_state = get_node_values("start_state")
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   539
                op.end_state = get_node_values("end_state")
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   540
                op.errors.extend(get_node_values("errors", child_name="error"))
1507
b956ea23d3a6 11735 pkg history data files should not be executable
Richard Lowe <richlowe@richlowe.net>
parents: 1505
diff changeset
   541
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   542
                return op
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   543
2648
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   544
        def __load(self, filename, uuid_be_dic=None):
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   545
                """Loads the history from a file located in self.path/history/
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   546
                {filename}.  The file should contain a serialized history
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   547
                object in XML format.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   548
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   549
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   550
                # Ensure all previous information is discarded.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   551
                self.clear()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   552
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   553
                try:
2648
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   554
                        if not uuid_be_dic:
16a431283f0e 7158089 pkg history could be faster
Tim Foster <tim.s.foster@oracle.com>
parents: 2524
diff changeset
   555
                                uuid_be_dic = bootenv.BootEnv.get_uuid_be_dic()
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   556
                except apx.ApiException as e:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   557
                        uuid_be_dic = {}
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   558
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   559
                try:
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   560
                        pathname = os.path.join(self.path, filename)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   561
                        d = xmini.parse(pathname)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   562
                        root = d.documentElement
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   563
                        for cnode in root.childNodes:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   564
                                if cnode.nodeName == "client":
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   565
                                        self.__load_client_data(cnode)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   566
                                elif cnode.nodeName == "operation":
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   567
                                        # Operations load differently due to
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   568
                                        # the stack.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   569
                                        self.__operations.append({
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   570
                                            "pathname": pathname,
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   571
                                            "operation":
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   572
                                                self.__load_operation_data(
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   573
                                                cnode, uuid_be_dic)
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   574
                                            })
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   575
                except KeyboardInterrupt:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   576
                        raise
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   577
                except Exception as e:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   578
                        raise apx.HistoryLoadException(e)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   579
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   580
        def __serialize_client_data(self, d):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   581
                """Internal function used to serialize current client data
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   582
                using the supplied 'd' (xml.dom.minidom) object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   583
                """
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   584
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   585
                assert self.client_name is not None
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   586
                assert self.client_version is not None
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   587
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   588
                root = d.documentElement
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   589
                client = d.createElement("client")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   590
                client.setAttribute("name", self.client_name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   591
                client.setAttribute("version", self.client_version)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   592
                root.appendChild(client)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   593
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   594
                if self.client_args:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   595
                        args = d.createElement("args")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   596
                        client.appendChild(args)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   597
                        for entry in self.client_args:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   598
                                arg = d.createElement("arg")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   599
                                args.appendChild(arg)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   600
                                arg.appendChild(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   601
                                    d.createCDATASection(str(entry)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   602
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   603
        def __serialize_operation_data(self, d):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   604
                """Internal function used to serialize current operation data
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   605
                using the supplied 'd' (xml.dom.minidom) object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   606
                """
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   607
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   608
                if self.operation_userid is None:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   609
                        raise apx.HistoryStoreException("Unable to determine "
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   610
                            "the id of the user that performed the current "
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   611
                            "operation; unable to store history information.")
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   612
                elif self.operation_username is None:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   613
                        raise apx.HistoryStoreException("Unable to determine "
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   614
                            "the username of the user that performed the "
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   615
                            "current operation; unable to store history "
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   616
                            "information.")
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   617
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   618
                root = d.documentElement
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   619
                op = d.createElement("operation")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   620
                op.setAttribute("name", self.operation_name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   621
                # Must explictly convert values to a string due to minidom bug
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   622
                # that causes a fatal whenever using types other than str.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   623
                op.setAttribute("username", str(self.operation_username))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   624
                op.setAttribute("userid", str(self.operation_userid))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   625
                op.setAttribute("result", ", ".join(self.operation_result))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   626
                op.setAttribute("start_time", self.operation_start_time)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   627
                op.setAttribute("end_time", self.operation_end_time)
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   628
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   629
                if self.operation_be:
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   630
                        op.setAttribute("be", self.operation_be)
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   631
                if self.operation_be_uuid:
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   632
                        op.setAttribute("be_uuid", self.operation_be_uuid)
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   633
                if self.operation_new_be:
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   634
                        op.setAttribute("new_be", self.operation_new_be)
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   635
                if self.operation_new_be_uuid:
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   636
                        op.setAttribute("new_be_uuid",
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   637
                            self.operation_new_be_uuid)
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   638
                if self.operation_snapshot:
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   639
                        op.setAttribute("snapshot", self.operation_snapshot)
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   640
                if self.operation_release_notes:
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   641
                        op.setAttribute("release-notes", self.operation_release_notes)
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   642
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   643
                root.appendChild(op)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   644
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   645
                if self.operation_start_state:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   646
                        state = d.createElement("start_state")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   647
                        op.appendChild(state)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   648
                        state.appendChild(d.createCDATASection(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   649
                            str(self.operation_start_state)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   650
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   651
                if self.operation_end_state:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   652
                        state = d.createElement("end_state")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   653
                        op.appendChild(state)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   654
                        state.appendChild(d.createCDATASection(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   655
                            str(self.operation_end_state)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   656
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   657
                if self.operation_errors:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   658
                        errors = d.createElement("errors")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   659
                        op.appendChild(errors)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   660
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   661
                        for entry in self.operation_errors:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   662
                                error = d.createElement("error")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   663
                                errors.appendChild(error)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   664
                                error.appendChild(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   665
                                    d.createCDATASection(str(entry)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   666
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   667
        def __save(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   668
                """Serializes the current history information and writes it to
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   669
                a file in self.path/{operation_start_time}-{sequence}.xml.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   670
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   671
                d = xmini.Document()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   672
                d.appendChild(d.createElement("history"))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   673
                self.__serialize_client_data(d)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   674
                self.__serialize_operation_data(d)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   675
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   676
                if not os.path.exists(self.path):
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   677
                        try:
1102
5ea5cdb4360d 5014 api sets successful result for image-create with failed catalog refresh
Shawn Walker <srw@sun.com>
parents: 1035
diff changeset
   678
                                # Only the right-most directory should be
5ea5cdb4360d 5014 api sets successful result for image-create with failed catalog refresh
Shawn Walker <srw@sun.com>
parents: 1035
diff changeset
   679
                                # created.  Assume that if the parent structure
5ea5cdb4360d 5014 api sets successful result for image-create with failed catalog refresh
Shawn Walker <srw@sun.com>
parents: 1035
diff changeset
   680
                                # does not exist, it shouldn't be created.
1507
b956ea23d3a6 11735 pkg history data files should not be executable
Richard Lowe <richlowe@richlowe.net>
parents: 1505
diff changeset
   681
                                os.mkdir(self.path, misc.PKG_DIR_MODE)
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   682
                        except EnvironmentError as e:
1102
5ea5cdb4360d 5014 api sets successful result for image-create with failed catalog refresh
Shawn Walker <srw@sun.com>
parents: 1035
diff changeset
   683
                                if e.errno not in (errno.EROFS, errno.EACCES,
5ea5cdb4360d 5014 api sets successful result for image-create with failed catalog refresh
Shawn Walker <srw@sun.com>
parents: 1035
diff changeset
   684
                                    errno.ENOENT):
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   685
                                        # Ignore read-only file system and
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   686
                                        # access errors as it isn't critical
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   687
                                        # to the image that this data is
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   688
                                        # written.
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   689
                                        raise apx.HistoryStoreException(e)
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   690
                                # Return, since without the directory, the rest
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   691
                                # of this will fail.
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   692
                                return
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   693
                        except KeyboardInterrupt:
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   694
                                raise
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   695
                        except Exception as e:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   696
                                raise apx.HistoryStoreException(e)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   697
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   698
                # Repeatedly attempt to write the history (only if it's because
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   699
                # the file already exists).  This is necessary due to multiple
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   700
                # operations possibly occuring within the same second (but not
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   701
                # microsecond).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   702
                pathname = self.pathname
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   703
                for i in range(1, 100):
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   704
                        try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   705
                                f = os.fdopen(os.open(pathname,
1507
b956ea23d3a6 11735 pkg history data files should not be executable
Richard Lowe <richlowe@richlowe.net>
parents: 1505
diff changeset
   706
                                    os.O_CREAT|os.O_EXCL|os.O_WRONLY,
b956ea23d3a6 11735 pkg history data files should not be executable
Richard Lowe <richlowe@richlowe.net>
parents: 1505
diff changeset
   707
                                    misc.PKG_FILE_MODE), "w")
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   708
                                d.writexml(f,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   709
                                    encoding=sys.getdefaultencoding())
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   710
                                f.close()
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   711
                                return
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   712
                        except EnvironmentError as e:
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   713
                                if e.errno == errno.EEXIST:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   714
                                        name, ext = os.path.splitext(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   715
                                            os.path.basename(pathname))
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   716
                                        name = name.split("-", 1)[0]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   717
                                        # Pick the next name in our sequence
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   718
                                        # and try again.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   719
                                        pathname = os.path.join(self.path,
3158
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   720
                                            "{0}-{1:>02d}{2}".format(name,
58c9c2c21e67 20177033 change string formatting for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3125
diff changeset
   721
                                            i + 1, ext))
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   722
                                        continue
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   723
                                elif e.errno not in (errno.EROFS,
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   724
                                    errno.EACCES):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   725
                                        # Ignore read-only file system and
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   726
                                        # access errors as it isn't critical
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   727
                                        # to the image that this data is
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   728
                                        # written.
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   729
                                        raise apx.HistoryStoreException(e)
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   730
                                # For all other failures, return, and avoid any
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   731
                                # further attempts.
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   732
                                return
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   733
                        except KeyboardInterrupt:
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   734
                                raise
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   735
                        except Exception as e:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   736
                                raise apx.HistoryStoreException(e)
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   737
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   738
        def purge(self, be_name=None, be_uuid=None):
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   739
                """Removes all history information by deleting the directory
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   740
                indicated by the value self.path and then creates a new history
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   741
                entry to record that this purge occurred.
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   742
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   743
                self.operation_name = "purge-history"
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   744
                self.operation_be = be_name
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   745
                self.operation_be_uuid = be_uuid
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   746
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   747
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   748
                        shutil.rmtree(self.path)
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   749
                except KeyboardInterrupt:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   750
                        raise
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   751
                except EnvironmentError as e:
834
f8b3396500b3 4883 import failure for non-sunos platforms due to missing pspawn
Tom Mueller <Tom.Mueller@sun.com>
parents: 732
diff changeset
   752
                        if e.errno in (errno.ENOENT, errno.ESRCH):
732
9ea802fef2fb 3540 client should be resilient against corrupt history
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 696
diff changeset
   753
                                # History already purged; record as successful.
9ea802fef2fb 3540 client should be resilient against corrupt history
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 696
diff changeset
   754
                                self.operation_result = RESULT_SUCCEEDED
9ea802fef2fb 3540 client should be resilient against corrupt history
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 696
diff changeset
   755
                                return
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   756
                        raise apx.HistoryPurgeException(e)
3171
525f5bdb3f62 20434301 change exception handling syntax for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3158
diff changeset
   757
                except Exception as e:
2524
0c32a79b1684 17252 pkg.client.api should provide history management interface
Shawn Walker <shawn.walker@oracle.com>
parents: 2459
diff changeset
   758
                        raise apx.HistoryPurgeException(e)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   759
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   760
                        self.operation_result = RESULT_SUCCEEDED
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   761
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   762
        def abort(self, result):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   763
                """Intended to be used by the client during top-level error
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   764
                handling to indicate that an unrecoverable error occurred
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   765
                during the current operation(s).  This allows History to end
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   766
                all of the current operations properly and handle any possible
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   767
                errors that might be encountered in History itself.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   768
                """
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   769
                try:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   770
                        # Ensure that all operations in the current stack are
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   771
                        # ended properly.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   772
                        while self.operation_name:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   773
                                self.operation_result = result
3125
cc1fb2164f05 18406315 HistoryStoreException results in NameError in client/history.py
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 2991
diff changeset
   774
                except apx.HistoryStoreException:
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   775
                        # Ignore storage errors as it's likely that whatever
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   776
                        # caused the client to abort() also caused the storage
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   777
                        # of the history information to fail.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   778
                        return
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   779
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   780
        def log_operation_start(self, name, be_name=None, be_uuid=None):
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   781
                """Marks the start of an operation to be recorded in image
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   782
                history."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   783
                self.operation_name = name
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   784
                self.operation_be = be_name
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   785
                self.operation_be_uuid = be_uuid
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   786
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   787
        def log_operation_end(self, error=None, result=None, release_notes=None):
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   788
                """Marks the end of an operation to be recorded in image
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   789
                history.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   790
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   791
                'result' should be a pkg.client.history constant value
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   792
                representing the outcome of an operation.  If not provided,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   793
                and 'error' is provided, the final result of the operation will
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   794
                be based on the class of 'error' and 'error' will be recorded
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   795
                for the current operation.  If 'result' and 'error' is not
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   796
                provided, success is assumed."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   797
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   798
                if error:
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   799
                        self.log_operation_error(error)
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2089
diff changeset
   800
                        self.operation_new_be = None
2141
3757a0f8a7d1 17385 history should store uuids for boot environments
Tim Foster <tim.s.foster@oracle.com>
parents: 2135
diff changeset
   801
                        self.operation_new_be_uuid = None
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   802
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   803
                if error and not result:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   804
                        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   805
                                # Attempt get an exact error match first.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   806
                                result = error_results[error.__class__]
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   807
                        except (AttributeError, KeyError):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   808
                                # Failing an exact match, determine if this
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   809
                                # error is a subclass of an existing one.
3234
3a90dc0b66c9 21188662 use six library for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3171
diff changeset
   810
                                for entry, val in six.iteritems(error_results):
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   811
                                        if isinstance(error, entry):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   812
                                                result = val
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   813
                                                break
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   814
                        if not result:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   815
                                # If a result could still not be determined,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   816
                                # assume unknown failure case.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   817
                                result = RESULT_FAILED_UNKNOWN
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   818
                elif not result:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   819
                        # Assume success if no error and no result.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   820
                        result = RESULT_SUCCEEDED
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   821
                if release_notes:
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2648
diff changeset
   822
                        self.operation_release_notes = release_notes
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   823
                self.operation_result = result
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   824
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   825
        def log_operation_error(self, error):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   826
                """Adds an error to the list of errors to be recorded in image
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   827
                history for the current operation."""
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   828
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 834
diff changeset
   829
                if self.operation_name:
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   830
                        out_stack = None
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   831
                        out_err = None
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   832
                        use_current_stack = True
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   833
                        if isinstance(error, Exception):
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   834
                                # Attempt to get the exception's stack trace
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   835
                                # from the stack.  If the exception on the stack
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   836
                                # isn't the same object as the one being logged,
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   837
                                # then we have to use the current stack (which
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   838
                                # is somewhat less useful) instead of being able
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   839
                                # to find the code location of the original
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   840
                                # error.
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   841
                                type, val, tb = sys.exc_info()
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   842
                                if error == val:
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   843
                                        output = traceback.format_exc()
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   844
                                        use_current_stack = False
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   845
3234
3a90dc0b66c9 21188662 use six library for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3171
diff changeset
   846
                        if isinstance(error, six.string_types):
2205
53d0be594162 3822 image plans should prevent conflicting actions from being installed
Danek Duvall <danek.duvall@oracle.com>
parents: 2141
diff changeset
   847
                                output = error
53d0be594162 3822 image plans should prevent conflicting actions from being installed
Danek Duvall <danek.duvall@oracle.com>
parents: 2141
diff changeset
   848
                        elif use_current_stack:
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   849
                                # Assume the current stack is more useful if
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   850
                                # the error doesn't inherit from Exception or
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   851
                                # we can't use the last exception's stack.
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   852
                                out_stack = "".join(traceback.format_stack())
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   853
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   854
                                if error:
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   855
                                        # This may result in the text
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   856
                                        # of the error itself being written
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   857
                                        # twice, but that is necessary in case
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   858
                                        # it is not contained within the
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   859
                                        # output of format_exc().
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   860
                                        out_err = str(error)
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   861
                                        if not out_err or out_err == "None":
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   862
                                                out_err = \
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   863
                                                    error.__class__.__name__
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   864
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   865
                                output = "".join([
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   866
                                    item for item in [out_stack, out_err]
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   867
                                    if item
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   868
                                ])
1235
7c34ba6e5e4f 9287 history can log wrong exception information when operations fail
Shawn Walker <srw@sun.com>
parents: 1102
diff changeset
   869
1032
ff3c6b09f430 8072 history load code needs to be more resilient against unexpected data format
Shawn Walker <srw@sun.com>
parents: 1027
diff changeset
   870
                        self.operation_errors.append(output.strip())
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   871
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   872
        def create_snapshot(self):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   873
                """Stores a snapshot of the current history and operation state
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   874
                information in memory so that it can be restored in the event of
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   875
                client failure (such as inability to store history information
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   876
                or the failure of a boot environment operation).  Each call to
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   877
                this function will overwrite the previous snapshot."""
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   878
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   879
                attrs = self.__snapshot = {}
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   880
                for attr in ("root_dir", "client_name", "client_version"):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   881
                        attrs[attr] = getattr(self, attr)
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   882
                attrs["client_args"] = [copy.copy(a) for a in self.client_args]
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   883
                # A deepcopy has to be performed here since this a list of dicts
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   884
                # and not just History operation objects.
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   885
                attrs["__operations"] = \
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   886
                    [copy.deepcopy(o) for o in self.__operations]
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   887
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   888
        def discard_snapshot(self):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   889
                """Discards the current history and operation state information
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   890
                snapshot."""
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   891
                self.__snapshot = None
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   892
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   893
        def restore_snapshot(self):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   894
                """Restores the last snapshot taken of history and operation
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   895
                state information completely discarding the existing history and
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   896
                operation state information.  If nothing exists to restore, this
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   897
                this function will silently return."""
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   898
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   899
                if not self.__snapshot:
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   900
                        return
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   901
3234
3a90dc0b66c9 21188662 use six library for python 3 migration
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3171
diff changeset
   902
                for name, val in six.iteritems(self.__snapshot):
1027
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   903
                        if not name.startswith("__"):
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   904
                                object.__setattr__(self, name, val)
e827313523d8 7774 image history discarded when bootenv operations fail or succeed
Shawn Walker <srw@sun.com>
parents: 1019
diff changeset
   905
                self.__operations = self.__snapshot["__operations"]