apache-vhost-file

Description

Apache vhost configuration

Variables

Name Type Default Description

path

string --

The path to the file. Required

custom_log

string ${APACHE_LOG_DIR}/access.log combined

This is in the format: "CustomLog log_location log_format", and the 'log_format' must refer to a LogFormat defined in the Apache conf.

document_root

string /var/www/html

The document root.

error_log

string ${APACHE_LOG_DIR}/error.log

The error log file for this vhost.

folder_directives

list []

A list of 'Directory', 'DirectoryMatch', 'Files', 'FilesMatch', 'Location', 'LocationMatch' directives.

group

string --

The group of the file.

listen_ip

string _default_

The address to listen to, can be any of the following, optionally followed by a colon and a port number (or *): - The IP address of the virtual host; - A fully qualified domain name for the IP address of the virtual host (not recommended); - The character *, which acts as a wildcard and matches any IP address. - The string default, which is an alias for *

listen_port

integer 80

The port to listen to.

listen_port_https

integer 443

The port to listen for https.

mode

string --

The permissions of the file.

owner

string --

The owner of the file.

server_admin

string --

The server admin email.

server_aliases

list --

A list of server aliases.

server_name

string localhost

The server name.

ssl_ca_certificate_file

string --

The ssl ca certificate file.

ssl_certificate_chain_file

string --

The ssl certificate chain file.

ssl_certificate_file

string --

The ssl certificate file.

use_https

boolean --

Whether to use https.

All http traffic will be redirected to https.

use_letsencrypt

boolean True

Whether to use letsencrypt certificates.

If this is the case, and 'use_https' is selected, the ssl_* certificate paths don't need to be provided, and the location to renew them will be added automatically.

Code

doc:
  short_help: Apache vhost configuration

args:
  listen_ip:
    doc:
      short_help: The ip to listen to.
      help: |
        The address to listen to, can be any of the following, optionally followed by a colon and a port number (or *):
          - The IP address of the virtual host;
          - A fully qualified domain name for the IP address of the virtual host (not recommended);
          - The character *, which acts as a wildcard and matches any IP address.
          - The string _default_, which is an alias for *
      references:
      - '[Apache VirtualHost documentation](https://httpd.apache.org/docs/current/mod/core.html#virtualhost)'
    type: string
    required: false
    default: _default_
  use_https:
    doc:
      short_help: Whether to use https.
      help: |
        Whether to use https.

        All http traffic will be redirected to https.
    type: boolean
    required: false
#    default: true
  use_letsencrypt:
    doc:
      short_help: Whether to use letsencrypt certificates.
      help: |
        Whether to use [letsencrypt](https://letsencrypt.org/) certificates.

        If this is the case, and 'use_https' is selected, the ssl_* certificate
        paths don't need to be provided, and the location to renew them will be added
        automatically.
      references:
      - '[LetsEncrypt homepage](https://letsencrypt.org/)'
    type: boolean
    required: false
    default: true
    cli:
      enabled: false
  listen_port_https:
    doc:
      short_help: The port to listen for https.
    type: integer
    default: 443
    required: false
  listen_port:
    doc:
      short_help: The port to listen to.
    type: integer
    required: false
    default: 80
  server_aliases:
    doc:
      short_help: A list of server aliases.
    type: list
    schema:
      type: string
    required: false
    cli:
      param_decls:
      - --alias
      metavar: ALIAS
  server_admin:
    doc:
      short_help: The server admin email.
    type: string
    required: false
    cli:
      metavar: EMAIL
  error_log:
    doc:
      short_help: The error log file for this vhost.
      references:
      - '[Apache logs documentation](https://httpd.apache.org/docs/2.4/logs.html)'
    type: string
    required: false
    default: ${APACHE_LOG_DIR}/error.log
    cli:
      metavar: PATH
  custom_log:
    doc:
      short_help: The custom log.
      help: |
        This is in the format: "CustomLog log_location log_format", and the 'log_format' must refer to a LogFormat defined in the Apache conf.
      references:
      - '[Apache logs documentation](https://httpd.apache.org/docs/2.4/logs.html)'
    type: string
    default: ${APACHE_LOG_DIR}/access.log combined
    cli:
      metavar: LOG_SPEC
  server_name:
    doc:
      short_help: The server name.
    type: string
    required: false
    default: localhost
  document_root:
    doc:
      short_help: The document root.
      references:
      - '[Apache vhost examples](https://httpd.apache.org/docs/2.4/vhosts/examples.html)'
      - '[Apache document root documentation](https://httpd.apache.org/docs/2.4/mod/core.html#documentroot)'
    type: string
    required: false
    default: /var/www/html
    cli:
      metavar: PATH
  ssl_certificate_file:
    doc:
      short_help: The ssl certificate file.
    type: string
    required: false
    cli:
      metavar: PATH
  ssl_ca_certificate_file:
    doc:
      short_help: The ssl ca certificate file.
    type: string
    required: false
    cli:
      metavar: PATH
  ssl_certificate_chain_file:
    doc:
      short_help: The ssl certificate chain file.
    type: string
    required: false
    cli:
      metavar: PATH
  folder_directives:
    doc:
      short_help: A list of 'Directory', 'DirectoryMatch', 'Files', 'FilesMatch',
        'Location', 'LocationMatch' directives.
      references:
      - "[Apache 'Directory' directive doc](https://httpd.apache.org/docs/2.4/mod/core.html)"
    type: list
    default: []
    cli:
      enabled: false
    schema:
      type: dict
      schema:
        directive_type:
          required: true
          type: string
          allowed:
          - Directory
          - DirectoryMatch
          - Files
          - FilesMatch
          - Location
          - LocationMatch
        path:
          required: true
          type: string
          empty: false
        Options:
          required: false
          empty: false
          type: string
        AllowOverride:
          required: false
          empty: false
          type: string
        DirectoryIndex:
          required: false
          empty: false
          type: string
        Allow:
          required: false
          type: string
        Require:
          required: false
          type: string
          empty: false
        AuthType:
          required: false
          type: string
          empty: false
        AuthName:
          required: false
          type: string
          empty: false
        AuthUserFile:
          required: false
          type: string
          empty: false
        SetHandler:
          required: false
          type: string
          empty: false
  _import:
  - file-with-content
frecklets:
- file-with-content:
    path: '{{:: path ::}}'
    group: '{{:: group ::}}'
    owner: '{{:: owner ::}}'
    mode: '{{:: mode ::}}'
    content: |-
      {%:: if use_https ::%}
      <VirtualHost {{:: listen_ip ::}}:{{:: listen_port ::}}>
        ServerName {{:: server_name ::}}
        {%:: if server_aliases ::%}
        {%:: for alias in server_aliases ::%}  ServerAlias {{:: alias ::}}
        {%:: endfor ::%}{%:: endif ::%}

        Redirect / https://{{:: server_name ::}}{%:: if listen_port_https != 443 ::%}:{{:: listen_port_https ::}}{%:: endif ::%}
      </VirtualHost>
      {%:: endif ::%}
      <VirtualHost {{:: listen_ip ::}}:{%:: if not use_https ::%}{{:: listen_port ::}}{%:: else ::%}{{:: listen_port_https ::}}{%:: endif ::%}>
        ServerName {{:: server_name ::}}
      {%:: if server_aliases ::%}
      {%:: for alias in server_aliases ::%}  ServerAlias {{:: alias ::}}
      {%:: endfor ::%}{%:: endif ::%}
        DocumentRoot "{{:: document_root ::}}"

        {%:: if server_admin ::%}ServerAdmin {{:: server_admin ::}}{%:: endif ::%}
        {%:: if error_log ::%}ErrorLog {{:: error_log ::}}{%:: endif ::%}
        {%:: if custom_log ::%}CustomLog {{:: custom_log ::}}{%:: endif ::%}

      {%:: if use_https ::%}
        SSLEngine on
        SSLVerifyClient none
        SSLProtocol TLSv1.2

      {%:: if use_letsencrypt ::%}
      SSLCertificateFile /etc/letsencrypt/live/{{:: server_name ::}}/cert.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/{{:: server_name ::}}/privkey.pem
      SSLCertificateChainFile /etc/letsencrypt/live/{{:: server_name ::}}/fullchain.pem
      {%:: else ::%}
      {%:: if ssl_certificate_file ::%}SSLCertificateFile {{:: ssl_certificate_file ::}}{%:: endif ::%}
      {%:: if ssl_ca_certificate_file ::%}SSLCertificateFile {{:: ssl_ca_certificate_file ::}}{%:: endif ::%}
      {%:: if ssl_certificate_chain_file ::%}SSLCertificateChainFile {{:: ssl_certificate_chain_file ::}}{%:: endif ::%}
      {%:: endif ::%}
      {%:: endif ::%}
      {%:: for d in folder_directives ::%}
        <{{:: d.directive_type ::}} "{{:: d.path ::}}">
      {%:: for var_name in ['AllowOverride', 'Options', 'Require', 'Allow', 'AuthType', 'AuthName', 'AuthUserFile', 'SetHandler', 'DirectoryIndex'] ::%}
          {%:: if d[var_name] ::%}{{:: var_name ::}} {{:: d[var_name] ::}}{%:: endif ::%}{%:: endfor ::%}
        </{{:: d.directive_type ::}}>
      {%:: endfor ::%}
      </VirtualHost>
frecklecute apache-vhost-file --help

Usage: frecklecute apache-vhost-file [OPTIONS] PATH

  Apache vhost configuration

Options:
  --custom-log LOG_SPEC           The custom log.
  --document-root PATH            The document root.
  --error-log PATH                The error log file for this vhost.
  --group GROUP                   The group of the file.
  --listen-ip LISTEN_IP           The ip to listen to.
  --listen-port LISTEN_PORT       The port to listen to.
  --listen-port-https LISTEN_PORT_HTTPS
                                  The port to listen for https.
  --mode MODE                     The permissions of the file.
  --owner USER                    The owner of the file.
  --server-admin EMAIL            The server admin email.
  --alias ALIAS                   A list of server aliases.
  --server-name SERVER_NAME       The server name.
  --ssl-ca-certificate-file PATH  The ssl ca certificate file.
  --ssl-certificate-chain-file PATH
                                  The ssl certificate chain file.
  --ssl-certificate-file PATH     The ssl certificate file.
  --use-https / --no-use-https    Whether to use https.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.apache_vhost_file.ApacheVhostFile
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class ApacheVhostFile(AutoPycklet):
    """Apache vhost configuration

       Args:
         custom_log: The custom log.
         document_root: The document root.
         error_log: The error log file for this vhost.
         folder_directives: A list of 'Directory', 'DirectoryMatch', 'Files', 'FilesMatch', 'Location', 'LocationMatch' directives.
         group: The group of the file.
         listen_ip: The ip to listen to.
         listen_port: The port to listen to.
         listen_port_https: The port to listen for https.
         mode: The permissions of the file.
         owner: The owner of the file.
         path: The path to the file.
         server_admin: The server admin email.
         server_aliases: A list of server aliases.
         server_name: The server name.
         ssl_ca_certificate_file: The ssl ca certificate file.
         ssl_certificate_chain_file: The ssl certificate chain file.
         ssl_certificate_file: The ssl certificate file.
         use_https: Whether to use https.
         use_letsencrypt: Whether to use letsencrypt certificates.

    """

    FRECKLET_ID = "apache-vhost-file"

    custom_log: str = None
    document_root: str = None
    error_log: str = None
    folder_directives: List = None
    group: str = None
    listen_ip: str = None
    listen_port: int = None
    listen_port_https: int = None
    mode: str = None
    owner: str = None
    path: str = None
    server_admin: str = None
    server_aliases: List = None
    server_name: str = None
    ssl_ca_certificate_file: str = None
    ssl_certificate_chain_file: str = None
    ssl_certificate_file: str = None
    use_https: bool = None
    use_letsencrypt: bool = None


    def __post_init__(self):
        super(ApacheVhostFile, self).__init__(var_names=["custom_log", "document_root", "error_log", "folder_directives", "group", "listen_ip", "listen_port", "listen_port_https", "mode", "owner", "path", "server_admin", "server_aliases", "server_name", "ssl_ca_certificate_file", "ssl_certificate_chain_file", "ssl_certificate_file", "use_https", "use_letsencrypt"])


frecklet_class = ApacheVhostFile
# -*- coding: utf-8 -*-


#
# module path: pycklets.apache_vhost_file.ApacheVhostFile
#


from pyckles import AutoPycklet

class ApacheVhostFile(AutoPycklet):
    """Apache vhost configuration

       Args:
         custom_log: The custom log.
         document_root: The document root.
         error_log: The error log file for this vhost.
         folder_directives: A list of 'Directory', 'DirectoryMatch', 'Files', 'FilesMatch', 'Location', 'LocationMatch' directives.
         group: The group of the file.
         listen_ip: The ip to listen to.
         listen_port: The port to listen to.
         listen_port_https: The port to listen for https.
         mode: The permissions of the file.
         owner: The owner of the file.
         path: The path to the file.
         server_admin: The server admin email.
         server_aliases: A list of server aliases.
         server_name: The server name.
         ssl_ca_certificate_file: The ssl ca certificate file.
         ssl_certificate_chain_file: The ssl certificate chain file.
         ssl_certificate_file: The ssl certificate file.
         use_https: Whether to use https.
         use_letsencrypt: Whether to use letsencrypt certificates.

    """

    FRECKLET_ID = "apache-vhost-file"

    def __init__(self, custom_log="${APACHE_LOG_DIR}/access.log combined", document_root="/var/www/html", error_log="${APACHE_LOG_DIR}/error.log", folder_directives=None, group=None, listen_ip="_default_", listen_port=80, listen_port_https=443, mode=None, owner=None, path=None, server_admin=None, server_aliases=None, server_name="localhost", ssl_ca_certificate_file=None, ssl_certificate_chain_file=None, ssl_certificate_file=None, use_https=None, use_letsencrypt=True):

        super(ApacheVhostFile, self).__init__(var_names=["custom_log", "document_root", "error_log", "folder_directives", "group", "listen_ip", "listen_port", "listen_port_https", "mode", "owner", "path", "server_admin", "server_aliases", "server_name", "ssl_ca_certificate_file", "ssl_certificate_chain_file", "ssl_certificate_file", "use_https", "use_letsencrypt"])
        self._custom_log = custom_log
        self._document_root = document_root
        self._error_log = error_log
        self._folder_directives = folder_directives
        self._group = group
        self._listen_ip = listen_ip
        self._listen_port = listen_port
        self._listen_port_https = listen_port_https
        self._mode = mode
        self._owner = owner
        self._path = path
        self._server_admin = server_admin
        self._server_aliases = server_aliases
        self._server_name = server_name
        self._ssl_ca_certificate_file = ssl_ca_certificate_file
        self._ssl_certificate_chain_file = ssl_certificate_chain_file
        self._ssl_certificate_file = ssl_certificate_file
        self._use_https = use_https
        self._use_letsencrypt = use_letsencrypt

    @property
    def custom_log(self):
        return self._custom_log

    @custom_log.setter
    def custom_log(self, custom_log):
        self._custom_log = custom_log

    @property
    def document_root(self):
        return self._document_root

    @document_root.setter
    def document_root(self, document_root):
        self._document_root = document_root

    @property
    def error_log(self):
        return self._error_log

    @error_log.setter
    def error_log(self, error_log):
        self._error_log = error_log

    @property
    def folder_directives(self):
        return self._folder_directives

    @folder_directives.setter
    def folder_directives(self, folder_directives):
        self._folder_directives = folder_directives

    @property
    def group(self):
        return self._group

    @group.setter
    def group(self, group):
        self._group = group

    @property
    def listen_ip(self):
        return self._listen_ip

    @listen_ip.setter
    def listen_ip(self, listen_ip):
        self._listen_ip = listen_ip

    @property
    def listen_port(self):
        return self._listen_port

    @listen_port.setter
    def listen_port(self, listen_port):
        self._listen_port = listen_port

    @property
    def listen_port_https(self):
        return self._listen_port_https

    @listen_port_https.setter
    def listen_port_https(self, listen_port_https):
        self._listen_port_https = listen_port_https

    @property
    def mode(self):
        return self._mode

    @mode.setter
    def mode(self, mode):
        self._mode = mode

    @property
    def owner(self):
        return self._owner

    @owner.setter
    def owner(self, owner):
        self._owner = owner

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        self._path = path

    @property
    def server_admin(self):
        return self._server_admin

    @server_admin.setter
    def server_admin(self, server_admin):
        self._server_admin = server_admin

    @property
    def server_aliases(self):
        return self._server_aliases

    @server_aliases.setter
    def server_aliases(self, server_aliases):
        self._server_aliases = server_aliases

    @property
    def server_name(self):
        return self._server_name

    @server_name.setter
    def server_name(self, server_name):
        self._server_name = server_name

    @property
    def ssl_ca_certificate_file(self):
        return self._ssl_ca_certificate_file

    @ssl_ca_certificate_file.setter
    def ssl_ca_certificate_file(self, ssl_ca_certificate_file):
        self._ssl_ca_certificate_file = ssl_ca_certificate_file

    @property
    def ssl_certificate_chain_file(self):
        return self._ssl_certificate_chain_file

    @ssl_certificate_chain_file.setter
    def ssl_certificate_chain_file(self, ssl_certificate_chain_file):
        self._ssl_certificate_chain_file = ssl_certificate_chain_file

    @property
    def ssl_certificate_file(self):
        return self._ssl_certificate_file

    @ssl_certificate_file.setter
    def ssl_certificate_file(self, ssl_certificate_file):
        self._ssl_certificate_file = ssl_certificate_file

    @property
    def use_https(self):
        return self._use_https

    @use_https.setter
    def use_https(self, use_https):
        self._use_https = use_https

    @property
    def use_letsencrypt(self):
        return self._use_letsencrypt

    @use_letsencrypt.setter
    def use_letsencrypt(self, use_letsencrypt):
        self._use_letsencrypt = use_letsencrypt



frecklet_class = ApacheVhostFile