#!/bin/bash
#  plone - Plone support

set -e

lib_load 'net/tool/wget'
lib_load 'sys/tool/supervisor'


######
# Settings

net_tool_plone_config_init() {
	lib_setting_vars -ro plone_installer_url_base
	plone_installer_url_base="https://launchpad.net/plone/"

	lib_setting_vars plone_site_list
	plone_site_list="$script_confdir/sites.list"

	# per-site settings
	lib_setting_vars --null plone_site_version plone_site_clients

	# distribution settings
	lib_setting_vars plone_buildout_user plone_daemon_user
	plone_buildout_user=plone_buildout
	plone_daemon_user=plone_daemon

	plone_server_packages=( \
		python-setuptools python-dev libssl-dev \
		libxml2-dev libxslt1-dev libbz2-dev libjpeg62-dev \
		libreadline-dev wv poppler-utils memcached
		)
}


######
# Plone user support

run_as_plone_user() {
	local user=$1
	local name=$2
	shift 2

	local dir
	dir=$(plone_site_path "$name")

	run_pushd "$dir/zeocluster"
	run_sudo_as_user $user "$@"
	run_popd
}
run_as_buildout() { run_as_plone_user $plone_buildout_user "$@"; }
run_as_daemon() { run_as_plone_user $plone_daemon_user "$@"; }


######
# Plone site list support

plone_site_entry() { has_args 1 "$@"; grep "^$1|" "$plone_site_list"; }
plone_site_exists() { has_args 1 "$@"; plone_site_entry "$1" >/dev/null 2>&1; }
plone_site_path() {
	min_args 1 "$@"
	if [[ $# -gt 1 ]]; then
		sed -i "s,^$1|.*$,$1|$2," "$plone_site_list"
	else
		plone_site_entry "$1" |cut -f2 -d'|'
	fi
}

plone_site_list() {
	[ -f "$plone_site_list" ] || error "$plone_site_list: not found"
	sed -e 's,|.*$,,' "$plone_site_list"
}

plone_site_add() {
	has_args 2 "$@"
	local name=$1
	local path=$2
	! plone_site_exists "$name" || error "$name: plone site exists"
	$pretend || echo "$name|$path" >>"$plone_site_list"
}

plone_site_remove() {
	has_args 1 "$@"
	plone_site_exists "$name" || error "$name: plone site does not exist"
	run sed -i "m,^$1|, d" "$plone_site_list"
}


######
# Plone site support

with_plone_site_path() {
	min_args 1 "$@"
	local name=$1
	shift

	local path
	path=$(plone_site_path "$name")

	"$@"
}

plone_site_create() {
	local name=$1
	local path=$2

	! plone_site_exists "$name" || error "$name: site exists"

	plone_site_add "$name" "$path"
	run_mkdir "$path"

	plone_site_backup_init "$name" "$path"
}

plone_site_rename() {
	has_args 2 "$@"
	local old=$1
	local new=$2
	[ "$old" != "$new" ] || error "names must be different"
	plone_site_exists "$old" || error "$old: site does not exist"
	! plone_site_exists "$new" || error "$new: site exists"

	# add with new name and old path
	local path
	path=$(plone_site_path "$old")
	plone_site_add "$new" "$path"
	#  remove old name
	plone_site_remove "$old"
}


######
# Plone site configuration support

# $1 - name
# $2 - version
# $3 - # off clients
plone_site_config_init() {
	min_args 1 "$@"
	with_plone_site_path "$1" _plone_site_config_init "$@"
}
_plone_site_config_init() {
	cat >"$path/$script_name.conf" <<CONF
#!/bin/bash
# $script_name.conf - Plone site configuration
# This file was generated automatically on $(date)
# by $script_name v$package_version (part of the $package_name package).
plone_site_version=$2
plone_site_clients=$3
CONF
}

plone_site_config_show() { with_plone_site_path "$1" _plone_site_config_show; }
_plone_site_config_show() { cat "$path/$script_name.conf"; }

plone_site_config_edit() { with_plone_site_path "$1" _plone_site_config_edit; }
_plone_site_config_edit() { run_editor "$path/$script_name.conf"; }

plone_site_config_read() { with_plone_site_path "$1" _plone_site_config_read; }
_plone_site_config_read() { config_read "$path/$script_name.conf"; }

with_plone_site_config() {
	local name=$1
	shift

	plone_site_exists "$name" || error "$name: site does not exists"

	local plone_site_version plone_site_clients
	plone_site_config_read "$name"
	"$@"
}


######
# Plone installer support

plone_installer_name() {
	has_args 1 "$@"
	local version=$1
	echo "Plone-${version}-UnifiedInstaller"
}

plone_installer_url() {
	has_args 1 "$@"
	local version=$1

	local file
	file=$(plone_installer_name "$version")

	local branch=${version%.*}
	echo "$plone_installer_url_base/$branch/$version/+download/$file.tgz"
}

plone_installer() {
	has_args 1 "$@"
	local version=$1

	local name
	name=$(plone_installer_name "$version")

	echo "$plone_installer_dir/$name"
}

plone_installer_download() {
	has_args 1 "$@"
	with_plone_site_config "$1" _plone_installer_download
}
_plone_installer_download() {
	local name=$1

	local url
	url=$(plone_installer_url "$plone_site_version")

	local archivedir="$script_tempdir/src"
	run_mkdir "$archivedir"
	run_pushd "$archivedir"
	wget_files "$url"
	run_popd
}

plone_installer_extract() {
	has_args 1 "$@"
	with_plone_site_config "$1" _plone_installer_extract  "$@"
}
_plone_installer_extract() {
	local name=$1

	local installer
	installer=$(plone_installer "$plone_site_version")
	[ -f "src/$installer.tgz" ] || _plone_installer_download "$name"

	run_pushd "$script_tempdir"
	run tar -xf "src/$installer.tgz"
	run_popd
}

plone_installer_exec() {
	min_args 1 "$@"
	with_plone_site_config "$1" _plone_installer_exec "$@"
}
_plone_installer_exec() {
	local name=$1
	shift

	local installer
	installer=$(plone_installer "$plone_site_version")

	local installdir="$script_tempdir/$installer"
	[ -d "$installdir" ] || _plone_installer_extract "$name"

	run_pushd "$installdir"
	run ./install.sh "$@"

	local ilog="$path/install.log"
	log_rotate "$ilog" 3
	run mv "install.log" "$ilog"

	run_popd
}

plone_installer_run() { with_plone_site_path "$1" _plone_installer_run "$@"; }
_plone_installer_run() { with_plone_site_config "$1" __plone_installer_run "$@"; }
__plone_installer_run() {
	local name=$1
	local -a opts
	opts+=( --target="$path" )
	opts+=( --clients="$plone_site_clients" )
	opts+=( --daemon-user="$plone_daemon_user" )
	opts+=( --owner="$plone_buildout_user" )
	opts+=( zeo )
	_plone_installer_exec "$name" "${opts[@]}"

	plone_site_backup_config "$name" "$path"
}


######
# Plone site backups

plone_site_backup_init() {
	local name=$1
	local path=$2

	run_mkdir "$path/backups/files"
	run_mkdir "$path/backups/blobs"
}

plone_site_backup_config() {
	local name=$1
	local path=$2

	cat <<CONF >>"$path/zeocluster/buildout.cfg"

[backup]
recipe = collective.recipe.backup
location = $path/backups/files
blobbackuplocation = $path/backups/blobs
CONF
}

plone_site_cron_install() {
	local name=$1
	cron_install "$name"
}
plone_site_cron_remove() {
	cron_remove "$name"
}

plone_site_cron_run() {
	:
}

######
# Plone cluster support

plone_cluster_conf_name() { has_args 1 "$@"; echo "plone-$1.conf"; }

plone_cluster_conf_show() {
	has_args 1 "$@"
	local name=$1

	local cfg
	cfg=$(plone_cluster_conf_name "$name")
	supervisor_conf_show "$cfg"
}

plone_cluster_conf_install() {
	has_args 1 "$@"
	local name=$1
	plone_site_exists "$name" || error "$name: plone site missing"

	local tmp
	tmp=$(cmd_tempfile)
	plone_cluster_conf_gen "$name" >"$tmp"

	local cfg
	cfg=$(plone_cluster_conf_name "$name")
	supervisor_conf_install "$cfg" "$tmp"
	supervisor_reload
}

plone_cluster_conf_remove() {
	has_args 1 "$@"
	local name=$1

	local cfg
	cfg=$(plone_cluster_conf_name "$name")
	supervisor_conf_remove "$cfg"
	supervisor_reload
}


######
# Plone cluster configuration support

plone_cluster_group() { echo "plone_${name}"; }

plone_cluster_conf_gen() { with_plone_site_path "$1" _plone_cluster_conf_gen "$@"; }
_plone_cluster_conf_gen() { with_plone_site_config "$1" __plone_cluster_conf_gen "$@"; }
__plone_cluster_conf_gen() {
	local name=$1

	local server="${name}_plone_server"
	plone_cluster_conf_gen_app "$server" "zeoserver fg"

	local programs="$server"
	for i in $(seq 1 $plone_site_clients); do
		local client="${name}_plone_client_$i"

		echo
		plone_cluster_conf_gen_app "$client" "client$i console"
		supervisor_conf_gen stopwaitseconds 30

		programs="$programs,$client"
	done

	local group
	group=$(plone_cluster_group "$name")
	supervisor_conf_gen group "$group"
	supervisor_conf_gen programs "$programs"
}

plone_cluster_conf_gen_app() {
	local name=$1
	local cmd=$2

	supervisor_conf_gen program "$name"
	supervisor_conf_gen user "$plone_daemon_user"
	supervisor_conf_gen directory "$path/zeocluster"
	supervisor_conf_gen command "$path/zeocluster/bin/$cmd"
}

######
# Plone cluster control

plone_cluster_control() {
	local func=$1
	local name=$2

	local group=all
	if [ "$name" != all ]; then
		group=$(plone_cluster_group "$name")
	fi
	$func "$group"
}

plone_cluster_start() { plone_cluster_control supervisor_start "$@"; }
plone_cluster_stop() { plone_cluster_control supervisor_stop "$@"; }
plone_cluster_restart() { plone_cluster_control supervisor_restart "$@"; }


######
# Plone cronjob support

plone_cron_install() {
	has_args 1 "$@"
	cron_script_install daily "$1" daily
	cron_script_install weekly "$1" weekly
}
plone_cron_remove() {
	cron_script_remove daily "$1" daily
	cron_script_remove weekly "$1" weekly
}

plone_cron_run() { with_plone_site_path "$1" _plone_cron_run "$@"; }
_plone_cron_run() {
	local period=$2

	case "$period" in
	(daily)
		run_as_daemon "$path/zeocluster/bin/backup" -q
		;;
	(weekly)
		run_as_daemon "$path/zeocluster/bin/zeopack"
		;;
	(*)
		warn "nothing to do?"
		;;
	esac
}


######
# Plone WebDAV support

lib_load 'net/webdav'

plone_webdav_script_import_dir() {
	plone_webdav_script_import_dir_next "$@"
	echo "propset '$1' title '$(basename "$1")'"
}

plone_webdav_script_import_file() {
	plone_webdav_script_import_file_next "$@"
	echo "propset '$2' title '$(basename "$2")'"
}

func_chain plone \
	webdav_script_import_dir \
	webdav_script_import_file

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.