def runExternalScriptWithTimeout(script, allow_insecure=False,\ script_args=(), timeout=30): """Run a script (e.g. preflight/postflight) and return its exit status. Args: script: string path to the script to execute. allow_insecure: bool skip the permissions check of executable. args: args to pass to the script. Returns: Tuple. (integer exit status from script, str stdout, str stderr). Raises: ScriptNotFoundError: the script was not found at the given path. RunExternalScriptError: there was an error running the script. """ from munkilib import utils if not os.path.exists(script): raise ScriptNotFoundError('script does not exist: %s' % script) if not allow_insecure: try: utils.verifyFileOnlyWritableByMunkiAndRoot(script) except utils.VerifyFilePermissionsError, e: msg = ('Skipping execution due to failed file permissions ' 'verification: %s\n%s' % (script, str(e))) raise utils.RunExternalScriptError(msg)
stdout=subprocess.PIPE, stderr=subprocess.PIPE) while timeout > 0: if proc.poll() is not None: (stdout, stderr) = proc.communicate() return proc.returncode, stdout.decode('UTF-8', 'replace'), \ stderr.decode('UTF-8', 'replace') time.sleep(0.1) timeout -= 0.1 else: try: proc.kill() except OSError, e: if e.errno != 3: raise raise utils.RunExternalScriptError('%s timed out' % script) return (0, None, None) else: raise utils.RunExternalScriptError('%s not executable' % script) def rundir(scriptdir, runtype, abort=False, submitscript=''): """ Run scripts in directory scriptdir runtype is passed to the script if abort is True, a non-zero exit status will abort munki submitscript is put at the end of the scriptlist """ if os.path.exists(scriptdir):