<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright 2016-2022 Micro Focus or one of its affiliates.

    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.

-->
<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>

    <groupId>com.github.jobservice</groupId>
    <artifactId>job-service-postgres-container</artifactId>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.github.jobservice</groupId>
        <artifactId>job-service-aggregator</artifactId>
        <version>6.0.0-31</version>
    </parent>
    
    <properties>
        <js.database.host>postgres</js.database.host>
        <js.database.username>postgres</js.database.username>
        <js.database.password>root</js.database.password>
        <js.database.name>jobservice</js.database.name>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.github.jobservice</groupId>
            <artifactId>job-service-db</artifactId>
            <version>${project.version}</version>
            <classifier>jar-with-dependencies</classifier>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${fabric8.docker.maven.version}</version>
                <executions>
                    <execution>
                        <id>docker-build</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>start</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>docker-push</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <filter>job-service-postgres</filter>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <autoPull>on</autoPull>
                    <removeVolumes>true</removeVolumes>
                    <images>
                        <!-- Postgres DB -->
                        <image>
                            <alias>${js.database.host}</alias>
                            <name>${dockerHubPublic}/library/postgres:11</name>
                            <run>
                                <ports>
                                    <port>${postgres.db.port}:5432</port>
                                </ports>
                                <env>
                                    <POSTGRES_PASSWORD>${js.database.password}</POSTGRES_PASSWORD>
                                    <POSTGRES_USER>${js.database.username}</POSTGRES_USER>
                                    <POSTGRES_DB>${js.database.name}</POSTGRES_DB>
                                </env>
                                <wait>
                                    <log>PostgreSQL init process complete</log>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>
                        
                        <!-- Run old version of jobservice postgres container against postgres database to install jobservice schema. -->
                        <image>
                            <alias>job-service-postgres-old</alias>
                            <name>${dockerHubPublic}/jobservice/job-service-postgres:2</name>
                            <run>
                                <links>
                                    <link>${js.database.host}</link>
                                </links>
                                <env>
                                    <POSTGRES_DB>${js.database.name}</POSTGRES_DB>
                                </env>
                                <cmd>
                                    ./install_job_service_db.sh -fd -db.connection jdbc:postgresql://${js.database.host}:5432 -db.name ${js.database.name} -db.user ${js.database.username} -db.pass ${js.database.password}
                                </cmd>
                                <wait>
                                    <log>DB update finished.</log>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>
                        
                        <!-- Run jobservice postgres container against postgres database to upgrade jobservice schema. -->
                        <image>
                            <alias>job-service-postgres</alias>
                            <name>${dockerJobServiceOrg}job-service-postgres${dockerProjectVersion}</name>
                            <build>
                                <from>${dockerHubPublic}/cafapi/java-postgres:2</from>
                                <optimise>true</optimise>
                                <assembly>
                                    <mode>tar</mode>
                                    <basedir>/</basedir>
                                    <inline>
                                        <dependencySets>
                                            <dependencySet>
                                                <fileMode>0755</fileMode>
                                                <useProjectArtifact>true</useProjectArtifact>
                                                <includes>
                                                    <include>com.github.jobservice:job-service-db</include>
                                                </includes>
                                                <outputFileNameMapping>job-service-db.jar</outputFileNameMapping>
                                            </dependencySet>
                                        </dependencySets>
                                        <fileSets>
                                            <fileSet>
                                                <directory>scripts</directory>
                                                <outputDirectory>/docker-entrypoint-initdb.d</outputDirectory>
                                                <lineEnding>unix</lineEnding>
                                                <includes>
                                                    <include>setup.sh</include>
                                                </includes>
                                                <fileMode>0755</fileMode>
                                            </fileSet>
                                            <fileSet>
                                                <directory>scripts/install</directory>
                                                <outputDirectory>/</outputDirectory>
                                                <lineEnding>unix</lineEnding>
                                                <includes>
                                                    <include>*.sh</include>
                                                </includes>
                                                <fileMode>0755</fileMode>
                                            </fileSet>
                                        </fileSets>
                                    </inline>
                                </assembly>
                                <env>
                                    <POSTGRES_DB>${js.database.name}</POSTGRES_DB>
                                </env>
                            </build>
                            <run>
                                <links>
                                    <link>${js.database.host}</link>
                                </links>
                                <cmd>
                                    ./install_job_service_db.sh -db.host ${js.database.host} -db.port 5432 -db.name
                                    ${js.database.name} -db.user ${js.database.username} -db.pass ${js.database.password}
                                </cmd>
                                <wait>
                                    <log>DB update finished.</log>
                                    <time>120000</time>
                                    <shutdown>500</shutdown>
                                </wait>
                                <log>
                                    <enabled>true</enabled>
                                </log>
                            </run>
                        </image>
                    </images>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>use-default-fixed-ports</id>
            <properties>
                <postgres.db.port>5432</postgres.db.port>
            </properties>
        </profile>
    </profiles>
</project>
