#!/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() { run_sudo apachectl -S; }

# apache_config_check() - Checks the current server configuration for errors.
apache_config_check() { run_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() { apache_server_run start; }

# apache_server_stop() - Stops the apache server.
apache_server_stop() { apache_server_run stop false; }

# apache_server_rsstart() - Restarts the apache server.
apache_server_restart() { apache_server_run restart; }

# apache_server_reload() - Reloads the apache server configuration.
apache_server_reload() { apache_server_run reload; }

apache_server_run() {
	local cmd=$1
	local check=${2:-true}
	! $check || apache_config_check
	run_sudo service apache2 "$1"
}


######
# 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 <name> [<domain>]		Creates a new site
USAGE
}

# apache_site_new() - Creates a new site.
apache_site_new() {
	min_max_args 1 2 "$@"
	local site=$1
	local sdomain=${2:-$domain}

	[ "$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.$sdomain"
	host "$vhost" || error "$vhost: unable to resolve name"

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

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

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

	apache_config_check
	apache_server_reload

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

	else
		echo "$cert: exists"
	fi
	run 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 Fri Jul 28 14:35:51 PDT 2017 by mcsh d14 v0.23.0.