xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/commandsequence.py (revision b369c884543333df6d8ab369d46ae78659bfe184)
12d57dc69SVladimir Kotal#
22d57dc69SVladimir Kotal# CDDL HEADER START
32d57dc69SVladimir Kotal#
42d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the
52d57dc69SVladimir Kotal# Common Development and Distribution License (the "License").
62d57dc69SVladimir Kotal# You may not use this file except in compliance with the License.
72d57dc69SVladimir Kotal#
82d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific
92d57dc69SVladimir Kotal# language governing permissions and limitations under the License.
102d57dc69SVladimir Kotal#
112d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each
122d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt.
132d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the
142d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying
152d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner]
162d57dc69SVladimir Kotal#
172d57dc69SVladimir Kotal# CDDL HEADER END
182d57dc69SVladimir Kotal#
192d57dc69SVladimir Kotal
202d57dc69SVladimir Kotal#
212d97c0a2SVladimir Kotal# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
222d57dc69SVladimir Kotal#
232d57dc69SVladimir Kotal
242d57dc69SVladimir Kotalimport logging
252d57dc69SVladimir Kotal
262d57dc69SVladimir Kotalfrom requests.exceptions import HTTPError
272d57dc69SVladimir Kotal
282d57dc69SVladimir Kotalfrom .command import Command
292d57dc69SVladimir Kotalfrom .utils import is_web_uri
302d57dc69SVladimir Kotalfrom .exitvals import (
312d57dc69SVladimir Kotal    CONTINUE_EXITVAL,
322d57dc69SVladimir Kotal    SUCCESS_EXITVAL,
332d57dc69SVladimir Kotal    FAILURE_EXITVAL
342d57dc69SVladimir Kotal)
352d57dc69SVladimir Kotalfrom .restful import call_rest_api
362d97c0a2SVladimir Kotalfrom .patterns import PROJECT_SUBST, COMMAND_PROPERTY, URL_SUBST
372d57dc69SVladimir Kotalimport re
382d57dc69SVladimir Kotal
392d57dc69SVladimir Kotal
402d57dc69SVladimir Kotalclass CommandSequenceBase:
412d57dc69SVladimir Kotal    """
422d57dc69SVladimir Kotal    Wrap the run of a set of Command instances.
432d57dc69SVladimir Kotal
442d57dc69SVladimir Kotal    This class intentionally does not contain any logging
452d57dc69SVladimir Kotal    so that it can be passed through Pool.map().
462d57dc69SVladimir Kotal    """
472d57dc69SVladimir Kotal
482d57dc69SVladimir Kotal    def __init__(self, name, commands, loglevel=logging.INFO, cleanup=None,
4989229afdSVladimir Kotal                 driveon=False, url=None, env=None, http_headers=None):
502d57dc69SVladimir Kotal        self.name = name
512d57dc69SVladimir Kotal        self.commands = commands
522d57dc69SVladimir Kotal        self.failed = False
532d57dc69SVladimir Kotal        self.retcodes = {}
542d57dc69SVladimir Kotal        self.outputs = {}
552d57dc69SVladimir Kotal        if cleanup and not isinstance(cleanup, list):
562d57dc69SVladimir Kotal            raise Exception("cleanup is not a list of commands")
572d57dc69SVladimir Kotal
582d57dc69SVladimir Kotal        self.cleanup = cleanup
592d57dc69SVladimir Kotal        self.loglevel = loglevel
602d57dc69SVladimir Kotal        self.driveon = driveon
61b2d29daeSVladimir Kotal        self.env = env
6289229afdSVladimir Kotal        self.http_headers = http_headers
632d57dc69SVladimir Kotal
642d97c0a2SVladimir Kotal        self.url = url
652d97c0a2SVladimir Kotal
662d57dc69SVladimir Kotal    def __str__(self):
672d57dc69SVladimir Kotal        return str(self.name)
682d57dc69SVladimir Kotal
692d57dc69SVladimir Kotal    def get_cmd_output(self, cmd, indent=""):
70b2d29daeSVladimir Kotal        """
71b2d29daeSVladimir Kotal        :param cmd: command
72b2d29daeSVladimir Kotal        :param indent: prefix for each line
73b2d29daeSVladimir Kotal        :return: command output as string
74b2d29daeSVladimir Kotal        """
75b2d29daeSVladimir Kotal
762d57dc69SVladimir Kotal        str = ""
772d57dc69SVladimir Kotal        for line in self.outputs.get(cmd, []):
782d57dc69SVladimir Kotal            str += '{}{}'.format(indent, line)
792d57dc69SVladimir Kotal
802d57dc69SVladimir Kotal        return str
812d57dc69SVladimir Kotal
822d57dc69SVladimir Kotal    def fill(self, retcodes, outputs, failed):
832d57dc69SVladimir Kotal        self.retcodes = retcodes
842d57dc69SVladimir Kotal        self.outputs = outputs
852d57dc69SVladimir Kotal        self.failed = failed
862d57dc69SVladimir Kotal
872d57dc69SVladimir Kotal
882d57dc69SVladimir Kotalclass CommandSequence(CommandSequenceBase):
892d57dc69SVladimir Kotal
902d57dc69SVladimir Kotal    re_program = re.compile('ERROR[:]*\\s+')
912d57dc69SVladimir Kotal
922d57dc69SVladimir Kotal    def __init__(self, base):
932d57dc69SVladimir Kotal        super().__init__(base.name, base.commands, loglevel=base.loglevel,
942d97c0a2SVladimir Kotal                         cleanup=base.cleanup, driveon=base.driveon,
95*b369c884SVladimir Kotal                         url=base.url, env=base.env,
96*b369c884SVladimir Kotal                         http_headers=base.http_headers)
972d57dc69SVladimir Kotal
982d57dc69SVladimir Kotal        self.logger = logging.getLogger(__name__)
992d57dc69SVladimir Kotal        self.logger.setLevel(base.loglevel)
1002d57dc69SVladimir Kotal
1012d57dc69SVladimir Kotal    def run_command(self, cmd):
1022d57dc69SVladimir Kotal        """
1032d57dc69SVladimir Kotal        Execute a command and return its return code.
1042d57dc69SVladimir Kotal        """
1052d57dc69SVladimir Kotal        cmd.execute()
1062d57dc69SVladimir Kotal        self.retcodes[str(cmd)] = cmd.getretcode()
1072d57dc69SVladimir Kotal        self.outputs[str(cmd)] = cmd.getoutput()
1082d57dc69SVladimir Kotal
1092d57dc69SVladimir Kotal        return cmd.getretcode()
1102d57dc69SVladimir Kotal
1112d57dc69SVladimir Kotal    def run(self):
1122d57dc69SVladimir Kotal        """
1132d57dc69SVladimir Kotal        Run the sequence of commands and capture their output and return code.
1142d57dc69SVladimir Kotal        First command that returns code other than 0 terminates the sequence.
1152d57dc69SVladimir Kotal        If the command has return code 2, the sequence will be terminated
1162d57dc69SVladimir Kotal        however it will not be treated as error (unless the 'driveon' parameter
1172d57dc69SVladimir Kotal        is True).
1182d57dc69SVladimir Kotal
1192d57dc69SVladimir Kotal        If a command contains PROJECT_SUBST pattern, it will be replaced
1202d57dc69SVladimir Kotal        by project name, otherwise project name will be appended to the
1212d57dc69SVladimir Kotal        argument list of the command.
1222d57dc69SVladimir Kotal
1232d57dc69SVladimir Kotal        Any command entry that is a URI, will be used to submit RESTful API
1242d57dc69SVladimir Kotal        request.
1252d57dc69SVladimir Kotal        """
1262d57dc69SVladimir Kotal
1272d57dc69SVladimir Kotal        for command in self.commands:
1282d97c0a2SVladimir Kotal            cmd_value = command.get(COMMAND_PROPERTY)[0]
1292d97c0a2SVladimir Kotal            if cmd_value.startswith(URL_SUBST) or is_web_uri(cmd_value):
1302d57dc69SVladimir Kotal                try:
1312d97c0a2SVladimir Kotal                    call_rest_api(command, {PROJECT_SUBST: self.name,
13289229afdSVladimir Kotal                                            URL_SUBST: self.url},
13389229afdSVladimir Kotal                                  self.http_headers)
1342d57dc69SVladimir Kotal                except HTTPError as e:
1352d57dc69SVladimir Kotal                    self.logger.error("RESTful command {} failed: {}".
1362d57dc69SVladimir Kotal                                      format(command, e))
1372d57dc69SVladimir Kotal                    self.failed = True
1382d57dc69SVladimir Kotal                    self.retcodes[str(command)] = FAILURE_EXITVAL
1392d57dc69SVladimir Kotal
1402d57dc69SVladimir Kotal                    break
1412d57dc69SVladimir Kotal            else:
1422d57dc69SVladimir Kotal                command_args = command.get(COMMAND_PROPERTY)
1432d57dc69SVladimir Kotal                command = Command(command_args,
1442d57dc69SVladimir Kotal                                  env_vars=command.get("env"),
145b2d29daeSVladimir Kotal                                  logger=self.logger,
1462d57dc69SVladimir Kotal                                  resource_limits=command.get("limits"),
1472d97c0a2SVladimir Kotal                                  args_subst={PROJECT_SUBST: self.name,
1482d97c0a2SVladimir Kotal                                              URL_SUBST: self.url},
1492d57dc69SVladimir Kotal                                  args_append=[self.name], excl_subst=True)
1502d57dc69SVladimir Kotal                retcode = self.run_command(command)
1512d57dc69SVladimir Kotal
1522d57dc69SVladimir Kotal                # If a command exits with non-zero return code,
1532d57dc69SVladimir Kotal                # terminate the sequence of commands.
1542d57dc69SVladimir Kotal                if retcode != SUCCESS_EXITVAL:
1552d57dc69SVladimir Kotal                    if retcode == CONTINUE_EXITVAL:
1562d57dc69SVladimir Kotal                        if not self.driveon:
1572d57dc69SVladimir Kotal                            self.logger.debug("command '{}' for project {} "
1582d57dc69SVladimir Kotal                                              "requested break".
1592d57dc69SVladimir Kotal                                              format(command, self.name))
1602d57dc69SVladimir Kotal                            self.run_cleanup()
1612d57dc69SVladimir Kotal                        else:
1622d57dc69SVladimir Kotal                            self.logger.debug("command '{}' for project {} "
1632d57dc69SVladimir Kotal                                              "requested break however "
1642d57dc69SVladimir Kotal                                              "the 'driveon' option is set "
1652d57dc69SVladimir Kotal                                              "so driving on.".
1662d57dc69SVladimir Kotal                                              format(command, self.name))
1672d57dc69SVladimir Kotal                            continue
1682d57dc69SVladimir Kotal                    else:
169923e5a3aSVladimir Kotal                        self.logger.error("command '{}' for project {} failed "
170923e5a3aSVladimir Kotal                                          "with code {}, breaking".
171923e5a3aSVladimir Kotal                                          format(command, self.name, retcode))
1722d57dc69SVladimir Kotal                        self.failed = True
1732d57dc69SVladimir Kotal                        self.run_cleanup()
1742d57dc69SVladimir Kotal
1752d57dc69SVladimir Kotal                    break
1762d57dc69SVladimir Kotal
1772d57dc69SVladimir Kotal    def run_cleanup(self):
1782d57dc69SVladimir Kotal        """
1792d57dc69SVladimir Kotal        Call cleanup sequence in case the command sequence failed
1802d57dc69SVladimir Kotal        or termination was requested.
1812d57dc69SVladimir Kotal        """
1822d57dc69SVladimir Kotal        if self.cleanup is None:
1832d57dc69SVladimir Kotal            return
1842d57dc69SVladimir Kotal
1852d57dc69SVladimir Kotal        for cleanup_cmd in self.cleanup:
1862d97c0a2SVladimir Kotal            arg0 = cleanup_cmd.get(COMMAND_PROPERTY)[0]
1872d97c0a2SVladimir Kotal            if arg0.startswith(URL_SUBST) or is_web_uri(arg0):
1882d57dc69SVladimir Kotal                try:
1892d97c0a2SVladimir Kotal                    call_rest_api(cleanup_cmd, {PROJECT_SUBST: self.name,
190*b369c884SVladimir Kotal                                                URL_SUBST: self.url},
191*b369c884SVladimir Kotal                                  self.http_headers)
1922d57dc69SVladimir Kotal                except HTTPError as e:
1932d57dc69SVladimir Kotal                    self.logger.error("RESTful command {} failed: {}".
1942d57dc69SVladimir Kotal                                      format(cleanup_cmd, e))
1952d57dc69SVladimir Kotal            else:
1962d57dc69SVladimir Kotal                command_args = cleanup_cmd.get(COMMAND_PROPERTY)
1972d57dc69SVladimir Kotal                self.logger.debug("Running cleanup command '{}'".
1982d57dc69SVladimir Kotal                                  format(command_args))
1992d57dc69SVladimir Kotal                cmd = Command(command_args,
200b2d29daeSVladimir Kotal                              logger=self.logger,
2012d97c0a2SVladimir Kotal                              args_subst={PROJECT_SUBST: self.name,
2022d97c0a2SVladimir Kotal                                          URL_SUBST: self.url},
2032d57dc69SVladimir Kotal                              args_append=[self.name], excl_subst=True)
2042d57dc69SVladimir Kotal                cmd.execute()
2052d57dc69SVladimir Kotal                if cmd.getretcode() != SUCCESS_EXITVAL:
2062d57dc69SVladimir Kotal                    self.logger.error("cleanup command '{}' failed with "
2072d57dc69SVladimir Kotal                                      "code {}".
2082d57dc69SVladimir Kotal                                      format(cmd.cmd, cmd.getretcode()))
2092d57dc69SVladimir Kotal                    self.logger.info('output: {}'.format(cmd.getoutputstr()))
2102d57dc69SVladimir Kotal
211b2d29daeSVladimir Kotal    def print_outputs(self, logger, loglevel=logging.INFO, lines=False):
212b2d29daeSVladimir Kotal        """
213b2d29daeSVladimir Kotal        Print command outputs.
214b2d29daeSVladimir Kotal        """
215b2d29daeSVladimir Kotal
216b2d29daeSVladimir Kotal        logger.debug("Output for project '{}':".format(self.name))
217b2d29daeSVladimir Kotal        for cmd in self.outputs.keys():
218b2d29daeSVladimir Kotal            if self.outputs[cmd] and len(self.outputs[cmd]) > 0:
219b2d29daeSVladimir Kotal                if lines:
220b2d29daeSVladimir Kotal                    logger.log(loglevel, "Output from '{}':".format(cmd))
221b2d29daeSVladimir Kotal                    logger.log(loglevel, '{}'.format(self.get_cmd_output(cmd)))
222b2d29daeSVladimir Kotal                else:
223b2d29daeSVladimir Kotal                    logger.log(loglevel, "'{}': {}".
224b2d29daeSVladimir Kotal                               format(cmd, self.outputs[cmd]))
225b2d29daeSVladimir Kotal
2262d57dc69SVladimir Kotal    def check(self, ignore_errors):
2272d57dc69SVladimir Kotal        """
2282d57dc69SVladimir Kotal        Check the output of the commands and perform logging.
2292d57dc69SVladimir Kotal
2305a30ebc3SVladimir Kotal        Return SUCCESS_EXITVAL on success, 1 if error was detected.
2312d57dc69SVladimir Kotal        """
2322d57dc69SVladimir Kotal
2332d57dc69SVladimir Kotal        ret = SUCCESS_EXITVAL
234b2d29daeSVladimir Kotal        self.print_outputs(self.logger, loglevel=logging.DEBUG)
2352d57dc69SVladimir Kotal
2365a30ebc3SVladimir Kotal        if ignore_errors and self.name in ignore_errors:
2372d57dc69SVladimir Kotal            self.logger.debug("errors of project '{}' ignored".
2382d57dc69SVladimir Kotal                              format(self.name))
2392d57dc69SVladimir Kotal            return
2402d57dc69SVladimir Kotal
2412d57dc69SVladimir Kotal        self.logger.debug("retcodes = {}".format(self.retcodes))
2422d57dc69SVladimir Kotal        if any(rv != SUCCESS_EXITVAL and rv != CONTINUE_EXITVAL
2432d57dc69SVladimir Kotal               for rv in self.retcodes.values()):
2442d57dc69SVladimir Kotal            ret = 1
2452d57dc69SVladimir Kotal            self.logger.error("processing of project '{}' failed".
2462d57dc69SVladimir Kotal                              format(self))
2472d57dc69SVladimir Kotal            indent = "  "
2482d57dc69SVladimir Kotal            self.logger.error("{}failed commands:".format(indent))
2492d57dc69SVladimir Kotal            failed_cmds = {k: v for k, v in
2502d57dc69SVladimir Kotal                           self.retcodes.items() if v != SUCCESS_EXITVAL}
2512d57dc69SVladimir Kotal            indent = "    "
2522d57dc69SVladimir Kotal            for cmd in failed_cmds.keys():
2532d57dc69SVladimir Kotal                self.logger.error("{}'{}': {}".
2542d57dc69SVladimir Kotal                                  format(indent, cmd, failed_cmds[cmd]))
2552d57dc69SVladimir Kotal                out = self.get_cmd_output(cmd,
2562d57dc69SVladimir Kotal                                          indent=indent + "  ")
2572d57dc69SVladimir Kotal                if out:
2582d57dc69SVladimir Kotal                    self.logger.error(out)
2592d57dc69SVladimir Kotal            self.logger.error("")
2602d57dc69SVladimir Kotal
2612d57dc69SVladimir Kotal        errored_cmds = {k: v for k, v in self.outputs.items()
2622d57dc69SVladimir Kotal                        if self.re_program.match(str(v))}
2632d57dc69SVladimir Kotal        if len(errored_cmds) > 0:
2642d57dc69SVladimir Kotal            ret = 1
2652d57dc69SVladimir Kotal            self.logger.error("Command output in project '{}'"
2662d57dc69SVladimir Kotal                              " contains errors:".format(self.name))
2672d57dc69SVladimir Kotal            indent = "  "
2682d57dc69SVladimir Kotal            for cmd in errored_cmds.keys():
2692d57dc69SVladimir Kotal                self.logger.error("{}{}".format(indent, cmd))
2702d57dc69SVladimir Kotal                out = self.get_cmd_output(cmd,
2712d57dc69SVladimir Kotal                                          indent=indent + "  ")
2722d57dc69SVladimir Kotal                if out:
2732d57dc69SVladimir Kotal                    self.logger.error(out)
2742d57dc69SVladimir Kotal                self.logger.error("")
2752d57dc69SVladimir Kotal
2762d57dc69SVladimir Kotal        return ret
277