5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
24 # Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
34 import pkg
.client
.rad_pkg
as entry
35 import pkg
.misc
as misc
36 import simplejson
as json
41 class _InfoFilter(logging
.Filter
):
42 def filter(self
, rec
):
43 return rec
.levelno
<= logging
.INFO
45 class _StreamHandler(logging
.StreamHandler
):
46 """Simple subclass to ignore exceptions raised during logging output."""
48 def handleError(self
, record
):
49 # Ignore exceptions raised during output to stdout/stderr.
55 """Create error message."""
57 if os
.getenv("__IPS_INVOKE_IN_RAD") == "true":
58 return {"status": 1, "errors": [{"reason": text
}]}
59 ips_logger
.error(text
)
63 """Initialize logger."""
67 ips_logger
= logging
.getLogger("__name__")
68 ips_logger
.propagate
= 0
69 ips_logger
.setLevel(logging
.INFO
)
71 # This logger is for delivering JSON result. Use stderr to distinguish
72 # it with progress output.
73 handler
= _StreamHandler(sys
.stderr
)
74 handler
.setLevel(logging
.INFO
)
76 # If this script is used in RAD, only retrieve log levels <= INFO.
77 if os
.getenv("__IPS_INVOKE_IN_RAD") == "true":
78 handler
.addFilter(_InfoFilter())
79 ips_logger
.addHandler(handler
)
85 prog_delay
= PROG_DELAY
86 if os
.getenv("__IPS_INVOKE_IN_RAD") != "true":
87 return error(_("This script can only be invoked by RAD"))
88 script_path
= os
.path
.realpath(__file__
)
90 opts
, pargs
= getopt
.getopt(sys
.argv
[1:],
91 "hR:?", ["help", "pargs=", "opts=", "prog-delay="])
93 if opt
== "--help" or opt
== "-h":
94 error("This is a RAD only script.")
95 elif opt
== "--pargs":
101 elif opt
== "--prog-delay":
102 prog_delay
= float(arg
)
104 error(_("unknown option {0} in file: {1}"
105 ).format(opt
, script_path
))
106 except getopt
.GetoptError
as e
:
107 return error(_("illegal global option -- {0} in file: {1}"
108 ).format(e
.opt
, script_path
))
109 except ValueError as e
:
110 return error(_("invalid option argument: {0} in file: {1}"
111 ).format(str(e
), script_path
))
113 return error(_("missing argument in file: {0}").format(
116 return entry
.rad_pkg(pargs
[0], pargs_json
=pargs_json
,
117 opts_json
=opts_json
, pkg_image
=pkg_image
,
118 prog_delay
=prog_delay
)
120 if __name__
== "__main__":
121 misc
.setlocale(locale
.LC_ALL
, "")
122 gettext
.install("pkg", "/usr/share/locale",
123 codeset
=locale
.getpreferredencoding())
125 ret_json
= main_func()
126 ips_logger
.info(json
.dumps(ret_json
))
130 # Ignore python's spurious pipe problems.
132 sys
.exit(ret_json
["status"])