summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2014-06-27 21:24:27 +0200
committerSimon Rettberg2014-06-27 21:24:27 +0200
commit5b4c21b01150153d27da941ae8be017359125c5d (patch)
tree42fc89e26f0bb279f43654fee2fa9877d20b9fa8
parentFix concurrent modification issue (diff)
downloadtmlite-bwlp-5b4c21b01150153d27da941ae8be017359125c5d.tar.gz
tmlite-bwlp-5b4c21b01150153d27da941ae8be017359125c5d.tar.xz
tmlite-bwlp-5b4c21b01150153d27da941ae8be017359125c5d.zip
New Tasks
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/DiskStat.java98
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java3
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/MoveFile.java63
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/Reboot.java62
4 files changed, 225 insertions, 1 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DiskStat.java b/src/main/java/org/openslx/taskmanager/tasks/DiskStat.java
new file mode 100644
index 0000000..4d2caa5
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/DiskStat.java
@@ -0,0 +1,98 @@
+package org.openslx.taskmanager.tasks;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.openslx.taskmanager.api.SystemCommandTask;
+
+public class DiskStat extends SystemCommandTask
+{
+
+ private static final Pattern dfLine = Pattern.compile( "^(.*\\S)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(/.*)$" );
+
+ private final Output status = new Output();
+ private final List<Output.Entry> list = new ArrayList<>();
+
+ @Override
+ protected String[] initCommandLine()
+ {
+ return new String[] {
+ "/bin/df",
+ "-P", "-B", "1024"
+ };
+ }
+
+ @Override
+ protected boolean processEnded( int exitCode )
+ {
+ if ( exitCode != 0 && list.isEmpty() ) {
+ status.error = "df returned exit code " + exitCode;
+ return false;
+ }
+ status.list = list;
+ return true;
+ }
+
+ @Override
+ protected void processStdOut( String line )
+ {
+ Matcher matcher = dfLine.matcher( line );
+ while ( matcher.find() ) {
+ try {
+ list.add( new Output.Entry(
+ matcher.group( 1 ),
+ matcher.group( 6 ),
+ Long.parseLong( matcher.group( 2 ) ),
+ Long.parseLong( matcher.group( 3 ) ),
+ Long.parseLong( matcher.group( 4 ) ),
+ Integer.parseInt( matcher.group( 5 ) ) )
+ );
+ } catch ( Exception e ) {
+ // Silently skip line, can't parse....
+ }
+ }
+ }
+
+ @Override
+ protected void processStdErr( String line )
+ {
+ status.error = line;
+ }
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( status );
+ return true;
+ }
+
+ /**
+ * Output - contains additional status data of this task
+ */
+ @SuppressWarnings( "unused" )
+ private static class Output
+ {
+ protected String error = null;
+ protected List<Entry> list = null;
+
+ public static class Entry
+ {
+ protected final String fileSystem, mountPoint;
+ protected final long sizeKb, usedKb, freeKb;
+ protected final int usedPercent;
+
+ public Entry( String fileSystem, String mountPoint, long sizeKb, long usedKb, long freeKb, int usedPercent )
+ {
+ this.fileSystem = fileSystem;
+ this.mountPoint = mountPoint;
+ this.sizeKb = sizeKb;
+ this.usedKb = usedKb;
+ this.freeKb = freeKb;
+ this.usedPercent = usedPercent;
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java b/src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java
index a77387d..893209e 100644
--- a/src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java
+++ b/src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java
@@ -39,7 +39,8 @@ public class MountVmStore extends SystemCommandTask
return new String[] {
"/usr/bin/sudo",
"-n",
- "-u", "root", Constants.BASEDIR + "/scripts/mount-store",
+ "-u", "root",
+ Constants.BASEDIR + "/scripts/mount-store",
"images",
this.address,
this.username,
diff --git a/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java b/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java
new file mode 100644
index 0000000..f783019
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/MoveFile.java
@@ -0,0 +1,63 @@
+package org.openslx.taskmanager.tasks;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import org.apache.commons.io.FilenameUtils;
+import org.openslx.satserver.util.Util;
+import org.openslx.taskmanager.api.AbstractTask;
+
+import com.google.gson.annotations.Expose;
+
+public class MoveFile extends AbstractTask
+{
+
+ protected static final String[] ALLOWED_DIRS =
+ { "/tmp/", "/opt/openslx/configs/" };
+
+ @Expose
+ private String source = null;
+ @Expose
+ private String destination = null;
+
+ private Output status = new Output();
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( status );
+ this.source = FilenameUtils.normalize( this.source );
+ this.destination = FilenameUtils.normalize( this.destination );
+ if ( this.source == null || !Util.startsWith( this.source, ALLOWED_DIRS ) ) {
+ status.error = "Source file not in allowed directory";
+ return false;
+ }
+ if ( this.destination == null || !Util.startsWith( this.destination, ALLOWED_DIRS ) ) {
+ status.error = "Destination file not in allowed directory";
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean execute()
+ {
+ try {
+ Files.move( Paths.get( this.source ), Paths.get( this.destination ) );
+ } catch ( Exception e1 ) {
+ status.error = e1.toString();
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Output - contains additional status data of this task
+ */
+ @SuppressWarnings( "unused" )
+ private static class Output
+ {
+ protected String error = null;
+ }
+
+}
diff --git a/src/main/java/org/openslx/taskmanager/tasks/Reboot.java b/src/main/java/org/openslx/taskmanager/tasks/Reboot.java
new file mode 100644
index 0000000..c90289e
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/Reboot.java
@@ -0,0 +1,62 @@
+package org.openslx.taskmanager.tasks;
+
+import org.openslx.taskmanager.api.SystemCommandTask;
+
+public class Reboot extends SystemCommandTask
+{
+ private final Output status = new Output();
+
+ @Override
+ protected String[] initCommandLine()
+ {
+ return new String[] {
+ "/usr/bin/sudo",
+ "-n",
+ "-u", "root",
+ "/sbin/reboot"
+ };
+ }
+
+ @Override
+ protected boolean processEnded( int exitCode )
+ {
+ return exitCode == 0;
+ }
+
+ @Override
+ protected void processStdOut( String line )
+ {
+ status.addMessage( line );
+ }
+
+ @Override
+ protected void processStdErr( String line )
+ {
+ status.addMessage( line );
+ }
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( status );
+ return true;
+ }
+
+ /**
+ * Output - contains additional status data of this task
+ */
+ class Output
+ {
+ private String messages = null;
+
+ private void addMessage( String str )
+ {
+ if ( messages == null ) {
+ messages = str;
+ } else {
+ messages += "\n" + str;
+ }
+ }
+ }
+
+}