-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-config.sh
82 lines (64 loc) · 2.62 KB
/
parse-config.sh
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
#!/bin/bash
log() {
# UTC time
TIME=$(date -u +"%FT%TZ")
# $1 is the message to log passed as the first - and only - function paramter
echo "time=\"${TIME}\" level=INFO msg=\"$1\""
}
load_dir() {
# $1, the first parameter is the directory to load files from
if [ -d $1 ]; then
cd $1
# For each file in the directory, append `{FILENAME}={FILEVALUE}'\n` to result string
for FILENAME in *; do
FILE_LINE_COUNT=$(wc -l <$FILENAME)
# Make sure the directory is not empty AND that it's not multi-lined
if [[ "$FILENAME" != "*" && $(($FILE_LINE_COUNT > 1)) == 0 ]]; then
# Replaces underscores with dots
# $2, is wether ot not to remap `_` to `.` in the key
KEY=$(echo "$FILENAME")
if [ "$2" = true ]; then
KEY=$(echo "$KEY" | tr '_' '.')
fi
# $3, is the numbers of characters to strip in front of the key
KEY="${KEY:$3}"
# TODO: remove when not needed - we use tag source instead
# Removes the .terraform suffix if it exists
if [[ "$KEY" == *".terraform" ]]; then
KEY=${KEY%".terraform"}
fi
# TODO: remove when not needed - we use tag visibility instead
# Removes the infrastructure.global. prefix if it exists
if [[ "$KEY" == "infrastructure.global."* ]]; then
KEY=${KEY#"infrastructure.global."}
fi
# Removes the infrastructure. prefix if it exists
if [[ "$KEY" == "infrastructure."* ]]; then
KEY=${KEY#"infrastructure."}
fi
# Removes the application.<anything>. prefix if it exists
if [[ "$KEY" == application.*.* ]]; then
KEY=${KEY#application.*.}
fi
log "source=$FILENAME destination=$KEY"
VALUE=$(cat "$FILENAME")
RESULT="$KEY=$VALUE"
printf "%s\n" "$RESULT" >> $WORKDIR/application.properties
fi
done
fi
}
# We have 3 directories to load env variables from:
# 1. $WORKDIR/aws-parameter-store
# 2. $WORKDIR/aws-secret
# For aws-parameter-store, we strip the first character since it's always a '_'
log "Loading AWS Parameter Store variables..."
load_dir "$WORKDIR/aws-parameter-store" true 1
log "Loaded AWS Parameter Store variables"
log "Loading AWS Secret variables..."
load_dir "$WORKDIR/aws-secret" true 0
log "Loaded AWS Secret variables"
log "Loading AWS Application Secret variables..."
load_dir "$WORKDIR/aws-secret-application" false 0
log "Loaded AWS Application Secret variables"
sort "$WORKDIR/application.properties" | uniq > "$WORKDIR/temp.properties" && mv "$WORKDIR/temp.properties" "$WORKDIR/application.properties"