use of org.opentosca.planbuilder.core.plugins.context.PropertyVariable in project container by OpenTOSCA.
the class BPELInvokerPluginHandler method handleArtifactReferenceUpload.
public boolean handleArtifactReferenceUpload(final TArtifactReference ref, final BPELPlanContext templateContext, final PropertyVariable serverIp, final PropertyVariable sshUser, final PropertyVariable sshKey, final TNodeTemplate infraTemplate, final Element elementToAppendTo) throws Exception {
LOG.debug("Handling DA " + ref.getReference());
if (Objects.isNull(serverIp)) {
LOG.error("Unable to upload artifact with server IP equal to null.");
return false;
}
/*
* Construct all needed data (paths, url, scripts)
*/
// TODO /home/ec2-user/ or ~ is a huge assumption
// the path to the file on the ubuntu vm being uploaded
final String ubuntuFilePath = "~/" + templateContext.getCSARFileName() + "/" + ref.getReference();
final String ubuntuFilePathVarName = "ubuntuFilePathVar" + templateContext.getIdForNames();
final Variable ubuntuFilePathVar = templateContext.createGlobalStringVariable(ubuntuFilePathVarName, ubuntuFilePath);
// the folder which has to be created on the ubuntu vm
final String ubuntuFolderPathScript = "sleep 1 && mkdir -p " + fileReferenceToFolder(ubuntuFilePath);
final String containerAPIAbsoluteURIXPathQuery = this.bpelFrags.createXPathQueryForURLRemoteFilePath(ref.getReference());
final String containerAPIAbsoluteURIVarName = "containerApiFileURL" + templateContext.getIdForNames();
/*
* create a string variable with a complete URL to the file we want to upload
*/
final Variable containerAPIAbsoluteURIVar = templateContext.createGlobalStringVariable(containerAPIAbsoluteURIVarName, "");
try {
Node assignNode = loadAssignXpathQueryToStringVarFragmentAsNode("assign" + templateContext.getIdForNames(), containerAPIAbsoluteURIXPathQuery, containerAPIAbsoluteURIVar.getVariableName());
assignNode = templateContext.importNode(assignNode);
elementToAppendTo.appendChild(assignNode);
} catch (final IOException e) {
LOG.error("Couldn't read internal file", e);
return false;
} catch (final SAXException e) {
LOG.error("Couldn't parse internal xml file");
return false;
}
// create the folder the file must be uploaded into and upload the file afterwards
final String mkdirScriptVarName = "mkdirScript" + templateContext.getIdForNames();
final Variable mkdirScriptVar = templateContext.createGlobalStringVariable(mkdirScriptVarName, ubuntuFolderPathScript);
final Map<String, Variable> runScriptRequestInputParams = new HashMap<>();
runScriptRequestInputParams.put("Script", mkdirScriptVar);
final List<String> runScriptInputParams = getRunScriptParams(infraTemplate, templateContext.getCsar());
final Map<String, Variable> transferFileRequestInputParams = new HashMap<>();
transferFileRequestInputParams.put("TargetAbsolutePath", ubuntuFilePathVar);
transferFileRequestInputParams.put("SourceURLorLocalPath", containerAPIAbsoluteURIVar);
final List<String> transferFileInputParams = getTransferFileParams(infraTemplate, templateContext.getCsar());
switch(serverIp.getPropertyName()) {
case Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_CONTAINERIP:
// create the folder
if (runScriptInputParams.contains(serverIp.getPropertyName())) {
runScriptRequestInputParams.put(serverIp.getPropertyName(), serverIp);
}
this.handle(templateContext, infraTemplate.getId(), true, "runScript", "ContainerManagementInterface", runScriptRequestInputParams, new HashMap<String, Variable>(), elementToAppendTo);
// transfer the file
if (transferFileInputParams.contains(serverIp.getPropertyName())) {
transferFileRequestInputParams.put(serverIp.getPropertyName(), serverIp);
}
this.handle(templateContext, infraTemplate.getId(), true, Interfaces.OPENTOSCA_DECLARATIVE_INTERFACE_OPERATINGSYSTEM_TRANSFERFILE, "ContainerManagementInterface", transferFileRequestInputParams, new HashMap<>(), elementToAppendTo);
break;
case Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_VMIP:
case Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_RASPBIANIP:
// create the folder
if (runScriptInputParams.contains(Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_VMIP)) {
runScriptRequestInputParams.put(Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_VMIP, serverIp);
}
if (sshUser != null && runScriptInputParams.contains("VMUserName")) {
runScriptRequestInputParams.put("VMUserName", sshUser);
}
if (sshKey != null && runScriptInputParams.contains("VMPrivateKey")) {
runScriptRequestInputParams.put("VMPrivateKey", sshKey);
}
this.handle(templateContext, infraTemplate.getId(), true, "runScript", Interfaces.OPENTOSCA_DECLARATIVE_INTERFACE_OPERATINGSYSTEM, runScriptRequestInputParams, new HashMap<>(), elementToAppendTo);
// transfer the file
if (transferFileInputParams.contains(Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_VMIP)) {
transferFileRequestInputParams.put(Properties.OPENTOSCA_DECLARATIVE_PROPERTYNAME_VMIP, serverIp);
}
if (sshUser != null && transferFileInputParams.contains("VMUserName")) {
transferFileRequestInputParams.put("VMUserName", sshUser);
}
if (sshKey != null && transferFileInputParams.contains("VMPrivateKey")) {
transferFileRequestInputParams.put("VMPrivateKey", sshKey);
}
this.handle(templateContext, infraTemplate.getId(), true, Interfaces.OPENTOSCA_DECLARATIVE_INTERFACE_OPERATINGSYSTEM_TRANSFERFILE, Interfaces.OPENTOSCA_DECLARATIVE_INTERFACE_OPERATINGSYSTEM, transferFileRequestInputParams, new HashMap<>(), elementToAppendTo);
break;
default:
return false;
}
return true;
}
use of org.opentosca.planbuilder.core.plugins.context.PropertyVariable in project container by OpenTOSCA.
the class BPELDockerContainerTypePluginHandler method handleCreate.
@Override
public boolean handleCreate(final BPELPlanContext templateContext) {
if (templateContext.getNodeTemplate() == null) {
BPELDockerContainerTypePluginHandler.LOG.warn("Appending logic to relationshipTemplate plan is not possible by this plugin");
return false;
}
final TNodeTemplate nodeTemplate = templateContext.getNodeTemplate();
// fetch port binding variables (ContainerPort, Port)
final PropertyVariable containerPortVar = templateContext.getPropertyVariable(nodeTemplate, "ContainerPort");
final PropertyVariable portVar = templateContext.getPropertyVariable(nodeTemplate, "Port");
if (containerPortVar == null | portVar == null) {
BPELDockerContainerTypePluginHandler.LOG.error("Couldn't fetch Property variables ContainerPort or Port");
return false;
}
final Variable portMappingVar = templateContext.createGlobalStringVariable("dockerContainerPortMappings" + System.currentTimeMillis(), "");
try {
Node assignContainerPortsNode = this.planBuilderFragments.createAssignXpathQueryToStringVarFragmentAsNode("assignPortMapping", "concat($" + containerPortVar.getVariableName() + ",',',$" + portVar.getVariableName() + ")", portMappingVar.getVariableName());
assignContainerPortsNode = templateContext.importNode(assignContainerPortsNode);
templateContext.getProvisioningPhaseElement().appendChild(assignContainerPortsNode);
} catch (final IOException | SAXException e) {
LOG.error("Error while assigning container ports.", e);
}
// fetch (optional) SSHPort variable
final Variable sshPortVar = templateContext.getPropertyVariable(nodeTemplate, "SSHPort");
// fetch (optional) ContainerIP variable
final Variable containerIpVar = templateContext.getPropertyVariable(nodeTemplate, "ContainerIP");
// fetch (optional) ContainerID variable
final Variable containerIdVar = templateContext.getPropertyVariable(nodeTemplate, "ContainerID");
// fetch (optional) PrivilegedMode variable
final PropertyVariable privilegedModeVar = templateContext.getPropertyVariable(nodeTemplate, "PrivilegedMode");
// fetch DockerEngine
final TNodeTemplate dockerEngineNode = DockerContainerTypePlugin.getDockerEngineNode(nodeTemplate, templateContext.getCsar());
if (dockerEngineNode == null) {
BPELDockerContainerTypePluginHandler.LOG.error("Couldn't fetch DockerEngineNode to install given DockerContainer NodeTemplate");
return false;
}
// fetch the DockerIp
final Variable dockerEngineUrlVar = templateContext.getPropertyVariable(dockerEngineNode, "DockerEngineURL");
// determine whether we work with an ImageId or a zipped DockerContainer
final PropertyVariable containerImageVar = templateContext.getPropertyVariable(nodeTemplate, "ImageID");
/* volume data handling */
// <ContainerMountPath>/etc/openmtc/certs</ContainerMountPath>
// <HostMountFiles>/home/ubuntu/ca-smartorchestra.crt</HostMountFiles>
final PropertyVariable containerMountPath = templateContext.getPropertyVariable(nodeTemplate, "ContainerMountPath");
Variable remoteVolumeDataVariable = null;
PropertyVariable hostVolumeDataVariable = null;
Variable vmIpVariable = null;
Variable vmPrivateKeyVariable = null;
if (containerMountPath != null && !PluginUtils.isVariableValueEmpty(containerMountPath)) {
final List<TDeploymentArtifact> volumeDas = fetchVolumeDeploymentArtifacts(nodeTemplate, templateContext.getCsar());
if (!volumeDas.isEmpty()) {
remoteVolumeDataVariable = createRemoteVolumeDataInputVariable(volumeDas, templateContext);
}
hostVolumeDataVariable = templateContext.getPropertyVariable(nodeTemplate, "HostMountFiles");
if (hostVolumeDataVariable != null && !PluginUtils.isVariableValueEmpty(hostVolumeDataVariable)) {
final TNodeTemplate infraNode = findInfrastructureTemplate(templateContext, dockerEngineNode);
vmIpVariable = findVMIP(templateContext, infraNode);
vmPrivateKeyVariable = findPrivateKey(templateContext, infraNode);
}
}
if ((containerImageVar == null || PluginUtils.isVariableValueEmpty(containerImageVar)) && (nodeTemplate.getDeploymentArtifacts() != null && !nodeTemplate.getDeploymentArtifacts().isEmpty())) {
// handle with DA -> construct URL to the DockerImage .zip
final TDeploymentArtifact da = fetchFirstDockerContainerDA(nodeTemplate, templateContext.getCsar());
return handleWithDA(templateContext, dockerEngineNode, da, portMappingVar, dockerEngineUrlVar, sshPortVar, containerIpVar, containerIdVar, fetchEnvironmentVariables(templateContext, nodeTemplate), null, null, containerMountPath, remoteVolumeDataVariable, hostVolumeDataVariable, vmIpVariable, vmPrivateKeyVariable, privilegedModeVar);
} else {
// handle with imageId
return handleWithImageId(templateContext, dockerEngineNode, containerImageVar, portMappingVar, dockerEngineUrlVar, sshPortVar, containerIpVar, containerIdVar, fetchEnvironmentVariables(templateContext, nodeTemplate), containerMountPath, remoteVolumeDataVariable, hostVolumeDataVariable, vmIpVariable, vmPrivateKeyVariable, privilegedModeVar);
}
}
use of org.opentosca.planbuilder.core.plugins.context.PropertyVariable in project container by OpenTOSCA.
the class BPELConnectsToPluginHandler method handle.
public boolean handle(final BPELPlanContext templateContext) {
final TRelationshipTemplate relationTemplate = templateContext.getRelationshipTemplate();
Csar csar = templateContext.getCsar();
// fetch topic
final Variable topicName = templateContext.getPropertyVariable(ModelUtils.getTarget(relationTemplate, csar), "Name");
/* fetch ip of mosquitto */
Variable mosquittoVmIp = null;
// find infrastructure nodes of mosquitto
List<TNodeTemplate> infrastructureNodes = new ArrayList<>();
ModelUtils.getInfrastructureNodes(ModelUtils.getTarget(relationTemplate, csar), infrastructureNodes, csar);
ModelUtils.getNodesFromNodeToSink(ModelUtils.getTarget(relationTemplate, csar), infrastructureNodes, csar);
for (final TNodeTemplate infraNode : infrastructureNodes) {
for (final String ipPropName : org.opentosca.container.core.convention.Utils.getSupportedVirtualMachineIPPropertyNames()) {
// fetch mosquitto ip
if (templateContext.getPropertyVariable(infraNode, ipPropName) != null) {
mosquittoVmIp = templateContext.getPropertyVariable(infraNode, ipPropName);
break;
}
}
if (mosquittoVmIp != null) {
break;
}
}
/* fetch user, key, ip and ubuntuTemplateId of client stack */
PropertyVariable clientVmIp = null;
PropertyVariable clientVmUser = null;
PropertyVariable clientVmPass = null;
String ubuntuTemplateId = null;
infrastructureNodes = new ArrayList<>();
ModelUtils.getInfrastructureNodes(ModelUtils.getSource(relationTemplate, csar), infrastructureNodes, templateContext.getCsar());
for (final TNodeTemplate infraNode : infrastructureNodes) {
for (final String ipPropName : org.opentosca.container.core.convention.Utils.getSupportedVirtualMachineIPPropertyNames()) {
if (templateContext.getPropertyVariable(infraNode, ipPropName) != null) {
clientVmIp = templateContext.getPropertyVariable(infraNode, ipPropName);
break;
}
}
for (final String loginNameProp : org.opentosca.container.core.convention.Utils.getSupportedVirtualMachineLoginUserNamePropertyNames()) {
if (templateContext.getPropertyVariable(infraNode, loginNameProp) != null) {
ubuntuTemplateId = infraNode.getId();
clientVmUser = templateContext.getPropertyVariable(infraNode, loginNameProp);
}
}
for (final String loginPwProp : org.opentosca.container.core.convention.Utils.getSupportedVirtualMachineLoginPasswordPropertyNames()) {
if (templateContext.getPropertyVariable(infraNode, loginPwProp) != null) {
ubuntuTemplateId = infraNode.getId();
clientVmPass = templateContext.getPropertyVariable(infraNode, loginPwProp);
}
}
}
/* create skript */
// the script itself
final String bashCommand = "echo \"topicName = hostName\" > $(find ~ -maxdepth 1 -path \"*.csar\")/mosquitto_connections.txt;";
// add it as a var to the plan
final Variable bashCommandVariable = templateContext.createGlobalStringVariable("addMosquittoConnection", bashCommand);
// create bpel query which replaces topicName and hostName with real
// values
final String xpathQuery = "replace(replace($" + bashCommandVariable.getVariableName() + ",'topicName',$" + topicName.getVariableName() + "),'hostName',$" + mosquittoVmIp.getVariableName() + ")";
// create bpel assign with created query
try {
// create assign and append
Node assignNode = loadAssignXpathQueryToStringVarFragmentAsNode("assignValuesToAddConnection" + System.currentTimeMillis(), xpathQuery, bashCommandVariable.getVariableName());
assignNode = templateContext.importNode(assignNode);
templateContext.getProvisioningPhaseElement().appendChild(assignNode);
} catch (final IOException e) {
BPELConnectsToPluginHandler.LOG.error("Couldn't load fragment from file", e);
return false;
} catch (final SAXException e) {
BPELConnectsToPluginHandler.LOG.error("Couldn't parse fragment to DOM", e);
return false;
}
/* add logic to execute script on client machine */
final Map<String, Variable> runScriptRequestInputParams = new HashMap<>();
runScriptRequestInputParams.put("VMIP", clientVmIp);
// these two are requested from the input message if they are not set
if (!PluginUtils.isVariableValueEmpty(clientVmUser)) {
runScriptRequestInputParams.put("VMUserName", clientVmUser);
} else {
runScriptRequestInputParams.put("VMUserName", null);
}
if (!PluginUtils.isVariableValueEmpty(clientVmPass)) {
runScriptRequestInputParams.put("VMPrivateKey", clientVmPass);
} else {
runScriptRequestInputParams.put("VMPrivateKey", null);
}
runScriptRequestInputParams.put("Script", bashCommandVariable);
this.invokerPlugin.handle(templateContext, ubuntuTemplateId, true, "runScript", Interfaces.OPENTOSCA_DECLARATIVE_INTERFACE_OPERATINGSYSTEM, runScriptRequestInputParams, new HashMap<String, Variable>(), templateContext.getProvisioningPhaseElement());
return true;
}
use of org.opentosca.planbuilder.core.plugins.context.PropertyVariable in project container by OpenTOSCA.
the class PatternBasedHandler method invokeArtifactReferenceUpload.
protected boolean invokeArtifactReferenceUpload(BPELPlanContext context, TArtifactReference ref, TNodeTemplate infraNode) {
PropertyVariable ip = this.getIpProperty(context, infraNode);
PropertyVariable user = this.getUserProperty(context, infraNode);
PropertyVariable key = this.getKeyProperty(context, infraNode);
if (!(Objects.nonNull(ip) && Objects.nonNull(user) && Objects.nonNull(key))) {
throw new RuntimeException("Couldn't fetch required variables to enable DA upload with the Remote Manager pattern");
}
return invoker.handleArtifactReferenceUpload(ref, context, ip, user, key, infraNode, context.getPrePhaseElement());
}
use of org.opentosca.planbuilder.core.plugins.context.PropertyVariable in project container by OpenTOSCA.
the class PatternBasedHandler method createDeployDescriptorPropertyToParameterMatching.
/**
* Try to find a parameter matching in the deployment technology descriptors. For every previously unmatched
* operation parameters a matching property in the deployment technology descriptors is searched.
*
* @param context The context, providing the deployment technology descriptor variable mapping
* @param abstractMatching The previous node property matching
* @param nodesForMatching Set of nodes that can be searched for deployment technology descriptor property matches
* @return The computed parameter matching
*/
private DeployTechDescriptorOperationMatching createDeployDescriptorPropertyToParameterMatching(final BPELPlanContext context, final OperationMatching abstractMatching, Set<TNodeTemplate> nodesForMatching) {
final DeployTechDescriptorOperationMatching matching = new DeployTechDescriptorOperationMatching(abstractMatching);
TOperation operation = matching.operationName;
if (operation.getInputParameters() != null) {
Set<TParameter> unmappedInputParams = operation.getInputParameters().stream().filter(tParameter -> !matching.abstractInputMatching.containsKey(tParameter)).collect(Collectors.toSet());
for (TParameter unmappedInputParam : unmappedInputParams) {
for (TNodeTemplate nodeToMatch : nodesForMatching) {
PropertyVariable propVar = context.getPropertyVariable(nodeToMatch, unmappedInputParam.getName());
if (propVar != null) {
matching.concreteInputMatching.put(unmappedInputParam, propVar);
matching.matchedNodes.add(nodeToMatch);
}
}
}
}
if (operation.getOutputParameters() != null) {
Set<TParameter> unmappedOutputParams = operation.getOutputParameters().stream().filter(tParameter -> !matching.abstractOutputMatching.containsKey(tParameter)).collect(Collectors.toSet());
for (TParameter unmappedOutputParam : unmappedOutputParams) {
for (TNodeTemplate nodeToMatch : nodesForMatching) {
PropertyVariable propVar = context.getPropertyVariable(nodeToMatch, unmappedOutputParam.getName());
if (propVar != null) {
matching.concreteOutputMatching.put(unmappedOutputParam, propVar);
matching.matchedNodes.add(nodeToMatch);
}
}
}
}
return matching;
}
Aggregations