summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/MountVmStore.java
blob: 791fcadb45ecbfb312aeede40f2cfc77ae61df96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;
			}
		}
	}

}