switch to 64-bit default run paths
[unleashed-pkg5.git] / src / rad-invoke.py
blobad8919198bc2095a2abebb3e1547466f8fdfd14c
1 #!/usr/bin/python2.7
3 # CDDL HEADER START
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]
20 # CDDL HEADER END
24 # Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
27 import sys
28 import gettext
29 import getopt
30 import locale
31 import logging
32 import os
33 import pkg
34 import pkg.client.rad_pkg as entry
35 import pkg.misc as misc
36 import simplejson as json
38 # progress delay.
39 PROG_DELAY = 5.0
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.
50 return
52 ips_logger = None
54 def error(text):
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)
60 sys.exit(1)
62 def __init_log():
63 """Initialize logger."""
65 global ips_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)
81 def main_func():
82 pkg_image = None
83 pargs_json = None
84 opts_json = None
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__)
89 try:
90 opts, pargs = getopt.getopt(sys.argv[1:],
91 "hR:?", ["help", "pargs=", "opts=", "prog-delay="])
92 for opt, arg in opts:
93 if opt == "--help" or opt == "-h":
94 error("This is a RAD only script.")
95 elif opt == "--pargs":
96 pargs_json = arg
97 elif opt == "--opts":
98 opts_json = arg
99 elif opt == "-R":
100 pkg_image = arg
101 elif opt == "--prog-delay":
102 prog_delay = float(arg)
103 else:
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))
112 if len(pargs) < 1:
113 return error(_("missing argument in file: {0}").format(
114 script_path))
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())
124 __init_log()
125 ret_json = main_func()
126 ips_logger.info(json.dumps(ret_json))
127 try:
128 logging.shutdown()
129 except IOError:
130 # Ignore python's spurious pipe problems.
131 pass
132 sys.exit(ret_json["status"])