commit | author | age
|
d99f23
|
1 |
#!/usr/bin/ksh |
OB |
2 |
# by Olaf Bohlen <olbohlen@eenfach.de> |
|
3 |
# licensed under BSD3 license |
|
4 |
|
|
5 |
APIURL="https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT_HTTPS}" |
|
6 |
NAMESPACE=$(</run/secrets/kubernetes.io/serviceaccount/namespace) |
|
7 |
TOKEN=$(</run/secrets/kubernetes.io/serviceaccount/token) |
|
8 |
CACRT="/run/secrets/kubernetes.io/serviceaccount/ca.crt" |
|
9 |
|
|
10 |
# some associate arrays we need |
|
11 |
typeset -A receiptname |
|
12 |
typeset -A receiptversion |
|
13 |
|
|
14 |
CURLOPTS='-s --cacert ${CACRT} -H "Accept: application/json" -H "User-Agent: ksh93 receipt processor 0.1" -H "Authorization: Bearer ${TOKEN}"' |
|
15 |
|
|
16 |
updatedreceipts="" |
|
17 |
|
|
18 |
while sleep 5; do |
|
19 |
# MAIN loop |
|
20 |
|
|
21 |
# get a CookieReceiptList item from the API and store the json in a variable |
|
22 |
respjson=$(curl -XGET ${CURLOPTS} '${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookiereceipts?limit=500') |
|
23 |
|
|
24 |
# check if we got the expected resource: |
|
25 |
kind=$(echo "${respjson}" | jq .kind) |
|
26 |
if [ x${kind} != 'x"CookieReceiptList"' ]; then |
|
27 |
printf "Error: unexpected Result from API\n" |
|
28 |
break ## jump out of this loop iteration |
|
29 |
fi |
|
30 |
|
|
31 |
# how many receipts are there? |
|
32 |
numreceipts=$(echo "${respjson}" | jq .items[].metadata.name | wc -l) |
|
33 |
|
|
34 |
# populate the in memory arrays with json metadata, we need |
|
35 |
# - the name |
|
36 |
# - the uid |
|
37 |
# - the resourceVersion |
|
38 |
# for every receipt to figure out if we need to process them |
|
39 |
i=0 |
|
40 |
while [ ${i} -lt ${numreceipts} ]; do |
|
41 |
##foo[${xid}]=$(echo "${RESPJSON}" | jq .items[1].metadata.name) |
|
42 |
receiptuid=$(echo "${respjson}" | jq .items[${i}].metadata.uid) |
|
43 |
receiptname[${receiptuid}]=$(echo "${respjson}" | jq .items[${i}].metadata.name) |
|
44 |
|
|
45 |
#receiptversion[${receiptuid}]=$(echo "${respjson}" | jq .items[${i}].metadata.resourceVersion) |
|
46 |
# check if we already have processed this version of the receipt, if not |
|
47 |
# store the uid of that receipt in the updatedreceipts var |
|
48 |
newversion=$(echo "${respjson}" | jq .items[${i}].metadata.resourceVersion) |
|
49 |
if [ "x${newversion}" != "x${receiptversion[${receiptuid}]}" ]; then |
|
50 |
# we have an update! |
|
51 |
updatedreceipts="${updatedreceipts} ${newversion}" |
|
52 |
receiptsversion[${receiptuid}]="${newversion}" |
|
53 |
fi |
|
54 |
i=$(( ${i} + 1 )) |
|
55 |
done |
|
56 |
# now that we have a list of updated receipts, we are going to process them |
|
57 |
for r in ${updatedreceipts}; do |
|
58 |
# get the name for the UID and fetch only that object |
|
59 |
receipt=$( curl -XGET ${CURLOPTS} '${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookiereceipts/${receiptname[${r}]}' ) |
|
60 |
|
|
61 |
# receipt contains now the json for one receipt, now we parse that |
|
62 |
# we set scale to Fahrenheit if sanescale is false, else scale will use Celsius |
|
63 |
scale_b=$( echo "${receipt}" | jq .spec.sanescale ) |
|
64 |
if [ "x${sanescale}" == "xfalse" ]; then |
|
65 |
scale=Fahrenheit |
|
66 |
fi |
|
67 |
|
|
68 |
temperature=$( echo ${receipt} | jq .spec.temperature ) |
|
69 |
|
|
70 |
preheat_b=$( echo "${receipt}" | jq .spec.preheat ) |
|
71 |
if [ "x${preheat_b}" == "xtrue" ]; then |
|
72 |
printf "Pre: we heat up the oven to %s degrees %s\n" "${temperature}" "${scale:-Celsius}" |
|
73 |
fi |
|
74 |
|
|
75 |
# how many ingredients do we have? |
|
76 |
num_in=$(echo "${receipt}" | jq .spec.ingredients[].name | wc -l) |
|
77 |
|
|
78 |
# list up that we fetch needed ingredients |
|
79 |
i=0 |
|
80 |
while [ ${i} -lt ${num_in} ]; do |
|
81 |
in_name=$( echo "${receipt}" | jq .spec.ingredients[${i}].name ) |
|
82 |
in_amount=$( echo "${receipt}" | jq .spec.ingredients[${i}].amount ) |
|
83 |
in_unit=$( echo "${receipt}" | jq .spec.ingredients[${i}].unit ) |
|
84 |
in_remarks=$( echo "${receipt}" | jq .spec.ingredients[${i}].remarks ) |
|
85 |
|
|
86 |
printf "Fetching %s%s of %s" ${in_amount} ${in_unit} ${in_name} |
|
87 |
if [ "x${in_remarks}" != "xnull" ]; then |
|
88 |
printf " (%s)" ${in_remarks} |
|
89 |
fi |
|
90 |
printf "\n" |
|
91 |
sleep 1 |
|
92 |
i=$(( ${i} + 1 )) |
|
93 |
done |
|
94 |
|
|
95 |
|
|
96 |
# now we need to (unfortunately just!) simulate the processing |
|
97 |
# the order of the steps is important, but lists are not ordered, so we have |
|
98 |
# an "order" attribute in each list item, and we select on that. |
|
99 |
# first again, we need the amount of instructions... |
|
100 |
|
|
101 |
num_steps=$(echo "${receipt}" | jq .spec.steps[].order | wc -l) |
|
102 |
|
|
103 |
# now let's iterate over that |
|
104 |
i=0 |
|
105 |
while [ ${i} -lt ${num_steps} ]; do |
|
106 |
instruction=$( echo "${receipt}" | jq ".spec.steps[] | select(.order == ${i}) | { instruction | join (' ')" ) |
|
107 |
##echo "${receipt}" | jq '.spec.steps[] | select(.order == 5) | { instruction } | join (" ")' |
|
108 |
printf "Step %i/%i: %s..." $(( ${i} + 1 )) ${num_steps} "${instruction}" |
|
109 |
sleep $(( ${RANDOM} % 6 + 1 )) |
|
110 |
printf "done\n" |
|
111 |
sleep 1 |
|
112 |
i=$(( ${i} + 1 )) |
|
113 |
done |
|
114 |
done |
|
115 |
done |
|
116 |
|