Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: duplicate node / new node from existing #312

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions nodel-framework/src/main/java/org/nodel/io/HTTPDownload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.nodel.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

public class HTTPDownload {
/**
* Downloads a file from a given URL to a folder, using tmp files to ensure integrity.
*/
public static void downloadFile(String[] fileURL, File folder) {
try {
// successfully avoided regex
URL URL = new URL(fileURL[0] + fileURL[1]);
String fileName = fileURL[1];
ReadableByteChannel readChannel = Channels.newChannel(URL.openStream());
// to ensure integrity, use a temporary file (renaming it after success)

File tmpFile = new File(folder, fileName + ".download");
// make sure all directories are created
tmpFile.getParentFile().mkdirs();
tmpFile.createNewFile();

File finalOutFile = new File(folder, fileName);
FileOutputStream fileStream = new FileOutputStream(tmpFile);

FileChannel writeChannel = fileStream.getChannel();
writeChannel.transferFrom(readChannel, 0, Long.MAX_VALUE);
if (finalOutFile.exists()) {
if (!finalOutFile.delete())
fileStream.close();
readChannel.close();
writeChannel.close();
throw new IOException("Could not delete destination file.");
}

fileStream.close();
readChannel.close();
writeChannel.close();
// rename the temp file now
if (!tmpFile.renameTo(finalOutFile)){
throw new IOException("Could not rename temporary.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
70 changes: 70 additions & 0 deletions nodel-jyhost/src/main/java/org/nodel/jyhost/NodelHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
Expand Down Expand Up @@ -38,7 +39,12 @@
import org.nodel.discovery.TopologyWatcher;
import org.nodel.host.BaseNode;
import org.nodel.io.Files;
import org.nodel.io.Stream;
import org.nodel.io.HTTPDownload;
import org.nodel.io.UTF8Charset;
import org.nodel.json.JSONArray;
import org.nodel.json.JSONException;
import org.nodel.json.JSONObject;
import org.nodel.reflection.Reflection;
import org.nodel.reflection.Serialisation;
import org.nodel.rest.REST;
Expand Down Expand Up @@ -523,6 +529,70 @@ public void newNode(String base, SimpleName name) {
}
}

public void newNodeFromExisting(String existingNodeURL, String existingNodeFiles, SimpleName name) throws JSONException, IOException {
testNameFilters(name);

String safeFilename = encodeIntoSafeFilename(name);
String safeTmpFilename = "_tmp_" + encodeIntoSafeFilename(name);

File tmpNodeDir = new File(_root, safeTmpFilename);
File finalNodeDir = new File(_root, safeFilename);

if (_nodeMap.containsKey(name) || tmpNodeDir.exists())
throw new RuntimeException("A node with the name '" + name + "' already exists.");

if (!tmpNodeDir.mkdir())
throw new RuntimeException("The platform did not allow the creation of the node folder for unspecified reasons (security issues?).");

// nodetoolkit.py isn't copied across. it's created for us but only after throwing an exception
// may as well just stream it to a new file to avoid the exception.
try (InputStream nodetoolkitStream = PyNode.class.getResourceAsStream("nodetoolkit.py")) {
String nodetoolkitString = Stream.readFully(nodetoolkitStream);
File nodetoolkit = new File(_root, ".nodel/nodetoolkit.py");
nodetoolkit.getParentFile().mkdirs();
Stream.writeFully(nodetoolkit, nodetoolkitString);
}

JSONArray jsonArray = new JSONArray(existingNodeFiles);

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String path = jsonobject.getString("path");
// seperate URL into array so I dont have to write regex
String[] fileURL = new String[2];
fileURL[0] = existingNodeURL + "REST/files/contents?path=";
fileURL[1] = path;

HTTPDownload.downloadFile(fileURL,tmpNodeDir);
}
tmpNodeDir.renameTo(finalNodeDir);

}

/**
* Creates a new node based on existing node.
*/
public void duplicateNode(BaseNode existing, SimpleName name) {
testNameFilters(name);

File newNodeDir = new File(_root, encodeIntoSafeFilename(name));

if (_nodeMap.containsKey(name) || newNodeDir.exists())
throw new RuntimeException("A node with the name '" + name + "' already exists.");

if (existing == null) {
// we need an existing node to copy from.
throw new RuntimeException("No Existing Node Given.");

} else {
// based on an existing node (from recipes folder or self nodes)
File root = existing.getRoot();

// copy the entire folder
Files.copyDir(root, newNodeDir);
}
}

/**
* Same as 'shouldBeIncluded' but throws exception with naming conflict error details.
*/
Expand Down
14 changes: 13 additions & 1 deletion nodel-jyhost/src/main/java/org/nodel/jyhost/NodelHostHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.nodel.io.Stream;
import org.nodel.io.UTF8Charset;
import org.nodel.json.XML;
import org.nodel.json.JSONException;
import org.nodel.logging.LogEntry;
import org.nodel.logging.Logging;
import org.nodel.reflection.Param;
Expand Down Expand Up @@ -163,7 +164,18 @@ public Diagnostics framework() {
public void newNode(@Param(name = "base") String base, SimpleName name) {
_nodelHost.newNode(base, name);
}

/**
* Duplicate the node based on existing node.
*/
@Service(name = "newNodeFromExisting", title = "New Node From Existing", desc = "Creates a node by duplicating an existing one.")
public void newNodeFromExisting(@Param(name = "existingNodeURL") String existingNodeURL,
@Param(name = "existingNodeFiles") String existingNodeFiles,
SimpleName name) throws JSONException, IOException {
_nodelHost.newNodeFromExisting(existingNodeURL, existingNodeFiles, name);

}


@Service(name = "toolkit", title = "Toolkit", desc = "The toolkit reference.")
public Info getToolkitReference() throws IOException {
try (InputStream nodetoolkitStream = PyNode.class.getResourceAsStream("nodetoolkit.py")) {
Expand Down
8 changes: 8 additions & 0 deletions nodel-jyhost/src/main/java/org/nodel/jyhost/PyNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,14 @@ public void rename(SimpleName newName) {
_logger.info("This node has been renamed. It will close down and restart under a new name shortly.");
}

/**
* Duplicate the node.
*/
@Service(name = "duplicate", title = "Duplicate", desc = "Duplicates a node.")
public void duplicate(SimpleName newNode) {
_nodelHost.duplicateNode(this, newNode);
}

/**
* Renames the node.
*/
Expand Down
27 changes: 27 additions & 0 deletions nodel-webui-js/src/index.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@
<button class="btn btn-default renamenodesubmit">Rename</button>
</div>
</li>
<li class="form">
<div>
<button class="btn btn-info duplicatenodesubmit">Duplicate</button>
</div>
</li>
<li class="form">
<div class="checkbox">
<label>
Expand Down Expand Up @@ -284,6 +289,28 @@
</div>
</div>
<!-- end offline modal -->
<!-- duplicate modal -->
<div class="modal" id="duplicate" tabindex="-1" role="dialog" aria-labelledby="duplicate" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&#215;</button>
<h4 class="modal-title" id="Duplicatelabel">Duplicate Node</h4>
</div>
<div class="modal-body">
<div class="form">
<p>New Node Name:</p>
<input id="duplicateNodeval" class="form-control duplicatenodeval" type="text" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button id="confirmDuplicate" class="btn btn-success btn-ok">Duplicate Node</button>
</div>
</div>
</div>
</div>
<!-- end offline modal -->
<!-- alert -->
<div class="alert collapse alert-floating alert-info">
<div class="message"></div>
Expand Down
85 changes: 83 additions & 2 deletions nodel-webui-js/src/nodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,10 @@ var createDynamicElements = function(){
$.templates("#localsTmpl").link(ele, localsList);
$(ele).find('.base').addClass('bound');
d.resolve();
} else if($(ele).data('nodel') == 'locals'){
$.templates("#localsTmpl").link(ele, localsList);
$(ele).find('.base').addClass('bound');
d.resolve();
} else d.resolve();
p.push(d);
});
Expand Down Expand Up @@ -1470,7 +1474,9 @@ var setEvents = function(){
$.each(data, function(key, value) {
var re = new RegExp("(.*)("+srchflt+")(.*)","ig");
var val = value.node.replace(re, '$1<strong>$2</strong>$3')
$('<li>'+val+'</li>').data('address', value.address).appendTo(list);
// stitched on the node name for the 'create node from existing' function, sorry! -Troy
$('<li>'+val+'</li>').data('node', value.node).data('address', value.address).appendTo(list);

return key < 20;
});
} else $(ele).siblings('div.autocomplete').remove();
Expand Down Expand Up @@ -1561,7 +1567,17 @@ var setEvents = function(){
$('body').on('mousedown touchstart', 'div.autocomplete ul li', function() {
if($(this).closest('div.autocomplete').siblings('input').hasClass('goto')) {
window.open($(this).data()['address']);
} else {
}
// for create node based on existing - autocomplete list isnt created dynamically!
// this means $.view(this).data is undefined, rather than make this element dynamic or make it populate data
// some other way, i've chosen to break the observer pattern, make the autocomplete entries also include node name (see nodel.js:1478),
// and then set the value of the text box dumbly - Troy
else if($(this).closest('div.autocomplete').siblings('input').hasClass('existnodenamval')){
var data = $(this).data();
$(this).closest('div.autocomplete').siblings('input').prop('value', data.node)
$(this).closest('div.autocomplete').siblings('input').prop('nodeURL', data.address)
}
else {
var data = $.view(this).data;
var fld = $(this).closest('div.autocomplete').siblings('input').data('link');
if(!fld) fld = '_$filldown';
Expand Down Expand Up @@ -1787,12 +1803,14 @@ var setEvents = function(){
$(this).find('.scriptnamval').val(null);
$(this).data('filedata', null);
});

$('body').on('shown.bs.dropdown', '.srchgrp', function () {
$(this).find('.node').val(null).get(0).focus();
});
$('body').on('shown.bs.dropdown', '.edtgrp', function () {
$(this).find('.renamenode').val(nodename).get(0).focus();
});

$('body').on('keyup', '.renamenode', function(e) {
var charCode = e.charCode || e.keyCode;
if(charCode == 13) $(this).closest('.form').find('.renamenodesubmit').click();
Expand All @@ -1813,6 +1831,64 @@ var setEvents = function(){
}
}
});
// Duplicate button
$('body').on('click', '.duplicatenodesubmit', function (e) {
$('#duplicate').modal('show');
});
$('body').on('shown.bs.modal', '#duplicate', function () {
$(this).find('.duplicatenodeval').get(0).focus();
});
// Duplicate Node
$('body').on('click', '#confirmDuplicate', function (e) {
var nodenameraw = $('#duplicateNodeval').val();
if(nodenameraw) {
var nodename = {"value": nodenameraw};
$.postJSON('REST/duplicate', JSON.stringify(nodename), function() {
checkRedirect(proto+'//' + host + '/nodes/' + encodeURIComponent(getVerySimpleName(nodenameraw)));
}).fail(function(req){
if(req.statusText!="abort"){
var error = 'Node duplicate failed';
if(req.responseText) {
var message = JSON.parse(req.responseText);
error = error + '<br/>' + message['message'];
}
alert(error, 'danger');
}
});
}
});
// Duplicate Node from Existing
$('body').on('click', '#confirmDuplicateExisting', function (e) {
var nodenameraw = $('#duplinodenamval_').val();
if(nodenameraw) {
var nodename = {"value": nodenameraw};
nodename["existingNodeURL"] = $('#existnodenamval_').prop("nodeURL")
nodeFiles = $.getJSON(nodename["existingNodeURL"] + 'REST/files', function(data) {
if (data.length > 0) {
nodename["existingNodeFiles"] = JSON.stringify(data) //double stringify for java
$.postJSON('REST/newNodeFromExisting', JSON.stringify(nodename), function() {
checkRedirect(proto+'//' + host + '/nodes/' + encodeURIComponent(getVerySimpleName(nodenameraw)));
}).fail(function(req){
if(req.statusText!="abort"){
var error = 'Node duplicate failed';
if(req.responseText) {
var message = JSON.parse(req.responseText);
error = error + '<br/>' + message['message'];
}
alert(error, 'danger');
}
});
}
// console.log(data)
});

}
});
$('body').on('keyup', '.duplicatenodeval', function(e) {
var charCode = e.charCode || e.keyCode;
if(charCode == 13) $('#confirmDuplicate').click();
});

$('body').on('click', '.restartnodesubmit', function (e) {
// Relative path : $.get(proto+'//' + host + '/nodes/' + encodeURIComponent(node) + '/REST/restart', function (data) {
$.get('REST/restart', function (data) {
Expand All @@ -1839,6 +1915,10 @@ var setEvents = function(){
$(ele).find('.recipepicker').empty();
$(ele).find('.nodenamval').focus();
$(ele).find('.nodenamval').val(null).get(0).focus();
// clear duplicate fields
$(ele).find('.existnodenamval').empty();
$(ele).find('.duplinodenamval').focus();
$(ele).find('.duplinodenamval').val(null).get(0).focus();
$.getJSON(proto+'//' + host + '/REST/recipes/list', function(data) {
if (data.length > 0) {
var picker = $(ele).find('.recipepicker');
Expand All @@ -1857,6 +1937,7 @@ var setEvents = function(){
});
//return false;
});

$('body').on('keyup', '.nodenamval', function(e) {
var charCode = e.charCode || e.keyCode;
if(charCode == 13) $(this).closest('form').find('.nodeaddsubmit').click();
Expand Down
Loading