added simple yaml template file processor
This commit is contained in:
112
template.sh
Executable file
112
template.sh
Executable file
@@ -0,0 +1,112 @@
|
|||||||
|
#!/bin/bash -e
|
||||||
|
|
||||||
|
while test $# -gt 0; do
|
||||||
|
case "$1" in
|
||||||
|
(-h|--help) cat <<EOF
|
||||||
|
$0 [OPTIONS] FILE
|
||||||
|
|
||||||
|
OPTIONS
|
||||||
|
|
||||||
|
-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.
|
||||||
|
|
||||||
|
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
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
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
|
||||||
|
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 "cat <<EOFXXXX
|
||||||
|
$(<$file)
|
||||||
|
EOFXXXX"
|
Reference in New Issue
Block a user