|
|
package org.openslx.taskmanager.tasks;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openslx.satserver.util.Exec;
import org.openslx.taskmanager.api.AbstractTask;
import com.google.gson.annotations.Expose;
public class CompileIPxe extends AbstractTask
{
private static final Logger LOG = Logger.getLogger( CompileIPxe.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();
@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()
{
boolean ret = true;
if ( !updateMenu() )
ret = false;
if ( !updateIpxe() )
ret = false;
return ret;
}
private boolean updateMenu()
{
// 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.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/ipxe-embed.template" ), StandardCharsets.UTF_8 );
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
// Substitution
template = template.replace( "%ipaddress%", this.ipaddress );
// Write out
try {
FileUtils.writeStringToFile( new File( "/opt/openslx/ipxe/ipxelinux.ipxe" ), template, StandardCharsets.UTF_8 );
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
// Compile
if ( 0 != Exec.syncAt( 120, "/opt/openslx/ipxe/src", "make", "EMBED=../ipxelinux.ipxe,../pxelinux.0", "bin/undionly.kkkpxe" ) ) {
status.error = "Compiling ipxelinux.0 failed";
return false;
}
try {
FileUtils.copyFile( new File( "/opt/openslx/ipxe/src/bin/undionly.kkkpxe" ), new File( "/srv/openslx/tftp/v4/ipxelinux.0" ) );
} catch ( Exception e ) {
status.error = e.toString();
return false;
}
return true;
}
class Output
{
protected String error = null;
}
}
|