use of com.scaleset.cfbuilder.ec2.metadata.CFNInit in project TOSCAna by StuPro-TOSCAna.
the class TransformModelNodeVisitor method visit.
@Override
public void visit(Compute node) {
try {
if (cfnModule.checkComputeToEc2(node)) {
logger.debug("Compute '{}' will be transformed to EC2", node.getEntityName());
String nodeName = toAlphanumerical(node.getEntityName());
// default security group the EC2 Instance
SecurityGroup webServerSecurityGroup = cfnModule.resource(SecurityGroup.class, nodeName + SECURITY_GROUP).groupDescription("Enables ports for " + nodeName + ".");
// open endpoint port
node.getEndpoint().getPort().ifPresent(port -> webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, port.port));
// check what image id should be taken
CapabilityMapper capabilityMapper = createCapabilityMapper();
OsCapability computeOs = node.getOs();
String imageId = capabilityMapper.mapOsCapabilityToImageId(computeOs);
ComputeCapability computeCompute = node.getHost();
String instanceType = capabilityMapper.mapComputeCapabilityToInstanceType(computeCompute, CapabilityMapper.EC2_DISTINCTION);
// create CFN init and store it
CFNInit init = new CFNInit(CONFIG_SETS);
cfnModule.putCFNInit(nodeName, init);
cfnModule.resource(Instance.class, nodeName).securityGroupIds(webServerSecurityGroup).imageId(imageId).instanceType(instanceType);
capabilityMapper.mapDiskSize(computeCompute, cfnModule, nodeName);
// Add Reference to keyName if KeyPair needed and open Port 22 (Allows SSH access)
if (cfnModule.hasKeyPair()) {
Instance instance = (Instance) cfnModule.getResource(nodeName);
instance.keyName(cfnModule.getKeyNameVar());
webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, 22);
}
} else {
logger.debug("Compute '{}' will not be transformed to EC2", node.getEntityName());
}
} catch (SdkClientException se) {
logger.error("SDKClient failed, no valid credentials or no internet connection");
throw new TransformationFailureException("Failed", se);
} catch (Exception e) {
logger.error("Error while creating EC2Instance resource.");
throw new TransformationFailureException("Failed at Compute node " + node.getEntityName(), e);
}
}
use of com.scaleset.cfbuilder.ec2.metadata.CFNInit in project TOSCAna by StuPro-TOSCAna.
the class CloudFormationModule method build.
/**
* Build the template
* 1. Add CFNInit to corresponding instance resource
* 2. Check if EC2 instances need access to S3. If yes, then
* 2a. Add necessary IAM resources to the module
* 2b. Add <tt>Authentication<tt> and <tt>IamInstanceProfile<tt> to corresponding instance resource
*/
@Override
public void build() {
for (Map.Entry<String, CFNInit> pair : cfnInitMap.entrySet()) {
Resource res = this.getResource(pair.getKey());
if (res instanceof Instance) {
Instance instance = (Instance) res;
if (!pair.getValue().getConfigs().isEmpty()) {
instance.addCFNInit(pair.getValue()).userData(new UserData(getUserDataFn(pair.getKey(), CONFIG_SETS, this)));
}
}
}
if (!fileUploadList.isEmpty()) {
Role instanceRole = getS3InstanceRole(this);
getS3Policy(this).roles(instanceRole);
getS3InstanceProfile(this).roles(instanceRole);
Authentication s3authentication = getS3Authentication(bucketName);
for (String instanceName : authenticationSet) {
Resource res = this.getResource(instanceName);
if (res instanceof Instance) {
Instance instance = (Instance) res;
instance.authentication(s3authentication).iamInstanceProfile(ref(INSTANCE_PROFILE));
}
}
}
if (this.hasKeyPair()) {
strParam(KEYNAME).type(KEYNAME_TYPE).description(KEYNAME_DESCRIPTION).constraintDescription(KEYNAME_CONSTRAINT_DESCRIPTION);
}
}
Aggregations