Skip to content

Commit 7c43513

Browse files
added template.sh
1 parent 684fe91 commit 7c43513

File tree

2 files changed

+196
-11
lines changed

2 files changed

+196
-11
lines changed

cf-deploy-kubernetes.sh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
fatal() {
4+
echo "ERROR: $1"
5+
exit 1
6+
}
7+
38
readonly DEFAULT_NAMESPACE=default
49

510
deployment_file=${1:-deployment.yml}
@@ -9,11 +14,13 @@ deployment_file=${1:-deployment.yml}
914
[ -z "$KUBERNETES_USER" ] && echo "Please set KUBERNETES_USER" && exit 1;
1015
[ -z "$KUBERNETES_PASSWORD" ] && echo "Please set KUBERNETES_PASSWORD" && exit 1;
1116
[ -z "$KUBERNETES_SERVER" ] && echo "Please set KUBERNETES_SERVER" && exit 1;
12-
[ -z "$DOCKER_IMAGE_TAG" ] && echo "Please set DOCKER_IMAGE_TAG" && exit 1;
17+
# [ -z "$DOCKER_IMAGE_TAG" ] && echo "Please set DOCKER_IMAGE_TAG" && exit 1;
1318

1419
[ ! -f "${deployment_file}" ] && echo "Couldn't find $deployment_file file at $(pwd)" && exit 1;
15-
sed -i "s/\$DOCKER_IMAGE_TAG/$DOCKER_IMAGE_TAG/g" $deployment_file
16-
sed -i "s/\$UNIQ_ID/$(date '+%y-%m-%d_%H:%M:%S')/g" $deployment_file
20+
21+
22+
DEPLOYMENT_FILE=$(date '+%y-%m-%d_%H:%M:%S')-${deployment_file}
23+
$(dirname $0)/template.sh "$deployment_file" > "$DEPLOYMENT_FILE" || fatal "Failed to apply deployment template on $deployment_file"
1724

1825

1926
echo "---> Setting up Kubernetes credentials..."
@@ -22,15 +29,9 @@ kubectl config set-cluster foo.kubernetes.com --insecure-skip-tls-verify=true --
2229
kubectl config set-context foo.kubernetes.com/deployer --user=deployer --namespace=$DEFAULT_NAMESPACE --cluster=foo.kubernetes.com
2330
kubectl config use-context foo.kubernetes.com/deployer
2431

32+
echo "---> Submittinig a deployment to Kubernetes..."
33+
kubectl apply -f "$DEPLOYMENT_FILE" || fatal "Deployment Failed"
2534

26-
# Check if the cloned dir already exists from previous builds
27-
if [ "$FORCE_RE_CREATE_RESOURCE" == "true" ]; then
28-
echo "---> Submittinig a deployment to Kubernetes with --force flag..."
29-
kubectl apply -f $deployment_file --force
30-
else
31-
echo "---> Submittinig a deployment to Kubernetes..."
32-
kubectl apply -f $deployment_file
33-
fi
3435

3536
echo "---> Waiting for a succesful deployment status..."
3637

template.sh

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#!/bin/bash
2+
#
3+
# Very simple templating system that replaces {{VAR}} by the value of $VAR.
4+
# Supports default values by writting {{VAR=value}} in the template.
5+
#
6+
# Copyright (c) 2017 Sébastien Lavoie
7+
# Copyright (c) 2017 Johan Haleby
8+
#
9+
# By: https://github.com/johanhaleby/bash-templater
10+
# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941
11+
12+
# Replaces all {{VAR}} by the $VAR value in a template file and outputs it
13+
14+
readonly PROGNAME=$(basename $0)
15+
16+
config_file="<none>"
17+
print_only="false"
18+
silent="false"
19+
20+
usage="${PROGNAME} [-h] [-d] [-f] [-s] --
21+
where:
22+
-h, --help
23+
Show this help text
24+
-p, --print-envs
25+
Don't do anything, just print the result of the variable expansion(s)
26+
-e, --env-file
27+
Specify a file to read variables from
28+
-s, --silent
29+
Don't print warning messages (for example if no variables are found)
30+
examples:
31+
VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt
32+
${PROGNAME} test.txt -e my-variables.txt
33+
${PROGNAME} test.txt -e my-variables.txt > new-test.txt"
34+
35+
if [ $# -eq 0 ]; then
36+
echo "$usage"
37+
exit 1
38+
fi
39+
40+
while [[ $1 =~ ^(-(h|p|e|s)|--(help|print-envs|env-file|silent)) ]]
41+
do
42+
key=$1
43+
value=$2
44+
45+
case $key in
46+
-h|--help)
47+
echo "$usage"
48+
exit 0
49+
;;
50+
-p|--print)
51+
print_only="true"
52+
;;
53+
-e|--env-file)
54+
config_file="$2"
55+
shift
56+
;;
57+
-s|--silent)
58+
silent="true"
59+
;;
60+
*)
61+
echo "Usage ERROR: Invalid Option $key"
62+
echo "$usage"
63+
exit 1
64+
;;
65+
esac
66+
shift # past argument or value
67+
done
68+
69+
70+
if [[ ! -f "${1}" ]]; then
71+
echo "You need to specify a template file" >&2
72+
echo "$usage"
73+
exit 1
74+
fi
75+
76+
template="${1}"
77+
78+
if [ "$#" -ne 0 ]; then
79+
while [ "$#" -gt 0 ]
80+
do
81+
case "$1" in
82+
-h|--help)
83+
echo "$usage"
84+
exit 0
85+
;;
86+
-p|--print-envs)
87+
print_only="true"
88+
;;
89+
-f|--file)
90+
config_file="$2"
91+
;;
92+
-s|--silent)
93+
silent="true"
94+
;;
95+
--)
96+
break
97+
;;
98+
-*)
99+
echo "Invalid option '$1'. Use --help to see the valid options" >&2
100+
exit 1
101+
;;
102+
# an option argument, continue
103+
*) ;;
104+
esac
105+
shift
106+
done
107+
fi
108+
109+
vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//')
110+
111+
if [[ -z "$vars" ]]; then
112+
if [ "$silent" == "false" ]; then
113+
echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2
114+
fi
115+
cat ${template}
116+
exit 0
117+
fi
118+
119+
# Load variables from file if needed
120+
if [ "${config_file}" != "<none>" ]; then
121+
if [[ ! -f "${config_file}" ]]; then
122+
echo "The file ${config_file} does not exists" >&2
123+
echo "$usage"
124+
exit 1
125+
fi
126+
127+
# Create temp file where & and "space" is escaped
128+
tmpfile=`mktemp`
129+
sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" "${config_file}" > $tmpfile
130+
source $tmpfile
131+
fi
132+
133+
var_value() {
134+
eval echo \$$1
135+
}
136+
137+
replaces=""
138+
139+
# Reads default values defined as {{VAR=value}} and delete those lines
140+
# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}}
141+
# You can even reference variables defined in the template before
142+
defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//')
143+
144+
for default in $defaults; do
145+
var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+")
146+
current=`var_value $var`
147+
148+
# Replace only if var is not set
149+
if [[ -z "$current" ]]; then
150+
eval $default
151+
fi
152+
153+
# remove define line
154+
replaces="-e '/^{{$var=/d' $replaces"
155+
vars="$vars
156+
$current"
157+
done
158+
159+
vars=$(echo $vars | sort | uniq)
160+
161+
if [[ "$print_only" == "true" ]]; then
162+
for var in $vars; do
163+
value=`var_value $var`
164+
echo "$var = $value"
165+
done
166+
exit 0
167+
fi
168+
169+
# Replace all {{VAR}} by $VAR value
170+
for var in $vars; do
171+
value=`var_value $var`
172+
if [[ -z "$value" ]]; then
173+
if [ $silent == "false" ]; then
174+
echo "Warning: $var is not defined and no default is set, replacing by empty" >&2
175+
fi
176+
fi
177+
178+
# Escape slashes
179+
value=$(echo "$value" | sed 's/\//\\\//g');
180+
replaces="-e 's/{{$var}}/${value}/g' $replaces"
181+
done
182+
183+
escaped_template_path=$(echo $template | sed 's/ /\\ /g')
184+
eval sed $replaces "$escaped_template_path"

0 commit comments

Comments
 (0)