Olaf Bohlen
2023-01-02 0be391f1cf7ff18758bd53d5dc0302868207987b
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
b6d772 59     printf "\n\nNew receipt found: %s\n" "${receiptname[${r}]}"
79e42e 60     printf "--------------------------------------------------------------------------\n\n"
OB 61     
d99f23 62     # get the name for the UID and fetch only that object
411862 63     receipt=$(eval curl -XGET ${CURLOPTS} '${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookiereceipts/${receiptname[${r}]}' )
d99f23 64
OB 65     # receipt contains now the json for one receipt, now we parse that
66     # we set scale to Fahrenheit if sanescale is false, else scale will use Celsius
411862 67     scale_b=$( echo "${receipt}" | jq -r .spec.sanescale )
d99f23 68     if [ "x${sanescale}" == "xfalse" ]; then
OB 69         scale=Fahrenheit
70     fi
71     
30cc66 72     temperature=$( echo "${receipt}" | jq -r .spec.temperature )
d99f23 73     
411862 74     preheat_b=$( echo "${receipt}" | jq -r .spec.preheat )
d99f23 75     if [ "x${preheat_b}" == "xtrue" ]; then
b6d772 76         printf "Pre: we heat up the oven to %s degrees %s\n\n" "${temperature}" "${scale:-Celsius}"
d99f23 77     fi
OB 78
79     # how many ingredients do we have?
411862 80     num_in=$(echo "${receipt}" | jq .spec.ingredients\[\].name | wc -l)
b6d772 81     
d99f23 82     # list up that we fetch needed ingredients
b6d772 83     printf "Fetching ingredients from receipt:\n"
0be391 84     printf "----------------------------------\n"
d99f23 85     i=0
OB 86     while [ ${i} -lt ${num_in} ]; do
411862 87         in_name=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].name )
OB 88         in_amount=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].amount )
89         in_unit=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].unit )
90         in_remarks=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].remarks )
d99f23 91             
411862 92         printf "Fetching %s%s of %s" "${in_amount}" "${in_unit}" "${in_name}"
d99f23 93         if [ "x${in_remarks}" != "xnull" ]; then
411862 94         printf " (%s)" "${in_remarks}"
d99f23 95         fi
0be391 96         printf "\n\n"
d99f23 97         sleep 1
OB 98         i=$(( ${i} + 1 ))
99     done
100
101
102     # now we need to (unfortunately just!) simulate the processing
103     # the order of the steps is important, but lists are not ordered, so we have
104     # an "order" attribute in each list item, and we select on that.
105     # first again, we need the amount of instructions...
106
411862 107     num_steps=$(echo "${receipt}" | jq .spec.steps\[\].order | wc -l)
d99f23 108
OB 109     # now let's iterate over that
79e42e 110     printf "\n\nProcessing the instructions:\n"
0be391 111     printf "----------------------------\n"
411862 112     i=1
0be391 113     while [ ${i} -le ${num_steps} ]; do
30cc66 114         instruction=$( echo "${receipt}" | jq '.spec.steps[] | select(.order == '${i}') | { instruction } | join (" ")' )
411862 115
OB 116         printf "Step %i/%i: %s..." ${i} ${num_steps} "${instruction}"
d99f23 117         sleep $(( ${RANDOM} % 6 + 1 ))
0be391 118         printf "done\n\n"
d99f23 119         sleep 1
OB 120         i=$(( ${i} + 1 ))
121     done
79e42e 122     printf "\nDone with this receipt."
OB 123     printf "\n=======================\n\n"
d99f23 124     done
OB 125 done
126