Auftrags-Script in FirstSpirit

essmann
I'm new here
0 0 990

Sometimes one needs the possibility running a shell-script during a (deployment-) task. For this purpose you can define so-called script-actions in the task administration of the project configuration. By inserting the following code into the script-action dialog, FirstSpirit starts the named shell-script and writes some useful messages into the logs. You only have to customize the part building the command by vector elements.

Note: The script is basically a stripped-down version of the ssh/rsync-scripts in the admin documentation.

import java.util.*;


class StreamCapture extends Thread {
    InputStream i;
    boolean useErrlog;
    StreamCapture(InputStream inputstream, boolean useErrorlog) {
        i = inputstream;
        useErrlog = useErrorlog;
    }


    public void run() {
        try {
            InputStreamReader reader = new InputStreamReader(i);
            BufferedReader buffr = new BufferedReader(reader);
            String l = null;
            while ((l = buffr.readLine()) != null) {
                if(useErrlog) {
                    context.logError(l);
                } else {
                    context.logInfo(l);
                }
            }
        } catch (IOException ex) {
            context.logError( " failed: " + ex);
        } finally {
            try {            
                i.close();
            } catch (IOException exClose) {
                // ignore
            }
        }
    }
}

// creating the command call
cmd = new Vector();

cmd1 = new Vector();
context.logInfo("Generierungsverzeichnis: " + context.getPath());

cmd1 = new Vector();
// Command with full path
cmd1.add("/home/fs41/firstspirit4/temp/rename.sh");
// params are added as additional vector elements
// e.g.: cmd1.add("Parameter");

cmd.add(cmd1);

for(c: cmd) {
    cmdstr = "";
    for (String s : c) {
        cmdstr += " " + s;
    }
    cmdstr = "ProcessBuilder(" + cmdstr.substring(1) + ")";
    context.logInfo(cmdstr);
    Process p = null;
    try {
        p = new ProcessBuilder(c).start();
        InputStream is = p.getInputStream();
        StreamCapture stdout = new StreamCapture(p.getInputStream(),false);
        StreamCapture stderr = new StreamCapture(p.getErrorStream(), true);
        stdout.start();
        stderr.start();
        result = p.waitFor();
        stdout.join(3000);
        stderr.join(3000);
        if ( result == 0 ) {
            context.logInfo("command completed");
        } else {
            context.logError("failed with exitcode "    + result + " for " + cmdstr);
        }
    } catch (InterruptedException e) {
        context.logError("timeout for " + cmdstr + ": " + e);
    } catch (Exception e) {
        context.logError("failed for " + cmdstr + ": " + e);
    } finally {
        if (p != null) {
            try {
                p.getOutputStream().close();
            } catch (IOException ignore) {
                // ignore
            }
        }
    }
}
Version history
Last update:
‎09-17-2010 05:57 AM
Updated by: