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