diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..806d27a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,19 @@
+# Intellij files
+.idea/
+
+# gradle's output dir
+build/
+out/
+src/main/resources-generated
+
+# the gradle temp dir
+.gradle
+
+# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
+!gradle-wrapper.jar
+
+#Classes
+classes/
+
+.bundle/
+contrib/cloudformation-template/bin/
\ No newline at end of file
diff --git a/.idea/copyright/Apache_2_0.xml b/.idea/copyright/Apache_2_0.xml
new file mode 100644
index 0000000..8275ae5
--- /dev/null
+++ b/.idea/copyright/Apache_2_0.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
new file mode 100644
index 0000000..f546630
--- /dev/null
+++ b/.idea/copyright/profiles_settings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3f3918b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,41 @@
+# GoCD - Amazon ECS Elastic Agent Plugin
+
+This Elastic Agent Plugin for Amazon EC2 Container Service allows you to run elastic agents on Amazon ECS (Docker container service on AWS). The plugin takes care of spinning up and shutting down EC2 instances based on the need of your deployment pipeline, thus removing bottlenecks and reducing the cost of your agent infrastructure.
+
+When a pipeline is triggered, GoCD sees that the jobs in the pipeline have been configured to use ECS agents, and passes on information about the configured elastic agent profiles to the plugin. The plugin, based on the user-defined configuration, decides how many EC2 instances to bring up or reuse and how many ECS elastic agents to bring up within those EC2 instances.
+
+The settings allow you to choose the AMI to be used for the EC2 instance, the instance type, security groups, the Docker image for the ECS container and memory limits among other settings. Since the Docker image is specified as a part of the profile, provisioning software for a build, test or deploy agent becomes much easier.
+
+Once the builds finish and the EC2 instances are idle for a while, they will be automatically scaled down and destroyed, removing the cost of running idle EC2 instances. Along with saving cost, this enables a flexible and dynamic build grid in which you don’t need to worry about configuration drift.
+
+The set of images [here](docs/plugin_as_images.md) explain this concept as well
+
+Table of Contents
+=================
+
+ * [Building the code base](#building-the-code-base)
+ * [Installing and configuring the plugin](docs/installation.md)
+ * [FAQ](docs/faq.md)
+ * [Troubleshooting](docs/troubleshooting.md)
+
+## Building the code base
+
+To build the jar, run `./gradlew clean test assemble`
+
+## License
+
+```plain
+Copyright 2020 ThoughtWorks, Inc.
+
+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.
+```
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..2ed82b8
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2020 ThoughtWorks, Inc.
+ *
+ * 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.
+ */
+
+apply from: "https://raw.githubusercontent.com/gocd/gocd-plugin-gradle-task-helpers/master/helper.gradle?_=${(int) (new Date().toInstant().epochSecond / 60)}"
+apply plugin: 'java'
+
+group = 'com.thoughtworks.gocd'
+
+gocdPlugin {
+ id = 'com.thoughtworks.gocd.elastic-agent.ecs'
+ pluginVersion = '6.3.0'
+ goCdVersion = '19.3.0'
+ name = 'GoCD Elastic Agent Plugin for Amazon ECS'
+ description = 'GoCD Elastic Agent Plugin for Amazon Elastic Container Service allow for more efficient use of instances'
+ vendorName = 'ThoughtWorks, Inc.'
+ vendorUrl = 'https://github.com/gocd/gocd-ecs-elastic-agent'
+
+ githubRepo {
+ owner = System.getenv('GITHUB_USER') ?: 'bob'
+ repo = 'gocd-ecs-elastic-agent'
+ token = System.getenv('GITHUB_TOKEN') ?: 'bad-token'
+ }
+
+ pluginProject = project
+
+ prerelease = !"No".equalsIgnoreCase(System.getenv('PRERELEASE'))
+ assetsToRelease = [project.tasks.findByName('jar')]
+}
+
+version = gocdPlugin.fullVersion(project)
+
+repositories {
+ jcenter()
+ mavenLocal()
+}
+
+sourceSets {
+ test {
+ java {
+ compileClasspath += configurations.compileOnly
+ runtimeClasspath += configurations.compileOnly
+ }
+ }
+}
+
+project.ext.versions = [
+ goPluginApi: "18.9.0",
+ lombok : "1.18.4",
+ gson : '2.8.5',
+ guava : '24.0-jre',
+ lang3 : '3.8.1',
+ joda : '2.10.1',
+ collection4: '4.2',
+ awsSDK : '1.11.454',
+ freemarker : '2.3.28',
+ jclOverSl4j: '1.7.25',
+ mockito : '2.22.0',
+ jsonAssert : '1.5.0',
+ jsoup : '1.10.2',
+ asserj : '3.6.2',
+ junit5 : '5.3.0-M1'
+]
+
+dependencies {
+ annotationProcessor group: 'org.projectlombok', name: 'lombok', version: project.versions.lombok
+
+ compileOnly group: 'cd.go.plugin', name: 'go-plugin-api', version: project.versions.goPluginApi
+ compileOnly group: 'org.projectlombok', name: 'lombok', version: project.versions.lombok
+
+ compile group: 'com.google.code.gson', name: 'gson', version: project.versions.gson
+ compile group: 'com.google.guava', name: 'guava', version: project.versions.guava
+ compile group: 'org.apache.commons', name: 'commons-lang3', version: project.versions.lang3
+ compile group: 'joda-time', name: 'joda-time', version: project.versions.joda
+ compile group: 'org.apache.commons', name: 'commons-collections4', version: project.versions.collection4
+
+ compile group: 'com.amazonaws', name: 'aws-java-sdk-ecs', version: project.versions.awsSDK
+ compile group: 'com.amazonaws', name: 'aws-java-sdk-ec2', version: project.versions.awsSDK
+ compile group: 'org.freemarker', name: 'freemarker', version: project.versions.freemarker
+ compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: project.versions.jclOverSl4j
+
+ testCompile group: 'org.mockito', name: 'mockito-core', version: project.versions.mockito
+ testCompile group: 'org.skyscreamer', name: 'jsonassert', version: project.versions.jsonAssert
+ testCompile group: 'org.jsoup', name: 'jsoup', version: project.versions.jsoup
+ testCompile group: 'org.assertj', name: 'assertj-core', version: project.versions.asserj
+
+ testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: project.versions.junit5
+ testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: project.versions.junit5
+ testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: project.versions.junit5
+}
+
+tasks.withType(JavaCompile) {
+ options.deprecation = true
+ options.encoding = 'utf-8'
+ options.warnings = true
+ options.compilerArgs << "-Xlint:all"
+ options.compilerArgs << "-Xlint:-serial"
+}
+
+test {
+ useJUnitPlatform()
+}
+
+jar {
+ from(configurations.compile) {
+ into "lib/"
+ }
+}
+
+task bundleInstall {
+ inputs.files(project.files('contrib/cloudformation-template/Gemfile', 'contrib/cloudformation-template/Gemfile.lock'))
+ outputs.dir(project.file('.bundle'))
+
+ doLast {
+ project.exec {
+ commandLine = ['bundle', 'install', '--path', project.file('.bundle'), '--jobs=4', '--clean']
+ workingDir = project.file('contrib/cloudformation-template')
+ standardOutput = System.out
+ errorOutput = System.err
+ }
+ }
+}
+
+task stackfile {
+ dependsOn bundleInstall
+
+ inputs.file(project.file('contrib/cloudformation-template/ecs_cloud_formation_template.rb'))
+ outputs.file(project.file("${project.buildDir}/ecs_cloud_formation_template.json"))
+
+ doLast {
+ project.exec {
+ commandLine = ['bundle', 'exec', 'ruby', 'ecs_cloud_formation_template.rb', 'expand']
+ workingDir = project.file('contrib/cloudformation-template')
+ standardOutput = new FileOutputStream(project.file("${project.buildDir}/ecs_cloud_formation_template.json"))
+ errorOutput = System.err
+ }
+ }
+}
diff --git a/contrib/cloudformation-template/Gemfile b/contrib/cloudformation-template/Gemfile
new file mode 100644
index 0000000..5be9b1e
--- /dev/null
+++ b/contrib/cloudformation-template/Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+gem 'cloudformation-ruby-dsl'
+
diff --git a/contrib/cloudformation-template/Gemfile.lock b/contrib/cloudformation-template/Gemfile.lock
new file mode 100644
index 0000000..f53e7dd
--- /dev/null
+++ b/contrib/cloudformation-template/Gemfile.lock
@@ -0,0 +1,35 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ aws-sdk (2.9.14)
+ aws-sdk-resources (= 2.9.14)
+ aws-sdk-core (2.9.14)
+ aws-sigv4 (~> 1.0)
+ jmespath (~> 1.0)
+ aws-sdk-resources (2.9.14)
+ aws-sdk-core (= 2.9.14)
+ aws-sigv4 (1.0.0)
+ cloudformation-ruby-dsl (1.4.6)
+ aws-sdk (>= 2.5.1)
+ bundler
+ detabulator
+ diffy
+ highline
+ json
+ rake
+ detabulator (0.1.0)
+ diffy (3.2.0)
+ highline (1.7.8)
+ jmespath (1.3.1)
+ json (2.1.0)
+ rake (12.0.0)
+
+PLATFORMS
+ ruby
+ x86_64-darwin-17
+
+DEPENDENCIES
+ cloudformation-ruby-dsl
+
+BUNDLED WITH
+ 1.16.3
diff --git a/contrib/cloudformation-template/README.md b/contrib/cloudformation-template/README.md
new file mode 100755
index 0000000..8c7f949
--- /dev/null
+++ b/contrib/cloudformation-template/README.md
@@ -0,0 +1,6 @@
+## Run following commands to create cloud-formation stack
+
+```bash
+bundle install --jobs 4 --path .bundle --clean
+bundle exec ruby ecs_cloud_formation_template.rb create --stack-name i
+```
diff --git a/contrib/cloudformation-template/ecs_cloud_formation_template.rb b/contrib/cloudformation-template/ecs_cloud_formation_template.rb
new file mode 100644
index 0000000..d5a88d0
--- /dev/null
+++ b/contrib/cloudformation-template/ecs_cloud_formation_template.rb
@@ -0,0 +1,154 @@
+#!/usr/bin/env ruby
+
+require 'bundler/setup'
+require 'cloudformation-ruby-dsl/cfntemplate'
+require 'cloudformation-ruby-dsl/spotprice'
+require 'cloudformation-ruby-dsl/table'
+
+template do
+
+ value AWSTemplateFormatVersion: '2010-09-09'
+
+ parameter 'ClusterName',
+ :Description => 'The name of the ECS cluster',
+ :Type => 'String',
+ :Default => 'GoCD',
+ :UsePreviousValue => true
+
+ output 'AccessKey',
+ Value: ref('AccessKey')
+
+ output 'SecretKey',
+ Value: get_att('AccessKey', 'SecretAccessKey')
+
+ output 'GoCDEC2OptimizedRole',
+ Value: get_att('GoCDEC2OptimizedRole', 'Arn')
+
+ output 'GoCDEC2OptimizedInstanceProfile',
+ Value: get_att('GoCDEC2OptimizedInstanceProfile', 'Arn')
+
+ resource 'GoCDECSCluster',
+ Type: 'AWS::ECS::Cluster',
+ Properties: {
+ ClusterName: ref('ClusterName')
+ }
+
+ resource 'AccessKey',
+ Type: 'AWS::IAM::AccessKey',
+ Properties: {
+ UserName: ref('GoCDECSPluginUser')
+ }
+
+ resource 'GoCDECSPluginUser',
+ Type: 'AWS::IAM::User',
+ Properties: {
+ Policies: [
+ {
+ PolicyName: 'ManageEC2Instances',
+ PolicyDocument: {
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Action: %w(
+ ec2:runInstances
+ ec2:createTags
+ ec2:terminateInstances
+ ec2:describeInstances
+ ec2:describeSubnets
+ ec2:createVolume
+ ec2:attachVolume
+ ec2:stopInstances
+ ec2:startInstances
+ ec2:requestSpotInstances
+ ec2:describeSpotInstanceRequests
+ ec2:deleteTags
+ iam:PassRole
+ iam:GetRole
+ ),
+ Resource: ['*'],
+ },
+ ],
+ },
+ },
+ {
+ PolicyName: 'ManageECSInstances',
+ PolicyDocument: {
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Action: %w(
+ ecs:describeClusters
+ ecs:deregisterContainerInstance
+ ecs:describeContainerInstances
+ ecs:listContainerInstances
+ ecs:registerTaskDefinition
+ ecs:deregisterTaskDefinition
+ ecs:startTask
+ ecs:stopTask
+ ecs:listTasks
+ ecs:describeTasks
+ ecs:describeTaskDefinition
+ ),
+ Resource: ['*'],
+ },
+ ],
+ },
+ }
+ ],
+ }
+
+ resource 'GoCDEC2OptimizedRole',
+ Type: 'AWS::IAM::Role',
+ Properties: {
+ AssumeRolePolicyDocument: {
+ Statement: {
+ Effect: 'Allow',
+ Principal: {
+ Service: [
+ 'ec2.amazonaws.com'
+ ]
+ },
+ Action: [
+ 'sts:AssumeRole'
+ ]
+ }
+ },
+ Policies: [
+ {
+ PolicyName: 'AllowECSAgentToManageContainers',
+ PolicyDocument: {
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Action:
+ %w(
+ ecs:describeClusters
+ ecs:discoverPollEndpoint
+ ecs:registerContainerInstance
+ ecs:deregisterContainerInstance
+ ecs:poll
+ ecs:startTelemetrySession
+ ecs:submitContainerStateChange
+ ecs:submitTaskStateChange
+ logs:CreateLogStream
+ logs:PutLogEvents
+ ),
+ Resource: [
+ '*'
+ ]
+ }
+ ]
+ }
+ }
+ ]
+ }
+
+ resource 'GoCDEC2OptimizedInstanceProfile',
+ Type: 'AWS::IAM::InstanceProfile',
+ Properties: {
+ Roles: [ref('GoCDEC2OptimizedRole')]
+ }
+end.exec!
diff --git a/contrib/scripts/bootstrap-via-installer/Dockerfile b/contrib/scripts/bootstrap-via-installer/Dockerfile
new file mode 100644
index 0000000..151cb45
--- /dev/null
+++ b/contrib/scripts/bootstrap-via-installer/Dockerfile
@@ -0,0 +1,26 @@
+FROM ...
+
+
+# GoCD agent needs the jdk and git/svn/mercurial...
+# Uncomment one of the lines below to ensure that openjdk is installed.
+# apt-get install openjdk-8-jre-headless git
+# yum install java-1.8.0-openjdk-headless git
+
+# add steps to download and install the gocd agent
+# See https://docs.gocd.io/current/installation/install/agent/linux.html
+
+# download tini to ensure that an init process exists
+ADD https://github.com/krallin/tini/releases/download/v0.14.0/tini /tini
+RUN chmod +x /tini
+ENTRYPOINT ["/tini", "--"]
+
+# ensure that the container logs on stdout
+ADD log4j.properties /var/lib/go-agent/log4j.properties
+ADD log4j.properties /var/lib/go-agent/go-agent-log4j.properties
+
+ADD go-agent /go-agent
+RUN chmod 755 /go-agent
+
+# Run the bootstrapper as the `go` user
+USER go
+CMD /go-agent
diff --git a/contrib/scripts/bootstrap-via-installer/go-agent b/contrib/scripts/bootstrap-via-installer/go-agent
new file mode 100644
index 0000000..e8342da
--- /dev/null
+++ b/contrib/scripts/bootstrap-via-installer/go-agent
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+# Use this script to start up your GoCD agent process. The script assumes that
+# the agent is installed using .deb/.rpm as the case may be with your OS.
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+if ! [[ -x /etc/init.d/go-agent && -e /etc/defaults/go-agent && -d /var/lib/go-agent ]]; then
+ echo "It looks like the agent was not installed via deb/rpm"
+ exit -1
+fi
+
+if [[ "$(whoami)" != 'go' ]]; then
+ echo "Must run this script as the `go` user"
+ exit -1
+fi
+
+mkdir -p /var/lib/go-agent/config || die "Could not create /var/lib/go-agent/config"
+
+# write out autoregister.properties
+(
+cat < /var/lib/go-agent/config/autoregister.properties
+
+# write out server url, and prevent backgrounding
+echo "GO_SERVER_URL=${GO_EA_SERVER_URL}" > /etc/default/go-agent
+
+# prevent environment variables from leaking into the agent
+unset GO_EA_AUTO_REGISTER_KEY
+unset GO_EA_AUTO_REGISTER_ENVIRONMENT
+unset GO_EA_AUTO_REGISTER_ELASTIC_AGENT_ID
+unset GO_EA_AUTO_REGISTER_ELASTIC_PLUGIN_ID
+unset GO_EA_SERVER_URL
+
+exec /usr/share/go-agent/agent.sh go-agent
diff --git a/contrib/scripts/bootstrap-via-installer/log4j.properties b/contrib/scripts/bootstrap-via-installer/log4j.properties
new file mode 100644
index 0000000..7c7762f
--- /dev/null
+++ b/contrib/scripts/bootstrap-via-installer/log4j.properties
@@ -0,0 +1,23 @@
+#
+# Copyright 2020 ThoughtWorks, Inc.
+#
+# 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.
+#
+
+# default to INFO logging on stdout
+log4j.rootLogger=INFO, stdout
+
+# write logs to stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.conversionPattern=%d{ISO8601} [%-9t] %-5p %-16c{4}:%L %x- %m%n
diff --git a/contrib/scripts/bootstrap-without-installed-agent/Dockerfile b/contrib/scripts/bootstrap-without-installed-agent/Dockerfile
new file mode 100644
index 0000000..61b1d4f
--- /dev/null
+++ b/contrib/scripts/bootstrap-without-installed-agent/Dockerfile
@@ -0,0 +1,25 @@
+FROM ...
+
+# GoCD agent needs the jdk and git/svn/mercurial...
+# Uncomment one of the lines below to ensure that openjdk is installed.
+# apt-get install openjdk-8-jre-headless git
+# yum install java-1.8.0-openjdk-headless git
+
+# download tini to ensure that an init process exists
+ADD https://github.com/krallin/tini/releases/download/v0.14.0/tini /tini
+RUN chmod +x /tini
+ENTRYPOINT ["/tini", "--"]
+
+# Add a user to run the go agent
+RUN adduser go go -h /go -S -D
+
+# ensure that the container logs on stdout
+ADD log4j.properties /go/log4j.properties
+ADD log4j.properties /go/go-agent-log4j.properties
+
+ADD go-agent /go-agent
+RUN chmod 755 /go-agent
+
+# Run the bootstrapper as the `go` user
+USER go
+CMD /go/go-agent
diff --git a/contrib/scripts/bootstrap-without-installed-agent/go-agent b/contrib/scripts/bootstrap-without-installed-agent/go-agent
new file mode 100644
index 0000000..4c351a9
--- /dev/null
+++ b/contrib/scripts/bootstrap-without-installed-agent/go-agent
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+# Use this script to start up your GoCD agent process. The script assumes that —
+# - no agent is installed via deb/rpm
+# - curl is available on $PATH
+# - user `go` exists
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+download_file () {
+ local remote_path="$1"
+ local local_path="$2"
+ local url="${GO_EA_SERVER_URL}${remote_path}"
+
+ echo "Downloading remote file ${url} into ${local_path}."
+ curl --fail --silent --insecure "${url}" -o "${local_path}" || die "Could not fetch ${url}."
+}
+
+get_checksums () {
+ local url="${GO_EA_SERVER_URL}/admin/latest-agent.status"
+ curl --fail --silent --insecure "${url}" -D - || die "Could not fetch ${url}"
+}
+
+get_checksum () {
+ local checksums="${1}"
+ local header="${2}"
+ echo "${checksums}" | tr -d '\r' | grep "${header}" | sed -e "s/${header}: //g"
+}
+
+if [[ "$(whoami)" != 'go' ]]; then
+ echo "Must run this script as the `go` user"
+ exit -1
+fi
+
+mkdir -p /go/config || die "Could not create /go/config"
+cd /go || die "Could not chdir to /go"
+
+# write out autoregister.properties
+(
+cat < ./config/autoregister.properties
+
+while true; do
+ download_file '/admin/agent' 'agent.jar'
+ download_file '/admin/agent-plugins.zip' 'agent-plugins.zip'
+ download_file '/admin/tfs-impl.jar' 'tfs-impl.jar'
+
+ checksums=$(get_checksums)
+ agent_md5=$(get_checksum "${checksums}" 'Agent-Content-MD5')
+ tfs_md5=$(get_checksum "${checksums}" 'TFS-SDK-Content-MD5')
+ plugins_md5=$(get_checksum "${checksums}" 'Agent-Plugins-Content-MD5')
+ agent_launcher_md5=$(get_checksum "${checksums}" 'Agent-Launcher-Content-MD5')
+
+ echo "Launching the GoCD Agent, and waiting for it to exit..."
+ RUN_CMD=(java -Dcruise.console.publish.interval=10 \
+ -Xms128m \
+ -Xmx256m \
+ -Djava.security.egd=file:/dev/./urandom \
+ -Dagent.plugins.md5="${plugins_md5}" \
+ -Dagent.binary.md5="${agent_md5}" \
+ -Dagent.launcher.md5="${agent_launcher_md5}" \
+ -Dagent.tfs.md5="${tfs_md5}" \
+ -jar \
+ agent.jar \
+ -serverUrl \
+ "${GO_EA_SERVER_URL}")
+ "${RUN_CMD[@]}"
+ echo "The GoCD Agent exited with code $?. Waiting 10 seconds before re-launching"
+ sleep 10
+done
diff --git a/contrib/scripts/bootstrap-without-installed-agent/log4j.properties b/contrib/scripts/bootstrap-without-installed-agent/log4j.properties
new file mode 100644
index 0000000..7c7762f
--- /dev/null
+++ b/contrib/scripts/bootstrap-without-installed-agent/log4j.properties
@@ -0,0 +1,23 @@
+#
+# Copyright 2020 ThoughtWorks, Inc.
+#
+# 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.
+#
+
+# default to INFO logging on stdout
+log4j.rootLogger=INFO, stdout
+
+# write logs to stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.conversionPattern=%d{ISO8601} [%-9t] %-5p %-16c{4}:%L %x- %m%n
diff --git a/docs/cluster_profile_configuration.md b/docs/cluster_profile_configuration.md
new file mode 100644
index 0000000..a7b6a59
--- /dev/null
+++ b/docs/cluster_profile_configuration.md
@@ -0,0 +1,201 @@
+# Configure cluster profile
+
+* Navigate to **_Admin > Elastic Profiles_** in the main menu.
+* Click the **_Add Cluster Profile_** button to create a new cluster profile.
+* Select `GoCD Elastic Agent Plugin for Amazon ECS` value for plugin Id.
+
+
+
+
+
+**_Note:_** *Configuration marked with (\*) are mandatory*
+
+## Cluster configuration
+
+
+
+1. **Cluster Id\*:** UUID of the newly defined cluster profile.
+
+2. **GoCD Server URL\*:** This is used by container to register with GoCD server. Server hostname must resolve in your container. Don't use `localhost`.
+
+3. **Container auto register time-out\*:** If an agent running on a container created by this plugin does not register with this server within the specified timeout period (specified in minutes), the plugin will assume that the container failed to startup and will be terminated.
+
+## Advance Container Configuration
+
+
+
+3. **Environment Variables:** These variables will be passed onto the container when it is started up. Read more about [ENV](https://docs.docker.com/engine/reference/builder/#env).
+
+ For example
+
+ ```text
+ TZ=PST
+ JAVA_HOME=/opt/java
+ ```
+
+4. **Container data volume size:** Maximum volume size in GB that container can use to store container data. Defaults to `10G`.
+
+## AWS Credentials
+
+Optionally, specify `Access Key` and `Secret Access Key` of AWS account. These are used by plugin to make API calls. Specified API keys must have appropriate privileges to access aws resources. Please refer [pre-requisites](installation.md#prerequisites) for more information.
+
+
+
+If not specified, plugin will try to detect it in following order:
+
+1. **Environment Variables -** `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` (RECOMMENDED since they are recognized by all the AWS SDKs and CLI except for .NET), or `AWS_ACCESS_KEY` and `AWS_SECRET_KEY` (only recognized by Java SDK)
+
+2. **Java System Properties -** `aws.accessKeyId` and `aws.secretKey`
+
+3. **Instance profile -** Instance profile credentials delivered through the Amazon EC2 metadata service
+
+Read more about API keys [here](https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
+
+## EC2 instance settings
+
+These settings are applied to all ec2 instances launched by plugin irrespective of platform.
+
+
+
+1. **AWS keypair name:** The name of the key pair that you may use to SSH or RDP into the EC2 instance. User can override this from elastic profile. Read more about [Key Pairs](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)
+
+2. **Subnet id(s):** Enter comma separated subnet ids. If multiple subnet ids are specified, the subnet having the least number of EC2 instances will be used to spin up a new EC2 instance. If left unspecified or the specified subnet ids are not available at the time of launching the EC2 instance, AWS will choose a default subnet from your default VPC for you. User can override this from elastic profile. Read more about [VPCs & Subnets](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Subnets.html)
+
+3. **Security Group Id(s):** Enter comma separated security group ids. EC2 instances will be assigned the security groups(s) specified here. User can override this from elastic profile. Read more about [SecurityGroup](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html)
+
+4. **IAM profile for EC2 instance\*:** The name of the IAM profile that will allow the ECS agent to make API calls to AWS on your behalf. Please refer [pre-requisites](../prerequisites/) for the bare minimum privileges your profile must have to allow plugin to make API calls. User can override this from elastic profile.
+
+## EC2 instance settings for Linux
+
+This is to configure linux specific defaults for EC2 instance. It will be used to launch new EC2 instance. However, user can override few of the defaults from an elastic profile.
+
+
+
+1. **AMI ID:** The AMI ID that will be used when an instance is spun up. The ECS agent will run on this ECS optimized EC2 instance. We recommend using an [Amazon ECS-Optimized AMI](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html). ECS optimized Linux AMIs are available [here](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html). User can override this from elastic profile.
+
+2. **Instance type:** This instance type will be used to spin up EC2 instances that will run docker containers with this profile. Read more about [Instance Type](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html)
+
+3. **Operating system volume type:** Allows to override default operating system volume. This is used to store operating system and container volumes. It will get deleted on instance termination. Defaults to `8G`. User may have to increase size of the volume if containers are generating too much persistent data. See [docker volumes](https://docs.docker.com/storage/volumes/) for more information.
+
+4. **Docker Volume type:** Allows to override default storage with specified type and size. This is used to store docker images and metadata. It will get deleted on ec2 instance termination. Defaults to `22G`. User may have to increase size of th`e volume if docker images size is too big to fit in 22G of default volume. See [docker storage](https://docs.docker.com/storage/storagedriver/) for more information.
+
+5. **Instance creation timeout:** If an EC2 instance created by this plugin does not register with the container service within this timeout period, the plugin will assume that the instance has failed to startup and will be terminated. Defaults to `5 minutes`.
+
+6. **Minimum instance required in cluster:** Minimum linux instances you'd like to have running at any point of time. Defaults to `0`.
+
+7. **Maximum instances allowed:** Restricts maximum number of linux instances in the cluster. Plugin will not launch new linux instance if the cluster is already running specified number of instances. Defaults to `5`.
+
+8. **Instance stop policy\*:** Plugin will stop ec2 instances in the ECS cluster based on specified stop policy.
+ - **Stop Idle Instance:** Plugin stops the instance which is idle for more than the specified idle timeout. Defaults to `10 minutes`.
+ - **Stop Oldest Instance:** Plugin stops the oldest instance in the group. This option is useful when you're upgrading the instances in the cluster to a new EC2 instance type, so you can gradually replace instances of the old type with instances of the new type.
+
+9. **Terminate stopped instance after\*:** The plugin terminates the instance which is in `stopped` state for more than specified period. Defaults to `5 minutes`.
+
+10. **Spot Instance Configuration**
+ - **Maximum Spot Instances Allowed in Cluster:** Restricts the maximum number of linux Spot Instances allowed in the cluster. Plugin will not launch new Spot Instance if the cluster is already running specified number of instances or has Spot Instance requests pending. Defaults to 10.
+ - **Terminate Idle Spot Instances after(in minutes):** The plugin terminates a Spot Instance which is idle for more than specified period. Defaults to `30 minutes`.
+
+11. **Userdata script:** This allows user to execute command on startup of ec2 instance. Read more about [userdata script](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html).
+
+ For example
+
+ ```bash
+ yum update -y
+ yum install -y subversion git
+ ```
+
+## EC2 instance settings for Windows
+
+This is to configure windows specific defaults for EC2 instance. It will be used to launch new EC2 instance. However, user can override few of the defaults from an elastic profile.
+
+
+
+1. **AMI ID:** The AMI ID that will be used when an instance is spun up. The ECS agent will run on this ECS optimized EC2 instance. We recommend using an [Amazon ECS-Optimized AMI](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html). ECS optimized Windows AMIs are available [here](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_Windows_getting_started.html#launch_windows_container_instance). User can override this from elastic profile.
+
+2. **Instance type:** This instance type will be used to spin up EC2 instances that will run docker containers with this profile. Read more about [Instance Type](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html)
+
+3. **Operating system volume type:** Allows to override default operating system volume. This is used to store all persistent data including docker. It will get deleted on instance termination. Defaults to `50G`.
+
+5. **Instance creation timeout:** If an EC2 instance created by this plugin does not register with the container service within this timeout period, the plugin will assume that the instance has failed to startup and will be terminated. Defaults to `15 minutes`.
+
+6. **Minimum instance required in cluster:** Minimum linux instances you'd like to have running at any point of time. Defaults to `0`.
+
+7. **Maximum instances allowed:** Restricts maximum number of linux instances in the cluster. Plugin will not launch new linux instance if the cluster is already running specified number of instances. Defaults to `5`.
+
+8. **Instance stop policy\*:** Plugin will stop ec2 instances in the ECS cluster based on specified stop policy.
+ - **Stop Idle Instance:** Plugin stops the instance which is idle for more than the specified idle timeout. Defaults to `10 minutes`.
+ - **Stop Oldest Instance:** Plugin stops the oldest instance in the group. This option is useful when you're upgrading the instances in the cluster to a new EC2 instance type, so you can gradually replace instances of the old type with instances of the new type.
+
+9. **Terminate stopped instance after\*:** The plugin terminates the instance which is in `stopped` state for more than specified period. Defaults to `5 minutes`.
+
+10. **Spot Instance Configuration**
+ - **Maximum Spot Instances Allowed in Cluster:** Restricts the maximum number of linux Spot Instances allowed in the cluster. Plugin will not launch new Spot Instance if the cluster is already running specified number of instances or has Spot Instance requests pending. Defaults to 10.
+ - **Terminate Idle Spot Instances after(in minutes):** The plugin terminates a Spot Instance which is idle for more than specified period. Defaults to `30 minutes`.
+
+11. **Userdata script:** This allows user to execute powershell commands on startup of ec2 instance. Do not use `` or `