summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/AptUpgrade.java
blob: aabd54de5403ea356cad4087dd3af3c797fcd4de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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();
	}

}