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