-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeg
executable file
·101 lines (89 loc) · 2.53 KB
/
keg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "Usage: keg [-s] name file [file ...]"
exit 1;
fi
put() {
if [ "$#" != 2 ]; then exit 1; fi
key=$1; value=`echo $2 | sed -e "s/ /:SP:/g"`
envmap="`echo "$envmap" | sed -e "s/--$key=[^ ]*//g"` --$key=$value"
}
get() {
key=$1
value="$(echo $envmap |sed -e "s/.*--${key}=\([^ ]*\).*/\1/" -e 's/:SP:/ /g' )"
}
keys() {
keys=`echo $envmap | sed -e "s/=[^ ]*//g" -e "s/\([ ]*\)--/\1/g"`
}
secret=false
if [ "$1" = "-s" ]; then
secret=true
shift
fi
name=$1
shift
# read env key values from file(s) into map
while [ "$1" != "" ]; do
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line == *"="* ]] && [[ $line != *"#"* ]]
then
IFS='=' read -r k v <<< "$line"
put "${k}" "${v}"
fi
done < "$1"
shift
done
keys
if [ "$secret" = true ] ; then
# generates secret yaml
fileout="${name}.yaml"
echo ""
echo "apiVersion: v1" | tee ${fileout}
echo "kind: Secret" | tee -a ${fileout}
echo "metadata:" | tee -a ${fileout}
echo " name: $name" | tee -a ${fileout}
echo "type: Opaque" | tee -a ${fileout}
echo "data:" | tee -a ${fileout}
for key in ${keys}
do
echo -n " ${key//_/-}: " | tr '[:upper:]' '[:lower:]' | tee -a ${fileout}
get ${key}
echo -n "${value}" | base64 | tee -a ${fileout}
done
else
# generates a configmap command
fileout="create-configmap-${name}.sh"
echo "#!/bin/sh" > ${fileout}
chmod +x ${fileout}
echo ""
echo -n "kubectl create configmap $name" | tee -a ${fileout}
for key in ${keys}
do
echo -n " --from-literal=" | tee -a ${fileout}
echo -n "${key//_/-}" | tr '[:upper:]' '[:lower:]' | tee -a ${fileout}
get ${key}
echo -n "=\"${value}\"" | tee -a ${fileout}
done
echo "" | tee -a ${fileout}
fi
echo ""
echo "Created ${fileout}"
echo ""
# generates environment snippet to paste into deployment yaml
fileout="env-snippet-${name}.yaml"
echo " env:" | tee ${fileout}
for key in ${keys}
do
echo " - name: ${key}" | tee -a ${fileout}
echo " valueFrom:" | tee -a ${fileout}
if [ "$secret" = true ] ; then
echo " secretKeyRef:" | tee -a ${fileout}
else
echo " configMapKeyRef:" | tee -a ${fileout}
fi
echo " name: $name" | tee -a ${fileout}
echo " key: ${key//_/-}" | tr '[:upper:]' '[:lower:]' | tee -a ${fileout}
done
echo ""
echo "Created ${fileout}"
echo ""