Esempio n. 1
0
    def test_fill_template_opens_template_and_fills_in_variables(self):
        with mock.patch("__builtin__.open", mock.mock_open(read_data="henk")) as mock_open:
            with mock.patch("django.template.Template") as mock_template:
                mock_template_instance = mock_template.return_value = mock.Mock()

                with mock.patch("django.template.Context") as mock_context:
                    mock_context_instance = mock_context.return_value = mock.Mock()

                    fill_template("sdfdsfdfs", {"a": 1})
                    mock_open.assert_called_once_with("sdfdsfdfs", "r")
                    mock_template.assert_called_once_with("henk")
                    mock_context.assert_called_once_with({"a": 1})

                    mock_template_instance.render.assert_called_once_with(mock_context_instance)
Esempio n. 2
0
def apply_config(config):

    logger.debug("Checking configuration for php_options")
    common.check_vars(config, ["php_options"])

    options = None
    extensions = {}

    logger.debug("Checking configuration for php_options.options")
    # Options is dict, and template determines which keys are valid
    if "options" in config["php_options"]:
        logger.debug("php_options.options found")
        options = config["php_options"]["options"]

    logger.debug("Checking configuration for php_options.extensions")
    # Extensions is a list. We'll map it to a dict here, to give it to the template
    if "extensions" in config["php_options"]:
        logger.debug("php_options.extensions found")
        for item in config["php_options"]["extensions"]:
            logger.debug("Enabling extension '%s'" % item)
            extensions[item] = True

    logger.info("Writing /etc/php5/mods-available/hypernode.ini")
    common.write_file("/etc/php5/mods-available/hypernode.ini",
                      common.fill_template("/etc/hypernode/templates/20.phpini",
                                           {"options": options,
                                            "extensions": extensions}))

    logger.info("Enabling hypernode.ini using php5enmod")
    subprocess.call(["php5enmod", "hypernode/99"])

    logger.info("Restarting PHP5-FPM daemon")
    subprocess.call(["service", "php5-fpm", "restart"])
Esempio n. 3
0
def apply_config(config):

    # There are three cases that we might expect:
    # 1. All ssl-fields are present, filled out and represent a certificate
    #    that passes the openssl-check.
    # 2. None of the ssl-fields are present.
    # 3. Other cases: not all of the ssl-fields are present, or they don't pass
    #    the openssl-check
    #
    # These cases yield the following actions
    # 1. The certificate is written to disk, apache is restarted
    # 2. Any certificate present on disk will be removed, the apache-
    #    configuration for the SSL-vhost will be removed, and apache will be
    #    restarted
    # 3. This is an exception, and means that there is an error in the
    #    configuration

    # We always need an app_name. Not found => exception
    common.check_vars(config, ["app_name"])

    if "ssl_common_name" in config and "ssl_body" in config and "ssl_certificate" in config and "ssl_key_chain" in config:
        logger.debug("Configuring SSL")

        logger.debug("Verifying SSL key and certificate")
        verify_ssl(config["ssl_certificate"], config["ssl_body"], config["ssl_key_chain"])

        template_vars = {'app_name': config['app_name'],
                         'servername': config['ssl_common_name'],
                         'crtpath': CRTPATH}

        if 'ssl_key_chain' in config and config['ssl_key_chain']:
            logger.info("Writing %s", CAPATH)
            common.write_file(CAPATH,
                              config["ssl_key_chain"],
                              umask=0077)

            template_vars['capath'] = CAPATH

        logger.info("Writing %s", CRTPATH)
        common.write_file(CRTPATH,
                          "%s\n\n%s" % (config["ssl_certificate"], config["ssl_body"]),
                          umask=0077)

        logger.info("Writing /etc/apache2/sites-enabled/default-ssl")
        common.write_file("/etc/apache2/sites-enabled/default-ssl",
                          common.fill_template("/etc/hypernode/templates/05.ssl.default-ssl-vhost",
                                               vars=template_vars))

        logger.info("Restarting apache2")
        subprocess.call(["service", "apache2", "restart"])
        return

    elif "ssl_common_name" not in config and "ssl_body" not in config and "ssl_certificate" not in config and "ssl_key_chain" not in config:
        logger.debug("Disabling SSL")
        disable_ssl()
        return
    else:
        raise RuntimeError("Incomplete SSL parameters in configuration")
Esempio n. 4
0
def apply_config(config):

    common.check_vars(config, ["hostnames", "app_name"])

    logger.debug("Setting hostname to %s" % config["app_name"])

    logger.info("Writing /etc/hostname")
    common.write_file("/etc/hostname", config["app_name"])

    logger.info("Writing /etc/hosts from template /etc/hypernode/templates/03.hostname.hosts")
    common.write_file("/etc/hosts",
                      common.fill_template("/etc/hypernode/templates/03.hostname.hosts",
                                           {"hostnames": config["hostnames"],
                                           "app_name": config["app_name"]}))

    logger.info("Calling `hostname`")
    subprocess.call(["hostname", config["app_name"]])

    logger.info("Restarting rsyslog")
    subprocess.call(["service", "rsyslog", "restart"])