Skip to content

Commit

Permalink
Add KeyName parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
80degreeswest committed Jun 9, 2020
1 parent ef3f13e commit 8981bce
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public String getMainDashboard(Model model) throws UnknownHostException {
model.addAttribute("createClusterRequest", bfMgrCtrl.getDefaultCreateClusterRequest());
model.addAttribute("subnetList", bfMgrCtrl.getSubnets());
model.addAttribute("securityGroupList", bfMgrCtrl.getSecurityGroups());
model.addAttribute("keyNamesList", bfMgrCtrl.getKeyNames());
model.addAttribute("clusters", bfMgrCtrl.getBuildfarmClusters());
return "dashboard";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public class CreateClusterRequest {
private String workerTag;
private String workerConfig;
private String elbType;
private String keyName;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eightydegreeswest.bfmgr.service;

import com.amazonaws.services.ec2.model.KeyPairInfo;
import com.amazonaws.services.ec2.model.SecurityGroup;
import com.amazonaws.services.ec2.model.Subnet;
import com.eightydegreeswest.bfmgr.model.BuildfarmCluster;
Expand All @@ -15,4 +16,5 @@ public interface BfMgrCtrl {
CreateClusterRequest getDefaultCreateClusterRequest();
List<Subnet> getSubnets();
List<SecurityGroup> getSecurityGroups();
List<KeyPairInfo> getKeyNames();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.amazonaws.services.ec2.model.DescribeSubnetsRequest;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.KeyPairInfo;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.SecurityGroup;
import com.amazonaws.services.ec2.model.Subnet;
Expand Down Expand Up @@ -143,6 +144,7 @@ public CreateClusterRequest getDefaultCreateClusterRequest() {
createClusterRequest.setWorkerRepo(workerRepo);
createClusterRequest.setWorkerTag(buildfarmTag);
createClusterRequest.setElbType("internet-facing");
createClusterRequest.setKeyName("");
return createClusterRequest;
}

Expand All @@ -156,6 +158,15 @@ public List<SecurityGroup> getSecurityGroups() {
return awsEc2Client.describeSecurityGroups().getSecurityGroups();
}

@Override
public List<KeyPairInfo> getKeyNames() {
try {
return awsEc2Client.describeKeyPairs().getKeyPairs();
} catch (Exception e) {
return new ArrayList<>();
}
}

private String getLoadBalancerEndpoint(String clusterName) {
try {
return awsElbClient.describeLoadBalancers(new DescribeLoadBalancersRequest().withNames(clusterName)).getLoadBalancers().get(0).getDNSName();
Expand Down Expand Up @@ -228,6 +239,7 @@ private Collection<Parameter> getCloudFormationParameters(CreateClusterRequest c
parameters.add(getParameter("ClusterName", createClusterRequest.getClusterName()));
parameters.add(getParameter("RequiredTagName", getAssetTag()));
parameters.add(getParameter("ElbType", createClusterRequest.getElbType()));
parameters.add(getParameter("KeyName", createClusterRequest.getKeyName()));
return parameters;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.eightydegreeswest.bfmgr.service.impl;

import com.amazonaws.services.ec2.model.KeyPairInfo;
import com.amazonaws.services.ec2.model.SecurityGroup;
import com.amazonaws.services.ec2.model.Subnet;
import com.eightydegreeswest.bfmgr.model.BuildfarmCluster;
import com.eightydegreeswest.bfmgr.model.CreateClusterRequest;
import com.eightydegreeswest.bfmgr.service.BfMgrCtrl;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -46,4 +48,9 @@ public List<Subnet> getSubnets() {
public List<SecurityGroup> getSecurityGroups() {
throw new UnsupportedOperationException();
}

@Override
public List<KeyPairInfo> getKeyNames() {
return new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eightydegreeswest.bfmgr.service.impl;

import com.amazonaws.services.ec2.model.KeyPairInfo;
import com.amazonaws.services.ec2.model.SecurityGroup;
import com.amazonaws.services.ec2.model.Subnet;
import com.eightydegreeswest.bfmgr.model.BfInstance;
Expand All @@ -12,7 +13,6 @@
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.core.DockerClientBuilder;
import java.io.File;
Expand Down Expand Up @@ -194,6 +194,11 @@ public List<SecurityGroup> getSecurityGroups() {
return new ArrayList<>();
}

@Override
public List<KeyPairInfo> getKeyNames() {
return new ArrayList<>();
}

private void removeContainer(String container) {
try {
dockerClient.removeContainerCmd(container).withForce(true).exec();
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/aws.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@
"Default" : "internet-facing",
"AllowedValues" : ["internet-facing", "internal"],
"Description":"Load balancer type."
},
"KeyName": {
"Type": "AWS::EC2::KeyPair::KeyName",
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance"
}
},
"Conditions": {
"HasKeyName": {"Fn::Not": [{"Fn::Equals": ["", {"Ref": "KeyName"}]}]}
},
"Resources":{
"TemplateServer":{
"Type":"AWS::EC2::LaunchTemplate",
Expand Down Expand Up @@ -126,6 +133,7 @@
}
},
"InstanceInitiatedShutdownBehavior":"terminate",
"KeyName": {"Fn::If": ["HasKeyName", {"Ref": "KeyName"}, {"Ref": "AWS::NoValue"}]},
"IamInstanceProfile" : {
"Name" : "buildfarm-manager"
},
Expand Down Expand Up @@ -235,6 +243,7 @@
}
},
"InstanceInitiatedShutdownBehavior":"terminate",
"KeyName": {"Fn::If": ["HasKeyName", {"Ref": "KeyName"}, {"Ref": "AWS::NoValue"}]},
"IamInstanceProfile" : {
"Name" : "buildfarm-manager"
},
Expand Down
11 changes: 10 additions & 1 deletion src/main/resources/bfmgr-aws.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
"ReleaseNumber": {
"Type": "String",
"Description": "Enter bfmgr release number"
},
"KeyName": {
"Type": "AWS::EC2::KeyPair::KeyName",
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance"
}
},
"Conditions": {
"HasKeyName": {"Fn::Not": [{"Fn::Equals": ["", {"Ref": "KeyName"}]}]}
},
"Resources": {
"BfMgrRole": {
"Type": "AWS::IAM::Role",
Expand Down Expand Up @@ -78,6 +85,7 @@
"Type" : "AWS::IAM::InstanceProfile",
"Properties" : {
"Path" : "/",
"InstanceProfileName" : "buildfarm-manager",
"Roles" : ["buildfarm-manager"]
},
"DependsOn": [
Expand Down Expand Up @@ -134,7 +142,8 @@
]
]
}
}
},
"KeyName": {"Fn::If": ["HasKeyName", {"Ref": "KeyName"}, {"Ref": "AWS::NoValue"}]}
},
"DependsOn": [
"BfMgrRole",
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ <h5 class="modal-title" id="createClusterModalLabel">Create a new Buildfarm Clus
<option value="internal">internal</option>
</select>
</div>
<div class="form-group row">
<label for="keyName" class="col-sm-3 col-form-label">SSH Key Name</label>
<select class="col-sm-8" id="keyName" th:field="*{keyName}">
<option value="">Select SSH Key</option>
<option th:each="keyNameItem : ${keyNamesList}" th:value="${ keyNameItem.getKeyName() }"><p th:text="${ keyNameItem.getKeyName() }" th:remove="tag"></p></option>
</select>
</div>
<div class="form-group row">
<label for="ami" class="col-sm-3 col-form-label">AMI</label>
<input class="col-sm-8" id="ami" type="text" th:field="*{ami}" />
Expand Down

0 comments on commit 8981bce

Please sign in to comment.