Olaf Bohlen
2023-03-21 0cfd14f46c75d2eef2ed792f2cb27c68415a6be1
commit | author | age
267308 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 recipename
12 typeset -A recipeversion
13
14 CURLOPTS="-s --cacert ${CACRT} -H 'Accept: application/json' -H 'User-Agent: ksh93 recipe processor 0.1' -H 'Authorization: Bearer ${TOKEN}'"
15
16
17 while sleep 5; do
18     # MAIN loop
19
20     # we will later store here modified recipe uids, so we clear it for every run
21     updatedrecipes=""
22
23     # get a CookieRecipeList item from the API and store the json in a variable
24     respjson=$(eval curl -XGET ${CURLOPTS} "${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookierecipes?limit=500")
25
26     # check if we got the expected resource:
27     kind=$(echo "${respjson}" | jq .kind)
28     if [ x${kind} != 'x"CookieRecipeList"' ]; then
29     printf "Error: unexpected Result from API\n"
30     break ## jump out of this loop iteration
31     fi
32     
33     # how many recipes are there?
34     numrecipes=$(echo "${respjson}" | jq .items\[\].metadata.name | wc -l)
35
36     # populate the in memory arrays with json metadata, we need
37     # - the name
38     # - the uid
39     # - the resourceVersion
40     # for every recipe to figure out if we need to process them
41     i=0
42     while [ ${i} -lt ${numrecipes} ]; do
43     recipeuid=$(echo "${respjson}" | jq -r .items[${i}].metadata.uid)
44     recipename[${recipeuid}]=$(echo "${respjson}" | jq -r .items[${i}].metadata.name)
45     
46     # check if we already have processed this version of the recipe, if not
47     # store the uid of that recipe in the updatedrecipes var
48     newversion=$(echo "${respjson}" | jq -r .items[${i}].metadata.resourceVersion)
49     if [ "x${newversion}" != "x${recipeversion[${recipeuid}]}" ]; then
50            # we have an update!
51            updatedrecipes="${updatedrecipes} ${recipeuid}"
52            recipeversion[${recipeuid}]="${newversion}"
53     fi
54     i=$(( ${i} + 1 ))
55     done
56
57     # now that we have a list of updated recipes, we are going to process them
58     for r in ${updatedrecipes}; do
59     printf "\n\nNew recipe found: %s\n" "${recipename[${r}]}"
60     printf "--------------------------------------------------------------------------\n\n"
61     
62     # get the name for the UID and fetch only that object
63     recipe=$(eval curl -XGET ${CURLOPTS} '${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookierecipes/${recipename[${r}]}' )
64
65     # recipe contains now the json for one recipe, now we parse that
66     # we set scale to Fahrenheit if sanescale is false, else scale will use Celsius
67     scale_b=$( echo "${recipe}" | jq -r .spec.sanescale )
68     if [ "x${sanescale}" == "xfalse" ]; then
69         scale=Fahrenheit
70     fi
71     
72     temperature=$( echo "${recipe}" | jq -r .spec.temperature )
73     
74     preheat_b=$( echo "${recipe}" | jq -r .spec.preheat )
75     if [ "x${preheat_b}" == "xtrue" ]; then
76         printf "Pre: we heat up the oven to %s degrees %s\n\n" "${temperature}" "${scale:-Celsius}"
77     fi
78
79     # how many ingredients do we have?
80     num_in=$(echo "${recipe}" | jq .spec.ingredients\[\].name | wc -l)
81     
82     # list up that we fetch needed ingredients
83     printf "Fetching ingredients from recipe:\n"
84     printf "----------------------------------\n"
85     i=0
86     while [ ${i} -lt ${num_in} ]; do
87         in_name=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].name )
88         in_amount=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].amount )
89         in_unit=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].unit )
90         in_remarks=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].remarks )
91             
92         printf "Fetching %s%s of %s" "${in_amount}" "${in_unit}" "${in_name}"
93         if [ "x${in_remarks}" != "xnull" ]; then
94         printf " (%s)" "${in_remarks}"
95         fi
96         printf "\n\n"
97         sleep 1
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
107     num_steps=$(echo "${recipe}" | jq .spec.steps\[\].order | wc -l)
108
109     # now let's iterate over that
110     printf "\n\nProcessing the instructions:\n"
111     printf "----------------------------\n"
112     i=1
113     while [ ${i} -le ${num_steps} ]; do
114         instruction=$( echo "${recipe}" | jq '.spec.steps[] | select(.order == '${i}') | { instruction } | join (" ")' )
115
116         printf "Step %i/%i: %s..." ${i} ${num_steps} "${instruction}"
117         sleep $(( ${RANDOM} % 6 + 1 ))
118         printf "done\n\n"
119         sleep 1
120         i=$(( ${i} + 1 ))
121     done
122     printf "\nDone with this recipe."
123     printf "\n=======================\n\n"
124     done
125 done
126