summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/utils/ServerUtil.java
blob: 9a8b1409966aaf203a0dde177f642fd34a6ab9a3 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Gateway.
 *
 * Gluster Management Gateway is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Gateway is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.gateway.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;
import org.gluster.storage.management.core.constants.CoreConstants;
import org.gluster.storage.management.core.exceptions.ConnectionException;
import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import org.gluster.storage.management.core.model.Server;
import org.gluster.storage.management.core.model.Server.SERVER_STATUS;
import org.gluster.storage.management.core.model.Status;
import org.gluster.storage.management.core.utils.ProcessResult;
import org.gluster.storage.management.core.utils.ProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;


@Component
public class ServerUtil {
	@Autowired
	ServletContext servletContext;

	@Autowired
	private SshUtil sshUtil;
	
	@Autowired
	private String appVersion;
	
	private static final Logger logger = Logger.getLogger(ServerUtil.class);

	private static final String SCRIPT_DIR = "scripts";
	private static final String SCRIPT_COMMAND = "python";
	private static final String REMOTE_SCRIPT_GET_DISK_FOR_DIR = "get_disk_for_dir.py";
	private static final String REMOTE_SCRIPT_GET_SERVER_DETAILS = "get_server_details.py";
	private static final String REMOTE_SCRIPT_GET_FILE_SYSTEM_TYPE = "get_filesystem_type.py";
	private static final String REMOTE_SCRIPT_BASE_DIR = "/opt/glustermg";
	private static final String REMOTE_SCRIPT_DIR_NAME = "backend";

	public void setSshUtil(SshUtil sshUtil) {
		this.sshUtil = sshUtil;
	}

	public ProcessResult executeGlusterScript(boolean runInForeground, String scriptName, String...arguments) {
		return executeGlusterScript(runInForeground, scriptName, Arrays.asList(arguments));
	}
	
	public ProcessResult executeGlusterScript(boolean runInForeground, String scriptName, List<String> arguments) {
		List<String> command = new ArrayList<String>();

		command.add(SCRIPT_COMMAND);
		command.add(getScriptPath(scriptName));
		command.addAll(arguments);
		return ProcessUtil.executeCommand(runInForeground, command);
	}

	private String getScriptPath(String scriptName) {
		return servletContext.getRealPath(SCRIPT_DIR) + CoreConstants.FILE_SEPARATOR + scriptName;
	}
	
	private String getRemoteScriptDir() {
		return REMOTE_SCRIPT_BASE_DIR + File.separator + appVersion + File.separator + REMOTE_SCRIPT_DIR_NAME;
	}
	
	/**
	 * Fetch details of the given server. The server name must be populated in the object before calling this method.
	 * 
	 * @param server
	 *            Server whose details are to be fetched
	 */
	public void fetchServerDetails(Server server) {
		try {
			Server serverDetails = fetchServerDetails(server.getName());
			server.copyFrom(serverDetails); // Update the details in <Server> object
			server.setDisks(serverDetails.getDisks());
		} catch (ConnectionException e) {
			logger.warn("Couldn't connect to server [" + server.getName() + "]. Marking it offline!", e);
			server.setStatus(SERVER_STATUS.OFFLINE);
		}
	}
	
	public boolean isServerOnline(Server server) {
		// fetch latest details and check if server is still online
		fetchServerDetails(server);
		return server.isOnline();
	}
	
	public String fetchHostName(String serverName) {
		Object response = fetchServerDetails(serverName);
		return ((Server) response).getName();
	}

	private Server fetchServerDetails(String serverName) {
		// fetch standard server details like cpu, disk, memory details
		return executeScriptOnServer(serverName, REMOTE_SCRIPT_GET_SERVER_DETAILS, Server.class);
	}

	/**
	 * Executes given script on all given servers in parallel, collects the output in objects of given class, and
	 * returns a list of all returned objects.
	 * 
	 * @param serverNames
	 * @param scriptWithArgs
	 * @param expectedClass
	 * @param failOnError
	 *            If true, an exception will be thrown as soon as the script execution fails on any of the servers. If
	 *            false, the exception will be caught and logged. Execution on all other servers will continue.
	 * @return
	 */
	public <T> List<T> executeScriptOnServers(List<String> serverNames, String scriptWithArgs,
			Class<T> expectedClass, boolean failOnError) {
		List<T> result = Collections.synchronizedList(new ArrayList<T>());
		try {
			List<Thread> threads = createScriptExecutionThreads(serverNames, getRemoteScriptDir() + File.separator
					+ scriptWithArgs, expectedClass, result, failOnError);
			ProcessUtil.waitForThreads(threads);
			return result;
		} catch (InterruptedException e) {
			String errMsg = "Exception while executing script [" + scriptWithArgs + "] on servers [" + serverNames + "]! Error: [" + e.getMessage() + "]";
			logger.error(errMsg, e);
			throw new GlusterRuntimeException(errMsg, e);
		}
	}

	/**
	 * Creates threads that will run in parallel and execute the given command on each of the given servers
	 * 
	 * @param serverNames
	 * @param commandWithArgs
	 * @param expectedClass
	 * @param result
	 * @param failOnError
	 *            If true, an exception will be thrown as soon as the script execution fails on any of the servers. If
	 *            false, the exception will be caught and logged. Execution on all other servers will continue.
	 * @return
	 * @throws InterruptedException
	 */
	private <T> List<Thread> createScriptExecutionThreads(List<String> serverNames, String commandWithArgs, Class<T> expectedClass, List<T> result,
			boolean failOnError)
			throws InterruptedException {
		List<Thread> threads = new ArrayList<Thread>();
		for (int i = serverNames.size()-1; i >= 0 ; i--) {
			Thread thread = new RemoteExecutionThread<T>(serverNames.get(i), commandWithArgs, expectedClass, result, failOnError);
			threads.add(thread);
			thread.start();
			if(i >= 5 && i % 5 == 0) {
				// After every 5 servers, wait for 1 second so that we don't end up with too many running threads
				Thread.sleep(1000);
			}
		}
		return threads;
	}


	public class RemoteExecutionThread<T> extends Thread {
		private String serverName;
		private String commandWithArgs;
		private List<T> result;
		private Class<T> expectedClass;
		private boolean failOnError = false;
		
		public RemoteExecutionThread(String serverName, String commandWithArgs, Class<T> expectedClass, List<T> result,
				boolean failOnError) {
			this.serverName = serverName;
			this.commandWithArgs = commandWithArgs;
			this.result = result;
			this.expectedClass = expectedClass;
			this.failOnError = failOnError;
		}
		
		@Override
		public void run() {
			try {
				result.add(executeOnServer(serverName, commandWithArgs, expectedClass));
			} catch(Exception e) {
				String errMsg = "Couldn't execute command [" + commandWithArgs + "] on [" + serverName + "]!";
				logger.error(errMsg, e);
				if(failOnError) {
					throw new GlusterRuntimeException(errMsg, e);
				}
			}
		}
	}


	/**
	 * Executes given script on given server. Since the remote server may contain multiple versions of backend, this
	 * method will invoke the script present in directory of same version as the gateway.
	 * 
	 * @param serverName
	 * @param scriptWithArgs
	 *            The script name followed by arguments to be passed. Note that the script name should not contain path
	 *            as it will be automatically identified by the method.
	 * @param expectedClass
	 *            Class of the object expected from script execution
	 * @return Output (console/error) from the script execution
	 * @throws GlusterRuntimeException in case the remote execution fails.
	 */
	public String executeScriptOnServer(String serverName, String scriptWithArgs) {
		return executeOnServer(serverName, getRemoteScriptDir() + File.separator + scriptWithArgs, String.class);
	}

	/**
	 * Executes given script on given server. Since the remote server may contain multiple versions of backend, this
	 * method will invoke the script present in directory of same version as the gateway.
	 * 
	 * @param serverName
	 * @param scriptWithArgs
	 *            The script name followed by arguments to be passed. Note that the script name should not contain path
	 *            as it will be automatically identified by the method.
	 * @param expectedClass
	 *            Class of the object expected from script execution
	 * @return Object of the expected class from remote execution of the command.
	 * @throws GlusterRuntimeException in case the remote execution fails.
	 */
	public <T> T executeScriptOnServer(String serverName, String scriptWithArgs,
			Class<T> expectedClass) {
		return executeOnServer(serverName, getRemoteScriptDir() + File.separator + scriptWithArgs,
				expectedClass);
	}
	
	/**
	 * Executes given command on given server
	 * 
	 * @param serverName
	 * @param commandWithArgs
	 * @param expectedClass
	 *            Class of the object expected from script execution
	 * @return Object of the expected class from remote execution of the command. In case the remote execution fails
	 *         ungracefully, an object of class {@link Status} will be returned.
	 */
	@SuppressWarnings("unchecked")
	public <T> T executeOnServer(String serverName, String commandWithArgs,
			Class<T> expectedClass) {
		String output = executeOnServer(serverName, commandWithArgs);
		if (expectedClass == String.class) {
			return (T) output;
		}

		return unmarshal(expectedClass, output);
	}

	public String executeOnServer(String serverName, String commandWithArgs) {
		ProcessResult result = sshUtil.executeRemote(serverName, commandWithArgs);
		
		if (!result.isSuccess()) {
			throw new GlusterRuntimeException("Command [" + commandWithArgs + "] failed on [" + serverName
					+ "] with error [" + result.getExitValue() + "][" + result.getOutput() + "]");
		}
		return result.getOutput();
	}

	// This is the old executeOnServer that used socket communication.
	// We can keep it commented for the time being.
	// private String executeOnServerUsingSocket(String serverName, String commandWithArgs) {
	// try {
	// InetAddress address = InetAddress.getByName(serverName);
	// Socket connection = new Socket(address, 50000);
	//
	// PrintWriter writer = new PrintWriter(connection.getOutputStream(), true);
	// writer.println(commandWithArgs);
	// writer.println(); // empty line means end of request
	//
	// InputStream inputStream = connection.getInputStream();
	// int available = inputStream.available();
	//
	// StringBuffer output = new StringBuffer();
	// if( available > 0 ) {
	// // This happens when PeerAgent sends complete file
	// byte[] responseData = new byte[available];
	// inputStream.read(responseData);
	// output.append(new String(responseData, "UTF-8"));
	// } else {
	// // This happens in case of normal XML response from PeerAgent
	// BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
	//
	// String line;
	// while (!(line = reader.readLine()).trim().isEmpty()) {
	// output.append(line + CoreConstants.NEWLINE);
	// }
	// }
	// connection.close();
	//
	// return output.toString();
	// } catch (Exception e) {
	// throw new GlusterRuntimeException("Error during remote execution: [" + e.getMessage() + "]");
	// }
	// }

	public void getFileFromServer(String serverName, String remoteFileName, String localDirName) {
		sshUtil.getFile(serverName, remoteFileName, localDirName);
	}

	/**
	 * Unmarshals given input string into object of given class
	 * 
	 * @param expectedClass
	 *            Class whose object is expected
	 * @param input
	 *            Input string
	 * @return Object of given expected class
	 */
	public <T> T unmarshal(Class<T> expectedClass, String input) {
		try {
			// create JAXB context and instantiate marshaller
			JAXBContext context = JAXBContext.newInstance(expectedClass);
			Unmarshaller um = context.createUnmarshaller();
			return (T)um.unmarshal(new ByteArrayInputStream(input.getBytes()));
		} catch (JAXBException e) {
			String errMsg = "Error during unmarshalling string [" + input + "] for class [" + expectedClass.getName()
					+ ": [" + e.getMessage() + "]";
			logger.error(errMsg, e);
			throw new GlusterRuntimeException(errMsg, e);
		}
	}

	/**
	 * @param serverName
	 *            Server on which the directory is present
	 * @param brickDir
	 *            Directory whose disk is to be fetched
	 * @return Status object containing the disk name, or error message in case the remote script fails.
	 */
	public Status getDiskForDir(String serverName, String brickDir) {
		return executeScriptOnServer(serverName, REMOTE_SCRIPT_GET_DISK_FOR_DIR + " " + brickDir, Status.class);
	}
	
	public <T> T getBean(Class<T> clazz) {
		ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
		return ctx.getBean(clazz);
	}
	
	public List<String> getFsTypes(String serverName) {
		String output = executeScriptOnServer(serverName, REMOTE_SCRIPT_GET_FILE_SYSTEM_TYPE);
		return Arrays.asList(output.trim().split(CoreConstants.NEWLINE));
	}
}