Commit 201f5ade authored by Spiros Koulouzis's avatar Spiros Koulouzis

implement caller for new provisioner

parent 1035ba0c
...@@ -20,6 +20,13 @@ ...@@ -20,6 +20,13 @@
<artifactId>drip-provisioning-core</artifactId> <artifactId>drip-provisioning-core</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip-provisioning-agent</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
......
...@@ -19,6 +19,7 @@ import com.rabbitmq.client.AMQP; ...@@ -19,6 +19,7 @@ import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -34,7 +35,7 @@ import java.util.logging.Logger; ...@@ -34,7 +35,7 @@ import java.util.logging.Logger;
*/ */
public class RPCServer { public class RPCServer {
private static final String RPC_QUEUE_NAME = "provisioner_queue"; private static String RPC_QUEUE_NAME = "provisioner_queue_v1";
private static String HOST = "127.0.0.1"; private static String HOST = "127.0.0.1";
public static void main(String[] argv) { public static void main(String[] argv) {
...@@ -55,7 +56,9 @@ public class RPCServer { ...@@ -55,7 +56,9 @@ public class RPCServer {
} }
} }
HOST = prop.getProperty("rabbitmq.host", "127.0.0.1"); HOST = prop.getProperty("rabbitmq.host", "127.0.0.1");
RPC_QUEUE_NAME = prop.getProperty("rpc.queue.name", "provisioner_queue_v1");
Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, MessageFormat.format("rabbitmq.host: {0}", HOST)); Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, MessageFormat.format("rabbitmq.host: {0}", HOST));
Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, MessageFormat.format("rpc.queue.name: {0}", RPC_QUEUE_NAME));
start(); start();
} }
...@@ -70,8 +73,13 @@ public class RPCServer { ...@@ -70,8 +73,13 @@ public class RPCServer {
Channel channel = connection.createChannel(); Channel channel = connection.createChannel();
//We define the queue name //We define the queue name
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
//Set our own customized consummer DefaultConsumer c;
Consumer c = new Consumer(channel); if (RPC_QUEUE_NAME.endsWith("v0")) {
c = new nl.uva.sne.drip.drip.provisioner.v0.Consumer(channel);
} else {
c = new nl.uva.sne.drip.drip.provisioner.v1.Consumer(channel);
}
//Start listening for messages //Start listening for messages
channel.basicConsume(RPC_QUEUE_NAME, false, c); channel.basicConsume(RPC_QUEUE_NAME, false, c);
......
/*
* 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.drip.provisioner.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author S. Koulouzis
*/
public class MessageParsing {
public static File getTopology(JSONArray parameters, String tempInputDirPath, int level) throws JSONException, IOException {
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get("name");
if (name.equals("topology")) {
JSONObject attributes = param.getJSONObject("attributes");
int fileLevel = Integer.valueOf((String) attributes.get("level"));
if (fileLevel == level) {
String originalFilename = (String) attributes.get("filename");
String fileName = "";
// String[] parts = originalFilename.split("_");
// String prefix = "";
// //Clear date part form file name
// if (isNumeric(parts[0])) {
// for (int j = 1; j < parts.length; j++) {
// fileName += prefix + parts[j];
// prefix = "_";
// }
// } else {
fileName = originalFilename;
// }
File topologyFile = new File(tempInputDirPath + File.separator + fileName);
topologyFile.createNewFile();
String val = (String) param.get("value");
writeValueToFile(val, topologyFile);
return topologyFile;
}
}
}
return null;
}
public static void writeValueToFile(String value, File file) throws FileNotFoundException {
try (PrintWriter out = new PrintWriter(file)) {
out.print(value);
}
if (!file.exists() || file.length() < value.getBytes().length) {
throw new FileNotFoundException("File " + file.getAbsolutePath() + " doesn't exist or contents are missing ");
}
}
}
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package nl.uva.sne.drip.drip.provisioner; package nl.uva.sne.drip.drip.provisioner.v0;
import com.rabbitmq.client.AMQP; import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
...@@ -27,7 +27,6 @@ import java.io.FileOutputStream; ...@@ -27,7 +27,6 @@ import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermission;
...@@ -40,6 +39,7 @@ import java.util.Properties; ...@@ -40,6 +39,7 @@ import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import nl.uva.sne.drip.drip.provisioner.utils.MessageParsing;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.json.JSONArray; import org.json.JSONArray;
...@@ -161,12 +161,12 @@ public class Consumer extends DefaultConsumer { ...@@ -161,12 +161,12 @@ public class Consumer extends DefaultConsumer {
geniConfFile = cloudConfFile; geniConfFile = cloudConfFile;
} }
File topologyFile = getTopology(parameters, tempInputDirPath, 0); File topologyFile = MessageParsing.getTopology(parameters, tempInputDirPath, 0);
File mainTopologyFile = new File(tempInputDirPath + "topology_main"); File mainTopologyFile = new File(tempInputDirPath + "topology_main");
FileUtils.moveFile(topologyFile, mainTopologyFile); FileUtils.moveFile(topologyFile, mainTopologyFile);
mainTopologyPath = mainTopologyFile.getAbsolutePath(); mainTopologyPath = mainTopologyFile.getAbsolutePath();
topologyFile = getTopology(parameters, tempInputDirPath, 1); topologyFile = MessageParsing.getTopology(parameters, tempInputDirPath, 1);
File secondaryTopologyFile = new File(tempInputDirPath + File.separator + topologyFile.getName() + ".yml"); File secondaryTopologyFile = new File(tempInputDirPath + File.separator + topologyFile.getName() + ".yml");
String outputFilePath = tempInputDirPath + File.separator + topologyFile.getName() + "_provisioned.yml"; String outputFilePath = tempInputDirPath + File.separator + topologyFile.getName() + "_provisioned.yml";
TopologyElement x = new TopologyElement(); TopologyElement x = new TopologyElement();
...@@ -365,15 +365,6 @@ public class Consumer extends DefaultConsumer { ...@@ -365,15 +365,6 @@ public class Consumer extends DefaultConsumer {
return jo.toString(); return jo.toString();
} }
private void writeValueToFile(String value, File file) throws FileNotFoundException {
try (PrintWriter out = new PrintWriter(file)) {
out.print(value);
}
if (!file.exists() || file.length() < value.getBytes().length) {
throw new FileNotFoundException("File " + file.getAbsolutePath() + " doesn't exist or contents are missing ");
}
}
private File getCloudConfigurationFile(JSONArray parameters, String tempInputDirPath) throws JSONException { private File getCloudConfigurationFile(JSONArray parameters, String tempInputDirPath) throws JSONException {
for (int i = 0; i < parameters.length(); i++) { for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i); JSONObject param = (JSONObject) parameters.get(i);
...@@ -382,7 +373,7 @@ public class Consumer extends DefaultConsumer { ...@@ -382,7 +373,7 @@ public class Consumer extends DefaultConsumer {
try { try {
File confFile = new File(tempInputDirPath + File.separator + name); File confFile = new File(tempInputDirPath + File.separator + name);
if (confFile.createNewFile()) { if (confFile.createNewFile()) {
writeValueToFile((String) param.get("value"), confFile); MessageParsing.writeValueToFile((String) param.get("value"), confFile);
return confFile; return confFile;
} else { } else {
return null; return null;
...@@ -396,39 +387,6 @@ public class Consumer extends DefaultConsumer { ...@@ -396,39 +387,6 @@ public class Consumer extends DefaultConsumer {
return null; return null;
} }
private File getTopology(JSONArray parameters, String tempInputDirPath, int level) throws JSONException, IOException {
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = (JSONObject) parameters.get(i);
String name = (String) param.get("name");
if (name.equals("topology")) {
JSONObject attributes = param.getJSONObject("attributes");
int fileLevel = Integer.valueOf((String) attributes.get("level"));
if (fileLevel == level) {
String originalFilename = (String) attributes.get("filename");
String fileName = "";
// String[] parts = originalFilename.split("_");
// String prefix = "";
// //Clear date part form file name
// if (isNumeric(parts[0])) {
// for (int j = 1; j < parts.length; j++) {
// fileName += prefix + parts[j];
// prefix = "_";
// }
// } else {
fileName = originalFilename;
// }
File topologyFile = new File(tempInputDirPath + File.separator + fileName);
topologyFile.createNewFile();
String val = (String) param.get("value");
writeValueToFile(val, topologyFile);
return topologyFile;
}
}
}
return null;
}
private Map<String, File> getCertificates(JSONArray parameters, String tempInputDirPath) throws JSONException, IOException { private Map<String, File> getCertificates(JSONArray parameters, String tempInputDirPath) throws JSONException, IOException {
Map<String, File> files = new HashMap<>(); Map<String, File> files = new HashMap<>();
for (int i = 0; i < parameters.length(); i++) { for (int i = 0; i < parameters.length(); i++) {
...@@ -439,7 +397,7 @@ public class Consumer extends DefaultConsumer { ...@@ -439,7 +397,7 @@ public class Consumer extends DefaultConsumer {
String fileName = (String) attribute.get("filename"); String fileName = (String) attribute.get("filename");
File certificate = new File(tempInputDirPath + File.separator + fileName + ".pem"); File certificate = new File(tempInputDirPath + File.separator + fileName + ".pem");
if (certificate.createNewFile()) { if (certificate.createNewFile()) {
writeValueToFile((String) param.get("value"), certificate); MessageParsing.writeValueToFile((String) param.get("value"), certificate);
Set<PosixFilePermission> perms = new HashSet<>(); Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_READ);
Files.setPosixFilePermissions(Paths.get(certificate.getAbsolutePath()), perms); Files.setPosixFilePermissions(Paths.get(certificate.getAbsolutePath()), perms);
...@@ -469,7 +427,7 @@ public class Consumer extends DefaultConsumer { ...@@ -469,7 +427,7 @@ public class Consumer extends DefaultConsumer {
String sshKeyContent = (String) param.get("value"); String sshKeyContent = (String) param.get("value");
File sshKeyFile = new File(tempInputDirPath + File.separator + "user.pem"); File sshKeyFile = new File(tempInputDirPath + File.separator + "user.pem");
if (sshKeyFile.createNewFile()) { if (sshKeyFile.createNewFile()) {
writeValueToFile(sshKeyContent, sshKeyFile); MessageParsing.writeValueToFile(sshKeyContent, sshKeyFile);
return sshKeyFile; return sshKeyFile;
} }
} }
...@@ -485,7 +443,7 @@ public class Consumer extends DefaultConsumer { ...@@ -485,7 +443,7 @@ public class Consumer extends DefaultConsumer {
String scriptContent = (String) param.get("value"); String scriptContent = (String) param.get("value");
File scriptFile = new File(tempInputDirPath + File.separator + "guiscipt.sh"); File scriptFile = new File(tempInputDirPath + File.separator + "guiscipt.sh");
if (scriptFile.createNewFile()) { if (scriptFile.createNewFile()) {
writeValueToFile(scriptContent, scriptFile); MessageParsing.writeValueToFile(scriptContent, scriptFile);
return scriptFile; return scriptFile;
} }
} }
...@@ -493,8 +451,4 @@ public class Consumer extends DefaultConsumer { ...@@ -493,8 +451,4 @@ public class Consumer extends DefaultConsumer {
return null; return null;
} }
private static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
} }
rabbitmq.host=127.0.0.1 rabbitmq.host=127.0.0.1
rpc.queue.name=provisioner_queue_v1
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