{% extends "deploy_functions_base.sh.j2" %}


{% block render_functions %}

{{ render_template("k8s/deployment.yml.j2", "{}-deployment.yml".format(service.name)) }}

{% if service.is_backend %}

{% if service.settings.workers %}
{{ render_template("k8s/workers.yml", "{}-workers.yml".format(service.name)) }}
{% endif %}

{% if service.settings.scheduled_tasks %}
{{ render_template("k8s/scheduled_tasks.yml", "{}-tasks.yml".format(service.name)) }}
{% endif %}

{% endif %}

{% endblock %}


{% block backend_functions scoped %}
{% if service.is_backend %}
{% if service.settings.migrate %}
migrate_{{ service.name }} () {
    cnc toolbox run {{ environment.name }} --tag {{ deployer.tag_for_service(service.name) }} \
    --service-name {{ service.name }} -- '{{ " ".join(service.settings.migrate) }}'; migrate_result=$?
    if [ $migrate_result -ne 0 ]; then exit $migrate_result; fi
}
{% endif %}

{% if service.settings.seed %}
seed_{{ service.name }} () {
    cnc toolbox run {{ environment.name }} --tag {{ deployer.tag_for_service(service.name) }} \
    --service-name {{ service.name }} -- '{{ " ".join(service.settings.seed) }}'; seed_result=$?
    if [ $seed_result -ne 0 ]; then exit $seed_result; fi
}
{% endif %}


create_secrets_{{ service.name }}() {
    gcloud container clusters get-credentials --region {{ environment.collection.region }} {{ environment.collection.instance_name }}

    {% for item in service.environment_items %}
    echo -en {{ shlex.quote(item.value) }} | base64 -w 0 > temp.txt
    kubectl delete secret {{ item.instance_name }} --namespace={{ service.instance_name }} --ignore-not-found
    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Secret
    metadata:
      name: {{ item.instance_name }}
      namespace: {{ service.instance_name }}
    type: Opaque
    data:
      value: $(cat temp.txt)
EOF
    rm temp.txt
    {% endfor %}
}

{% if service.settings.workers %}
deploy_{{ service.name }}_workers () {
    gcloud container clusters get-credentials --region {{ environment.collection.region }} {{ environment.collection.instance_name }}
    kubectl apply -f {{ deployer.rendered_files_path }}/{{ service.name }}-workers.yml
}
{% endif %}

{% if service.settings.scheduled_tasks %}
deploy_{{ service.name }}_tasks () {
    gcloud container clusters get-credentials --region {{ environment.collection.region }} {{ environment.collection.instance_name }}

    # solve a race condition in creating some common resources with workers
    sleep 5

    kubectl apply -f {{ deployer.rendered_files_path }}/{{ service.name }}-tasks.yml
}
{% endif %}

deploy_{{ service.name }}_workers_tasks () {
    create_secrets_{{ service.name }}

    {% if service.settings.workers %}
    deploy_{{ service.name }}_workers
    {% endif %}

    {% if service.settings.scheduled_tasks %}
    deploy_{{ service.name }}_tasks
    {% endif %}
}

{% endif %} # service.is_backend

deploy_{{ service.name }} () {
    gcloud container clusters get-credentials --zone {{ environment.collection.region }} {{ environment.collection.instance_name }}
    kubectl apply -f {{ deployer.rendered_files_path }}/{{ service.name }}-deployment.yml
}

{% endblock %}