summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/InitializeDiskTask.java
blob: ffb9619d6328aa9c89159cdb31d93af91a337811 (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
/**
 * InitializeDiskTask.java
 *
 * 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 com.gluster.storage.management.gateway.tasks;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import com.gluster.storage.management.core.constants.GlusterConstants;
import com.gluster.storage.management.core.exceptions.ConnectionException;
import com.gluster.storage.management.core.model.InitDiskStatusResponse;
import com.gluster.storage.management.core.model.InitDiskStatusResponse.FORMAT_STATUS;
import com.gluster.storage.management.core.model.Status;
import com.gluster.storage.management.core.model.TaskInfo;
import com.gluster.storage.management.core.model.TaskInfo.TASK_TYPE;
import com.gluster.storage.management.core.model.TaskStatus;
import com.gluster.storage.management.gateway.services.ClusterService;
import com.gluster.storage.management.gateway.utils.ServerUtil;
import com.sun.jersey.core.util.Base64;

public class InitializeDiskTask extends Task {

	private static final String INITIALIZE_DISK_SCRIPT = "format_device.py";
	private static final String INITIALIZE_DISK_STATUS_SCRIPT = "get_format_device_status.py";
	
	private String serverName;
	private String diskName;
	private String fsType;
	private String mountPoint;
	private ServerUtil serverUtil;

	public InitializeDiskTask(ClusterService clusterService, String clusterName, String serverName, String diskName,
			String fsType, String deviceMountPoint) {
		// Reference contains "Server:disk"
		super(clusterService, clusterName, TASK_TYPE.DISK_FORMAT, serverName + ":" + diskName, "Initialize disk "
				+ serverName + ":" + diskName, false, false, false);

		setServerName(serverName);
		setDiskName(diskName);
		setFsType(fsType);
		setMountpoint(deviceMountPoint);
		taskInfo.setName(getId());
		init();
	}

	public InitializeDiskTask(ClusterService clusterService, String clusterName, TaskInfo info) {
		super(clusterService, clusterName, info);
		init();
	}

	private void init() {
		ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
		serverUtil = ctx.getBean(ServerUtil.class);
	}
	
	@Override
	public String getId() {
		return new String(
				Base64.encode(getClusterName() + "-" + taskInfo.getType() + "-" + serverName + ":" + diskName));
	}

	@Override
	public void resume() {
		getTaskInfo().setStatus(
				new TaskStatus(new Status(Status.STATUS_CODE_FAILURE,
						"Stop/Pause/Resume is not supported in Disk Initialization")));
	}

	@Override
	public void stop() {
		getTaskInfo().setStatus(
				new TaskStatus(new Status(Status.STATUS_CODE_FAILURE,
						"Stop/Pause/Resume is not supported in Disk Initialization")));
	}

	@Override
	public void pause() {
		getTaskInfo().setStatus(
				new TaskStatus(new Status(Status.STATUS_CODE_FAILURE,
						"Stop/Pause/Resume is not supported in Disk Initialization")));
	}
	
	@Override
	public void commit() {
		// TODO Auto-generated method stub
	}

	@Override
	public TASK_TYPE getType() {
		return TASK_TYPE.DISK_FORMAT;
	}

	
	@Override
	public void start() {
		try {
			startInitializeDisk(serverName);
		} catch(ConnectionException e) {
			// online server might have gone offline.  update the failure status
			getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_FAILURE, e.getMessage())));
		}
	}

	private void startInitializeDisk(String serverName) {
		String output = serverUtil.executeScriptOnServer(serverName, INITIALIZE_DISK_SCRIPT + getFsType() + " "
				+ mountPoint + " " + getDiskName() );
		TaskStatus taskStatus = new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output));
		taskStatus.setPercentageSupported((getFsType().equals(GlusterConstants.FSTYPE_XFS)) ? false : true);
		getTaskInfo().setStatus(taskStatus);
	}

	@Override
	public TaskStatus checkStatus() {
		
		try {
			return getInitializingDeviceStatus(serverName, getDiskName());
		} catch(ConnectionException e) {
			// online server might have gone offline. update the failure status
			return new TaskStatus(new Status(Status.STATUS_CODE_FAILURE, e.getMessage()));
		}
	}
	
	private TaskStatus getInitializingDeviceStatus(String serverName, String diskName) {
		InitDiskStatusResponse initDiskStatusResponse;
		TaskStatus taskStatus = new TaskStatus();
		
		try {
			initDiskStatusResponse = serverUtil.executeScriptOnServer(serverName, INITIALIZE_DISK_STATUS_SCRIPT + " "
				+ diskName, InitDiskStatusResponse.class);
		} catch(RuntimeException e) {
			taskStatus.setCode(Status.STATUS_CODE_FAILURE);
			taskStatus.setMessage(e.getMessage());
			throw e;
		}

		if (initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.COMPLETED) {
			taskStatus.setCode(Status.STATUS_CODE_SUCCESS);
		} else if (initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.IN_PROGRESS) {
			taskStatus.setCode(Status.STATUS_CODE_RUNNING);
			taskStatus.setPercentCompleted(Math.round(initDiskStatusResponse.getCompletedBlocks()
					/ initDiskStatusResponse.getTotalBlocks() * 100));
		} else if(initDiskStatusResponse.getFormatStatus() == FORMAT_STATUS.NOT_RUNNING) {
			taskStatus.setCode(Status.STATUS_CODE_FAILURE);
		}
		
		taskStatus.setMessage(initDiskStatusResponse.getMessage());
		return taskStatus;
	}
	
	public void setDiskName(String diskName) {
		this.diskName = diskName;
	}

	public String getDiskName() {
		return diskName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String getServerName() {
		return serverName;
	}

	public void setFsType(String fsType) {
		this.fsType = fsType;
	}

	public String getFsType() {
		return fsType;
	}
	
	public void setMountpoint(String deviceMountPoint) {
		this.mountPoint = deviceMountPoint;
	}
	
	public String getMountpoint() {
		return mountPoint;
	}
}