| 
1 | 1 | #!/bin/bash  | 
2 | 2 | set -e  | 
3 | 3 | 
 
  | 
4 |  | -#Function to install the vectorDB model  | 
5 |  | -Put_model() {  | 
 | 4 | +INSTALL_NEW_MODEL=true  | 
6 | 5 | 
 
  | 
 | 6 | +# get trained models from elasticsearch  | 
 | 7 | +echo "Pulling trained models from elasticsearch."  | 
7 | 8 | sleep=2  | 
8 | 9 | for i in $(seq 1 4); do  | 
9 |  | -  sleep=$((sleep*2))  | 
10 |  | - | 
11 |  | -  sleep $sleep  | 
12 |  | -  # learn more https://www.elastic.co/docs/api/doc/elasticsearch-serverless/operation/operation-ml-put-trained-model#operation-ml-put-trained-model-wait_for_completion  | 
13 |  | -  response=$(curl -s -w "%{http_code}" -kX PUT "$ES/_ml/trained_models/.elser_model_1?wait_for_completion=true&pretty" -H 'Content-Type: application/json' -d'  | 
14 |  | -  {  | 
15 |  | -    "input": {  | 
16 |  | -    "field_names": ["text_field"]  | 
17 |  | -    }  | 
18 |  | -  }  | 
19 |  | -  ')  | 
20 |  | - | 
21 |  | -  http_code=$(tail -n1 <<< "$response")  | 
22 |  | -  content=$(sed '$ d' <<< "$response")  | 
23 |  | - | 
24 |  | -  # Check the HTTP status code  | 
25 |  | -  if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then  | 
26 |  | -    echo "Request sent successfully."  | 
27 |  | -    break  | 
28 |  | -  else  | 
29 |  | -    echo "Failed to install the vectorDB model. HTTP status code: $http_code"  | 
30 |  | -    echo "Reponse: $content"  | 
31 |  | -    if [ "$i" -eq 4 ]; then  | 
32 |  | -      exit 1  | 
 | 10 | + | 
 | 11 | +    sleep=$((sleep*2))  | 
 | 12 | + | 
 | 13 | +    sleep $sleep  | 
 | 14 | +    result=$(curl -s -w "%{http_code}" -kX GET "$ES/_ml/trained_models?pretty")  | 
 | 15 | + | 
 | 16 | +    http_code=$(tail -n1 <<< "$result")  | 
 | 17 | +    content=$(sed '$ d' <<< "$result")  | 
 | 18 | +    # Check the result  | 
 | 19 | +    if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then  | 
 | 20 | +        echo "Trained models successfully pulled from elasticsearch."  | 
 | 21 | +        break  | 
 | 22 | +    else  | 
 | 23 | +        echo "Failed to get the trained models from elasticsearch. HTTP status code: $http_code"  | 
 | 24 | +        echo "Reponse: $content"  | 
 | 25 | +        if [ "$i" -eq 4 ]; then  | 
 | 26 | +          exit 1  | 
 | 27 | +        fi  | 
33 | 28 |     fi  | 
34 |  | -  fi  | 
35 | 29 | done  | 
36 | 30 | 
 
  | 
37 |  | -}  | 
 | 31 | +# fetch all model_ids created by customer (api_user) from the result  | 
 | 32 | +model_ids=$(echo "$content" | jq ".trained_model_configs[] | select(.model_id and .created_by == \"api_user\") | .model_id")  | 
 | 33 | + | 
 | 34 | +if [[ -z "$model_ids" ]]; then  | 
 | 35 | +   echo "User has not installed any trained model yet."  | 
 | 36 | +else  | 
 | 37 | +   echo "User's installed trained models: '$model_ids'."  | 
 | 38 | +fi  | 
 | 39 | + | 
 | 40 | +# loop through result and delete unwanted models  | 
 | 41 | +for model_id in $model_ids  | 
 | 42 | +do  | 
 | 43 | +    # need to remove double quotes  | 
 | 44 | +    model=${model_id//"\""/""}  | 
 | 45 | +    echo "Check if model '$model' should be deleted from elasticsearch."  | 
 | 46 | +    if [ "$ELSER_MODEL_TYPE" != "$model" ]; then  | 
 | 47 | +        # deleting trained model  | 
 | 48 | +        echo "Deleting trained model '$model' from elasticsearch"  | 
 | 49 | +        sleep=2  | 
 | 50 | +        for i in $(seq 1 4); do  | 
 | 51 | +          sleep=$((sleep*2))  | 
 | 52 | + | 
 | 53 | +          sleep $sleep  | 
 | 54 | +          result=$(curl -s -w "%{http_code}" -kX DELETE "$ES/_ml/trained_models/$model?pretty")  | 
 | 55 | +          http_code=$(tail -n1 <<< "$result")  | 
 | 56 | +          content=$(sed '$ d' <<< "$result")  | 
 | 57 | +          # Check the result  | 
 | 58 | +          if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then  | 
 | 59 | +              echo "Trained model '$model' successfully deleted from elasticsearch."  | 
 | 60 | +              break  | 
 | 61 | +          else  | 
 | 62 | +              echo "Failed to delete the trained model '$model' from elasticsearch. HTTP status code: $http_code"  | 
 | 63 | +              echo "Reponse: $content"  | 
 | 64 | +              if [ "$i" -eq 4 ]; then  | 
 | 65 | +                exit 1  | 
 | 66 | +              fi  | 
 | 67 | +          fi  | 
 | 68 | +        done  | 
 | 69 | +    else  | 
 | 70 | +        INSTALL_NEW_MODEL=false  | 
 | 71 | +        echo "Do not delete model '$model' from elasticsearch."  | 
 | 72 | +    fi  | 
 | 73 | +done  | 
 | 74 | + | 
 | 75 | +# deploy a new trained model using retry  | 
 | 76 | +if [ "$INSTALL_NEW_MODEL" = true ] ; then  | 
 | 77 | +    sleep=2  | 
 | 78 | +    echo "Deploying the new trained model '$ELSER_MODEL_TYPE' to elasticsearch ..."  | 
 | 79 | +    for i in $(seq 1 4); do  | 
 | 80 | +        sleep=$((sleep*2))  | 
 | 81 | + | 
 | 82 | +        sleep $sleep  | 
 | 83 | +        # learn more https://www.elastic.co/docs/api/doc/elasticsearch-serverless/operation/operation-ml-put-trained-model#operation-ml-put-trained-model-wait_for_completion  | 
 | 84 | +        response=$(curl -s -w "%{http_code}" -kX PUT "$ES/_ml/trained_models/$ELSER_MODEL_TYPE?wait_for_completion=true&pretty" -H 'Content-Type: application/json' -d'  | 
 | 85 | +        {  | 
 | 86 | +            "input": {  | 
 | 87 | +            "field_names": ["text_field"]  | 
 | 88 | +            }  | 
 | 89 | +        }  | 
 | 90 | +        ')  | 
 | 91 | + | 
 | 92 | +        http_code=$(tail -n1 <<< "$response")  | 
 | 93 | +        content=$(sed '$ d' <<< "$response")  | 
38 | 94 | 
 
  | 
39 |  | -Put_model  | 
 | 95 | +        # Check the result  | 
 | 96 | +        if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then  | 
 | 97 | +            echo "Trained model '$ELSER_MODEL_TYPE' installed successfully."  | 
 | 98 | +            break  | 
 | 99 | +        else  | 
 | 100 | +            echo "Failed to install the model '$ELSER_MODEL_TYPE'. HTTP status code: $http_code"  | 
 | 101 | +            echo "Reponse: $content"  | 
 | 102 | +            if [ "$i" -eq 4 ]; then  | 
 | 103 | +                exit 1  | 
 | 104 | +            fi  | 
 | 105 | +        fi  | 
 | 106 | +    done  | 
 | 107 | +else  | 
 | 108 | +    echo "Model '$ELSER_MODEL_TYPE' already installed. Do not install it."  | 
 | 109 | +fi  | 
0 commit comments