|
|
|
#!/bin/bash -e
|
|
|
|
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
(-h|--help) if test -e "${!#}"; then
|
|
|
|
cat <<EOF
|
|
|
|
$0 $(sed -n 's,^ *# *,,p' "${!#}" | tr '\n' ' ') ${!#}
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
cat <<EOF
|
|
|
|
$0 [OPTIONS] FILE
|
|
|
|
|
|
|
|
OPTIONS
|
|
|
|
|
|
|
|
-h, --help Show this help
|
|
|
|
|
|
|
|
-h, --help file Show usage of template file (if documented)
|
|
|
|
|
|
|
|
-v, --var name=value Set environment variable to be used in the template.
|
|
|
|
|
|
|
|
-p, --pwd name Generate a random password with the given name.
|
|
|
|
|
|
|
|
FILE
|
|
|
|
|
|
|
|
A template file. In the template file, you may use bash variables
|
|
|
|
and execute bash commands, e.g. you are allowed wo use the \$()
|
|
|
|
expression.
|
|
|
|
|
|
|
|
Document the parameters that shall be used in comment lines, this
|
|
|
|
are lines thet start with # followed by -v or -p options.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
|
|
|
|
Parse a template file and generate an instance for it, using the
|
|
|
|
environment variables nd the given parameters. The use of
|
|
|
|
environment variables and command line option --var is equivalent.
|
|
|
|
|
|
|
|
EXAMPLE
|
|
|
|
|
|
|
|
file wordpress.tpl
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
# -v NAME=
|
|
|
|
# -v PORT=
|
|
|
|
# -p PASSWORD
|
|
|
|
version: '3.3'
|
|
|
|
services:
|
|
|
|
mysql:
|
|
|
|
image: mysql
|
|
|
|
volumes:
|
|
|
|
- type: bind
|
|
|
|
source: /var/volumes/\${NAME}/mysql
|
|
|
|
target: /var/lib/mysql
|
|
|
|
environment:
|
|
|
|
- 'MYSQL_DATABASE=wordpress'
|
|
|
|
- 'MYSQL_USER=wordpress'
|
|
|
|
- 'MYSQL_PASSWORD=\${PASSWORD}'
|
|
|
|
- 'MYSQL_ROOT_PASSWORD=\$(pwgen 40 1)'
|
|
|
|
deploy:
|
|
|
|
placement:
|
|
|
|
constraints:
|
|
|
|
- node.labels.location == ug
|
|
|
|
wordpress:
|
|
|
|
image: wordpress
|
|
|
|
ports:
|
|
|
|
- \${PORT}:80
|
|
|
|
volumes:
|
|
|
|
- type: bind
|
|
|
|
source: /var/volumes/\${NAME}/wordpress
|
|
|
|
target: /var/www/html
|
|
|
|
environment:
|
|
|
|
- 'WORDPRESS_DB_HOST=mysql'
|
|
|
|
- 'WORDPRESS_DB_NAME=wordpress'
|
|
|
|
- 'WORDPRESS_DB_USER=wordpress'
|
|
|
|
- 'WORDPRESS_DB_PASSWORD=\${PASSWORD}'
|
|
|
|
deploy:
|
|
|
|
placement:
|
|
|
|
constraints:
|
|
|
|
- node.labels.location == ug
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
create instance from template:
|
|
|
|
|
|
|
|
$0 -v NAME=my-site -v PORT=8006 -p PASSWORD wordpress.tpl > my-site.yaml
|
|
|
|
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
exit;;
|
|
|
|
(-v|--var) shift;
|
|
|
|
if [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9_]*= ]]; then
|
|
|
|
export $1
|
|
|
|
else
|
|
|
|
echo "ERROR not a variable assignment: $1" 1>&2
|
|
|
|
echo " see: $0 --help" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi;;
|
|
|
|
(-p|--pwd) shift;
|
|
|
|
if [[ $1 =~ ^[a-zA-Z][a-zA-Z0-9_]*$ ]]; then
|
|
|
|
export $1=$(pwgen 40 1)
|
|
|
|
else
|
|
|
|
echo "ERROR not a variable name: $1" 1>&2
|
|
|
|
echo " see: $0 --help" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi;;
|
|
|
|
(*) if test $# -ne 1 -o ! -e "$1"; then
|
|
|
|
echo "ERROR please specify exactly one template file $*" 1>&2
|
|
|
|
echo " this not a template file: $*" 1>&2
|
|
|
|
echo " see: $0 --help" 1>&2
|
|
|
|
exite
|
|
|
|
fi
|
|
|
|
file="$1";
|
|
|
|
esac
|
|
|
|
if test $# -lt 1; then
|
|
|
|
echo "ERROR too few arguments" 1>&2
|
|
|
|
echo " see: $0 --help" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
if ! test -e "$file"; then
|
|
|
|
echo "ERROR please specify a template file" 1>&2
|
|
|
|
echo " see: $0 --help" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval "sed '/^ *#/d' <<EOFXXXX
|
|
|
|
$(<$file)
|
|
|
|
EOFXXXX"
|