summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/CompileIPxe.java
blob: c0534c7463bdf081eaa03d608bc8f4f1fa8f6b14 (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
package org.openslx.taskmanager.tasks;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.openslx.taskmanager.api.AbstractTask;

import com.google.gson.annotations.Expose;

public class CompileIPxe extends AbstractTask
{

	@Expose
	private String ip = null;
	@Expose
	private String defaultentry = null;
	@Expose
	private int timeout = 0;
	@Expose
	private String custom = null;

	private Output status = new Output();

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( this.status );
		if ( this.ip == null ) {
			status.error = "No IP address set.";
			return false;
		}
		if ( this.defaultentry == null )
			this.defaultentry = "net";
		if ( this.custom == null )
			this.custom = "";
		return true;
	}

	@Override
	protected boolean execute()
	{
		// Prepare menu
		String template;
		try {
			template = FileUtils.readFileToString( new File( "./data/pxemenu.template" ), StandardCharsets.UTF_8 );
		} catch ( IOException e ) {
			status.error = e.toString();
			return false;
		}
		// Substitutions
		template = template.replaceAll( "%ip%", this.ip );
		template = template.replaceAll( "%timeout%", Integer.toString( this.timeout * 10 ) );
		template = template.replaceAll( "%totaltimeout%", Integer.toString( this.timeout * 40 ) );
		template = template.replaceAll( "%default%", this.defaultentry );
		template = template.replaceAll( "%custom%", this.custom );
		// Default selection net
		if ( this.defaultentry.equals( "net" ) )
			template = template.replaceAll( "%default-net%", "MENU DEFAULT" );
		else
			template = template.replaceAll( "%default-net%", "" );
		// Default selection hdd
		if ( this.defaultentry.equals( "hdd" ) )
			template = template.replaceAll( "%default-hdd%", "MENU DEFAULT" );
		else
			template = template.replaceAll( "%default-hdd%", "" );
		// Write out
		try {
			FileUtils.writeStringToFile( new File( "/srv/openslx/tftp/pxelinux.cfg/default" ), template, StandardCharsets.UTF_8 );
		} catch ( IOException e ) {
			status.error = e.toString();
			return false;
		}
		return true;
	}

	class Output
	{
		protected String error = null;
	}

}