3185N/A#!/usr/bin/python2.7
3185N/A#
3185N/A# CDDL HEADER START
3185N/A#
3185N/A# The contents of this file are subject to the terms of the
3185N/A# Common Development and Distribution License (the "License").
3185N/A# You may not use this file except in compliance with the License.
3185N/A#
3185N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3185N/A# or http://www.opensolaris.org/os/licensing.
3185N/A# See the License for the specific language governing permissions
3185N/A# and limitations under the License.
3185N/A#
3185N/A# When distributing Covered Code, include this CDDL HEADER in each
3185N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3185N/A# If applicable, add the following below this CDDL HEADER, with the
3185N/A# fields enclosed by brackets "[]" replaced with your own identifying
3185N/A# information: Portions Copyright [yyyy] [name of copyright owner]
3185N/A#
3185N/A# CDDL HEADER END
3185N/A#
3185N/A
3185N/A#
3185N/A# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3185N/A#
3185N/A
3185N/Aimport sys
3185N/Aimport gettext
3185N/Aimport getopt
3185N/Aimport locale
3185N/Aimport logging
3185N/Aimport os
3185N/Aimport pkg
3185N/Aimport pkg.client.rad_pkg as entry
3185N/Aimport pkg.misc as misc
3185N/Aimport simplejson as json
3185N/A
3185N/A
3185N/Aclass _InfoFilter(logging.Filter):
3185N/A def filter(self, rec):
3185N/A return rec.levelno <= logging.INFO
3185N/A
3185N/Aclass _StreamHandler(logging.StreamHandler):
3185N/A """Simple subclass to ignore exceptions raised during logging output."""
3185N/A
3185N/A def handleError(self, record):
3185N/A # Ignore exceptions raised during output to stdout/stderr.
3185N/A return
3185N/A
3185N/Aips_logger = None
3185N/A
3185N/Adef error(text):
3185N/A """Create error message."""
3185N/A
3185N/A if os.getenv("__IPS_INVOKE_IN_RAD") == "true":
3251N/A return {"status": entry.ERROR, "errors": [{"reason": text}]}
3185N/A ips_logger.error(text)
3185N/A sys.exit(1)
3185N/A
3185N/Adef __init_log():
3185N/A """Initialize logger."""
3185N/A
3185N/A global ips_logger
3185N/A
3185N/A ips_logger = logging.getLogger("__name__")
3185N/A ips_logger.propagate = 0
3185N/A ips_logger.setLevel(logging.INFO)
3185N/A
3264N/A handler = _StreamHandler(sys.stdout)
3185N/A handler.setLevel(logging.INFO)
3185N/A
3185N/A # If this script is used in RAD, only retrieve log levels <= INFO.
3185N/A if os.getenv("__IPS_INVOKE_IN_RAD") == "true":
3185N/A handler.addFilter(_InfoFilter())
3185N/A ips_logger.addHandler(handler)
3185N/A
3185N/Adef main_func():
3185N/A pkg_image = None
3185N/A pargs_json = None
3185N/A opts_json = None
3251N/A prog_delay = entry.PROG_DELAY
3185N/A if os.getenv("__IPS_INVOKE_IN_RAD") != "true":
3185N/A return error(_("This script can only be invoked by RAD"))
3185N/A script_path = os.path.realpath(__file__)
3185N/A try:
3185N/A opts, pargs = getopt.getopt(sys.argv[1:],
3185N/A "hR:?", ["help", "pargs=", "opts=", "prog-delay="])
3185N/A for opt, arg in opts:
3185N/A if opt == "--help" or opt == "-h":
3185N/A error("This is a RAD only script.")
3185N/A elif opt == "--pargs":
3185N/A pargs_json = arg
3185N/A elif opt == "--opts":
3185N/A opts_json = arg
3185N/A elif opt == "-R":
3185N/A pkg_image = arg
3185N/A elif opt == "--prog-delay":
3185N/A prog_delay = float(arg)
3185N/A else:
3185N/A error(_("unknown option {0} in file: {1}"
3185N/A ).format(opt, script_path))
3185N/A except getopt.GetoptError as e:
3185N/A return error(_("illegal global option -- {0} in file: {1}"
3185N/A ).format(e.opt, script_path))
3185N/A except ValueError as e:
3185N/A return error(_("invalid option argument: {0} in file: {1}"
3185N/A ).format(str(e), script_path))
3185N/A if len(pargs) < 1:
3185N/A return error(_("missing argument in file: {0}").format(
3185N/A script_path))
3185N/A return entry.rad_pkg(pargs[0], pargs_json=pargs_json,
3185N/A opts_json=opts_json, pkg_image=pkg_image,
3185N/A prog_delay=prog_delay)
3185N/A
3185N/Aif __name__ == "__main__":
3185N/A misc.setlocale(locale.LC_ALL, "")
3185N/A gettext.install("pkg", "/usr/share/locale",
3185N/A codeset=locale.getpreferredencoding())
3185N/A __init_log()
3185N/A ret_json = main_func()
3185N/A ips_logger.info(json.dumps(ret_json))
3185N/A try:
3185N/A logging.shutdown()
3185N/A except IOError:
3185N/A # Ignore python's spurious pipe problems.
3185N/A pass
3185N/A sys.exit(ret_json["status"])