Automate creation of the clusters' keys

The automatic creation of cluster keys is a mandatory
requirement to have the cluster created and started
correctly.

Amend the general README.md to remove the step
that is now automated.

Change-Id: I64920a5e604532f215fc4c3f18be6ca7b408efe4
diff --git a/Makefile.common b/Makefile.common
new file mode 100644
index 0000000..4f31260
--- /dev/null
+++ b/Makefile.common
@@ -0,0 +1,6 @@
+include $(dir $(realpath $(lastword $(MAKEFILE_LIST))))common.env
+
+
+cluster-keys:
+	aws ec2 describe-key-pairs --region $(AWS_REGION) --key-names $(CLUSTER_KEYS) > /dev/null 2>&1 || \
+		aws ec2 create-key-pair --region $(AWS_REGION) --key-name $(CLUSTER_KEYS) --query 'KeyMaterial' --output text > $(CLUSTER_KEYS).pem
\ No newline at end of file
diff --git a/makefile.env b/common.env
similarity index 91%
rename from makefile.env
rename to common.env
index 9ea2b73..037d7b8 100644
--- a/makefile.env
+++ b/common.env
@@ -1,6 +1,7 @@
 # AWS general settings
 AWS_PREFIX=gerrit
 AWS_REGION=us-east-1
+CLUSTER_KEYS=$(AWS_PREFIX)-cluster-keys
 
 # Gerrit version
 GERRIT_VERSION=3.1
diff --git a/dual-master/Makefile b/dual-master/Makefile
index 1182aa8..0d168e7 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -1,5 +1,5 @@
 include setup.env
-include ../makefile.env
+include ../Makefile.common
 
 CLUSTER_TEMPLATE:=cf-cluster.yml
 SERVICE_MASTER_TEMPLATE:=cf-service-master.yml
@@ -7,7 +7,7 @@
 AWS_FC_COMMAND=export AWS_PAGER=;aws cloudformation
 
 .PHONY: create-all delete-all \
-				cluster service-master-1 dns-routing \
+				cluster cluster-keys service-master-1 dns-routing \
 				wait-for-cluster-creation wait-for-service-master-1-creation wait-for-service-master-2-creation wait-for-dns-routing-creation \
 				wait-for-cluster-deletion wait-for-service-master-1-deletion wait-for-service-master-2-deletion wait-for-dns-routing-deletion \
 				gerrit-build gerrit-publish
@@ -17,7 +17,7 @@
 						wait-for-service-master-1-creation wait-for-service-master-2-creation \
 						dns-routing wait-for-dns-routing-creation
 
-cluster:
+cluster: cluster-keys
 	$(AWS_FC_COMMAND) create-stack \
 		--stack-name $(CLUSTER_STACK_NAME) \
 		--capabilities CAPABILITY_IAM  \
diff --git a/dual-master/README.md b/dual-master/README.md
index e8fc8d7..8ac1bdc 100644
--- a/dual-master/README.md
+++ b/dual-master/README.md
@@ -144,23 +144,17 @@
 
 ### Getting Started
 
-* Create a key pair to access the EC2 instances in the cluster:
-
-```
-aws ec2 create-key-pair --key-name gerrit-cluster-keys \
-  --query 'KeyMaterial' --output text > gerrit-cluster.pem
-```
-
-*NOTE: the EC2 key pair are useful when you need to connect to the EC2 instances
-for troubleshooting purposes. Store them in a `pem` file to use when ssh-ing into your
-instances as follow: `ssh -i yourKeyPairs.pem <ec2_instance_ip>`*
-
 * Create the cluster, services and DNS routing stacks:
 
 ```
 make create-all
 ```
 
+*NOTE: the creation of the cluster needs an EC2 key pair are useful when you need to connect
+to the EC2 instances for troubleshooting purposes. The key pair is automatically generated
+and store them in a `pem` file on the current directory.
+To use when ssh-ing into your instances as follow: `ssh -i cluster-keys.pem ec2-user@<ec2_instance_ip>`*
+
 ### Cleaning up
 
 ```
diff --git a/dual-master/setup.env.template b/dual-master/setup.env.template
index e834803..d6cbec8 100644
--- a/dual-master/setup.env.template
+++ b/dual-master/setup.env.template
@@ -1,5 +1,4 @@
 CLUSTER_STACK_NAME:=$(AWS_PREFIX)-cluster
-CLUSTER_KEYS:=$(AWS_PREFIX)-cluster-keys
 CLUSTER_DESIRED_CAPACITY:=1
 SERVICE_MASTER1_STACK_NAME:=$(AWS_PREFIX)-service-master-1
 SERVICE_MASTER2_STACK_NAME:=$(AWS_PREFIX)-service-master-2
diff --git a/gerrit/Makefile b/gerrit/Makefile
index e461648..bc39a08 100644
--- a/gerrit/Makefile
+++ b/gerrit/Makefile
@@ -1,5 +1,5 @@
 include ../$(RECIPE)/setup.env
-include ../makefile.env
+include ../Makefile.common
 
 docker-registry-login:
 	aws ecr get-login-password --region $(AWS_REGION) \
diff --git a/master-slave/Makefile b/master-slave/Makefile
index ae35bb8..a84b575 100644
--- a/master-slave/Makefile
+++ b/master-slave/Makefile
@@ -1,5 +1,5 @@
 include setup.env
-include ../makefile.env
+include ../Makefile.common
 
 CLUSTER_TEMPLATE:=cf-cluster.yml
 SERVICE_MASTER_TEMPLATE:=cf-service-master.yml
@@ -8,7 +8,7 @@
 AWS_FC_COMMAND=export AWS_PAGER=;aws cloudformation
 
 .PHONY: create-all delete-all \
-				cluster service-master dns-routing \
+				cluster cluster-keys service-master dns-routing \
 				wait-for-cluster-creation wait-for-service-master-creation wait-for-dns-routing-creation \
 				wait-for-cluster-deletion wait-for-service-master-deletion wait-for-dns-routing-deletion \
 				gerrit-build gerrit-publish
@@ -18,7 +18,7 @@
 						wait-for-service-master-creation wait-for-service-slave-creation \
 						dns-routing wait-for-dns-routing-creation
 
-cluster:
+cluster: cluster-keys
 	$(AWS_FC_COMMAND) create-stack \
 		--stack-name $(CLUSTER_STACK_NAME) \
 		--capabilities CAPABILITY_IAM  \
diff --git a/master-slave/README.md b/master-slave/README.md
index 6b5baa5..19c704e 100644
--- a/master-slave/README.md
+++ b/master-slave/README.md
@@ -169,17 +169,6 @@
 
 ### Getting Started
 
-* Create a key pair to access the EC2 instances in the cluster:
-
-```
-aws ec2 create-key-pair --key-name gerrit-cluster-keys \
-  --query 'KeyMaterial' --output text > gerrit-cluster.pem
-```
-
-*NOTE: the EC2 key pair are useful when you need to connect to the EC2 instances
-for troubleshooting purposes. Store them in a `pem` file to use when ssh-ing into your
-instances as follow: `ssh -i yourKeyPairs.pem <ec2_instance_ip>`*
-
 * Create the cluster, services and DNS routing stacks:
 
 ```
@@ -190,6 +179,11 @@
 and `All-Projects` to happen.
 You can now check in the slave logs to see when the slave is up and running.
 
+*NOTE: the creation of the cluster needs an EC2 key pair are useful when you need to connect
+to the EC2 instances for troubleshooting purposes. The key pair is automatically generated
+and store them in a `pem` file on the current directory.
+To use when ssh-ing into your instances as follow: `ssh -i cluster-keys.pem ec2-user@<ec2_instance_ip>`*
+
 ### Cleaning up
 
 ```
diff --git a/master-slave/setup.env.template b/master-slave/setup.env.template
index 0462ef8..ca36034 100644
--- a/master-slave/setup.env.template
+++ b/master-slave/setup.env.template
@@ -1,5 +1,4 @@
 CLUSTER_STACK_NAME:=$(AWS_PREFIX)-cluster
-CLUSTER_KEYS:=$(AWS_PREFIX)-cluster-keys
 CLUSTER_DESIRED_CAPACITY:=1
 SERVICE_MASTER_STACK_NAME:=$(AWS_PREFIX)-service-master
 SERVICE_SLAVE_STACK_NAME:=$(AWS_PREFIX)-service-slave
diff --git a/single-master/Makefile b/single-master/Makefile
index e0b8be8..48ad8d6 100644
--- a/single-master/Makefile
+++ b/single-master/Makefile
@@ -1,5 +1,5 @@
 include setup.env
-include ../makefile.env
+include ../Makefile.common
 
 CLUSTER_TEMPLATE:=cf-cluster.yml
 SERVICE_TEMPLATE:=cf-service.yml
@@ -7,7 +7,7 @@
 AWS_FC_COMMAND=export AWS_PAGER=;aws cloudformation
 
 .PHONY: create-all delete-all \
-				cluster service dns-routing \
+				cluster cluster-keys service dns-routing \
 				wait-for-cluster-creation wait-for-service-creation wait-for-dns-routing-creation \
 				wait-for-cluster-deletion wait-for-service-deletion wait-for-dns-routing-deletion \
 				gerrit-build gerrit-publish
@@ -16,7 +16,7 @@
 						service wait-for-service-creation \
 						dns-routing wait-for-dns-routing-creation
 
-cluster:
+cluster: cluster-keys
 	$(AWS_FC_COMMAND) create-stack \
 		--stack-name $(CLUSTER_STACK_NAME) \
 		--capabilities CAPABILITY_IAM  \
diff --git a/single-master/README.md b/single-master/README.md
index 0077248..e87da8b 100644
--- a/single-master/README.md
+++ b/single-master/README.md
@@ -145,23 +145,17 @@
 
 ### Getting Started
 
-* Create a key pair to access the EC2 instances in the cluster:
-
-```
-aws ec2 create-key-pair --key-name gerrit-cluster-keys \
-  --query 'KeyMaterial' --output text > gerrit-cluster.pem
-```
-
-*NOTE: the EC2 key pair are useful when you need to connect to the EC2 instances
-for troubleshooting purposes. Store them in a `pem` file to use when ssh-ing into your
-instances as follow: `ssh -i yourKeyPairs.pem <ec2_instance_ip>`*
-
 * Create the cluster, service and DNS routing stacks:
 
 ```
 make create-all
 ```
 
+*NOTE: the creation of the cluster needs an EC2 key pair are useful when you need to connect
+to the EC2 instances for troubleshooting purposes. The key pair is automatically generated
+and store them in a `pem` file on the current directory.
+To use when ssh-ing into your instances as follow: `ssh -i cluster-keys.pem ec2-user@<ec2_instance_ip>`*
+
 ### Cleaning up
 
 ```
diff --git a/single-master/setup.env.template b/single-master/setup.env.template
index b2cdf54..dd4ba69 100644
--- a/single-master/setup.env.template
+++ b/single-master/setup.env.template
@@ -1,6 +1,5 @@
 CLUSTER_STACK_NAME:=$(AWS_PREFIX)-cluster
 SERVICE_STACK_NAME:=$(AWS_PREFIX)-service
-CLUSTER_KEYS:=$(AWS_PREFIX)-cluster-keys
 DNS_ROUTING_STACK_NAME:=$(AWS_PREFIX)-dns-routing
 HOSTED_ZONE_NAME:=mycompany.com
 SUBDOMAIN:=$(AWS_PREFIX)-master-demo