<commit_before><?xml version="1.0"?>
<!--
  ~ ELASTICSEARCH CONFIDENTIAL
  ~ __________________
  ~
  ~  [2014] Elasticsearch Incorporated. All Rights Reserved.
  ~
  ~ NOTICE:  All information contained herein is, and remains
  ~ the property of Elasticsearch Incorporated and its suppliers,
  ~ if any.  The intellectual and technical concepts contained
  ~ herein are proprietary to Elasticsearch Incorporated
  ~ and its suppliers and may be covered by U.S. and Foreign Patents,
  ~ patents in process, and are protected by trade secret or copyright law.
  ~ Dissemination of this information or reproduction of this material
  ~ is strictly forbidden unless prior written permission is obtained
  ~ from Elasticsearch Incorporated.
  -->

<project name="smoke-test-tribe-node-with-shield"
         xmlns:ac="antlib:net.sf.antcontrib">

    <taskdef name="xhttp" classname="org.elasticsearch.ant.HttpTask" classpath="${test_classpath}" />
    <typedef name="xhttp" classname="org.elasticsearch.ant.HttpCondition" classpath="${test_classpath}"/>

    <import file="${elasticsearch.integ.antfile.default}"/>
    <import file="${elasticsearch.tools.directory}/ant/shield-overrides.xml"/>

    <property name="tribe_node.pidfile" location="${integ.scratch}/tribe-node.pid"/>
    <available property="tribe_node.pidfile.exists" file="${tribe_node.pidfile}"/>

    <property name="cluster1.pidfile" location="${integ.scratch}/cluster1.pid"/>
    <available property="cluster1.pidfile.exists" file="${cluster1.pidfile}"/>

    <property name="cluster2.pidfile" location="${integ.scratch}/cluster2.pid"/>
    <available property="cluster2.pidfile.exists" file="${cluster2.pidfile}"/>

    <macrodef name="create-index">
        <attribute name="name" />
        <attribute name="port" />
        <sequential>
            <xhttp uri="http://127.0.0.1:@{port}/@{name}" method="PUT" username="test_admin" password="<PASSWORD>" />
            <waitfor maxwait="30" maxwaitunit="second"
                     checkevery="500" checkeveryunit="millisecond"
                     timeoutproperty="@{timeoutproperty}">
                <xhttp uri="http://127.0.0.1:@{port}/_cluster/health/@{name}?wait_for_status=yellow" username="test_admin" password="<PASSWORD>" />
            </waitfor>
        </sequential>
    </macrodef>

    <target name="start-tribe-node-and-2-clusters-with-shield" depends="setup-workspace">
        <ac:for list="${xplugins.list}" param="xplugin.name">
            <sequential>
                <fail message="Expected @{xplugin.name}-${version}.zip as a dependency, but could not be found in ${integ.deps}/plugins}">
                    <condition>
                        <not>
                            <available file="${integ.deps}/plugins/@{xplugin.name}-${elasticsearch.version}.zip"/>
                        </not>
                    </condition>
                </fail>
            </sequential>
        </ac:for>

        <ac:for param="file">
            <path>
                <fileset dir="${integ.deps}/plugins"/>
            </path>
            <sequential>
                <local name="plugin.name"/>
                <convert-plugin-name file="@{file}" outputproperty="plugin.name"/>
                <install-plugin name="${plugin.name}" file="@{file}"/>
            </sequential>
        </ac:for>

        <local name="home"/>
        <property name="home" location="${integ.scratch}/elasticsearch-${elasticsearch.version}"/>

        <echo>Adding roles.yml</echo>
        <copy file="shield-roles.yml" tofile="${home}/config/shield/roles.yml" overwrite="true"/>

        <echo>Adding shield users...</echo>
        <run-script script="${home}/bin/shield/esusers">
            <nested>
                <arg value="useradd"/>
                <arg value="test_admin"/>
                <arg value="-p"/>
                <arg value="changeme"/>
                <arg value="-r"/>
                <arg value="admin"/>
            </nested>
        </run-script>

        <echo>Starting two nodes, each node in a different cluster</echo>
        <ac:trycatch property="failure.message">
            <ac:try>
                <startup-elasticsearch es.transport.tcp.port="9600"
                                       es.http.port="9700"
                                       es.pidfile="${cluster1.pidfile}"
                                       es.unicast.hosts="127.0.0.1:9600"
                                       es.cluster.name="cluster1"/>
            </ac:try>
            <ac:catch>
                <echo>Failed to start cluster1 with message: ${failure.message}</echo>
                <stop-node es.pidfile="${cluster1.pidfile}"/>
            </ac:catch>
        </ac:trycatch>

        <ac:trycatch property="failure.message">
            <ac:try>
                <startup-elasticsearch es.transport.tcp.port="9800"
                                       es.http.port="9900"
                                       es.pidfile="${cluster2.pidfile}"
                                       es.unicast.hosts="127.0.0.1:9800"
                                       es.cluster.name="cluster2"/>
            </ac:try>
            <ac:catch>
                <echo>Failed to start cluster2 with message: ${failure.message}</echo>
                <stop-node es.pidfile="${cluster1.pidfile}"/>
                <stop-node es.pidfile="${cluster2.pidfile}"/>
            </ac:catch>
        </ac:trycatch>


        <ac:trycatch property="failure.message">
            <ac:try>
                <echo>Starting a tribe node, configured to connect to cluster1 and cluster2</echo>
                <startup-elasticsearch es.pidfile="${tribe_node.pidfile}">
                    <additional-args>
                        <arg value="-Des.tribe.cluster1.cluster.name=cluster1"/>
                        <arg value="-Des.tribe.cluster1.discovery.zen.ping.unicast.hosts=127.0.0.1:9600"/>

                        <arg value="-Des.tribe.cluster2.cluster.name=cluster2"/>
                        <arg value="-Des.tribe.cluster2.discovery.zen.ping.unicast.hosts=127.0.0.1:9800"/>
                    </additional-args>
                </startup-elasticsearch>
                <xhttp uri="http://127.0.0.1:${integ.http.port}/_cluster/health?wait_for_nodes=5" username="test_admin" password="<PASSWORD>" />

                <!--
                    From the rest tests we only connect to the tribe node, so we need create the indices externally:

                    By creating the index after the tribe node has started we can be sure that the tribe node knows
                    about it. See: https://github.com/elastic/elasticsearch/issues/13292
                 -->
                <echo>Creating index1 in cluster1</echo>
                <create-index name="index1" port="9700"/>

                <echo>Creating index2 in cluster2</echo>
                <create-index name="index2" port="9900"/>
            </ac:try><commit_msg>test: echo log file after the index is created to find out why sometimes on CI the indices of that index don't get into a started state.<commit_after><?xml version="1.0"?>
<!--
  ~ ELASTICSEARCH CONFIDENTIAL
  ~ __________________
  ~
  ~  [2014] Elasticsearch Incorporated. All Rights Reserved.
  ~
  ~ NOTICE:  All information contained herein is, and remains
  ~ the property of Elasticsearch Incorporated and its suppliers,
  ~ if any.  The intellectual and technical concepts contained
  ~ herein are proprietary to Elasticsearch Incorporated
  ~ and its suppliers and may be covered by U.S. and Foreign Patents,
  ~ patents in process, and are protected by trade secret or copyright law.
  ~ Dissemination of this information or reproduction of this material
  ~ is strictly forbidden unless prior written permission is obtained
  ~ from Elasticsearch Incorporated.
  -->

<project name="smoke-test-tribe-node-with-shield"
         xmlns:ac="antlib:net.sf.antcontrib">

    <taskdef name="xhttp" classname="org.elasticsearch.ant.HttpTask" classpath="${test_classpath}" />
    <typedef name="xhttp" classname="org.elasticsearch.ant.HttpCondition" classpath="${test_classpath}"/>

    <import file="${elasticsearch.integ.antfile.default}"/>
    <import file="${elasticsearch.tools.directory}/ant/shield-overrides.xml"/>

    <property name="tribe_node.pidfile" location="${integ.scratch}/tribe-node.pid"/>
    <available property="tribe_node.pidfile.exists" file="${tribe_node.pidfile}"/>

    <property name="cluster1.pidfile" location="${integ.scratch}/cluster1.pid"/>
    <available property="cluster1.pidfile.exists" file="${cluster1.pidfile}"/>

    <property name="cluster2.pidfile" location="${integ.scratch}/cluster2.pid"/>
    <available property="cluster2.pidfile.exists" file="${cluster2.pidfile}"/>

    <macrodef name="create-index">
        <attribute name="name" />
        <attribute name="port" />
        <sequential>
            <xhttp uri="http://127.0.0.1:@{port}/@{name}" method="PUT" username="test_admin" password="<PASSWORD>" />
            <waitfor maxwait="30" maxwaitunit="second"
                     checkevery="500" checkeveryunit="millisecond"
                     timeoutproperty="@{timeoutproperty}">
                <xhttp uri="http://127.0.0.1:@{port}/_cluster/health/@{name}?wait_for_status=yellow" username="test_admin" password="<PASSWORD>" />
            </waitfor>
        </sequential>
    </macrodef>

    <target name="start-tribe-node-and-2-clusters-with-shield" depends="setup-workspace">
        <ac:for list="${xplugins.list}" param="xplugin.name">
            <sequential>
                <fail message="Expected @{xplugin.name}-${version}.zip as a dependency, but could not be found in ${integ.deps}/plugins}">
                    <condition>
                        <not>
                            <available file="${integ.deps}/plugins/@{xplugin.name}-${elasticsearch.version}.zip"/>
                        </not>
                    </condition>
                </fail>
            </sequential>
        </ac:for>

        <ac:for param="file">
            <path>
                <fileset dir="${integ.deps}/plugins"/>
            </path>
            <sequential>
                <local name="plugin.name"/>
                <convert-plugin-name file="@{file}" outputproperty="plugin.name"/>
                <install-plugin name="${plugin.name}" file="@{file}"/>
            </sequential>
        </ac:for>

        <local name="home"/>
        <property name="home" location="${integ.scratch}/elasticsearch-${elasticsearch.version}"/>

        <echo>Adding roles.yml</echo>
        <copy file="shield-roles.yml" tofile="${home}/config/shield/roles.yml" overwrite="true"/>

        <echo>Adding shield users...</echo>
        <run-script script="${home}/bin/shield/esusers">
            <nested>
                <arg value="useradd"/>
                <arg value="test_admin"/>
                <arg value="-p"/>
                <arg value="changeme"/>
                <arg value="-r"/>
                <arg value="admin"/>
            </nested>
        </run-script>

        <echo>Starting two nodes, each node in a different cluster</echo>
        <ac:trycatch property="failure.message">
            <ac:try>
                <startup-elasticsearch es.transport.tcp.port="9600"
                                       es.http.port="9700"
                                       es.pidfile="${cluster1.pidfile}"
                                       es.unicast.hosts="127.0.0.1:9600"
                                       es.cluster.name="cluster1"/>
            </ac:try>
            <ac:catch>
                <echo>Failed to start cluster1 with message: ${failure.message}</echo>
                <stop-node es.pidfile="${cluster1.pidfile}"/>
            </ac:catch>
        </ac:trycatch>

        <ac:trycatch property="failure.message">
            <ac:try>
                <startup-elasticsearch es.transport.tcp.port="9800"
                                       es.http.port="9900"
                                       es.pidfile="${cluster2.pidfile}"
                                       es.unicast.hosts="127.0.0.1:9800"
                                       es.cluster.name="cluster2"/>
            </ac:try>
            <ac:catch>
                <echo>Failed to start cluster2 with message: ${failure.message}</echo>
                <stop-node es.pidfile="${cluster1.pidfile}"/>
                <stop-node es.pidfile="${cluster2.pidfile}"/>
            </ac:catch>
        </ac:trycatch>


        <ac:trycatch property="failure.message">
            <ac:try>
                <echo>Starting a tribe node, configured to connect to cluster1 and cluster2</echo>
                <startup-elasticsearch es.pidfile="${tribe_node.pidfile}">
                    <additional-args>
                        <arg value="-Des.tribe.cluster1.cluster.name=cluster1"/>
                        <arg value="-Des.tribe.cluster1.discovery.zen.ping.unicast.hosts=127.0.0.1:9600"/>

                        <arg value="-Des.tribe.cluster2.cluster.name=cluster2"/>
                        <arg value="-Des.tribe.cluster2.discovery.zen.ping.unicast.hosts=127.0.0.1:9800"/>
                    </additional-args>
                </startup-elasticsearch>
                <xhttp uri="http://127.0.0.1:${integ.http.port}/_cluster/health?wait_for_nodes=5" username="test_admin" password="<PASSWORD>" />

                <!--
                    From the rest tests we only connect to the tribe node, so we need create the indices externally:

                    By creating the index after the tribe node has started we can be sure that the tribe node knows
                    about it. See: https://github.com/elastic/elasticsearch/issues/13292
                 -->
                <echo>Creating index1 in cluster1</echo>
                <create-index name="index1" port="9700"/>
                <!-- TODO: remove this after we know why on CI the shards of index1 don't get into a started state -->
                <loadfile property="cluster1-logs" srcFile="${integ.scratch}/elasticsearch-${elasticsearch.version}/logs/cluster1.log" />
                <echo>post index1 creation es logs: ${cluster1-logs}</echo>

                <echo>Creating index2 in cluster2</echo>
                <create-index name="index2" port="9900"/>
                <!-- TODO: remove this after we know why on CI the shards of index2 don't get into a started state -->
                <loadfile property="cluster2-logs" srcFile="${integ.scratch}/elasticsearch-${elasticsearch.version}/logs/cluster2.log" />
                <echo>post index2 creation es logs: ${cluster2-logs}</echo>
            </ac:try>
{'fraction_non_alphanumeric': 0.1401130713166678, 'fraction_numerical': 0.01852734827327839, 'mean_word_length': 1.9696601941747574, 'pattern_counts': {'":': 0, '<': 229, '<?xml version=': 2, '>': 229, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '364264', 'n_tokens_mistral': 4212, 'n_tokens_neox': 3925, 'n_words': 805}