summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/services/ClusterService.java
blob: e7a5af3dd9293888319d8bb77fa3bba862a654d7 (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
/*******************************************************************************
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Affero 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 Console 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 Affero General Public License
 * for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.gateway.services;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityTransaction;

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.GlusterServer;
import org.gluster.storage.management.core.utils.LRUCache;
import org.gluster.storage.management.gateway.data.ClusterInfo;
import org.gluster.storage.management.gateway.data.PersistenceDao;
import org.gluster.storage.management.gateway.data.ServerInfo;
import org.gluster.storage.management.gateway.utils.ServerUtil;
import org.gluster.storage.management.gateway.utils.SshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Service class for functionality related to clusters
 */
@Component
public class ClusterService {
	@Autowired
	private PersistenceDao<ClusterInfo> clusterDao;
	
	@Autowired
	private PersistenceDao<ServerInfo> serverDao;

	@Autowired
	private GlusterServerService glusterServerService;

	@Autowired
	private SshUtil sshUtil;
	
	@Autowired
	private ServerUtil serverUtil;
	
	private LRUCache<String, GlusterServer> onlineServerCache = new LRUCache<String, GlusterServer>(3);
	
	private static final Logger logger = Logger.getLogger(ClusterService.class);
	
	public void addOnlineServer(String clusterName, GlusterServer server) {
		onlineServerCache.put(clusterName, server);
	}
	
	public void removeOnlineServer(String clusterName) {
		onlineServerCache.remove(clusterName);
	}
	
	// uses cache
	public GlusterServer getOnlineServer(String clusterName, String exceptServerName) {
		GlusterServer server = onlineServerCache.get(clusterName);
		if (server != null && !server.getName().equalsIgnoreCase(exceptServerName)) {
			return server;
		}

		return getNewOnlineServer(clusterName, exceptServerName);
	}

	public GlusterServer getNewOnlineServer(String clusterName) {
		return getNewOnlineServer(clusterName, "");
	}
	
	public GlusterServer getOnlineServer(String clusterName) {
		return getOnlineServer(clusterName, "");
	}

	// Doesn't use cache
	public GlusterServer getNewOnlineServer(String clusterName, String exceptServerName) {
		ClusterInfo cluster = getCluster(clusterName);
		if (cluster == null) {
			throw new GlusterRuntimeException("Cluster [" + clusterName + "] is not found!");
		}

		for (ServerInfo serverInfo : cluster.getServers()) {
			GlusterServer server = new GlusterServer(serverInfo.getName());
			try {
				serverUtil.fetchServerDetails(server); // Online status come with server details
				// server is online. add it to cache and return
				if (server.isOnline() && !server.getName().equalsIgnoreCase(exceptServerName)) {
					addOnlineServer(clusterName, server);
					return server;
				}
			} catch (ConnectionException e) {
				// server is offline. continue checking next one.
				continue;
			}
		}

		// no online server found.
		throw new GlusterRuntimeException("No online server found in cluster [" + clusterName + "]");
	}
	
	public List<ClusterInfo> getAllClusters() {
		return clusterDao.findAll();
	}
	
	public ClusterInfo getCluster(String clusterName) {
		List<ClusterInfo> clusters = clusterDao.findBy("UPPER(name) = ?1", clusterName.toUpperCase());
		if(clusters.size() == 0) {
			return null;
		}

		return clusters.get(0);
	}
	
	public ClusterInfo getClusterForServer(String serverName) {
		List<ServerInfo> servers = serverDao.findBy("UPPER(name) = ?1", serverName.toUpperCase());
		if(servers.size() == 0) {
			return null;
		}

		return servers.get(0).getCluster();
	}
	
	public void createCluster(String clusterName) {
		EntityTransaction txn = clusterDao.startTransaction();
		ClusterInfo cluster = new ClusterInfo();
		cluster.setName(clusterName);

		try {
			clusterDao.save(cluster);
			txn.commit();
		} catch (RuntimeException e) {
			txn.rollback();
			logger.error("Exception while trying to save cluster [" + clusterName + "] : [" + e.getMessage() + "]", e);
			throw e;
		}
	}
	
	public void registerCluster(String clusterName, String knownServer) {
		EntityTransaction txn = clusterDao.startTransaction();
		ClusterInfo cluster = new ClusterInfo();
		cluster.setName(clusterName);
		
		GlusterServer server = new GlusterServer(knownServer);
		try {
			List<GlusterServer> glusterServers = glusterServerService.getGlusterServers(server.getName());
			List<ServerInfo> servers = new ArrayList<ServerInfo>();
			for(GlusterServer glusterServer : glusterServers) {
				String serverName = glusterServer.getName();
				
				serverUtil.fetchServerDetails(glusterServer);
				if(glusterServer.isOnline()) {
					checkAndSetupPublicKey(serverName);
				}

				ServerInfo serverInfo = new ServerInfo(serverName);
				serverInfo.setCluster(cluster);
				clusterDao.save(serverInfo);
				servers.add(serverInfo);
			}
			cluster.setServers(servers);
			clusterDao.save(cluster);
			txn.commit();
		} catch(RuntimeException e) {
			logger.error("Error in registering cluster [" + clusterName + "] : " + e.getMessage(), e);
			txn.rollback();
			logger.error("Error in registering cluster [" + clusterName + "] : " + e.getMessage(), e);
			throw e;
		}
	}
	
	private void checkAndSetupPublicKey(String serverName) {
		if(sshUtil.isPublicKeyInstalled(serverName)) {
			return;
		}
		
		if(!sshUtil.hasDefaultPassword(serverName)) {
			// public key not installed, default password doesn't work. can't install public key
			throw new GlusterRuntimeException(
					"Gluster Management Gateway uses the default password to set up keys on the server."
							+ CoreConstants.NEWLINE + "However it seems that the password on server [" + serverName
							+ "] has been changed manually." + CoreConstants.NEWLINE
							+ "Please reset it back to the standard default password and try again.");
		}
		
		// install public key (this will also disable password based ssh login)
		sshUtil.installPublicKey(serverName);
	}
	
	public void unregisterCluster(String clusterName) {
		ClusterInfo cluster = getCluster(clusterName);
		
		if (cluster == null) {
			throw new GlusterRuntimeException("Cluster [" + clusterName + "] doesn't exist!");
		}

		unregisterCluster(cluster);
	}

	public void unregisterCluster(ClusterInfo cluster) {
		EntityTransaction txn = clusterDao.startTransaction();
		try {
			for(ServerInfo server : cluster.getServers()) {
				clusterDao.delete(server);
			}
			cluster.getServers().clear();
			clusterDao.update(cluster);
			clusterDao.delete(cluster);
			txn.commit();
		} catch (RuntimeException e) {
			logger.error("Error in unregistering cluster [" + cluster.getName() + "] : " + e.getMessage(), e);
			txn.rollback();
			throw e;
		}
	}
	
	public void mapServerToCluster(String clusterName, String serverName) {
		EntityTransaction txn = clusterDao.startTransaction();
		ClusterInfo cluster = getCluster(clusterName);
		ServerInfo server = new ServerInfo(serverName);
		server.setCluster(cluster);
		try {
			clusterDao.save(server);
			cluster.addServer(server);
			clusterDao.update(cluster);
			txn.commit();
		} catch (Exception e) {
			txn.rollback();
			throw new GlusterRuntimeException("Couldn't create cluster-server mapping [" + clusterName + "]["
					+ serverName + "]! Error: " + e.getMessage(), e);
		}
	}
	
	public void unmapServerFromCluster(String clusterName, String serverName) {
		EntityTransaction txn = clusterDao.startTransaction();
		ClusterInfo cluster = getCluster(clusterName);
		List<ServerInfo> servers = cluster.getServers();
		for(ServerInfo server : servers) {
			if(server.getName().equalsIgnoreCase(serverName)) {
				servers.remove(server);
				clusterDao.delete(server);
				break;
			}
		}
		try {
			clusterDao.update(cluster);
			txn.commit();
		} catch(Exception e) {
			txn.rollback();
			throw new GlusterRuntimeException("Couldn't unmap server [" + serverName + "] from cluster [" + clusterName
					+ "]! Error: " + e.getMessage(), e);
		}
	}
}