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.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();
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/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
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
if ( 0 != Exec.syncAt( 240, "/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, "/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 {
FileUtils.copyFile( new File( "/opt/openslx/ipxe/src/bin/undionly.kkkpxe" ), new File( "/srv/openslx/tftp/ipxelinux.0" ) );
} catch ( Exception e ) {
status.error = e.toString();
return false;
}
return true;
}
class Output
{
protected String error = null;
}
}