Commit 9e000fff authored by Spiros Koulouzis's avatar Spiros Koulouzis

Implmented deployer controller

parent 3793b78f
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.rest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import nl.uva.sne.drip.api.dao.CloudCredentialsDao;
import nl.uva.sne.drip.commons.types.Message;
import nl.uva.sne.drip.commons.types.Parameter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.DeployerCaller;
import nl.uva.sne.drip.api.service.ProvisionService;
import nl.uva.sne.drip.api.service.UserKeyService;
import nl.uva.sne.drip.api.service.UserScriptService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.types.CloudCredentials;
import nl.uva.sne.drip.commons.types.DeployParameter;
import nl.uva.sne.drip.commons.types.LoginKey;
import nl.uva.sne.drip.commons.types.ProvisionInfo;
import org.springframework.web.bind.annotation.PathVariable;
/**
*
* @author S. Koulouzis
*/
@RestController
@RequestMapping("/user/deployer")
@Component
public class DeployController {
@Value("${message.broker.host}")
private String messageBrokerHost;
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
@Autowired
private ProvisionService provisionService;
@RequestMapping(value = "/deploy/{id}", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String deploy(@PathVariable("id") String provisionID) {
try (DRIPCaller deployer = new DeployerCaller(messageBrokerHost);) {
Message deployerInvokationMessage = buildDeployerMessage(provisionID);
Message response = deployer.call(deployerInvokationMessage);
// Message response = generateFakeResponse();
List<Parameter> params = response.getParameters();
for (Parameter p : params) {
String name = p.getName();
}
return null;
} catch (IOException | TimeoutException | JSONException | InterruptedException ex) {
Logger.getLogger(DeployController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
private Message buildDeployerMessage(String provisionID) {
ProvisionInfo pro = provisionService.getDao().findOne(provisionID);
String cloudConfID = pro.getCloudConfID();
CloudCredentials cCred = cloudCredentialsDao.findOne(cloudConfID);
List<LoginKey> loginKeys = cCred.getLoginKeys();
List<DeployParameter> deployParams = pro.getDeployParameters();
List<Parameter> parameters = new ArrayList<>();
for (DeployParameter dp : deployParams) {
String cName = dp.getCloudCertificateName();
Parameter messageParameter = new Parameter();
messageParameter.setName("credential");
messageParameter.setEncoding("UTF-8");
String key = null;
for (LoginKey lk : loginKeys) {
String lkName = lk.getName();
if (lkName == null) {
lkName = lk.getAttributes().get("domain_name");
}
if (lkName.equals(cName)) {
key = lk.getKey();
break;
}
}
messageParameter.setValue(key);
Map<String, String> attributes = new HashMap<>();
attributes.put("IP", dp.getIP());
attributes.put("role", dp.getRole());
attributes.put("user", dp.getUser());
messageParameter.setAttributes(attributes);
parameters.add(messageParameter);
}
Message deployInvokationMessage = new Message();
deployInvokationMessage.setParameters(parameters);
deployInvokationMessage.setCreationDate(System.currentTimeMillis());
return deployInvokationMessage;
}
}
...@@ -53,6 +53,7 @@ import nl.uva.sne.drip.api.service.UserKeyService; ...@@ -53,6 +53,7 @@ import nl.uva.sne.drip.api.service.UserKeyService;
import nl.uva.sne.drip.api.service.UserScriptService; import nl.uva.sne.drip.api.service.UserScriptService;
import nl.uva.sne.drip.api.service.UserService; import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.types.CloudCredentials; import nl.uva.sne.drip.commons.types.CloudCredentials;
import nl.uva.sne.drip.commons.types.DeployParameter;
import nl.uva.sne.drip.commons.types.LoginKey; import nl.uva.sne.drip.commons.types.LoginKey;
import nl.uva.sne.drip.commons.types.Plan; import nl.uva.sne.drip.commons.types.Plan;
import nl.uva.sne.drip.commons.types.UserScript; import nl.uva.sne.drip.commons.types.UserScript;
...@@ -97,12 +98,12 @@ public class ProvisionController { ...@@ -97,12 +98,12 @@ public class ProvisionController {
@RequestMapping(value = "/provision", method = RequestMethod.POST) @RequestMapping(value = "/provision", method = RequestMethod.POST)
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
String plann(@RequestBody ProvisionInfo req) { String provision(@RequestBody ProvisionInfo req) {
try (DRIPCaller provisioner = new ProvisionerCaller(messageBrokerHost);) { try (DRIPCaller provisioner = new ProvisionerCaller(messageBrokerHost);) {
Message provisionerInvokationMessage = buildProvisionerMessage(req); Message provisionerInvokationMessage = buildProvisionerMessage(req);
// Message response = provisioner.call(provisionerInvokationMessage); Message response = provisioner.call(provisionerInvokationMessage);
Message response = generateFakeResponse(); // Message response = generateFakeResponse();
List<Parameter> params = response.getParameters(); List<Parameter> params = response.getParameters();
for (Parameter p : params) { for (Parameter p : params) {
...@@ -112,21 +113,30 @@ public class ProvisionController { ...@@ -112,21 +113,30 @@ public class ProvisionController {
Map<String, Object> kvMap = Converter.ymlString2Map(value); Map<String, Object> kvMap = Converter.ymlString2Map(value);
req.setKvMap(kvMap); req.setKvMap(kvMap);
req.setPlanID(req.getPlanID()); req.setPlanID(req.getPlanID());
provisionService.getDao().save(req);
} else { } else {
String value = p.getValue(); String value = p.getValue();
String[] lines = value.split("\n"); String[] lines = value.split("\n");
List<DeployParameter> deployParameters = new ArrayList<>();
for (String line : lines) { for (String line : lines) {
DeployParameter deployParam = new DeployParameter();
String[] parts = line.split(" "); String[] parts = line.split(" ");
String deployIP = parts[0]; String deployIP = parts[0];
String deployUser = parts[1]; String deployUser = parts[1];
String deployCertPath = parts[2]; String deployCertPath = parts[2];
String cloudCertificateName = FilenameUtils.removeExtension(FilenameUtils.getBaseName(deployCertPath));
String deployRole = parts[3]; String deployRole = parts[3];
req.setDeployIP(deployIP);
req.setDeployUser(deployUser); deployParam.setIP(deployIP);
req.setDeployRole(deployRole); deployParam.setRole(deployRole);
deployParam.setUser(deployUser);
deployParam.setCloudCertificateName(cloudCertificateName);
deployParameters.add(deployParam);
} }
req.setDeployParameters(deployParameters);
} }
provisionService.getDao().save(req);
} }
return req.getId(); return req.getId();
} catch (IOException | TimeoutException | JSONException | InterruptedException ex) { } catch (IOException | TimeoutException | JSONException | InterruptedException ex) {
......
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.api.rpc;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
*
* @author S. Koulouzis
*/
public class DeployerCaller extends DRIPCaller {
private static final String REQUEST_QUEUE_NAME = "deployer_queue";
public DeployerCaller(String messageBrokerHost) throws IOException, TimeoutException {
super(messageBrokerHost, REQUEST_QUEUE_NAME);
}
}
...@@ -15,17 +15,8 @@ ...@@ -15,17 +15,8 @@
*/ */
package nl.uva.sne.drip.api.service; package nl.uva.sne.drip.api.service;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import nl.uva.sne.drip.api.dao.ToscaDao;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.commons.types.ToscaRepresentation;
import nl.uva.sne.drip.commons.utils.Converter;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import nl.uva.sne.drip.api.dao.ProvisionInfoDao; import nl.uva.sne.drip.api.dao.ProvisionInfoDao;
/** /**
......
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.commons.types;
/**
*
* @author S. Koulouzis
*/
public class DeployParameter {
private String cloudCertificateName;
private String IP;
private String user;
private String role;
/**
* @return the cloudCertificateName
*/
public String getCloudCertificateName() {
return cloudCertificateName;
}
/**
* @param cloudCertificateName the cloudCertificateName to set
*/
public void setCloudCertificateName(String cloudCertificateName) {
this.cloudCertificateName = cloudCertificateName;
}
/**
* @return the IP
*/
public String getIP() {
return IP;
}
/**
* @param IP the IP to set
*/
public void setIP(String IP) {
this.IP = IP;
}
/**
* @return the user
*/
public String getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
}
/**
* @return the role
*/
public String getRole() {
return role;
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role = role;
}
}
...@@ -15,11 +15,8 @@ ...@@ -15,11 +15,8 @@
*/ */
package nl.uva.sne.drip.commons.types; package nl.uva.sne.drip.commons.types;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
//import javax.persistence.Column; //import javax.persistence.Column;
//import javax.persistence.Entity; //import javax.persistence.Entity;
//import javax.persistence.GeneratedValue; //import javax.persistence.GeneratedValue;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package nl.uva.sne.drip.commons.types; package nl.uva.sne.drip.commons.types;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
...@@ -36,9 +37,7 @@ public class ProvisionInfo { ...@@ -36,9 +37,7 @@ public class ProvisionInfo {
private String userKeyID; private String userKeyID;
private Map<String, Object> kvMap; private Map<String, Object> kvMap;
private String deployIP; private List<DeployParameter> deployParameters;
private String deployUser;
private String deployRole;
/** /**
* @return the cloudConfID * @return the cloudConfID
...@@ -124,40 +123,11 @@ public class ProvisionInfo { ...@@ -124,40 +123,11 @@ public class ProvisionInfo {
this.kvMap = kvMap; this.kvMap = kvMap;
} }
public void setDeployIP(String deployIP) { public void setDeployParameters(List<DeployParameter> deployParameters) {
this.deployIP = deployIP; this.deployParameters = deployParameters;
} }
public void setDeployRole(String deployRole) { public List<DeployParameter> getDeployParameters() {
this.deployRole = deployRole; return deployParameters;
} }
/**
* @return the deployIP
*/
public String getDeployIP() {
return deployIP;
}
/**
* @return the deployRole
*/
public String getDeployRole() {
return deployRole;
}
/**
* @return the deployUser
*/
public String getDeployUser() {
return deployUser;
}
/**
* @param deployUser the deployUser to set
*/
public void setDeployUser(String deployUser) {
this.deployUser = deployUser;
}
} }
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAm6AALYxkJFNzD3bfVJ4+hMY5j0/kqM9CURLKXMlYuAysnvoG8wZKx9Bedefm
eNSse4zTg798ZA2kDMZFIrwp1AseTwtj8DDu5fhG5DjyI3g6iJltS5zFQdMXneDlHXBX8cncSzNY
Rx0NdjEMAe7YttvI8FNlxL0VnMFli/HB/ftzYMe5+AmkSROncVGHiwoiUpj+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CgYEAx74O8JleXaHpxEAmh4h7VbLmJ3mOylfBmOdzcHeedJQw
ack2SAv65WBI9S9MEQ7J/vFuyw5HNk3C/mcWgzDQXSNIhHLvl/Z9sux/Qpm3SQWLz0RBxKV3dJ4r
wcAxzVA93+/L1Nee+VOKnlyRumvVa6+XLsLagpap2AVcTqlerMcCgYEAx3T2pXtqkCE9eU/ov22r
pdaKjgHoGOUg1CMEfWi/Ch6sYIIRyrHz6dhy+yR1pXNgPbLWdrn8l88F3+IsmbaMupMgRmqwEC3G
9Y2FglGIVvRdZaagvRxLzRCcvcN4v6OYs9ST4o1xlv7Qxphld+0XDKv7VSCv/rASuK8BqlFL3E8C
gYArMXJRnRjG7qh6g9TRIjZphdI3XxX9s5Rt2D8iZvuhAhqmBZjzY4PR7kxYmO2+EpCjzNnEl0XW
/GHaWbiIjhnAykx4N9KP7gGom3O5lzwHUme1XnFKcO2wDjQwJbufRmba8iQF1srN577mF+Z7ha4V
J1duCTzvWF1KFX6sk/uhKQKBgAcDFai7rgNjJ8YcCRKxyFcMM9LKPl6hr4XFtWKzTAQPEABUkkuN
9gVClsg9f+VRKRECOIf0Ae1UWeCFEwxUXp4wjfHrzkTDVztKvmbWdvSXorDwKrZ7SC7tZpVFSfly
xuuLjadpUZT9YFmbAfY1X5oSccOMYqORjRbxEB3svb4BAoGAGTgFuq9Zojh/KIqY8b4HpEfmh6CQ
hLVfD98Nqd6GDbxgvIM0v4mFXE92x2jn35Ia0JdFyh3B8Vkl7sqQZfxDFXI9O9pte2mPJxY9ICaY
55+X/SN1pd53BH+gaPZJy/R+Vpvs5MN48howjUKy5UKpoFeUWrS5QArjtvNCm4SGlXw=
-----END RSA PRIVATE KEY-----
...@@ -7,7 +7,7 @@ import os ...@@ -7,7 +7,7 @@ import os
from vm_info import VmInfo from vm_info import VmInfo
import docker_kubernetes import docker_kubernetes
connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.2')) connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.3'))
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue='deployer_queue') channel.queue_declare(queue='deployer_queue')
...@@ -43,7 +43,9 @@ def on_request(ch, method, props, body): ...@@ -43,7 +43,9 @@ def on_request(ch, method, props, body):
kuber_file.close() kuber_file.close()
response = {} response = {}
response["creationDate"] = 1487002029722 outcontent = {}
current_milli_time = lambda: int(round(time.time() * 1000))
response["creationDate"] = current_milli_time()
response["parameters"] = [] response["parameters"] = []
par = {} par = {}
par["url"] = "null" par["url"] = "null"
......
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