-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.sh
executable file
·90 lines (72 loc) · 2.87 KB
/
state.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
83
84
85
86
87
88
89
#!/bin/bash -xv
####################################################################
# Be very careful when modifying anything in this box #
# Initialize indicies must be first #
declare -A DELETE_INDEX #
declare -A APPLY_INDEX #
#
# Get literal dir path of the state script #
SOURCE="${BASH_SOURCE[0]}" #
while [ -h "$SOURCE" ]; do #
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" #
SOURCE="$(readlink "$SOURCE")" #
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" #
done #
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" #
#
# Functions in lib/apply and lib/delete are added to idicies #
source ${DIR}/lib/apply #
source ${DIR}/lib/delete #
source ${DIR}/lib/req #
####################################################################
# Find Environment Definition
export WORKDIR=$(get-workspace)
if [ -z "${WORKDIR}" ]
then
echo "No Dictum initialized workspace found"
exit 1
fi
# Set temp workspace
TEMPDIR=$(mktemp -d -t sm-XXXXXXXXXX)
# Try to get cluster state
if get-configmap > /dev/null 2>&1
then
get-configmap | get-pairs-stdin > ${TEMPDIR}/existing.kv
fi
# Get Incoming
get-pairs-arg ${WORKDIR}/.state/.state > ${TEMPDIR}/incoming.kv
# Temp KV locations
EXISTING_STATE="${TEMPDIR}/existing.kv"
INCOMING_STATE="${TEMPDIR}/incoming.kv"
if [ -s ${EXISTING_STATE} ]
then
get-deletions ${EXISTING_STATE} ${INCOMING_STATE} > ${TEMPDIR}/diff.kv
sed -i '1!G;h;$!d' ${TEMPDIR}/diff.kv
DIFF=${TEMPDIR}/diff.kv
# Delete Resources
while IFS='' read -r LINE || [ -n "${LINE}" ]
do
name=$(get-key "${LINE}")
filepath=$(get-value "${LINE}")
resource="${WORKDIR}/${filepath}"
resource="$(envsubst <<< ${resource})"
res_type=$(grep -o "[a-zA-Z1-9]*[\:]" <<< ${name} | sed s/\://g)
action=$(echo ${DELETE_INDEX[$res_type]})
$action $resource
done < ${DIFF}
fi
if [[ ! -f ${WORKDIR}/.state/.state.del ]]
then
# Apply the new state
while IFS='' read -r LINE || [ -n "${LINE}" ]
do
name=$(get-key "${LINE}")
filepath=$(get-value "${LINE}")
resource="${WORKDIR}/${filepath}"
resource="$(envsubst <<< ${resource})"
res_type=$(grep -o "[a-zA-Z1-9]*[\:]" <<< $name | sed s/\://g)
action=$(echo ${APPLY_INDEX[$res_type]})
$action $resource
done < $INCOMING_STATE
store-state
fi