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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
package org.openslx.taskmanager.tasks;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openslx.satserver.util.Exec;
import org.openslx.satserver.util.Exec.ExecCallback;
import org.openslx.taskmanager.api.AbstractTask;
import org.openslx.taskmanager.api.BoundedLog;
import com.google.gson.annotations.Expose;
public class CompileIPxeLegacy extends AbstractTask
{
private static final Logger LOG = Logger.getLogger( CompileIPxeLegacy.class );
@Expose
private String defaultentry = null;
@Expose
private int timeout = 0;
@Expose
private String custom = null;
@Expose
private String ipaddress = null;
@Expose
private String masterpassword = null;
private Output status = new Output();
private static AtomicBoolean isRunning = new AtomicBoolean();
@Override
protected boolean initTask()
{
this.setStatusObject( this.status );
if ( this.ipaddress == null || this.ipaddress.isEmpty() ) {
status.error = "No IP address given!";
return false;
}
if ( this.defaultentry == null )
this.defaultentry = "net";
if ( this.custom == null )
this.custom = "";
if ( this.masterpassword == null )
this.masterpassword = "";
return true;
}
@Override
protected boolean execute()
{
if ( !isRunning.compareAndSet( false, true ) ) {
status.error = "Another operation is already in progress.";
return false;
}
try {
boolean ret = true;
if ( !updateMenu() )
ret = false;
if ( !updateIpxe() )
ret = false;
return ret;
} finally {
isRunning.set( false );
}
}
private boolean updateMenu()
{
// Prepare menu
String template;
try {
template = FileUtils.readFileToString( new File( "./data/pxelinux-pxemenu.template" ), StandardCharsets.UTF_8 );
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
// Substitutions
template = template.replace( "%timeout%", Integer.toString( this.timeout * 10 ) );
template = template.replace( "%totaltimeout%", Integer.toString( this.timeout * 40 ) );
template = template.replace( "%default%", this.defaultentry );
template = template.replace( "%custom%", this.custom );
template = template.replace( "%ipaddress%", this.ipaddress );
template = template.replace( "%masterpassword%", this.masterpassword );
// Default selection net
if ( this.defaultentry.equals( "net" ) )
template = template.replace( "%default-net%", "MENU DEFAULT" );
else
template = template.replace( "%default-net%", "" );
// Default selection hdd
if ( this.defaultentry.equals( "hdd" ) )
template = template.replace( "%default-hdd%", "MENU DEFAULT" );
else
template = template.replace( "%default-hdd%", "" );
// Write out
try {
Charset cs;
if ( Charset.isSupported( "IBM437" ) )
cs = Charset.forName( "IBM437" );
else if ( Charset.isSupported( "Cp437" ) )
cs = Charset.forName( "Cp437" );
else
cs = StandardCharsets.UTF_8;
FileUtils.writeStringToFile( new File( "/srv/openslx/tftp/pxelinux.cfg/default" ), template, cs );
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
return true;
}
private boolean updateIpxe()
{
// Prepare menu
String template;
try {
template = FileUtils.readFileToString( new File( "./data/pxelinux-ipxe-embed.template" ), StandardCharsets.UTF_8 );
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
// Substitution
String hybridEmbed = template.replace( "%ipaddress%", this.ipaddress );
String usbEmbed = hybridEmbed.replaceFirst( "ifopen", "ifopen net0\n"
+ ":retry_dhcp\n"
+ "dhcp net0 && goto dhcp_ok ||\n"
+ "echo DHCP not successful, trying again in 10s...\n"
+ "sleep 10\n"
+ "goto retry_dhcp\n"
+ ":dhcp_ok\n"
+ "clear net0/ip\n"
+ "clear net0/netmask\n"
+ "set net0.dhcp/210:string http://" + this.ipaddress + "/tftp/ ||\n"
+ "set 210:string http://" + this.ipaddress + "/tftp/ ||\n" );
// Write out
try {
FileUtils.writeStringToFile( new File( "/opt/openslx/ipxe/ipxelinux.ipxe" ), hybridEmbed, StandardCharsets.UTF_8 );
FileUtils.writeStringToFile( new File( "/opt/openslx/ipxe/usb.ipxe" ), usbEmbed, StandardCharsets.UTF_8 );
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
// Compile
ProcLogger pl = new ProcLogger();
if ( 0 != Exec.syncAt( 240, pl, "/opt/openslx/ipxe/src", "make", "EMBED=../ipxelinux.ipxe,../pxelinux.0", "bin/undionly.kkkpxe" ) ) {
status.error = "Compiling ipxelinux.0 failed";
return false;
}
if ( 0 != Exec.syncAt( 240, pl, "/opt/openslx/ipxe/src", "make", "EMBED=../usb.ipxe,../pxelinux.0", "bin/ipxe.usb" ) ) {
FileUtils.deleteQuietly( new File( "/opt/openslx/ipxe/openslx-bootstick.raw" ) );
status.error = "Compiling ipxe usb image failed";
return false;
}
try {
FileUtils.copyFile( new File( "/opt/openslx/ipxe/src/bin/ipxe.usb" ), new File( "/opt/openslx/ipxe/openslx-bootstick.raw" ) );
} catch ( Exception e ) {
status.error = "Warning: could not create bootstick image";
}
try {
// Delete first in case it was a symlink
File dest = new File( "/srv/openslx/tftp/ipxelinux.0" );
FileUtils.deleteQuietly( dest );
FileUtils.copyFile( new File( "/opt/openslx/ipxe/src/bin/undionly.kkkpxe" ), dest );
} catch ( Exception e ) {
status.error = e.toString();
return false;
}
return true;
}
class Output
{
protected String error = null;
protected final BoundedLog log = new BoundedLog( 20, true );
protected void addLog( String data )
{
log.addLog( data );
}
}
private class ProcLogger implements ExecCallback
{
@Override
public void processStdOut( String line )
{
status.addLog( line );
}
@Override
public void processStdErr( String line )
{
status.addLog( line );
}
}
}
|