summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/LdadpLauncher.java
blob: d1550c50dbf23c9a644580cccdf98b9865365db0 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                      
                                                   

                                            
                                       







                                                     


                               

                                             

                                                                                          








                                                                      


                                                





                                            
                                                                                             



                                                                                                 

                                    



                                                          

                                                                                            




                                                                    
                                         





                                                                 
                                                      





                                                   
                                          






















                                                       
package org.openslx.taskmanager.tasks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.openslx.satserver.util.Constants;
import org.openslx.satserver.util.Util;
import org.openslx.taskmanager.api.SystemCommandTask;

import com.google.gson.annotations.Expose;

public class LdadpLauncher extends SystemCommandTask
{
	@Expose
	private int[] ids = null;
	
	@Expose
	private String command;

	private Output status = new Output();

	private static AtomicReference<LdadpLauncher> isRunning = new AtomicReference<>();

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( this.status );
		if ( ids == null ) {
			status.addMessage( "No ids passed to task." );
			return false;
		}
		if ( Util.isEmpty( command ) ) {
			command = "start";
		}
		return true;
	}

	@Override
	protected String[] initCommandLine()
	{
		if ( !command.equals( "check" ) && !isRunning.compareAndSet( null, this ) ) {
			LdadpLauncher other = isRunning.get();
			if ( other != null && !Arrays.equals( this.ids, other.ids ) ) {
				status.addMessage( "Another operation is already in progress." );
			}
			return null;
		}
		List<String> args = new ArrayList<>();
		args.addAll( Arrays.asList( new String[] {
				"/usr/bin/sudo",
				"-n",
				"-u", "root", Constants.BASEDIR + "/scripts/ldadp-launcher",
				"--" + command,
				"--"
		} ) );
		for ( int i = 0; i < ids.length; ++i ) {
			args.add( Integer.toString( this.ids[i] ) );
		}
		this.timeoutSeconds = 16;
		return args.toArray( new String[ args.size() ] );
	}

	@Override
	protected boolean processEnded( int exitCode )
	{
		isRunning.compareAndSet( this, null );
		return exitCode == 0;
	}

	@Override
	protected void processStdOut( String line )
	{
		status.addMessage( line );
	}

	@Override
	protected void processStdErr( String line )
	{
		status.addMessage( line );
	}

	class Output
	{
		private String messages = null;

		private void addMessage( String str )
		{
			if ( messages == null ) {
				messages = str;
			} else {
				messages += "\n" + str;
			}
		}
	}

}