<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/maven-v4_0_0.xsd">

  <parent>
    <groupId>com.atlassian.pom</groupId>
    <artifactId>atlassian-public-pom</artifactId>
    <version>28</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atlassian.bamboo</groupId>
  <artifactId>atlassian-bamboo-elastic-image</artifactId>
  <version>2.9</version>
  <packaging>ami</packaging>
  <name>Atlassian Bamboo Elastic Agent Default Image</name>

  <scm>
    <connection>scm:hg:ssh://hg@bitbucket.org/atlassian/bamboo/</connection>
    <developerConnection>scm:hg:ssh://hg@bitbucket.org/atlassian/bamboo/</developerConnection>
    <url>https://atlaseye.atlassian.com/browse/bamboo/trunk/bamboo-elasic-image</url>
  </scm>

  <properties>
    <imageBucket>${s3.imageBucketStem}-${s3.homeRegion}</imageBucket>
    <imagePrefix>elasticbamboo-${project.version}-ARCH-${elasticbamboo.label}${timestamp}</imagePrefix>
    <installerArtifactId>atlassian-bamboo-agent-elastic-installer</installerArtifactId>
    <installerClassifier>jar-with-dependencies</installerClassifier>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.atlassian.bamboo</groupId>
      <artifactId>${installerArtifactId}</artifactId>
      <version>5.0-rc1</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.1</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <phase>process-resources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.atlassian.bamboo</groupId>
                  <artifactId>${installerArtifactId}</artifactId>
                  <classifier>${installerClassifier}</classifier>
                  <outputDirectory>${project.build.directory}</outputDirectory>
                </artifactItem>
              </artifactItems>
              <overWriteSnapshots>true</overWriteSnapshots>
              <stripVersion>true</stripVersion>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>build-s3-assembly</id>
            <goals>
              <goal>single</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
              <finalName>imagefiles</finalName>
              <descriptors>
                <descriptor>src/main/resources/assembly.xml</descriptor>
              </descriptors>
              <appendAssemblyId>false</appendAssemblyId>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <executions>
          <execution>
            <id>enforce-build-environment</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requirePluginVersions>
                  <banSnapshots>false</banSnapshots>
                </requirePluginVersions>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <!-- used for tagging EC2 images -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>create</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <format>{0,date,yyyy-MM-dd_HH:mm:ss}</format>
          <items>
            <item>timestamp</item>
          </items>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.atlassian.maven.plugins</groupId>
        <artifactId>maven-aws-plugin</artifactId>
        <version>1.0.18</version>
        <extensions>true</extensions>
        <configuration>
          <!-- Secrets config -->
          <awsSecretAccessKey>${aws.secretAccessKey}</awsSecretAccessKey>
          <ec2PrivateKeyFile>${ec2.privateKeyFile}</ec2PrivateKeyFile>
          <awsAccessKeyID>${aws.accessKeyID}</awsAccessKeyID>
          <rebuildImage>true</rebuildImage>
          <amiIDFile>${project.build.directory}/${project.build.finalName}.${project.packaging}</amiIDFile>
          <imageListFile>src/main/ec2/bamboo_ami_list.txt</imageListFile>
          <ec2KeyName>elasticbamboo</ec2KeyName>
          <instanceType32>c1.medium</instanceType32>
          <instanceType64>m1.large</instanceType64>
          <ec2ImageTag>${project.version}::${user.name}::${buildNumber}</ec2ImageTag>
          <user>ec2-user</user>
          <files>
            <file>${ec2.publicCertificate}</file>
            <file>${ec2.privateKeyFile}</file>
            <file>${aws.accountPrivateKeyFile}</file>
            <file>src/main/ec2/customise.sh</file>
            <file>src/main/ec2/bamboo-capabilities.properties</file>
            <file>${project.build.directory}/imagefiles.zip</file>
          </files>
          <destination>/tmp</destination>
          <command>sudo -i sh -x /tmp/customise.sh ${aws.secretAccessKey} ${project.version} ${imagePrefix} ${aws.accountNumber} ${imageBucket} ${aws.accessKeyID} ${s3.agentAssemblyBucketStem}-${s3.homeRegion}</command>
          <manifest>${imageBucket}/${imagePrefix}.manifest.xml</manifest>
          <windowsDefaultAmiId>ami-976edafe</windowsDefaultAmiId>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
