"""Support for ApachePlease note: The functions in here are Debian-specific. Placing them in thisseparate file will allow them to load only on Debian-based systems, while stillloading under the ``apache`` namespace."""importloggingimportosimportsalt.utils.decorators.pathimportsalt.utils.pathlog=logging.getLogger(__name__)__virtualname__="apache"SITE_ENABLED_DIR="/etc/apache2/sites-enabled"
[docs]def__virtual__():""" Only load the module if apache is installed """cmd=_detect_os()ifsalt.utils.path.which(cmd)and__grains__["os_family"]=="Debian":return__virtualname__return(False,"apache execution module not loaded: apache not installed.")
def_detect_os():""" Apache commands and paths differ depending on packaging """# TODO: Add pillar support for the apachectl locationif__grains__["os_family"]=="RedHat":return"apachectl"if__grains__["os_family"]=="Debian":return"apache2ctl"return"apachectl"
[docs]defcheck_site_enabled(site):""" Checks to see if the specific site symlink is in /etc/apache2/sites-enabled. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.check_site_enabled example.com salt '*' apache.check_site_enabled example.com.conf """ifsite.endswith(".conf"):site_file=siteelse:site_file=f"{site}.conf"ifos.path.islink(f"{SITE_ENABLED_DIR}/{site_file}"):returnTrueifsite=="default"andos.path.islink(f"{SITE_ENABLED_DIR}/000-{site_file}"):returnTruereturnFalse
[docs]defa2ensite(site):""" Runs a2ensite for the given site. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.a2ensite example.com """ret={}command=["a2ensite",site]try:status=__salt__["cmd.retcode"](command,python_shell=False)exceptExceptionase:# pylint: disable=broad-exceptreturneret["Name"]="Apache2 Enable Site"ret["Site"]=siteifstatus==1:ret["Status"]=f"Site {site} Not found"elifstatus==0:ret["Status"]=f"Site {site} enabled"else:ret["Status"]=statusreturnret
[docs]defa2dissite(site):""" Runs a2dissite for the given site. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.a2dissite example.com """ret={}command=["a2dissite",site]try:status=__salt__["cmd.retcode"](command,python_shell=False)exceptExceptionase:# pylint: disable=broad-exceptreturneret["Name"]="Apache2 Disable Site"ret["Site"]=siteifstatus==256:ret["Status"]=f"Site {site} Not found"elifstatus==0:ret["Status"]=f"Site {site} disabled"else:ret["Status"]=statusreturnret
[docs]defcheck_mod_enabled(mod):""" Checks to see if the specific mod symlink is in /etc/apache2/mods-enabled. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.check_mod_enabled status salt '*' apache.check_mod_enabled status.load salt '*' apache.check_mod_enabled status.conf """ifmod.endswith(".load")ormod.endswith(".conf"):mod_file=modelse:mod_file=f"{mod}.load"returnos.path.islink(f"/etc/apache2/mods-enabled/{mod_file}")
[docs]defa2enmod(mod):""" Runs a2enmod for the given mod. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.a2enmod vhost_alias """ret={}command=["a2enmod",mod]try:status=__salt__["cmd.retcode"](command,python_shell=False)exceptExceptionase:# pylint: disable=broad-exceptreturneret["Name"]="Apache2 Enable Mod"ret["Mod"]=modifstatus==1:ret["Status"]=f"Mod {mod} Not found"elifstatus==0:ret["Status"]=f"Mod {mod} enabled"else:ret["Status"]=statusreturnret
[docs]defa2dismod(mod):""" Runs a2dismod for the given mod. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.a2dismod vhost_alias """ret={}command=["a2dismod",mod]try:status=__salt__["cmd.retcode"](command,python_shell=False)exceptExceptionase:# pylint: disable=broad-exceptreturneret["Name"]="Apache2 Disable Mod"ret["Mod"]=modifstatus==256:ret["Status"]=f"Mod {mod} Not found"elifstatus==0:ret["Status"]=f"Mod {mod} disabled"else:ret["Status"]=statusreturnret
[docs]defcheck_conf_enabled(conf):""" .. versionadded:: 2016.3.0 Checks to see if the specific conf symlink is in /etc/apache2/conf-enabled. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.check_conf_enabled security salt '*' apache.check_conf_enabled security.conf """ifconf.endswith(".conf"):conf_file=confelse:conf_file=f"{conf}.conf"returnos.path.islink(f"/etc/apache2/conf-enabled/{conf_file}")
@salt.utils.decorators.path.which("a2enconf")defa2enconf(conf):""" .. versionadded:: 2016.3.0 Runs a2enconf for the given conf. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.a2enconf security """ret={}command=["a2enconf",conf]try:status=__salt__["cmd.retcode"](command,python_shell=False)exceptExceptionase:# pylint: disable=broad-exceptreturneret["Name"]="Apache2 Enable Conf"ret["Conf"]=confifstatus==1:ret["Status"]=f"Conf {conf} Not found"elifstatus==0:ret["Status"]=f"Conf {conf} enabled"else:ret["Status"]=statusreturnret@salt.utils.decorators.path.which("a2disconf")defa2disconf(conf):""" .. versionadded:: 2016.3.0 Runs a2disconf for the given conf. This will only be functional on Debian-based operating systems (Ubuntu, Mint, etc). CLI Examples: .. code-block:: bash salt '*' apache.a2disconf security """ret={}command=["a2disconf",conf]try:status=__salt__["cmd.retcode"](command,python_shell=False)exceptExceptionase:# pylint: disable=broad-exceptreturneret["Name"]="Apache2 Disable Conf"ret["Conf"]=confifstatus==256:ret["Status"]=f"Conf {conf} Not found"elifstatus==0:ret["Status"]=f"Conf {conf} disabled"else:ret["Status"]=statusreturnret