<?xml version="1.0" encoding="UTF-8"?>
<!--
  #%L
  Wildfly Camel Testsuite
  %%
  Copyright (C) 2013 - 2014 RedHat
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.wildfly.camel</groupId>
        <artifactId>wildfly-camel-itests-docker</artifactId>
        <version>3.2.0</version>
    </parent>

    <name>Wildfly Camel :: Testsuite :: Docker :: Domain</name>

    <artifactId>wildfly-camel-itests-docker-domain</artifactId>
    <packaging>war</packaging>

    <!-- Properties -->
    <properties>
        <docker.image.name>wildflyext/wildfly-camel</docker.image.name>
        <docker.alias.master>domain-master</docker.alias.master>
        <docker.alias.slave>domain-slave</docker.alias.slave>
        <docker.cmd.master>--domain-config domain-camel.xml --host-config host-master.xml -b 0.0.0.0 -bmanagement 0.0.0.0</docker.cmd.master>
        <docker.cmd.slave>--domain-config domain-camel.xml --host-config host-camel-slave.xml -b 0.0.0.0 -bmanagement 0.0.0.0</docker.cmd.slave>
        <wildfly.mgmt.username>admin</wildfly.mgmt.username>
        <wildfly.mgmt.password>admin</wildfly.mgmt.password>
    </properties>
    
    <!-- Dependencies -->
    <dependencies>
        <!-- Provided -->
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <!-- Build -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.jolokia</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-containers</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                        <configuration>
                            <images>
                                <image>
                                    <name>${docker.image.name}</name>
                                    <alias>${docker.alias.master}</alias>
                                    <run>
                                        <env>
                                            <WILDFLY_MANAGEMENT_USER>${wildfly.mgmt.username}</WILDFLY_MANAGEMENT_USER>
                                            <WILDFLY_MANAGEMENT_PASSWORD>${wildfly.mgmt.password}</WILDFLY_MANAGEMENT_PASSWORD>
                                        </env>
                                        <ports>
                                            <port>9990:9990</port>
                                        </ports>
                                        <cmd>${docker.cmd.master}</cmd>
                                        <log>
                                            <enabled>true</enabled>
                                        </log>
                                    </run>
                                </image>
                                <image>
                                    <name>${docker.image.name}</name>
                                    <alias>${docker.alias.slave}</alias>
                                    <run>
                                        <env>
                                            <WILDFLY_MANAGEMENT_USER>${wildfly.mgmt.username}</WILDFLY_MANAGEMENT_USER>
                                            <WILDFLY_MANAGEMENT_PASSWORD>${wildfly.mgmt.password}</WILDFLY_MANAGEMENT_PASSWORD>
                                        </env>
                                        <ports>
                                            <port>8181:8080</port>
                                        </ports>
                                        <links>
                                            <link>${docker.alias.master}:domain-controller</link>
                                        </links>
                                        <cmd>${docker.cmd.slave}</cmd>
                                        <log>
                                            <enabled>true</enabled>
                                        </log>
                                        <wait>
                                            <url>http://${env.DOCKER_IP}:8181</url>
                                            <time>${env.DOCKER_PLUGIN_TIMEOUT}</time>
                                        </wait>
                                    </run>
                                </image>
                            </images>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-containers</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                        <configuration>
                            <images>
                                <image>
                                    <alias>${docker.alias.slave}</alias>
                                </image>
                                <image>
                                    <alias>${docker.alias.master}</alias>
                                </image>
                            </images>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <!-- [FIXME #175] Deployment fails with missing/unavailable dependencies -->
                                <sleep seconds="10" />
                                <echo>=========================================================================</echo>
                                <echo>docker run --rm -ti -p 9990:9990 -e WILDFLY_MANAGEMENT_USER=${wildfly.mgmt.username} -e WILDFLY_MANAGEMENT_PASSWORD=${wildfly.mgmt.password} --name=${docker.alias.master} ${docker.image.name} ${docker.cmd.master}</echo>
                                <echo>docker run --rm -ti -p 8181:8080 -e WILDFLY_MANAGEMENT_USER=${wildfly.mgmt.username} -e WILDFLY_MANAGEMENT_PASSWORD=${wildfly.mgmt.password} --link ${docker.alias.master}:domain-controller --name=${docker.alias.slave} ${docker.image.name} ${docker.cmd.slave}</echo>
                                <echo>=========================================================================</echo>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>get-domain-master-ip</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                // List all running containers
                                def proc = ["docker", "ps", "-q"].execute()
                                proc.waitFor()

                                if (proc.exitValue() == 0) {
                                    def containerIds = proc.in.text
                                    containerIds.split("\n").each { containerId -&gt;
                                        // If the container exposes port 9990 assume it's the domain master
                                        def portProc = ["docker", "inspect", "--format='{{(index (index .NetworkSettings.Ports \"9990/tcp\") 0).HostPort}}'", containerId].execute()
                                        portProc.waitFor()

                                        if(portProc.exitValue() == 0) {
                                            // Get the container IP address and store in a Maven property
                                            ipProc = ["docker", "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", containerId].execute()
                                            ipProc.waitFor()

                                            if(ipProc.exitValue() == 0) {
                                                project.properties.setProperty("wildfly.mgmt.address", ipProc.in.text)
                                            }
                                        }
                                    }
                                } else {
                                    fail("Unable to determine running containers")
                                }
                            </source>
                        </configuration>
                    </execution>
                    <execution>
                        <id>set-selinux-properties</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                // Change target directory SELinux label on environments that support it
                                if (System.properties['os.name'].toLowerCase().equals('linux')) {
                                    def proc = ["which", "chcon"].execute()
                                    proc.waitFor()
                                    if(proc.exitValue() == 0) {
                                        "chcon -Rt svirt_sandbox_file_t ${project.build.directory}".execute()
                                    }
                                }
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <systemPropertyVariables>
                                <wildfly.mgmt.address>${wildfly.mgmt.address}</wildfly.mgmt.address>
                                <wildfly.mgmt.username>${wildfly.mgmt.username}</wildfly.mgmt.username>
                                <wildfly.mgmt.password>${wildfly.mgmt.password}</wildfly.mgmt.password>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!-- Profiles -->
    <profiles>
        <profile>
            <id>docker.timeout</id>
            <activation>
                <property>
                    <name>!env.DOCKER_PLUGIN_TIMEOUT</name>
                </property>
            </activation>
            <properties>
                <env.DOCKER_PLUGIN_TIMEOUT>60000</env.DOCKER_PLUGIN_TIMEOUT>
            </properties>
        </profile>
    </profiles>

</project>
