summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java
blob: 791fcadb45ecbfb312aeede40f2cfc77ae61df96 (plain) (tree)
1
2
3
4
5
6
7

                                      
                     
                                                   
 
                                            
                                       










                                                     

                                   


                                       

                                        

                                             
        
                                                                                         












                                                                          
                                         





                                            
                                                                  











                                                                                                                            
                 


                                                

                                                                           

                                             

                  





                                                                         
                                                                             



                                                                         













                                                                           



                                                      
                                      

                                                                                 
                                           
















                                                   

                                              
                                           











                                                       
package org.openslx.taskmanager.tasks;

import java.util.Map;
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 MountVmStore extends SystemCommandTask
{
	@Expose
	private String address = null;
	@Expose
	private String type = null;
	@Expose
	private String opts = null;
	@Expose
	private String username = null;
	@Expose
	private String password = null;
	@Expose
	private boolean localNfs = true;

	private Output status = new Output();
	
	private static AtomicReference<MountVmStore> isRunning = new AtomicReference<>();

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( this.status );
		if ( this.address == null || this.type == null ) {
			status.addMessage( "Address or type not given." );
			return false;
		}
		if ( this.username == null )
			this.username = "";
		if ( this.password == null )
			this.password = "";
		this.timeoutSeconds = 60;
		return true;
	}

	@Override
	protected String[] initCommandLine()
	{
		while ( !isRunning.compareAndSet( null, this ) ) {
			MountVmStore current = isRunning.get();
			if ( current != null ) {
				if ( current.equals( this ) ) {
					// Exactly the same options - return other ID
					status.existingTask = current.getId();
					status.addMessage( "Concurrent call with same options - see 'existingTask' field" );
				} else {
					// Different options to existing, active task
					status.addMessage( "Another operation is already in progress." );
				}
				return null;
			}
		}
		return new String[] {
				"/usr/bin/sudo",
				"-n",
				"-u", "root",
				Constants.BASEDIR + "/scripts/mount-store",
				"images",
				this.address,
		};
	}
	
	@Override
	protected void initEnvironment( Map<String, String> environment )
	{
		environment.put( "TM_USERNAME", this.username );
		environment.put( "TM_PASSWORD", this.password );
		environment.put( "TM_NOLOCALNFS", this.localNfs ? "" : "1" );
		if ( !Util.isEmpty( this.opts ) ) {
			environment.put( "TM_MOUNT_OPTIONS", this.opts );
		}
	}
	
	@Override
	public boolean equals( Object obj )
	{
		if ( !(obj instanceof MountVmStore) )
			return false;
		MountVmStore o = (MountVmStore)obj;
		return Util.strcmp( this.address, o.address )
				&& Util.strcmp( this.type, o.type )
				&& Util.strcmp( this.opts, o.opts )
				&& Util.strcmp( this.username, o.username )
				&& Util.strcmp( this.password, o.password )
				&& this.localNfs == o.localNfs;
	}

	@Override
	protected boolean processEnded( int exitCode )
	{
		isRunning.set( null );
		if ( exitCode != 0 )
			status.addMessage( "Failed with exit code " + exitCode );
		status.exitCode = exitCode;
		return exitCode == 0;
	}

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

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

	class Output
	{
		public String messages = null;
		public int exitCode = -111;
		public String existingTask;

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

}