summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/AptUpgrade.java
blob: aabd54de5403ea356cad4087dd3af3c797fcd4de (plain) (tree)


























































































                                                                                               
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();
	}

}