package org.openslx.taskmanager.tasks; import java.util.ArrayList; import java.util.List; import org.openslx.satserver.util.Exec; import org.openslx.satserver.util.Util; import org.openslx.satserver.util.Exec.ExecCallback; import org.openslx.taskmanager.api.SystemCommandTask; import com.google.gson.annotations.Expose; public class IpxeVersion extends SystemCommandTask { /* FOR COMMIT PICKING * git log --pretty=format:"%H %at" --first-parent a58276abdd..openslx */ @Expose private Action action; @Expose private String ref; private Status status = new Status(); @Override protected String[] initCommandLine() { List args = new ArrayList<>(); args.add( "git" ); args.add( "-C" ); args.add( "/opt/openslx/ipxe" ); switch ( action ) { case CHECKOUT: args.add( "checkout" ); args.add( ref ); break; case LIST: EC vec = new EC(); Exec.syncAt( 1, vec, "/", "gcc", "-dumpversion" ); String start; if ( vec.version >= 10 ) { // Versions before this commit won't build with gcc 10 start = "a098f40893"; } else { start = "a58276abdd"; } args.add( "log" ); args.add( "--pretty=format:%H %at" ); args.add( "--first-parent" ); args.add( start + "..origin/openslx" ); status.versions = new ArrayList(); break; case FETCH: args.add( "fetch" ); break; case RESET: args.add( "reset" ); args.add( "--hard" ); break; } return args.toArray( new String[ args.size() ] ); } @Override protected boolean processEnded( int exitCode ) { if ( exitCode != 0 && status.error == null ) { status.error = "Exit code: " + exitCode; } return exitCode == 0; } @Override protected void processStdOut( String line ) { if ( action == Action.LIST ) { String[] parts = line.split( " " ); if ( parts.length == 2 && parts[0].length() == 40 && parts[1].length() == 10 ) { long d = Util.parseLong( parts[1], 0 ); if ( d != 0 ) { status.versions.add( new Version( parts[0], d ) ); } } } else { processStdErr( line ); } } @Override protected void processStdErr( String line ) { if ( status.error == null ) { status.error = line; } else { status.error += "\n" + line; } } @Override protected boolean initTask() { setStatusObject( status ); if ( action == null || ( action == Action.CHECKOUT && ref == null ) ) { status.error = "action or commit is null"; return false; } status.ref = ref; return true; } static class EC implements ExecCallback { public int version = 0; @Override public void processStdOut( String line ) { if ( line.matches( "[0-9].*" ) ) { version = Util.parseInt( line.replaceAll( "[^0-9].*$", "" ), 0 ); } } @Override public void processStdErr( String line ) { } } static enum Action { FETCH, RESET, CHECKOUT, LIST; } static class Version { public String hash; public long date; public Version( String hash, long date ) { this.date = date; this.hash = hash; } } static class Status { public String error; public List versions; public String ref; } }