use of com.scaleset.cfbuilder.ec2.metadata.CFNCommand in project TOSCAna by StuPro-TOSCAna.
the class OperationHandler method handleArtifact.
/**
* Adds all artifacts to file uploads and to the EC2 Instance in the CloudFormation template.
* Also adds them as commands with input variables as environment variables to the given config.
*
* @param operation to be handled
* @param serverName name of the Compute/EC2 where the artifacts must be stored and executed
* @param config name of the config (Create/Start/Configure)
*/
private void handleArtifact(Operation operation, String serverName, String config) {
// Add artifact
if (operation.getArtifact().isPresent()) {
String artifact = operation.getArtifact().get().getFilePath();
Set<OperationVariable> inputs = operation.getInputs();
CFNCommand cfnCommand = handleOperationCommand(artifact, inputs);
markFile(artifact);
CFNFile cfnFile = handleOperationFile(artifact, MODE_500, serverName);
// Add file to config and execution command
cfnModule.getCFNInit(serverName).getOrAddConfig(CONFIG_SETS, config).putFile(cfnFile).putCommand(cfnCommand);
}
}
use of com.scaleset.cfbuilder.ec2.metadata.CFNCommand in project TOSCAna by StuPro-TOSCAna.
the class OperationHandler method handleOperationCommand.
/**
* Takes an artifact path and input variables and returns the corresponding CloudFormation command with input variables.
*
* @param artifact path to the artifact
* @param inputs set with all input variables
* @return CFNCommand corresponding to the given artifact
*/
private CFNCommand handleOperationCommand(String artifact, Set<OperationVariable> inputs) {
String parent = new File(artifact).getParent();
if (parent == null) {
parent = "";
}
CFNCommand cfnCommand = new CFNCommand(artifact, // file is the full path, so need for "./"
ABSOLUTE_FILE_PATH + artifact).setCwd(ABSOLUTE_FILE_PATH + parent);
// add inputs to environment
for (OperationVariable input : inputs) {
String value = input.getValue().orElseThrow(() -> new IllegalArgumentException("Input value of " + input.getKey() + " expected to not be " + "null"));
if (cfnModule.checkFn(value)) {
cfnCommand.addEnv(input.getKey(), cfnModule.getFn(value));
} else {
cfnCommand.addEnv(input.getKey(), value);
}
}
return cfnCommand;
}
use of com.scaleset.cfbuilder.ec2.metadata.CFNCommand in project TOSCAna by StuPro-TOSCAna.
the class TransformModelNodeVisitor method visit.
@Override
public void visit(Apache node) {
try {
Compute compute = getCompute(node);
String computeName = toAlphanumerical(compute.getEntityName());
// instead of lifecycle create we add the package apache2 to the configset
cfnModule.getCFNInit(computeName).getOrAddConfig(CONFIG_SETS, CONFIG_CREATE).putPackage(// TODO apt only if linux
new CFNPackage("apt").addPackage("apache2"));
// handle configure
operationHandler.handleConfigure(node, computeName);
// handle start
operationHandler.handleStart(node, computeName);
// Source environment variables in /etc/apache/envvars from /etc/environment and restart apache2 directly
// afterwards
cfnModule.getCFNInit(computeName).getOrAddConfig(CONFIG_SETS, CONFIG_CONFIGURE).putCommand(new CFNCommand("Add Apache environment variables", APACHE_ENV_IMPORT));
// we add restart apache2 command to the configscript if start or configure existed
if (node.getStandardLifecycle().getConfigure().isPresent() || node.getStandardLifecycle().getStart().isPresent()) {
cfnModule.getCFNInit(computeName).getOrAddConfig(CONFIG_SETS, CONFIG_START).putCommand(new CFNCommand("restart apache2", APACHE_RESTART_COMMAND));
}
} catch (Exception e) {
logger.error("Error while creating Apache");
throw new TransformationFailureException("Failed at Apache node " + node.getEntityName(), e);
}
}
use of com.scaleset.cfbuilder.ec2.metadata.CFNCommand in project TOSCAna by StuPro-TOSCAna.
the class EnvironmentHandler method addSetEnvScriptsToInstances.
/**
* Adds the setEnv scripts to their respective instances and adds commands to execute them.
*/
private void addSetEnvScriptsToInstances() {
logger.debug("Adding setEnv scripts to Instances.");
for (Map.Entry<String, Map<String, String>> instanceEnvironment : environmentMap.entrySet()) {
String nodeName = instanceEnvironment.getKey();
String filePath = SET_ENV + instanceEnvironment.getKey() + ".sh";
String cfnSource = getFileURL(cfnModule.getBucketName(), filePath);
CFNFile cfnFile = new CFNFile(ABSOLUTE_FILE_PATH + filePath).setSource(cfnSource).setMode(// TODO Check what mode is needed (read? + execute?)
MODE_500).setOwner(// TODO Check what Owner is needed
OWNER_GROUP_ROOT).setGroup(OWNER_GROUP_ROOT);
CFNCommand cfnCommand = new CFNCommand(filePath, // file is the full path, so need for "./"
ABSOLUTE_FILE_PATH + filePath).setCwd(ABSOLUTE_FILE_PATH);
// Adds values of the environment variables to the environment of the setEnv scripts
for (Map.Entry<String, String> environmentVariable : instanceEnvironment.getValue().entrySet()) {
String value = environmentVariable.getValue();
if (cfnModule.checkFn(value)) {
cfnCommand.addEnv(environmentVariable.getKey(), cfnModule.getFn(value));
} else {
cfnCommand.addEnv(environmentVariable.getKey(), value);
}
}
cfnModule.getCFNInit(nodeName).getOrAddConfig(CONFIG_SETS, CONFIG_CONFIGURE).putFile(cfnFile).putCommand(cfnCommand);
}
}
use of com.scaleset.cfbuilder.ec2.metadata.CFNCommand in project TOSCAna by StuPro-TOSCAna.
the class OperationHandler method addCreate.
/**
* Manually adds a create command with the given input variables and file path to the given EC2 instance.
*
* @param filePath path to the artifact
* @param serverName name of the Instance
*/
public void addCreate(String filePath, String serverName) {
markUtilFile(filePath);
CFNFile cfnFile = handleOperationFile(filePath, MODE_500, serverName);
CFNCommand cfnCommand = handleOperationCommand(filePath, new HashSet<>());
// Add file to config and execution command
cfnModule.getCFNInit(serverName).getOrAddConfig(CONFIG_SETS, CONFIG_CREATE).putFile(cfnFile).putCommand(cfnCommand);
}
Aggregations