#!/bin/bash
# net/tool/apache - Apache Deployment Support

set -e


######
# Native Dependencies

net_tool_apache_client_packages=( )
net_tool_apache_server_packages=( apache2 )


######
# Library Configuration

# net_tool_apache_config_init() - Defines, documents, and initializes
# library settings.
net_tool_apache_config_init() {
	script_setting_vars www_confdir main_conf tmpl tmpl_conf
}

# net_tool_apache_config_check() - Initializes remaining library settings
# and validates their values.
net_tool_apache_config_check() {
	[ -f "$main_conf" ] || warn "$main_conf: does not exist"
	[ -f "$tmpl_conf" ] || warn "$tmpl_conf: does not exist"
}


######
# Apache Configuration CLI

# apache_config_dispatch() - Dispatches apache configuration commands.
apache_config_dispatch() { lib_cmd_dispatch apache_config "$@"; }

# apache_config_usage() - Prints configuration command usage.
apache_config_usage() {
	cat <<USAGE
...
Apache2 Configuration Commands:
	view				Views the server configuration
	check				Verifies server configuration
USAGE
}

# apache_config_view() - Prints the current server configuration.
apache_config_view() { sudo apachectl -S; }

# apache_config_check() - Checks the current server configuration for errors.
apache_config_check() { sudo apachectl configtest; }


######
# Apache Server CLI

# apache_server_dispatch() - Dispatches apache server commands.
apache_server_dispatch() { lib_cmd_dispatch apache_server "$@"; }

# apache_server_usage() - Prints server management command usage.
apache_server_usage() {
	cat <<USAGE
...
Apache2 Server Commands:
	start				Starts server
	stop				Stops server
	restart				Restarts server
	reload				Reloads server configuration
USAGE
}

# apache_server_start() - Starts the apache server.
apache_server_start() { sudo service apache2 start; }

# apache_server_stop() - Stops the apache server.
apache_server_stop() { sudo service apache2 stop; }

# apache_server_rsstart() - Restarts the apache server.
apache_server_restart() { sudo service apache2 restart; }

# apache_server_reload() - Reloads the apache server configuration.
apache_server_reload() { sudo service apache2 reload; }


######
# Apache Site CLI

# apache_site_dispatch() - Dispatches apache site commands.
apache_site_dispatch() { lib_cmd_dispatch apache_site "$@"; }

# apache_site_usage() - Prints site management command usage.
apache_site_usage() {
	cat <<USAGE
...
Apache2 Site Commands:
	new				Creates a new site
USAGE
}

# apache_site_new() - Creates a new site.
apache_site_new() {
	local site
	site=$1
	[ "$site" ] || error "usage: $script <vhost>"
	[ "$PWD" = "$www_confdir" ] || warn "$PWD != $www_confdir"

	local site_conf
	site_conf="$www_confdir/$site.conf"
	[ ! -f "$site_conf" ] || error "$site_conf: exists"

	local vhost
	vhost="$site.$domain"
	host "$vhost" || error "$vhost: unable to resolve name"

	[ -f "$tmpl_conf" ] || error "$tmpl_conf: does not exist"

	# change everything but the SSL cert
	sed \
		-e "s,$tmpl,$site,g" \
		-e "s,live/$site,live/$tmpl," \
		$tmpl_conf >$site_conf

	# include from main configuration file
	if ! grep "Include $site_conf" $main_conf; then
		echo "Include $site_conf" >>$main_conf
	fi
	# create logs
	mkdir -p "../logs/$site"

	apache_config_check
	apache_server_reload

	cert="/etc/letsencrypt/live/$vhost/fullchain.pem"
	if [ ! -d "/etc/letsencrypt/live/$vhost" ]; then
		sudo letsencrypt -d "$vhost" || true
		[ -f "$cert" ] || \
			error "ERROR: letsencrypt failed to obtain a certificate"

	else
		echo "$cert: exists"
	fi
	sed -i -e "s,live/$tmpl,live/$site," $site_conf

	apache_server_reload
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Thu May 4 19:00:13 PDT 2017 by mcsh i7 v0.19.0.