From 6581210de2a1644fcf665b93819b5cb08aa9b1b2 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 12 Feb 2019 15:03:04 +0100 Subject: [CompileIPxe*] Separate legacy pxelinux and new ipxe-only approach --- .../taskmanager/tasks/CompileIPxeLegacy.java | 175 +++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/main/java/org/openslx/taskmanager/tasks/CompileIPxeLegacy.java (limited to 'src/main/java/org/openslx/taskmanager/tasks/CompileIPxeLegacy.java') diff --git a/src/main/java/org/openslx/taskmanager/tasks/CompileIPxeLegacy.java b/src/main/java/org/openslx/taskmanager/tasks/CompileIPxeLegacy.java new file mode 100644 index 0000000..7002be6 --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/CompileIPxeLegacy.java @@ -0,0 +1,175 @@ +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 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 + 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; + } + +} -- cgit v1.2.3-55-g7522