From 5b4c21b01150153d27da941ae8be017359125c5d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 27 Jun 2014 21:24:27 +0200 Subject: New Tasks --- .../org/openslx/taskmanager/tasks/DiskStat.java | 98 ++++++++++++++++++++++ .../openslx/taskmanager/tasks/MountVmStore.java | 3 +- .../org/openslx/taskmanager/tasks/MoveFile.java | 63 ++++++++++++++ .../java/org/openslx/taskmanager/tasks/Reboot.java | 62 ++++++++++++++ 4 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/openslx/taskmanager/tasks/DiskStat.java create mode 100644 src/main/java/org/openslx/taskmanager/tasks/MoveFile.java create mode 100644 src/main/java/org/openslx/taskmanager/tasks/Reboot.java (limited to 'src/main/java') 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 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 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; + } + } + } + +} -- cgit v1.2.3-55-g7522