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