temp=$(kubectl get svc -n imxc -o wide | grep elasticsearch | grep NodePort | awk {'print $5'})
export ES_NODEPORT=${temp:5:(-4)}
echo $ES_NODEPORT

export MASTER_IP=$(kubectl get nodes -o wide | grep master | awk {'print $6'})
echo $MASTER_IP

export NUM_SHARDS=2
export NUM_REPLICAS=1

SOURCE_INDEX='kubernetes_event_info'
DEST_INDEX='kubernetes_event_info_backup'

# 기존 index 재매핑
curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/'"${SOURCE_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/'"${SOURCE_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "'${SOURCE_INDEX}'-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "'${SOURCE_INDEX}'"
      }
    },
    "analysis": {
      "analyzer": {
        "my_customer_ngram_analyzer": {
          "tokenizer": "my_customer_ngram_tokenizer"
        }
      },
      "tokenizer": {
        "my_customer_ngram_tokenizer": {
          "type": "ngram",
          "min_gram": "2",
          "max_gram": "3"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "type": {
        "type": "keyword"
      },
      "unixtime": {
        "type": "long"
      },
      "kind": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "firsttime": {
        "type": "long"
      },
      "lasttime": {
        "type": "long"
      },
      "data": {
        "type": "text",
        "index": false
      },
      "id": {
        "type": "keyword"
      },
      "reason": {
        "type": "keyword"
      },
      "message": {
        "type": "text",
        "fields": {
                "ngram": {
                  "type": "text",
                  "analyzer": "my_customer_ngram_analyzer"
                }
        }
      },
      "count": {
        "type": "integer"
      },
      "sourceComponent": {
        "type": "keyword"
      },
      "sourceHost": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "'${SOURCE_INDEX}'": {}
  }
}'

# 기존 index 데이터 백업용 index 매핑
curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/'"${DEST_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "7d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/'"${DEST_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "'${DEST_INDEX}'-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": '""${NUM_SHARDS}""',
      "number_of_replicas": '""${NUM_REPLICAS}""',
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "'${DEST_INDEX}'"
      }
    },
    "analysis": {
      "analyzer": {
        "my_customer_ngram_analyzer": {
          "tokenizer": "my_customer_ngram_tokenizer"
        }
      },
      "tokenizer": {
        "my_customer_ngram_tokenizer": {
          "type": "ngram",
          "min_gram": "2",
          "max_gram": "3"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "cluster": {
        "type": "keyword"
      },
      "namespace": {
        "type": "keyword"
      },
      "type": {
        "type": "keyword"
      },
      "unixtime": {
        "type": "long"
      },
      "kind": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "firsttime": {
        "type": "long"
      },
      "lasttime": {
        "type": "long"
      },
      "data": {
        "type": "text",
        "index": false
      },
      "id": {
        "type": "keyword"
      },
      "reason": {
        "type": "keyword"
      },
      "message": {
        "type": "text",
        "fields": {
                "ngram": {
                  "type": "text",
                  "analyzer": "my_customer_ngram_analyzer"
                }
        }
      },
      "count": {
        "type": "integer"
      },
      "sourceComponent": {
        "type": "keyword"
      },
      "sourceHost": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "'${DEST_INDEX}'": {}
  }
}'