Commit 3c78f488 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Implmemted rabbitmq logging for user

parent eb3081f7
...@@ -51,6 +51,7 @@ import nl.uva.sne.drip.api.dao.KeyPairDao; ...@@ -51,6 +51,7 @@ import nl.uva.sne.drip.api.dao.KeyPairDao;
import nl.uva.sne.drip.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.KeyException; import nl.uva.sne.drip.api.exception.KeyException;
import nl.uva.sne.drip.commons.utils.Converter; import nl.uva.sne.drip.commons.utils.Converter;
import nl.uva.sne.drip.commons.utils.DRIPLogHandler;
import nl.uva.sne.drip.drip.commons.data.v1.external.ConfigurationRepresentation; import nl.uva.sne.drip.drip.commons.data.v1.external.ConfigurationRepresentation;
import nl.uva.sne.drip.drip.commons.data.v1.external.KeyPair; import nl.uva.sne.drip.drip.commons.data.v1.external.KeyPair;
import nl.uva.sne.drip.drip.commons.data.v1.external.ScaleRequest; import nl.uva.sne.drip.drip.commons.data.v1.external.ScaleRequest;
...@@ -94,6 +95,13 @@ public class DeployService { ...@@ -94,6 +95,13 @@ public class DeployService {
private static final String[] CLOUD_SITE_NAMES = new String[]{"domain", "VMResourceID"}; private static final String[] CLOUD_SITE_NAMES = new String[]{"domain", "VMResourceID"};
private static final String[] PUBLIC_ADRESS_NAMES = new String[]{"public_address", "publicAddress"}; private static final String[] PUBLIC_ADRESS_NAMES = new String[]{"public_address", "publicAddress"};
private final Logger logger;
@Autowired
public DeployService() throws IOException, TimeoutException {
logger = Logger.getLogger(DeployService.class.getName());
logger.addHandler(new DRIPLogHandler(messageBrokerHost));
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))") @PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public DeployResponse findOne(String id) throws JSONException, IOException, TimeoutException, InterruptedException { public DeployResponse findOne(String id) throws JSONException, IOException, TimeoutException, InterruptedException {
...@@ -145,12 +153,15 @@ public class DeployService { ...@@ -145,12 +153,15 @@ public class DeployService {
// + File.separator + "workspace" + File.separator + "DRIP" // + File.separator + "workspace" + File.separator + "DRIP"
// + File.separator + "docs" + File.separator + "json_samples" // + File.separator + "docs" + File.separator + "json_samples"
// + File.separator + "deployer_ansible_response_benchmark.json"); // + File.separator + "deployer_ansible_response_benchmark.json");
logger.info("Calling deployer");
Message response = (deployer.call(deployerInvokationMessage)); Message response = (deployer.call(deployerInvokationMessage));
logger.info("Got response from deployer");
List<MessageParameter> params = response.getParameters(); List<MessageParameter> params = response.getParameters();
DeployResponse deploy = handleResponse(params, deployInfo); DeployResponse deploy = handleResponse(params, deployInfo);
deploy.setProvisionID(deployInfo.getProvisionID()); deploy.setProvisionID(deployInfo.getProvisionID());
deploy.setConfigurationID(deployInfo.getConfigurationID()); deploy.setConfigurationID(deployInfo.getConfigurationID());
deploy.setManagerType(deployInfo.getManagerType().toLowerCase()); deploy.setManagerType(deployInfo.getManagerType().toLowerCase());
logger.info("Deployment saved");
save(deploy); save(deploy);
return deploy; return deploy;
...@@ -172,7 +183,9 @@ public class DeployService { ...@@ -172,7 +183,9 @@ public class DeployService {
Map<String, Object> info; Map<String, Object> info;
deployerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); deployerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
try (DRIPCaller deployer = new DeployerCaller(messageBrokerHost);) { try (DRIPCaller deployer = new DeployerCaller(messageBrokerHost);) {
logger.info("Calling deployer");
Message response = (deployer.call(deployerInvokationMessage)); Message response = (deployer.call(deployerInvokationMessage));
logger.info("Got response from deployer");
List<MessageParameter> params = response.getParameters(); List<MessageParameter> params = response.getParameters();
info = buildSwarmInfo(params); info = buildSwarmInfo(params);
} }
...@@ -326,7 +339,9 @@ public class DeployService { ...@@ -326,7 +339,9 @@ public class DeployService {
Message message = buildDeployerMessage(deployment.getProvisionID(), "scale", confID, scaleReq.getScaleTargetName(), scaleReq.getNumOfInstances()); Message message = buildDeployerMessage(deployment.getProvisionID(), "scale", confID, scaleReq.getScaleTargetName(), scaleReq.getNumOfInstances());
message.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); message.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
try (DRIPCaller deployer = new DeployerCaller(messageBrokerHost);) { try (DRIPCaller deployer = new DeployerCaller(messageBrokerHost);) {
logger.info("Calling deployer");
Message response = (deployer.call(message)); Message response = (deployer.call(message));
logger.info("Got response from deployer");
List<MessageParameter> params = response.getParameters(); List<MessageParameter> params = response.getParameters();
handleResponse(params, null); handleResponse(params, null);
} }
......
...@@ -59,6 +59,7 @@ import org.springframework.security.access.prepost.PreAuthorize; ...@@ -59,6 +59,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import nl.uva.sne.drip.api.dao.ProvisionResponseDao; import nl.uva.sne.drip.api.dao.ProvisionResponseDao;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller1; import nl.uva.sne.drip.api.rpc.ProvisionerCaller1;
import nl.uva.sne.drip.commons.utils.DRIPLogHandler;
import nl.uva.sne.drip.drip.commons.data.v1.external.Key; import nl.uva.sne.drip.drip.commons.data.v1.external.Key;
import nl.uva.sne.drip.drip.commons.data.v1.external.KeyPair; import nl.uva.sne.drip.drip.commons.data.v1.external.KeyPair;
import nl.uva.sne.drip.drip.commons.data.v1.external.ScaleRequest; import nl.uva.sne.drip.drip.commons.data.v1.external.ScaleRequest;
...@@ -70,33 +71,40 @@ import nl.uva.sne.drip.drip.commons.data.v1.external.ScaleRequest; ...@@ -70,33 +71,40 @@ import nl.uva.sne.drip.drip.commons.data.v1.external.ScaleRequest;
@Service @Service
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
public class ProvisionService { public class ProvisionService {
@Autowired @Autowired
private ProvisionResponseDao provisionDao; private ProvisionResponseDao provisionDao;
@Autowired @Autowired
private CloudCredentialsService cloudCredentialsService; private CloudCredentialsService cloudCredentialsService;
@Autowired @Autowired
private SimplePlannerService simplePlanService; private SimplePlannerService simplePlanService;
@Autowired @Autowired
private ScriptService userScriptService; private ScriptService userScriptService;
@Autowired @Autowired
private KeyPairService keyPairService; private KeyPairService keyPairService;
@Value("${message.broker.host}") @Value("${message.broker.host}")
private String messageBrokerHost; private String messageBrokerHost;
private final Logger logger;
@Autowired
public ProvisionService() throws IOException, TimeoutException {
logger = Logger.getLogger(DeployService.class.getName());
logger.addHandler(new DRIPLogHandler(messageBrokerHost));
}
public ProvisionResponse save(ProvisionResponse ownedObject) { public ProvisionResponse save(ProvisionResponse ownedObject) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername(); String owner = user.getUsername();
ownedObject.setOwner(owner); ownedObject.setOwner(owner);
return provisionDao.save(ownedObject); return provisionDao.save(ownedObject);
} }
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))") @PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ProvisionResponse findOne(String id) { public ProvisionResponse findOne(String id) {
ProvisionResponse provisionInfo = provisionDao.findOne(id); ProvisionResponse provisionInfo = provisionDao.findOne(id);
...@@ -105,7 +113,7 @@ public class ProvisionService { ...@@ -105,7 +113,7 @@ public class ProvisionService {
} }
return provisionInfo; return provisionInfo;
} }
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))") @PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ProvisionResponse delete(String id) { public ProvisionResponse delete(String id) {
ProvisionResponse provisionInfo = provisionDao.findOne(id); ProvisionResponse provisionInfo = provisionDao.findOne(id);
...@@ -123,9 +131,9 @@ public class ProvisionService { ...@@ -123,9 +131,9 @@ public class ProvisionService {
public List<ProvisionResponse> findAll() { public List<ProvisionResponse> findAll() {
return provisionDao.findAll(); return provisionDao.findAll();
} }
public ProvisionResponse provisionResources(ProvisionRequest provisionRequest, int provisionerVersion) throws IOException, TimeoutException, JSONException, InterruptedException, Exception { public ProvisionResponse provisionResources(ProvisionRequest provisionRequest, int provisionerVersion) throws IOException, TimeoutException, JSONException, InterruptedException, Exception {
switch (provisionerVersion) { switch (provisionerVersion) {
case 0: case 0:
return callProvisioner0(provisionRequest); return callProvisioner0(provisionRequest);
...@@ -133,9 +141,9 @@ public class ProvisionService { ...@@ -133,9 +141,9 @@ public class ProvisionService {
return callProvisioner1(provisionRequest); return callProvisioner1(provisionRequest);
} }
return null; return null;
} }
private Message buildProvisioner0Message(ProvisionRequest pReq) throws JSONException, IOException { private Message buildProvisioner0Message(ProvisionRequest pReq) throws JSONException, IOException {
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
...@@ -145,24 +153,24 @@ public class ProvisionService { ...@@ -145,24 +153,24 @@ public class ProvisionService {
} }
MessageParameter conf = buildCloudCredentialParam(cred, 0).get(0); MessageParameter conf = buildCloudCredentialParam(cred, 0).get(0);
parameters.add(conf); parameters.add(conf);
List<MessageParameter> certs = buildCertificatesParam(cred); List<MessageParameter> certs = buildCertificatesParam(cred);
parameters.addAll(certs); parameters.addAll(certs);
List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID()); List<MessageParameter> topologies = buildTopologyParams(pReq.getPlanID());
parameters.addAll(topologies); parameters.addAll(topologies);
List<String> userKeyIDs = pReq.getUserKeyPairIDs(); List<String> userKeyIDs = pReq.getUserKeyPairIDs();
if (userKeyIDs != null) { if (userKeyIDs != null) {
List<MessageParameter> userKeys = buildUserKeysParams(userKeyIDs.get(0), 0); List<MessageParameter> userKeys = buildUserKeysParams(userKeyIDs.get(0), 0);
parameters.addAll(userKeys); parameters.addAll(userKeys);
} }
invokationMessage.setParameters(parameters); invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis())); invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage; return invokationMessage;
} }
private List<MessageParameter> buildCloudCredentialParam(CloudCredentials cred, int version) throws JsonProcessingException, JSONException, IOException { private List<MessageParameter> buildCloudCredentialParam(CloudCredentials cred, int version) throws JsonProcessingException, JSONException, IOException {
List<MessageParameter> cloudCredentialParams = new ArrayList<>(); List<MessageParameter> cloudCredentialParams = new ArrayList<>();
if (version == 0) { if (version == 0) {
...@@ -197,15 +205,15 @@ public class ProvisionService { ...@@ -197,15 +205,15 @@ public class ProvisionService {
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
String jsonInString = mapper.writeValueAsString(cred); String jsonInString = mapper.writeValueAsString(cred);
cloudCred.setValue("\"" + jsonInString + "\""); cloudCred.setValue("\"" + jsonInString + "\"");
cloudCredentialParams.add(cloudCred); cloudCredentialParams.add(cloudCred);
return cloudCredentialParams; return cloudCredentialParams;
} }
return null; return null;
} }
private List<MessageParameter> buildCertificatesParam(CloudCredentials cred) { private List<MessageParameter> buildCertificatesParam(CloudCredentials cred) {
// List<String> loginKeysIDs = cred.getkeyPairIDs(); // List<String> loginKeysIDs = cred.getkeyPairIDs();
List<KeyPair> loginKeys = new ArrayList<>(); List<KeyPair> loginKeys = new ArrayList<>();
...@@ -232,10 +240,10 @@ public class ProvisionService { ...@@ -232,10 +240,10 @@ public class ProvisionService {
} }
return parameters; return parameters;
} }
private List<MessageParameter> buildTopologyParams(String planID) throws JSONException, FileNotFoundException { private List<MessageParameter> buildTopologyParams(String planID) throws JSONException, FileNotFoundException {
PlanResponse plan = simplePlanService.getDao().findOne(planID); PlanResponse plan = simplePlanService.getDao().findOne(planID);
if (plan == null) { if (plan == null) {
throw new PlanNotFoundException(); throw new PlanNotFoundException();
} }
...@@ -250,7 +258,7 @@ public class ProvisionService { ...@@ -250,7 +258,7 @@ public class ProvisionService {
attributes.put("filename", FilenameUtils.removeExtension(plan.getName())); attributes.put("filename", FilenameUtils.removeExtension(plan.getName()));
topology.setAttributes(attributes); topology.setAttributes(attributes);
parameters.add(topology); parameters.add(topology);
Set<String> ids = plan.getLoweLevelPlanIDs(); Set<String> ids = plan.getLoweLevelPlanIDs();
if (ids != null) { if (ids != null) {
for (String lowID : ids) { for (String lowID : ids) {
...@@ -269,10 +277,10 @@ public class ProvisionService { ...@@ -269,10 +277,10 @@ public class ProvisionService {
} }
return parameters; return parameters;
} }
private List<MessageParameter> buildProvisionedTopologyParams(ProvisionResponse provisionInfo) throws JSONException { private List<MessageParameter> buildProvisionedTopologyParams(ProvisionResponse provisionInfo) throws JSONException {
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
Map<String, Object> map = provisionInfo.getKeyValue(); Map<String, Object> map = provisionInfo.getKeyValue();
for (String topoName : map.keySet()) { for (String topoName : map.keySet()) {
Map<String, Object> topo = (Map<String, Object>) map.get(topoName); Map<String, Object> topo = (Map<String, Object>) map.get(topoName);
...@@ -291,13 +299,13 @@ public class ProvisionService { ...@@ -291,13 +299,13 @@ public class ProvisionService {
} }
return parameters; return parameters;
} }
private List<MessageParameter> buildClusterKeyParams(ProvisionResponse provisionInfo) { private List<MessageParameter> buildClusterKeyParams(ProvisionResponse provisionInfo) {
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
List<String> ids = provisionInfo.getDeployerKeyPairIDs(); List<String> ids = provisionInfo.getDeployerKeyPairIDs();
for (String id : ids) { for (String id : ids) {
KeyPair pair = keyPairService.findOne(id); KeyPair pair = keyPairService.findOne(id);
MessageParameter param = new MessageParameter(); MessageParameter param = new MessageParameter();
param.setName("private_deployer_key"); param.setName("private_deployer_key");
param.setValue(pair.getPrivateKey().getKey()); param.setValue(pair.getPrivateKey().getKey());
...@@ -308,7 +316,7 @@ public class ProvisionService { ...@@ -308,7 +316,7 @@ public class ProvisionService {
attributes.put("name", pair.getPrivateKey().getName()); attributes.put("name", pair.getPrivateKey().getName());
param.setAttributes(attributes); param.setAttributes(attributes);
parameters.add(param); parameters.add(param);
param = new MessageParameter(); param = new MessageParameter();
param.setName("public_deployer_key"); param.setName("public_deployer_key");
param.setValue(pair.getPublicKey().getKey()); param.setValue(pair.getPublicKey().getKey());
...@@ -320,16 +328,16 @@ public class ProvisionService { ...@@ -320,16 +328,16 @@ public class ProvisionService {
attributes.put("name", pair.getPublicKey().getName()); attributes.put("name", pair.getPublicKey().getName());
parameters.add(param); parameters.add(param);
} }
return parameters; return parameters;
} }
private List<MessageParameter> buildUserKeyParams(ProvisionResponse provisionInfo) { private List<MessageParameter> buildUserKeyParams(ProvisionResponse provisionInfo) {
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
List<String> ids = provisionInfo.getUserKeyPairIDs(); List<String> ids = provisionInfo.getUserKeyPairIDs();
for (String id : ids) { for (String id : ids) {
KeyPair pair = keyPairService.findOne(id); KeyPair pair = keyPairService.findOne(id);
MessageParameter param = new MessageParameter(); MessageParameter param = new MessageParameter();
param.setName("private_user_key"); param.setName("private_user_key");
param.setValue(pair.getPrivateKey().getKey()); param.setValue(pair.getPrivateKey().getKey());
...@@ -340,7 +348,7 @@ public class ProvisionService { ...@@ -340,7 +348,7 @@ public class ProvisionService {
attributes.put("name", pair.getPrivateKey().getName()); attributes.put("name", pair.getPrivateKey().getName());
param.setAttributes(attributes); param.setAttributes(attributes);
parameters.add(param); parameters.add(param);
param = new MessageParameter(); param = new MessageParameter();
param.setName("public_user_key"); param.setName("public_user_key");
param.setValue(pair.getPublicKey().getKey()); param.setValue(pair.getPublicKey().getKey());
...@@ -353,14 +361,14 @@ public class ProvisionService { ...@@ -353,14 +361,14 @@ public class ProvisionService {
} }
return parameters; return parameters;
} }
private List<MessageParameter> buildCloudKeyParams(ProvisionResponse provisionInfo) { private List<MessageParameter> buildCloudKeyParams(ProvisionResponse provisionInfo) {
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
List<String> ids = provisionInfo.getCloudKeyPairIDs(); List<String> ids = provisionInfo.getCloudKeyPairIDs();
if (ids != null) { if (ids != null) {
for (String id : ids) { for (String id : ids) {
KeyPair pair = keyPairService.findOne(id); KeyPair pair = keyPairService.findOne(id);
MessageParameter param = new MessageParameter(); MessageParameter param = new MessageParameter();
param.setName("private_cloud_key"); param.setName("private_cloud_key");
param.setValue(pair.getPrivateKey().getKey()); param.setValue(pair.getPrivateKey().getKey());
...@@ -370,7 +378,7 @@ public class ProvisionService { ...@@ -370,7 +378,7 @@ public class ProvisionService {
attributes.put("key_pair_id", pair.getKeyPairId()); attributes.put("key_pair_id", pair.getKeyPairId());
param.setAttributes(attributes); param.setAttributes(attributes);
parameters.add(param); parameters.add(param);
param = new MessageParameter(); param = new MessageParameter();
param.setName("public_cloud_key"); param.setName("public_cloud_key");
param.setValue(pair.getPublicKey().getKey()); param.setValue(pair.getPublicKey().getKey());
...@@ -382,11 +390,11 @@ public class ProvisionService { ...@@ -382,11 +390,11 @@ public class ProvisionService {
parameters.add(param); parameters.add(param);
} }
} }
return parameters; return parameters;
} }
private MessageParameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException { private MessageParameter buildEC2Conf(CloudCredentials cred) throws JsonProcessingException, JSONException, IOException {
Properties prop = Converter.getEC2Properties(cred); Properties prop = Converter.getEC2Properties(cred);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
...@@ -397,9 +405,9 @@ public class ProvisionService { ...@@ -397,9 +405,9 @@ public class ProvisionService {
String charset = "UTF-8"; String charset = "UTF-8";
conf.setValue(new String(bytes, charset)); conf.setValue(new String(bytes, charset));
return conf; return conf;
} }
private List<MessageParameter> buildScriptParams(String userScriptID) { private List<MessageParameter> buildScriptParams(String userScriptID) {
Script script = userScriptService.findOne(userScriptID); Script script = userScriptService.findOne(userScriptID);
if (script == null) { if (script == null) {
...@@ -413,7 +421,7 @@ public class ProvisionService { ...@@ -413,7 +421,7 @@ public class ProvisionService {
parameters.add(scriptParameter); parameters.add(scriptParameter);
return parameters; return parameters;
} }
private List<MessageParameter> buildUserKeysParams(String userKeyID, int version) { private List<MessageParameter> buildUserKeysParams(String userKeyID, int version) {
KeyPair key = keyPairService.findOne(userKeyID); KeyPair key = keyPairService.findOne(userKeyID);
if (key == null) { if (key == null) {
...@@ -432,43 +440,47 @@ public class ProvisionService { ...@@ -432,43 +440,47 @@ public class ProvisionService {
parameters.add(keyParameter); parameters.add(keyParameter);
return parameters; return parameters;
} }
@PostFilter("(hasRole('ROLE_ADMIN'))") @PostFilter("(hasRole('ROLE_ADMIN'))")
public void deleteAll() { public void deleteAll() {
provisionDao.deleteAll(); provisionDao.deleteAll();
} }
private ProvisionResponse callProvisioner0(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception { private ProvisionResponse callProvisioner0(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception {
try (DRIPCaller provisioner = new ProvisionerCaller0(messageBrokerHost);) { try (DRIPCaller provisioner = new ProvisionerCaller0(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisioner0Message(provisionRequest); Message provisionerInvokationMessage = buildProvisioner0Message(provisionRequest);
provisionerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); provisionerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
logger.info("Calling provisioner");
Message response = (provisioner.call(provisionerInvokationMessage)); Message response = (provisioner.call(provisionerInvokationMessage));
logger.info("Got provisioner response");
return parseCreateResourcesResponse(response.getParameters(), provisionRequest, null, true, true); return parseCreateResourcesResponse(response.getParameters(), provisionRequest, null, true, true);
} }
} }
private ProvisionResponse callProvisioner1(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception { private ProvisionResponse callProvisioner1(ProvisionRequest provisionRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception {
try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) { try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisioner1Message(provisionRequest); Message provisionerInvokationMessage = buildProvisioner1Message(provisionRequest);
provisionerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); provisionerInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
logger.info("Calling provisioner");
Message response = (provisioner.call(provisionerInvokationMessage)); Message response = (provisioner.call(provisionerInvokationMessage));
logger.info("Got provisioner response");
return parseCreateResourcesResponse(response.getParameters(), provisionRequest, null, true, true); return parseCreateResourcesResponse(response.getParameters(), provisionRequest, null, true, true);
} }
} }
public void deleteProvisionedResources(ProvisionResponse provisionInfo) throws IOException, TimeoutException, InterruptedException, JSONException { public void deleteProvisionedResources(ProvisionResponse provisionInfo) throws IOException, TimeoutException, InterruptedException, JSONException {
try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) { try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) {
Message deleteInvokationMessage = buildTopoplogyModificationMessage(provisionInfo, "kill_topology", null); Message deleteInvokationMessage = buildTopoplogyModificationMessage(provisionInfo, "kill_topology", null);
deleteInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); deleteInvokationMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
logger.info("Calling provisioner");
Message response = (provisioner.call(deleteInvokationMessage)); Message response = (provisioner.call(deleteInvokationMessage));
logger.info("Got provisioner response");
parseDeleteResourcesResponse(response.getParameters(), provisionInfo); parseDeleteResourcesResponse(response.getParameters(), provisionInfo);
} }
} }
public ProvisionResponse scale(ScaleRequest scaleRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception { public ProvisionResponse scale(ScaleRequest scaleRequest) throws IOException, TimeoutException, JSONException, InterruptedException, Exception {
String provisionID = scaleRequest.getScaleTargetID(); String provisionID = scaleRequest.getScaleTargetID();
String scaleName = scaleRequest.getScaleTargetName(); String scaleName = scaleRequest.getScaleTargetName();
...@@ -498,12 +510,12 @@ public class ProvisionService { ...@@ -498,12 +510,12 @@ public class ProvisionService {
} }
} }
} }
if (!scaleNameExists) { if (!scaleNameExists) {
throw new BadRequestException("Name of topology dosen't exist"); throw new BadRequestException("Name of topology dosen't exist");
} }
int numOfInstances = Math.abs(currentNumOfInstances - scaleRequest.getNumOfInstances()); int numOfInstances = Math.abs(currentNumOfInstances - scaleRequest.getNumOfInstances());
if (currentNumOfInstances > scaleRequest.getNumOfInstances() && currentNumOfInstances != 0) { if (currentNumOfInstances > scaleRequest.getNumOfInstances() && currentNumOfInstances != 0) {
try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) { try (DRIPCaller provisioner = new ProvisionerCaller1(messageBrokerHost);) {
Map<String, String> extra = new HashMap<>(); Map<String, String> extra = new HashMap<>();
...@@ -511,10 +523,12 @@ public class ProvisionService { ...@@ -511,10 +523,12 @@ public class ProvisionService {
extra.put("number_of_instances", String.valueOf(numOfInstances)); extra.put("number_of_instances", String.valueOf(numOfInstances));
extra.put("cloud_provider", cloudProvider); extra.put("cloud_provider", cloudProvider);
extra.put("domain", domain); extra.put("domain", domain);
Message scaleMessage = buildTopoplogyModificationMessage(provisionInfo, "scale_topology_down", extra); Message scaleMessage = buildTopoplogyModificationMessage(provisionInfo, "scale_topology_down", extra);
scaleMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); scaleMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
logger.info("Calling provisioner");
Message response = provisioner.call(scaleMessage); Message response = provisioner.call(scaleMessage);
logger.info("Got response from provisioner");
parseCreateResourcesResponse(response.getParameters(), null, provisionInfo, false, false); parseCreateResourcesResponse(response.getParameters(), null, provisionInfo, false, false);
} }
} else if (currentNumOfInstances < scaleRequest.getNumOfInstances() || currentNumOfInstances == 0) { } else if (currentNumOfInstances < scaleRequest.getNumOfInstances() || currentNumOfInstances == 0) {
...@@ -524,28 +538,30 @@ public class ProvisionService { ...@@ -524,28 +538,30 @@ public class ProvisionService {
extra.put("number_of_instances", String.valueOf(numOfInstances)); extra.put("number_of_instances", String.valueOf(numOfInstances));
extra.put("cloud_provider", cloudProvider); extra.put("cloud_provider", cloudProvider);
extra.put("domain", domain); extra.put("domain", domain);
Message scaleMessage = buildTopoplogyModificationMessage(provisionInfo, "scale_topology_up", extra); Message scaleMessage = buildTopoplogyModificationMessage(provisionInfo, "scale_topology_up", extra);
scaleMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()); scaleMessage.setOwner(((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
logger.info("Calling provisioner");
Message response = provisioner.call(scaleMessage); Message response = provisioner.call(scaleMessage);
logger.info("Got response from provisioner");
parseCreateResourcesResponse(response.getParameters(), null, provisionInfo, false, false); parseCreateResourcesResponse(response.getParameters(), null, provisionInfo, false, false);
} }
} }
return provisionInfo; return provisionInfo;
} }
private Message buildProvisioner1Message(ProvisionRequest provisionRequest) throws JSONException, FileNotFoundException, IOException { private Message buildProvisioner1Message(ProvisionRequest provisionRequest) throws JSONException, FileNotFoundException, IOException {
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
MessageParameter action = new MessageParameter(); MessageParameter action = new MessageParameter();
action.setName("action"); action.setName("action");
action.setValue("start_topology"); action.setValue("start_topology");
parameters.add(action); parameters.add(action);
List<MessageParameter> topologies = buildTopologyParams(provisionRequest.getPlanID()); List<MessageParameter> topologies = buildTopologyParams(provisionRequest.getPlanID());
parameters.addAll(topologies); parameters.addAll(topologies);
List<String> userKeyIDs = provisionRequest.getUserKeyPairIDs(); List<String> userKeyIDs = provisionRequest.getUserKeyPairIDs();
if (userKeyIDs != null) { if (userKeyIDs != null) {
for (String keyID : userKeyIDs) { for (String keyID : userKeyIDs) {
...@@ -553,7 +569,7 @@ public class ProvisionService { ...@@ -553,7 +569,7 @@ public class ProvisionService {
parameters.addAll(userKeys); parameters.addAll(userKeys);
} }
} }
List<String> deployerKeys = provisionRequest.getDeployerKeyPairIDs(); List<String> deployerKeys = provisionRequest.getDeployerKeyPairIDs();
if (userKeyIDs != null) { if (userKeyIDs != null) {
for (String keyID : deployerKeys) { for (String keyID : deployerKeys) {
...@@ -569,34 +585,34 @@ public class ProvisionService { ...@@ -569,34 +585,34 @@ public class ProvisionService {
List<MessageParameter> cloudCredentialParams = buildCloudCredentialParam(cred, 1); List<MessageParameter> cloudCredentialParams = buildCloudCredentialParam(cred, 1);
parameters.addAll(cloudCredentialParams); parameters.addAll(cloudCredentialParams);
} }
invokationMessage.setParameters(parameters); invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis())); invokationMessage.setCreationDate((System.currentTimeMillis()));
return invokationMessage; return invokationMessage;
} }
private Message buildTopoplogyModificationMessage(ProvisionResponse provisionInfo, String actionName, Map<String, String> extraAttributes) throws JSONException, IOException { private Message buildTopoplogyModificationMessage(ProvisionResponse provisionInfo, String actionName, Map<String, String> extraAttributes) throws JSONException, IOException {
Message invokationMessage = new Message(); Message invokationMessage = new Message();
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
MessageParameter action = new MessageParameter(); MessageParameter action = new MessageParameter();
action.setName("action"); action.setName("action");
action.setValue(actionName); action.setValue(actionName);
parameters.add(action); parameters.add(action);
List<MessageParameter> topologies = buildProvisionedTopologyParams(provisionInfo); List<MessageParameter> topologies = buildProvisionedTopologyParams(provisionInfo);
parameters.addAll(topologies); parameters.addAll(topologies);
List<MessageParameter> clusterKeys = buildClusterKeyParams(provisionInfo); List<MessageParameter> clusterKeys = buildClusterKeyParams(provisionInfo);
parameters.addAll(clusterKeys); parameters.addAll(clusterKeys);
List<MessageParameter> userKeys = buildUserKeyParams(provisionInfo); List<MessageParameter> userKeys = buildUserKeyParams(provisionInfo);
parameters.addAll(userKeys); parameters.addAll(userKeys);
List<MessageParameter> cloudKeys = buildCloudKeyParams(provisionInfo); List<MessageParameter> cloudKeys = buildCloudKeyParams(provisionInfo);
parameters.addAll(cloudKeys); parameters.addAll(cloudKeys);
for (String id : provisionInfo.getCloudCredentialsIDs()) { for (String id : provisionInfo.getCloudCredentialsIDs()) {
CloudCredentials cred = cloudCredentialsService.findOne(id); CloudCredentials cred = cloudCredentialsService.findOne(id);
if (cred == null) { if (cred == null) {
...@@ -620,7 +636,7 @@ public class ProvisionService { ...@@ -620,7 +636,7 @@ public class ProvisionService {
invokationMessage.setCreationDate(System.currentTimeMillis()); invokationMessage.setCreationDate(System.currentTimeMillis());
return invokationMessage; return invokationMessage;
} }
private List<MessageParameter> buildDeployKeysParams(String keyID) { private List<MessageParameter> buildDeployKeysParams(String keyID) {
KeyPair key = keyPairService.findOne(keyID); KeyPair key = keyPairService.findOne(keyID);
if (key == null) { if (key == null) {
...@@ -628,29 +644,29 @@ public class ProvisionService { ...@@ -628,29 +644,29 @@ public class ProvisionService {
} }
List<MessageParameter> parameters = new ArrayList(); List<MessageParameter> parameters = new ArrayList();
MessageParameter keyParameter = new MessageParameter(); MessageParameter keyParameter = new MessageParameter();
keyParameter.setName("deployer_ssh_key"); keyParameter.setName("deployer_ssh_key");
keyParameter.setValue(key.getPublicKey().getKey()); keyParameter.setValue(key.getPublicKey().getKey());
keyParameter.setEncoding("UTF-8"); keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter); parameters.add(keyParameter);
return parameters; return parameters;
} }
private ProvisionResponse parseCreateResourcesResponse(List<MessageParameter> parameters, private ProvisionResponse parseCreateResourcesResponse(List<MessageParameter> parameters,
ProvisionRequest provisionRequest, ProvisionResponse provisionResponse, boolean saveUserKeys, boolean saveDeployerKeyI) throws Exception { ProvisionRequest provisionRequest, ProvisionResponse provisionResponse, boolean saveUserKeys, boolean saveDeployerKeyI) throws Exception {
if (provisionResponse == null) { if (provisionResponse == null) {
provisionResponse = new ProvisionResponse(); provisionResponse = new ProvisionResponse();
} }
List<DeployParameter> deployParameters = new ArrayList<>(); List<DeployParameter> deployParameters = new ArrayList<>();
Map<String, Object> kvMap = null; Map<String, Object> kvMap = null;
KeyPair userKey = new KeyPair(); KeyPair userKey = new KeyPair();
KeyPair deployerKey = new KeyPair(); KeyPair deployerKey = new KeyPair();
Map<String, Key> privateCloudKeys = new HashMap<>(); Map<String, Key> privateCloudKeys = new HashMap<>();
Map<String, Key> publicCloudKeys = new HashMap<>(); Map<String, Key> publicCloudKeys = new HashMap<>();
for (MessageParameter p : parameters) { for (MessageParameter p : parameters) {
String name = p.getName(); String name = p.getName();
if (name.toLowerCase().contains("exception")) { if (name.toLowerCase().contains("exception")) {
...@@ -674,7 +690,7 @@ public class ProvisionService { ...@@ -674,7 +690,7 @@ public class ProvisionService {
// String deployCertPath = parts[2]; // String deployCertPath = parts[2];
// String cloudCertificateName = FilenameUtils.removeExtension(FilenameUtils.getBaseName(deployCertPath)); // String cloudCertificateName = FilenameUtils.removeExtension(FilenameUtils.getBaseName(deployCertPath));
String deployRole = parts[2]; String deployRole = parts[2];
deployParam.setIP(deployIP); deployParam.setIP(deployIP);
deployParam.setRole(deployRole); deployParam.setRole(deployRole);
deployParam.setUser(deployUser); deployParam.setUser(deployUser);
...@@ -718,7 +734,7 @@ public class ProvisionService { ...@@ -718,7 +734,7 @@ public class ProvisionService {
key.setAttributes(p.getAttributes()); key.setAttributes(p.getAttributes());
publicCloudKeys.put(p.getAttributes().get("key_pair_id"), key); publicCloudKeys.put(p.getAttributes().get("key_pair_id"), key);
break; break;
case "private_cloud_key": case "private_cloud_key":
key = new Key(); key = new Key();
key.setKey(p.getValue()); key.setKey(p.getValue());
...@@ -727,7 +743,7 @@ public class ProvisionService { ...@@ -727,7 +743,7 @@ public class ProvisionService {
key.setAttributes(p.getAttributes()); key.setAttributes(p.getAttributes());
privateCloudKeys.put(p.getAttributes().get("key_pair_id"), key); privateCloudKeys.put(p.getAttributes().get("key_pair_id"), key);
break; break;
default: default:
value = p.getValue(); value = p.getValue();
if (kvMap == null) { if (kvMap == null) {
...@@ -743,7 +759,7 @@ public class ProvisionService { ...@@ -743,7 +759,7 @@ public class ProvisionService {
} else { } else {
userKeyIds = provisionResponse.getUserKeyPairIDs(); userKeyIds = provisionResponse.getUserKeyPairIDs();
} }
if (saveUserKeys) { if (saveUserKeys) {
if (userKeyIds != null && !userKeyIds.isEmpty()) { if (userKeyIds != null && !userKeyIds.isEmpty()) {
} else { } else {
...@@ -762,7 +778,7 @@ public class ProvisionService { ...@@ -762,7 +778,7 @@ public class ProvisionService {
deployerKeyIds.add(deployerKey.getId()); deployerKeyIds.add(deployerKey.getId());
} }
} }
ArrayList<String> cloudKeyPairIDs = new ArrayList<>(); ArrayList<String> cloudKeyPairIDs = new ArrayList<>();
List<KeyPair> allPirs = keyPairService.findAll(); List<KeyPair> allPirs = keyPairService.findAll();
for (String id : publicCloudKeys.keySet()) { for (String id : publicCloudKeys.keySet()) {
...@@ -785,7 +801,7 @@ public class ProvisionService { ...@@ -785,7 +801,7 @@ public class ProvisionService {
cloudPair = keyPairService.save(cloudPair); cloudPair = keyPairService.save(cloudPair);
cloudKeyPairIDs.add(cloudPair.getId()); cloudKeyPairIDs.add(cloudPair.getId());
} }
} }
ArrayList<String> existingCloudKeyPairIDs = provisionResponse.getCloudKeyPairIDs(); ArrayList<String> existingCloudKeyPairIDs = provisionResponse.getCloudKeyPairIDs();
if (existingCloudKeyPairIDs != null) { if (existingCloudKeyPairIDs != null) {
...@@ -794,25 +810,25 @@ public class ProvisionService { ...@@ -794,25 +810,25 @@ public class ProvisionService {
existingCloudKeyPairIDs = cloudKeyPairIDs; existingCloudKeyPairIDs = cloudKeyPairIDs;
} }
provisionResponse.setCloudKeyPairIDs(existingCloudKeyPairIDs); provisionResponse.setCloudKeyPairIDs(existingCloudKeyPairIDs);
provisionResponse.setDeployParameters(deployParameters); provisionResponse.setDeployParameters(deployParameters);
provisionResponse.setKvMap(kvMap); provisionResponse.setKvMap(kvMap);
if (provisionRequest != null) { if (provisionRequest != null) {
provisionResponse.setCloudCredentialsIDs(provisionRequest.getCloudCredentialsIDs()); provisionResponse.setCloudCredentialsIDs(provisionRequest.getCloudCredentialsIDs());
provisionResponse.setPlanID(provisionRequest.getPlanID()); provisionResponse.setPlanID(provisionRequest.getPlanID());
} }
if (userKeyIds != null) { if (userKeyIds != null) {
provisionResponse.setUserKeyPairIDs(userKeyIds); provisionResponse.setUserKeyPairIDs(userKeyIds);
} }
if (deployerKeyIds != null) { if (deployerKeyIds != null) {
provisionResponse.setDeployerKeyPairIDs(deployerKeyIds); provisionResponse.setDeployerKeyPairIDs(deployerKeyIds);
} }
provisionResponse = save(provisionResponse); provisionResponse = save(provisionResponse);
return provisionResponse; return provisionResponse;
} }
private void parseDeleteResourcesResponse(List<MessageParameter> parameters, ProvisionResponse provisionInfo) { private void parseDeleteResourcesResponse(List<MessageParameter> parameters, ProvisionResponse provisionInfo) {
// for (MessageParameter p : parameters) { // for (MessageParameter p : parameters) {
// System.err.println(p.getName() + " : " + p.getValue()); // System.err.println(p.getName() + " : " + p.getValue());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment