<commit_before>terraform {
  backend "gcs" {}
}

variable "secrets_dir" {}
variable "charts_dir" {}
variable "nonce" {}

# Terragrunt variables

variable "replica_count" {}
variable "backup_deltas" {}
variable "release_namespace" {}
variable "requests_cpu" {}
variable "requests_memory" {}
variable "limits_cpu" {}
variable "limits_memory" {}
variable "pv_capacity" {}
variable "pv_storage_class" {}
variable "pv_reclaim_policy" {}
variable "execute_destroy_pvcs" {}

# Secret variables

variable "secret_couchdb_admin_password" {}
variable "secret_couchdb_admin_username" {}
variable "secret_couchdb_auth_cookie" {}

data "template_file" "couchdb_values" {
  depends_on = ["null_resource.couchdb_recover"]
  template   = "${file("values.yaml")}"

  vars {
    couchdb_admin_username = "${var.secret_couchdb_admin_username}"
    couchdb_admin_password = "${var.secret_couchdb_admin_password}"
    couchdb_auth_cookie    = "${var.secret_couchdb_auth_cookie}"
    replica_count          = "${var.replica_count}"
    requests_cpu           = "${var.requests_cpu}"
    requests_memory        = "${var.requests_memory}"
    limits_cpu             = "${var.limits_cpu}"
    limits_memory          = "${var.limits_memory}"
    pv_capacity            = "${var.pv_capacity}"
    pv_storage_class       = "${var.pv_storage_class}"
  }
}

module "couchdb" {
  source           = "/exekube-modules/helm-release"
  tiller_namespace = "kube-system"
  client_auth      = "${var.secrets_dir}/kube-system/helm-tls"

  release_name            = "couchdb"
  release_namespace       = "${var.release_namespace}"
  release_values          = ""
  release_values_rendered = "${data.template_file.couchdb_values.rendered}"

  chart_name = "${var.charts_dir}/couchdb"
}

resource "null_resource" "couchdb_finish_cluster" {
  depends_on = ["module.couchdb"]

  triggers = {
    nonce = "${var.nonce}"
  }

  provisioner "local-exec" {
    command = <<EOF
      RETRIES=15
      RETRY_COUNT=1
      while [ "$CLUSTER_READY" != "true" ]; do
        echo "[Try $RETRY_COUNT of $RETRIES] Waiting for all CouchDB pods to join the cluster..."
        CLUSTER_READY="true"
        CLUSTER_MEMBERS_COUNT=$(kubectl exec --namespace gpii couchdb-couchdb-0 -c couchdb -- curl -s http://${var.secret_couchdb_admin_username}:${var.secret_couchdb_admin_password}@127.0.0.1:5984/_membership 2>/dev/null | jq -r .cluster_nodes[] | grep -c .)
        echo "$CLUSTER_MEMBERS_COUNT of ${var.replica_count} pods have joined the cluster."
        if [ "$CLUSTER_MEMBERS_COUNT" != "${var.replica_count}" ]; then
          CLUSTER_READY="false"
        fi
        RETRY_COUNT=$(($RETRY_COUNT+1))
        if [ "$RETRY_COUNT" == "$RETRIES" ]; then
          echo "Retry limit reached, giving up!"
          exit 1
        fi
        if [ "$CLUSTER_READY" == "false" ]; then
          sleep 10
        fi
      done

      RETRY_COUNT=1
      while [ "$STATUS" != '"Cluster is already finished"' ]; do
        RESULT=$(
          kubectl exec --namespace ${var.release_namespace} couchdb-couchdb-0 -c couchdb -- \
          curl -s http://${var.secret_couchdb_admin_username}:${var.secret_couchdb_admin_password}@127.0.0.1:5984/_cluster_setup \
          -X POST -H 'Content-Type: application/json' -d '{"action": "finish_cluster"}')
        echo "[Try $RETRY_COUNT of $RETRIES] Posting \"finish_cluster\", CouchDB returned: $RESULT"
        STATUS=$(echo $RESULT | jq ".reason")
        RETRY_COUNT=$(($RETRY_COUNT+1))
        if [ "$RETRY_COUNT" == "$RETRIES" ]; then
          echo "Retry limit reached, giving up!"
          exit 1
        fi
        if [ "$STATUS" != '"Cluster is already finished"' ]; then
          sleep 10
        fi
      done
    EOF
  }
}

resource "null_resource" "couchdb_enable_pv_backups" {<commit_msg>Don't use kubectl exec in couchdb module<commit_after>terraform {
  backend "gcs" {}
}

variable "secrets_dir" {}
variable "charts_dir" {}
variable "nonce" {}

# Terragrunt variables

variable "replica_count" {}
variable "backup_deltas" {}
variable "release_namespace" {}
variable "requests_cpu" {}
variable "requests_memory" {}
variable "limits_cpu" {}
variable "limits_memory" {}
variable "pv_capacity" {}
variable "pv_storage_class" {}
variable "pv_reclaim_policy" {}
variable "execute_destroy_pvcs" {}

# Secret variables

variable "secret_couchdb_admin_password" {}
variable "secret_couchdb_admin_username" {}
variable "secret_couchdb_auth_cookie" {}

data "template_file" "couchdb_values" {
  depends_on = ["null_resource.couchdb_recover"]
  template   = "${file("values.yaml")}"

  vars {
    couchdb_admin_username = "${var.secret_couchdb_admin_username}"
    couchdb_admin_password = "${var.secret_couchdb_admin_password}"
    couchdb_auth_cookie    = "${var.secret_couchdb_auth_cookie}"
    replica_count          = "${var.replica_count}"
    requests_cpu           = "${var.requests_cpu}"
    requests_memory        = "${var.requests_memory}"
    limits_cpu             = "${var.limits_cpu}"
    limits_memory          = "${var.limits_memory}"
    pv_capacity            = "${var.pv_capacity}"
    pv_storage_class       = "${var.pv_storage_class}"
  }
}

module "couchdb" {
  source           = "/exekube-modules/helm-release"
  tiller_namespace = "kube-system"
  client_auth      = "${var.secrets_dir}/kube-system/helm-tls"

  release_name            = "couchdb"
  release_namespace       = "${var.release_namespace}"
  release_values          = ""
  release_values_rendered = "${data.template_file.couchdb_values.rendered}"

  chart_name = "${var.charts_dir}/couchdb"
}

resource "null_resource" "couchdb_finish_cluster" {
  depends_on = ["module.couchdb"]

  triggers = {
    nonce = "${var.nonce}"
  }

  provisioner "local-exec" {
    command = <<EOF
      COUCHDB_URL="http://${var.secret_couchdb_admin_username}:${var.secret_couchdb_admin_password}@127.0.0.1:5984"
      PORT_FORWARD_CMD="kubectl -n ${var.release_namespace} port-forward statefulset/couchdb-couchdb 5984:5984"

      $PORT_FORWARD_CMD </dev/null &>/dev/null &
      sleep 5

      RETRIES=15
      RETRY_COUNT=1
      while [ "$CLUSTER_READY" != "true" ]; do
        echo "[Try $RETRY_COUNT of $RETRIES] Waiting for all CouchDB pods to join the cluster..."
        CLUSTER_READY="true"
        CLUSTER_MEMBERS_COUNT=$(curl -s $COUCHDB_URL/_membership 2>/dev/null | jq -r .cluster_nodes[] | grep -c .)
        echo "$CLUSTER_MEMBERS_COUNT of ${var.replica_count} pods have joined the cluster."
        if [ "$CLUSTER_MEMBERS_COUNT" != "${var.replica_count}" ]; then
          CLUSTER_READY="false"
        fi
        RETRY_COUNT=$(($RETRY_COUNT+1))
        if [ "$RETRY_COUNT" == "$RETRIES" ]; then
          echo "Retry limit reached, giving up!"
          kill $(pgrep -f "^$PORT_FORWARD_CMD")
          exit 1
        fi
        if [ "$CLUSTER_READY" == "false" ]; then
          sleep 10
        fi
      done

      RETRY_COUNT=1
      while [ "$STATUS" != '"Cluster is already finished"' ]; do
        RESULT=$(
          curl -s $COUCHDB_URL/_cluster_setup \
          -X POST -H 'Content-Type: application/json' -d '{"action": "finish_cluster"}')
        echo "[Try $RETRY_COUNT of $RETRIES] Posting \"finish_cluster\", CouchDB returned: $RESULT"
        STATUS=$(echo $RESULT | jq ".reason")
        RETRY_COUNT=$(($RETRY_COUNT+1))
        if [ "$RETRY_COUNT" == "$RETRIES" ]; then
          echo "Retry limit reached, giving up!"
          kill $(pgrep -f "^$PORT_FORWARD_CMD")
          exit 1
        fi
        if [ "$STATUS" != '"Cluster is already finished"' ]; then
          sleep 10
        fi
      done

      kill $(pgrep -f "^$PORT_FORWARD_CMD")
    EOF
  }
}

resource "null_resource" "couchdb_enable_pv_backups" {
{'fraction_non_alphanumeric': 0.1345879299156392, 'fraction_numerical': 0.008695652173913044, 'mean_word_length': 3.0707871104067617, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4010017', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2567, 'n_words': 500}