Ejemplo n.º 1
0
    def __init__(self, config_file=None):
        """
        Initialize this class.  Note that a VersionException will be raised
        if the version number contained within the configuration file is
        greater than the version number of the requested IBM Application
        Gateway image.
        """

        super(Container, self).__init__()

        # If a configuration file is specified we need to ensure that the
        # IAG version supports the version of the specified configuration
        # file.
        if config_file is not None:
            with open(config_file, 'r') as stream:
                data = yaml.safe_load(stream)

                if data['version'] > Environment().get("image.tag"):
                    raise VersionException(
                        "The configuration file is not supported "
                        "with the specified image version: {0}".format(
                            Environment().get("image.tag")))

        if run_kubernetes():
            self.client_ = KubernetesContainer(config_file)
        else:
            self.client_ = DockerContainer(config_file=config_file)
Ejemplo n.º 2
0
    def startContainer(self, removeAtExit=True, protocol="https"):
        """
        The following command is used to start the IBM Application Gateway
        container using the supplied configuration.

        removeAtExit: should the container be automatically removed when the
                      script exits?
        """

        image = "{0}:{1}".format(Environment().get("image.name"),
                                 Environment().get("image.tag"))

        logger.info("Starting the container from {0}".format(image))

        logger.info("Protocol to start {0}".format(protocol))

        self.client_.startContainer(image)

        if removeAtExit:
            atexit.register(self.stopContainer)

        # Wait for the container to become healthy.  We should really be using
        # the health of the container, but this takes a while to kick in.  So,
        # we instead poll the https port of the server until we make a
        # successful SSL connection.

        running = False
        attempt = 0

        while not running and attempt < 30:
            time.sleep(1)

            try:
                logger.info("Protocol in loop {0}".format(protocol))
                logger.info("Port in loop {0}".format(self.port(protocol)))
                logger.info("IP in loop {0}".format(self.ipaddr()))
                requests.get("{0}://{1}:{2}".format(protocol, self.ipaddr(),
                                                    self.port(protocol)),
                             verify=False,
                             allow_redirects=False,
                             timeout=2)

                running = True
            except:
                self.client_.reload()
                attempt += 1

        if not running:
            message = "The container failed to start within the allocated time."
            logger.critical(message)

            logger.critical(self.client_.container_.logs())

            raise Exception(message)

        logger.info("The container has started")