summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager
diff options
context:
space:
mode:
authorSimon Rettberg2023-07-03 18:16:09 +0200
committerSimon Rettberg2023-07-03 18:16:09 +0200
commitf59d25e65344059e25a486523d55ff6487ed3d31 (patch)
treed013ffcb49435b443637597ec03dfd1c30d6e0f3 /src/main/java/org/openslx/taskmanager
parent[IrcClient] Silence protocol log spam (diff)
downloadtmlite-bwlp-f59d25e65344059e25a486523d55ff6487ed3d31.tar.gz
tmlite-bwlp-f59d25e65344059e25a486523d55ff6487ed3d31.tar.xz
tmlite-bwlp-f59d25e65344059e25a486523d55ff6487ed3d31.zip
Add tasks for apt update and upgrade
Diffstat (limited to 'src/main/java/org/openslx/taskmanager')
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/AptGetUpgradable.java98
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/AptUpgrade.java91
2 files changed, 189 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/AptGetUpgradable.java b/src/main/java/org/openslx/taskmanager/tasks/AptGetUpgradable.java
new file mode 100644
index 0000000..8c34143
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/AptGetUpgradable.java
@@ -0,0 +1,98 @@
+package org.openslx.taskmanager.tasks;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.openslx.satserver.util.Util;
+import org.openslx.taskmanager.api.SystemCommandTask;
+
+public class AptGetUpgradable extends SystemCommandTask
+{
+
+ private static final Pattern REGEX = Pattern.compile( "^(\\S+)/(\\S+)\\s+(\\S+)\\s.*\\[upgrad.*from:\\s+(\\S+)[,\\]]" );
+
+ private final Output status = new Output();
+
+ private StringBuilder otherOutput = new StringBuilder();
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( status );
+ this.timeoutSeconds = 25;
+ return true;
+ }
+
+ @Override
+ protected String[] initCommandLine()
+ {
+ return new String[] { "apt", "list", "--upgradable" };
+ }
+
+ @Override
+ protected void initEnvironment( Map<String, String> environment )
+ {
+ environment.put( "LANG", "C.UTF-8" );
+ environment.put( "LC_ALL", "C.UTF-8" );
+ }
+
+ @Override
+ protected boolean processEnded( int exitCode )
+ {
+ if ( this.status.packages.isEmpty() && Util.isEmpty( this.status.error ) ) {
+ this.status.error = this.otherOutput.toString();
+ }
+ return exitCode == 0;
+ }
+
+ @Override
+ protected void processStdOut( String line )
+ {
+ Matcher m = REGEX.matcher( line );
+ if ( m.matches() ) {
+ status.packages.add( new Package( m.group( 1 ), m.group( 2 ), m.group( 4 ), m.group( 3 ) ) );
+ } else {
+ otherOutput.append( line );
+ otherOutput.append( '\n' );
+ }
+ }
+
+ @Override
+ protected void processStdErr( String line )
+ {
+ if ( line.isEmpty() || line.contains( "stable CLI interface" ) )
+ return;
+ if ( status.error == null ) {
+ status.error = line;
+ } else {
+ status.error += "\n" + line;
+ }
+ }
+
+ private static class Output
+ {
+ public final List<Package> packages = new ArrayList<>();
+ public String error;
+ }
+
+ @SuppressWarnings( "unused" )
+ private static class Package
+ {
+ public final String name;
+ public final String source;
+ public final String newVersion;
+ public final String oldVersion;
+
+ public Package( String name, String source, String oldVersion, String newVersion )
+ {
+ this.name = name;
+ this.source = source;
+ this.newVersion = newVersion;
+ this.oldVersion = oldVersion;
+ }
+ }
+
+}
diff --git a/src/main/java/org/openslx/taskmanager/tasks/AptUpgrade.java b/src/main/java/org/openslx/taskmanager/tasks/AptUpgrade.java
new file mode 100644
index 0000000..aabd54d
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/AptUpgrade.java
@@ -0,0 +1,91 @@
+package org.openslx.taskmanager.tasks;
+
+import java.util.Map;
+
+import org.openslx.taskmanager.api.SystemCommandTask;
+
+import com.google.gson.annotations.Expose;
+
+public class AptUpgrade extends SystemCommandTask
+{
+
+ private static enum Mode
+ {
+ UPDATE, UPGRADE, FULL_UPGRADE, FIX, AUTOREMOVE;
+ }
+
+ @Expose
+ private Mode mode;
+
+ private final Output status = new Output();
+
+ @Override
+ protected boolean initTask()
+ {
+ this.timeoutSeconds = 600;
+ this.setStatusObject( status );
+ if ( mode == null ) {
+ this.status.error = "Invalid mode selected";
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected String[] initCommandLine()
+ {
+ if ( mode == Mode.UPDATE )
+ return new String[] { "sudo", "-n", "apt-get", "-y", "update" };
+ if ( mode == Mode.UPGRADE )
+ return new String[] { "sudo", "-n", "apt-get", "-y", "upgrade" };
+ if ( mode == Mode.AUTOREMOVE )
+ return new String[] { "sudo", "-n", "apt-get", "-y", "autoremove" };
+ if ( mode == Mode.FULL_UPGRADE )
+ return new String[] { "sudo", "-n", "apt-get", "-y", "full-upgrade" };
+ if ( mode == Mode.FIX )
+ return new String[] { "sudo", "-n", "apt-get", "-y", "install", "-f" };
+ return null;
+ }
+
+ @Override
+ protected void initEnvironment( Map<String, String> environment )
+ {
+ environment.put( "LANG", "C.UTF-8" );
+ environment.put( "LC_ALL", "C.UTF-8" );
+ environment.put( "DEBIAN_FRONTEND", "noninteractive" );
+ }
+
+ @Override
+ protected boolean processEnded( int exitCode )
+ {
+ return exitCode == 0;
+ }
+
+ @Override
+ protected void processStdOut( String line )
+ {
+ this.status.output.append( line );
+ this.status.output.append( '\n' );
+ while ( this.status.output.length() > 20000 ) {
+ this.status.output.delete( 0, 1000 );
+ }
+ }
+
+ @Override
+ protected void processStdErr( String line )
+ {
+ if ( status.error == null ) {
+ status.error = line;
+ } else {
+ status.error += "\n" + line;
+ }
+ }
+
+ private static class Output
+ {
+ private String error;
+
+ private StringBuilder output = new StringBuilder();
+ }
+
+}