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