1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.ServiceModel;
|
---|
26 | using HeuristicLab.Services.Hive.DataTransfer;
|
---|
27 | using HeuristicLab.Services.Hive.ServiceContracts;
|
---|
28 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
29 | using DT = HeuristicLab.Services.Hive.DataTransfer;
|
---|
30 |
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Services.Hive {
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
|
---|
36 | /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
|
---|
37 | /// </summary>
|
---|
38 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
|
---|
39 | [HiveOperationContextBehavior]
|
---|
40 | public class HiveService : IHiveService {
|
---|
41 | private IHiveDao dao {
|
---|
42 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
43 | }
|
---|
44 | private IOptimizedHiveDao optimizedDao {
|
---|
45 | get { return ServiceLocator.Instance.OptimizedHiveDao; }
|
---|
46 | }
|
---|
47 | private Access.IRoleVerifier authen {
|
---|
48 | get { return ServiceLocator.Instance.RoleVerifier; }
|
---|
49 | }
|
---|
50 | private IAuthorizationManager author {
|
---|
51 | get { return ServiceLocator.Instance.AuthorizationManager; }
|
---|
52 | }
|
---|
53 | private DataAccess.ITransactionManager trans {
|
---|
54 | get { return ServiceLocator.Instance.TransactionManager; }
|
---|
55 | }
|
---|
56 | private IEventManager eventManager {
|
---|
57 | get { return ServiceLocator.Instance.EventManager; }
|
---|
58 | }
|
---|
59 | private Access.IUserManager userManager {
|
---|
60 | get { return ServiceLocator.Instance.UserManager; }
|
---|
61 | }
|
---|
62 | private HeartbeatManager heartbeatManager {
|
---|
63 | get { return ServiceLocator.Instance.HeartbeatManager; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | #region Task Methods
|
---|
67 | public Guid AddTask(Task task, TaskData taskData, IEnumerable<Guid> resourceIds) {
|
---|
68 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
69 | return trans.UseTransaction(() => {
|
---|
70 | var t = DT.Convert.ToEntity(task);
|
---|
71 | t.RequiredPlugins.AddRange(task.PluginsNeededIds.Select(pluginId => new DA.RequiredPlugin { Task = t, PluginId = pluginId }));
|
---|
72 |
|
---|
73 | t.JobData = DT.Convert.ToEntity(taskData);
|
---|
74 | t.JobData.LastUpdate = DateTime.Now;
|
---|
75 |
|
---|
76 | optimizedDao.AddTask(t);
|
---|
77 |
|
---|
78 | dao.AssignJobToResource(t.TaskId, resourceIds);
|
---|
79 |
|
---|
80 | optimizedDao.UpdateTaskState(t.TaskId, DA.TaskState.Waiting, null, userManager.CurrentUserId, null);
|
---|
81 |
|
---|
82 | return t.TaskId;
|
---|
83 | }, false, true);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public Guid AddChildTask(Guid parentTaskId, Task task, TaskData taskData) {
|
---|
87 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
88 | task.ParentTaskId = parentTaskId;
|
---|
89 | return AddTask(task, taskData, optimizedDao.GetAssignedResourceIds(parentTaskId).ToList());
|
---|
90 | }
|
---|
91 |
|
---|
92 | public Task GetTask(Guid taskId) {
|
---|
93 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
94 | author.AuthorizeForTask(taskId, Permission.Read);
|
---|
95 |
|
---|
96 | return trans.UseTransaction(() => {
|
---|
97 | return DT.Convert.ToDto(optimizedDao.GetTaskById(taskId));
|
---|
98 | }, false, false);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public IEnumerable<Task> GetTasks() {
|
---|
102 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
103 | return trans.UseTransaction(() => {
|
---|
104 | var tasks = dao.GetTasks(x => true);
|
---|
105 | foreach (var task in tasks)
|
---|
106 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
107 | return tasks;
|
---|
108 | });
|
---|
109 | }
|
---|
110 |
|
---|
111 | public IEnumerable<LightweightTask> GetLightweightTasks(IEnumerable<Guid> taskIds) {
|
---|
112 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
113 |
|
---|
114 | return trans.UseTransaction(() => {
|
---|
115 | var tasks = dao.GetTasks(x => taskIds.Contains(x.TaskId)).Select(x => new LightweightTask(x)).ToArray();
|
---|
116 | foreach (var task in tasks)
|
---|
117 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
118 | return tasks;
|
---|
119 | }, false, false);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public IEnumerable<LightweightTask> GetLightweightChildTasks(Guid? parentTaskId, bool recursive, bool includeParent) {
|
---|
123 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
124 |
|
---|
125 | return trans.UseTransaction(() => {
|
---|
126 | var tasks = GetChildTasks(parentTaskId, recursive, includeParent).Select(x => new LightweightTask(x)).ToArray();
|
---|
127 | foreach (var task in tasks)
|
---|
128 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
129 | return tasks;
|
---|
130 | }, false, false);
|
---|
131 | }
|
---|
132 |
|
---|
133 | public IEnumerable<LightweightTask> GetLightweightJobTasks(Guid jobId) {
|
---|
134 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
135 | author.AuthorizeForJob(jobId, Permission.Read);
|
---|
136 |
|
---|
137 | return trans.UseTransaction(() => {
|
---|
138 | return optimizedDao.GetLightweightTasks(jobId).ToArray();
|
---|
139 | }, false, true);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public IEnumerable<LightweightTask> GetLightweightJobTasksWithoutStateLog(Guid jobId) {
|
---|
143 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
144 | author.AuthorizeForJob(jobId, Permission.Read);
|
---|
145 |
|
---|
146 | return trans.UseTransaction(() => {
|
---|
147 | return dao.GetLightweightTasksWithoutStateLog(task => task.JobId == jobId).ToArray();
|
---|
148 | }, false, false);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public TaskData GetTaskData(Guid taskId) {
|
---|
152 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
153 | author.AuthorizeForTask(taskId, Permission.Read);
|
---|
154 |
|
---|
155 | return trans.UseTransaction(() => {
|
---|
156 | return dao.GetTaskData(taskId);
|
---|
157 | });
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void UpdateTask(Task taskDto) {
|
---|
161 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
162 | author.AuthorizeForTask(taskDto.Id, Permission.Full);
|
---|
163 |
|
---|
164 | trans.UseTransaction(() => {
|
---|
165 | var task = optimizedDao.GetTaskByDto(taskDto);
|
---|
166 | optimizedDao.UpdateTask(task);
|
---|
167 | });
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void UpdateTaskData(Task task, TaskData taskData) {
|
---|
171 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
172 | author.AuthorizeForTask(task.Id, Permission.Full);
|
---|
173 |
|
---|
174 | trans.UseTransaction(() => {
|
---|
175 | var t = optimizedDao.GetTaskByDto(task);
|
---|
176 | optimizedDao.UpdateTask(t);
|
---|
177 | });
|
---|
178 |
|
---|
179 | trans.UseTransaction(() => {
|
---|
180 | var data = optimizedDao.GetTaskDataByDto(taskData);
|
---|
181 | data.LastUpdate = DateTime.Now;
|
---|
182 | optimizedDao.UpdateTaskData(data);
|
---|
183 | });
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void DeleteTask(Guid taskId) {
|
---|
187 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
188 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
189 | trans.UseTransaction(() => {
|
---|
190 | dao.DeleteTask(taskId);
|
---|
191 | });
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void DeleteChildTasks(Guid parentTaskId) {
|
---|
195 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
196 | author.AuthorizeForTask(parentTaskId, Permission.Full);
|
---|
197 | trans.UseTransaction(() => {
|
---|
198 | var tasks = GetChildTasks(parentTaskId, true, false);
|
---|
199 | foreach (var task in tasks) {
|
---|
200 | dao.DeleteTask(task.Id);
|
---|
201 | dao.DeleteTaskData(task.Id);
|
---|
202 | };
|
---|
203 | });
|
---|
204 | }
|
---|
205 |
|
---|
206 | public Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
|
---|
207 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
208 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
209 |
|
---|
210 | return trans.UseTransaction(() => {
|
---|
211 | var task = optimizedDao.UpdateTaskState(taskId, DT.Convert.ToEntity(taskState), slaveId, userId, exception);
|
---|
212 |
|
---|
213 | if (task.Command.HasValue && task.Command.Value == DA.Command.Pause && task.State == DA.TaskState.Paused) {
|
---|
214 | task.Command = null;
|
---|
215 | } else if (task.Command.HasValue && task.Command.Value == DA.Command.Abort && task.State == DA.TaskState.Aborted) {
|
---|
216 | task.Command = null;
|
---|
217 | } else if (task.Command.HasValue && task.Command.Value == DA.Command.Stop && task.State == DA.TaskState.Aborted) {
|
---|
218 | task.Command = null;
|
---|
219 | } else if (taskState == TaskState.Paused && !task.Command.HasValue) {
|
---|
220 | // slave paused and uploaded the task (no user-command) -> set waiting.
|
---|
221 | task = optimizedDao.UpdateTaskState(taskId, DA.TaskState.Waiting, slaveId, userId, exception);
|
---|
222 | }
|
---|
223 |
|
---|
224 | return DT.Convert.ToDto(task);
|
---|
225 | });
|
---|
226 | }
|
---|
227 |
|
---|
228 | public IEnumerable<Task> GetTasksByResourceId(Guid resourceId) {
|
---|
229 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
230 | var tasks = trans.UseTransaction(() => dao.GetJobsByResourceId(resourceId));
|
---|
231 | foreach (var task in tasks)
|
---|
232 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
233 | return tasks;
|
---|
234 | }
|
---|
235 | #endregion
|
---|
236 |
|
---|
237 | #region Task Control Methods
|
---|
238 | public void StopTask(Guid taskId) {
|
---|
239 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
240 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
241 | trans.UseTransaction(() => {
|
---|
242 | var task = dao.GetTask(taskId);
|
---|
243 | if (task.State == TaskState.Calculating || task.State == TaskState.Transferring) {
|
---|
244 | task.Command = Command.Stop;
|
---|
245 | dao.UpdateTask(task);
|
---|
246 | } else {
|
---|
247 | if (task.State != TaskState.Aborted && task.State != TaskState.Finished && task.State != TaskState.Failed) {
|
---|
248 | task = UpdateTaskState(taskId, TaskState.Aborted, null, null, string.Empty);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | });
|
---|
252 | }
|
---|
253 |
|
---|
254 | public void PauseTask(Guid taskId) {
|
---|
255 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
256 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
257 | trans.UseTransaction(() => {
|
---|
258 | var job = dao.GetTask(taskId);
|
---|
259 | if (job.State == TaskState.Calculating || job.State == TaskState.Transferring) {
|
---|
260 | job.Command = Command.Pause;
|
---|
261 | dao.UpdateTask(job);
|
---|
262 | } else {
|
---|
263 | job = UpdateTaskState(taskId, TaskState.Paused, null, null, string.Empty);
|
---|
264 | }
|
---|
265 | });
|
---|
266 | }
|
---|
267 |
|
---|
268 | public void RestartTask(Guid taskId) {
|
---|
269 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
270 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
271 | trans.UseTransaction(() => {
|
---|
272 | Task task = dao.UpdateTaskState(taskId, DA.TaskState.Waiting, null, userManager.CurrentUserId, string.Empty);
|
---|
273 | task.Command = null;
|
---|
274 | dao.UpdateTask(task);
|
---|
275 | });
|
---|
276 | }
|
---|
277 | #endregion
|
---|
278 |
|
---|
279 | #region Job Methods
|
---|
280 | public Job GetJob(Guid id) {
|
---|
281 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
282 | author.AuthorizeForJob(id, Permission.Read);
|
---|
283 | return trans.UseTransaction(() => {
|
---|
284 | var job = dao.GetJobs(x =>
|
---|
285 | x.JobId == id
|
---|
286 | && (x.OwnerUserId == userManager.CurrentUserId || x.JobPermissions.Count(hep => hep.Permission != DA.Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0)
|
---|
287 | ).FirstOrDefault();
|
---|
288 | if (job != null) {
|
---|
289 | job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
290 | job.OwnerUsername = userManager.GetUserById(job.OwnerUserId).UserName;
|
---|
291 | }
|
---|
292 | return job;
|
---|
293 | });
|
---|
294 | }
|
---|
295 |
|
---|
296 | public IEnumerable<Job> GetJobs() {
|
---|
297 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
298 | return trans.UseTransaction(() => {
|
---|
299 | var jobs = dao.GetJobs(x => x.OwnerUserId == userManager.CurrentUserId || x.JobPermissions.Count(hep => hep.Permission != DA.Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0);
|
---|
300 | foreach (var job in jobs) {
|
---|
301 | author.AuthorizeForJob(job.Id, Permission.Read);
|
---|
302 | job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
303 | job.OwnerUsername = userManager.GetUserById(job.OwnerUserId).UserName;
|
---|
304 | }
|
---|
305 | return jobs;
|
---|
306 | });
|
---|
307 | }
|
---|
308 |
|
---|
309 | public IEnumerable<Job> GetAllJobs() {
|
---|
310 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
311 | return trans.UseTransaction(() => {
|
---|
312 | var jobs = dao.GetJobs(x => true);
|
---|
313 | foreach (var job in jobs) { // no authorization here, since this method is admin-only! (admin is allowed to read all task)
|
---|
314 | job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
315 | job.OwnerUsername = userManager.GetUserById(job.OwnerUserId).UserName;
|
---|
316 | }
|
---|
317 | return jobs;
|
---|
318 | });
|
---|
319 | }
|
---|
320 |
|
---|
321 | public Guid AddJob(Job jobDto) {
|
---|
322 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
323 | return trans.UseTransaction(() => {
|
---|
324 | jobDto.OwnerUserId = userManager.CurrentUserId;
|
---|
325 | jobDto.DateCreated = DateTime.Now;
|
---|
326 | return dao.AddJob(jobDto);
|
---|
327 | });
|
---|
328 | }
|
---|
329 |
|
---|
330 | public void UpdateJob(Job jobDto) {
|
---|
331 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
332 | author.AuthorizeForJob(jobDto.Id, Permission.Full);
|
---|
333 | trans.UseTransaction(() => {
|
---|
334 | dao.UpdateJob(jobDto);
|
---|
335 | });
|
---|
336 | }
|
---|
337 |
|
---|
338 | public void DeleteJob(Guid jobId) {
|
---|
339 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
340 | author.AuthorizeForJob(jobId, Permission.Full);
|
---|
341 | trans.UseTransaction(() => {
|
---|
342 | dao.DeleteJob(jobId); // child task will be deleted by db-trigger
|
---|
343 | });
|
---|
344 | }
|
---|
345 | #endregion
|
---|
346 |
|
---|
347 | #region JobPermission Methods
|
---|
348 | public void GrantPermission(Guid jobId, Guid grantedUserId, Permission permission) {
|
---|
349 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
350 | trans.UseTransaction(() => {
|
---|
351 | Job job = dao.GetJob(jobId);
|
---|
352 | if (job == null) throw new FaultException<FaultReason>(new FaultReason("Could not find task with id " + jobId));
|
---|
353 | Permission perm = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
354 | if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
|
---|
355 | dao.SetJobPermission(jobId, userManager.CurrentUserId, grantedUserId, DT.Convert.ToEntity(permission));
|
---|
356 | });
|
---|
357 | }
|
---|
358 |
|
---|
359 | public void RevokePermission(Guid jobId, Guid grantedUserId) {
|
---|
360 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
361 | trans.UseTransaction(() => {
|
---|
362 | Job job = dao.GetJob(jobId);
|
---|
363 | if (job == null) throw new FaultException<FaultReason>(new FaultReason("Could not find task with id " + jobId));
|
---|
364 | DA.Permission perm = dao.GetPermissionForJob(job.Id, userManager.CurrentUserId);
|
---|
365 | if (perm != DA.Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
|
---|
366 | dao.SetJobPermission(jobId, userManager.CurrentUserId, grantedUserId, DA.Permission.NotAllowed);
|
---|
367 | });
|
---|
368 | }
|
---|
369 |
|
---|
370 | public IEnumerable<JobPermission> GetJobPermissions(Guid jobId) {
|
---|
371 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
372 | return trans.UseTransaction(() => {
|
---|
373 | DA.Permission currentUserPermission = dao.GetPermissionForJob(jobId, userManager.CurrentUserId);
|
---|
374 | if (currentUserPermission != DA.Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to list permissions for this experiment"));
|
---|
375 | return dao.GetJobPermissions(x => x.JobId == jobId);
|
---|
376 | });
|
---|
377 | }
|
---|
378 |
|
---|
379 | public bool IsAllowedPrivileged() {
|
---|
380 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
381 | return authen.IsInRole(HiveRoles.IsAllowedPrivileged);
|
---|
382 | }
|
---|
383 | #endregion
|
---|
384 |
|
---|
385 | #region Login Methods
|
---|
386 | public void Hello(Slave slaveInfo) {
|
---|
387 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
388 | if (userManager.CurrentUser.UserName != "hiveslave")
|
---|
389 | slaveInfo.OwnerUserId = userManager.CurrentUserId;
|
---|
390 |
|
---|
391 | trans.UseTransaction(() => {
|
---|
392 | var slave = dao.GetSlave(slaveInfo.Id);
|
---|
393 |
|
---|
394 | if (slave == null) {
|
---|
395 | var healGroup = dao.GetResources(x => x.Name == "HEAL").FirstOrDefault();
|
---|
396 | if (healGroup != null) {
|
---|
397 | slaveInfo.ParentResourceId = healGroup.Id;
|
---|
398 | }
|
---|
399 | dao.AddSlave(slaveInfo);
|
---|
400 | } else {
|
---|
401 | slave.Name = slaveInfo.Name;
|
---|
402 | slave.Description = slaveInfo.Description;
|
---|
403 | slave.OwnerUserId = slaveInfo.OwnerUserId;
|
---|
404 |
|
---|
405 | slave.Cores = slaveInfo.Cores;
|
---|
406 | slave.CpuArchitecture = slaveInfo.CpuArchitecture;
|
---|
407 | slave.CpuSpeed = slaveInfo.CpuSpeed;
|
---|
408 | slave.FreeCores = slaveInfo.FreeCores;
|
---|
409 | slave.FreeMemory = slaveInfo.FreeMemory;
|
---|
410 | slave.Memory = slaveInfo.Memory;
|
---|
411 | slave.OperatingSystem = slaveInfo.OperatingSystem;
|
---|
412 |
|
---|
413 | slave.LastHeartbeat = DateTime.Now;
|
---|
414 | slave.SlaveState = SlaveState.Idle;
|
---|
415 |
|
---|
416 | // don't update those properties: dbSlave.IsAllowedToCalculate, dbSlave.ParentResourceId
|
---|
417 |
|
---|
418 | dao.UpdateSlave(slave);
|
---|
419 | }
|
---|
420 | });
|
---|
421 | }
|
---|
422 |
|
---|
423 | public void GoodBye(Guid slaveId) {
|
---|
424 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
425 | trans.UseTransaction(() => {
|
---|
426 | var slave = dao.GetSlave(slaveId);
|
---|
427 | if (slave != null) {
|
---|
428 | slave.SlaveState = SlaveState.Offline;
|
---|
429 | dao.UpdateSlave(slave);
|
---|
430 | }
|
---|
431 | });
|
---|
432 | }
|
---|
433 | #endregion
|
---|
434 |
|
---|
435 | #region Heartbeat Methods
|
---|
436 | public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
|
---|
437 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
438 |
|
---|
439 | List<MessageContainer> result = new List<MessageContainer>();
|
---|
440 | try {
|
---|
441 | result = heartbeatManager.ProcessHeartbeat(heartbeat);
|
---|
442 | }
|
---|
443 | catch (Exception ex) {
|
---|
444 | DA.LogFactory.GetLogger(this.GetType().Namespace).Log("Exception processing Heartbeat: " + ex.ToString());
|
---|
445 | }
|
---|
446 |
|
---|
447 | if (HeuristicLab.Services.Hive.Properties.Settings.Default.TriggerEventManagerInHeartbeat) {
|
---|
448 | TriggerEventManager(false);
|
---|
449 | }
|
---|
450 |
|
---|
451 | return result;
|
---|
452 | }
|
---|
453 | #endregion
|
---|
454 |
|
---|
455 | #region Plugin Methods
|
---|
456 | public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
|
---|
457 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
458 | return trans.UseTransaction(() => {
|
---|
459 | plugin.UserId = userManager.CurrentUserId;
|
---|
460 | plugin.DateCreated = DateTime.Now;
|
---|
461 |
|
---|
462 | var existing = dao.GetPlugins(x => x.Hash != null).Where(x => x.Hash.SequenceEqual(plugin.Hash));
|
---|
463 | if (existing.Count() > 0) {
|
---|
464 | // a plugin already exists.
|
---|
465 | throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(existing.Single().Id));
|
---|
466 | }
|
---|
467 |
|
---|
468 | Guid pluginId = dao.AddPlugin(plugin);
|
---|
469 | foreach (PluginData pluginData in pluginDatas) {
|
---|
470 | pluginData.PluginId = pluginId;
|
---|
471 | dao.AddPluginData(pluginData);
|
---|
472 | }
|
---|
473 | return pluginId;
|
---|
474 | });
|
---|
475 | }
|
---|
476 |
|
---|
477 | public Plugin GetPlugin(Guid pluginId) {
|
---|
478 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
479 | return trans.UseTransaction(() => {
|
---|
480 | return DT.Convert.ToDto(optimizedDao.GetPluginById(pluginId));
|
---|
481 | });
|
---|
482 | }
|
---|
483 |
|
---|
484 | public Plugin GetPluginByHash(byte[] hash) {
|
---|
485 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
486 | return trans.UseTransaction(() => {
|
---|
487 | return dao.GetPlugins(x => x.Hash == hash).FirstOrDefault();
|
---|
488 | });
|
---|
489 | }
|
---|
490 |
|
---|
491 | // note: this is a possible security problem, since a client is able to download all plugins, which may contain proprietary code (which can be disassembled)
|
---|
492 | // change so that only with GetPluginByHash it is possible to download plugins
|
---|
493 | public IEnumerable<Plugin> GetPlugins() {
|
---|
494 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
495 | return trans.UseTransaction(() => {
|
---|
496 | return dao.GetPlugins(x => x.Hash != null);
|
---|
497 | });
|
---|
498 | }
|
---|
499 |
|
---|
500 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
501 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
502 | var pluginDatas = new List<PluginData>();
|
---|
503 | return trans.UseTransaction(() => {
|
---|
504 | foreach (Guid guid in pluginIds) {
|
---|
505 | pluginDatas.AddRange(dao.GetPluginDatas(x => x.PluginId == guid).ToList());
|
---|
506 | }
|
---|
507 | return pluginDatas;
|
---|
508 | });
|
---|
509 | }
|
---|
510 |
|
---|
511 | public void DeletePlugin(Guid pluginId) {
|
---|
512 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
513 | trans.UseTransaction(() => {
|
---|
514 | dao.DeletePlugin(pluginId);
|
---|
515 | });
|
---|
516 | }
|
---|
517 | #endregion
|
---|
518 |
|
---|
519 | #region ResourcePermission Methods
|
---|
520 | public void GrantResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
|
---|
521 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
522 | trans.UseTransaction(() => {
|
---|
523 | Resource resource = dao.GetResource(resourceId);
|
---|
524 | if (resource == null) throw new FaultException<FaultReason>(new FaultReason("Could not find resource with id " + resourceId));
|
---|
525 | if (resource.OwnerUserId != userManager.CurrentUserId && !authen.IsInRole(HiveRoles.Administrator)) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permission for this resource"));
|
---|
526 | foreach (Guid id in grantedUserIds)
|
---|
527 | dao.AddResourcePermission(new ResourcePermission { ResourceId = resourceId, GrantedByUserId = userManager.CurrentUserId, GrantedUserId = id });
|
---|
528 | });
|
---|
529 | }
|
---|
530 |
|
---|
531 | public void RevokeResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
|
---|
532 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
533 | trans.UseTransaction(() => {
|
---|
534 | Resource resource = dao.GetResource(resourceId);
|
---|
535 | if (resource == null) throw new FaultException<FaultReason>(new FaultReason("Could not find resource with id " + resourceId));
|
---|
536 | if (resource.OwnerUserId != userManager.CurrentUserId && !authen.IsInRole(HiveRoles.Administrator)) throw new FaultException<FaultReason>(new FaultReason("Not allowed to revoke permission for this resource"));
|
---|
537 | foreach (Guid id in grantedUserIds)
|
---|
538 | dao.DeleteResourcePermission(resourceId, id);
|
---|
539 | });
|
---|
540 | }
|
---|
541 |
|
---|
542 | public IEnumerable<ResourcePermission> GetResourcePermissions(Guid resourceId) {
|
---|
543 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
544 | return trans.UseTransaction(() => {
|
---|
545 | Resource resource = dao.GetResource(resourceId);
|
---|
546 | if (resource == null) throw new FaultException<FaultReason>(new FaultReason("Could not find resource with id " + resourceId));
|
---|
547 | return dao.GetResourcePermissions(x => x.ResourceId == resourceId);
|
---|
548 | });
|
---|
549 | }
|
---|
550 | #endregion
|
---|
551 |
|
---|
552 | #region Resource Methods
|
---|
553 | public IEnumerable<Resource> GetChildResources(Guid resourceId) {
|
---|
554 | return trans.UseTransaction(() => { return dao.GetChildResources(resourceId); });
|
---|
555 | }
|
---|
556 | #endregion
|
---|
557 |
|
---|
558 | #region Slave Methods
|
---|
559 | public int GetNewHeartbeatInterval(Guid slaveId) {
|
---|
560 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
561 |
|
---|
562 | Slave s = trans.UseTransaction(() => { return dao.GetSlave(slaveId); });
|
---|
563 | if (s != null) {
|
---|
564 | return s.HbInterval;
|
---|
565 | } else {
|
---|
566 | return -1;
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | public Guid AddSlave(Slave slave) {
|
---|
571 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
572 | return trans.UseTransaction(() => dao.AddSlave(slave));
|
---|
573 | }
|
---|
574 |
|
---|
575 | public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
|
---|
576 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
577 | return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
|
---|
578 | }
|
---|
579 |
|
---|
580 | public Slave GetSlave(Guid slaveId) {
|
---|
581 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
582 | return trans.UseTransaction(() => { return dao.GetSlave(slaveId); });
|
---|
583 | }
|
---|
584 |
|
---|
585 | public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
|
---|
586 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
587 | return trans.UseTransaction(() => { return dao.GetSlaveGroup(slaveGroupId); });
|
---|
588 | }
|
---|
589 |
|
---|
590 | public IEnumerable<Slave> GetSlaves() {
|
---|
591 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
592 | return trans.UseTransaction(() => {
|
---|
593 | return dao.GetSlaves(x => true).Where(x => x.OwnerUserId == null
|
---|
594 | || x.OwnerUserId == userManager.CurrentUserId
|
---|
595 | || userManager.VerifyUser(userManager.CurrentUserId, GetResourcePermissions(x.Id).Select(y => y.GrantedUserId).ToList())
|
---|
596 | || authen.IsInRole(HiveRoles.Administrator)).ToArray();
|
---|
597 | });
|
---|
598 | }
|
---|
599 |
|
---|
600 | public IEnumerable<SlaveGroup> GetSlaveGroups() {
|
---|
601 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
602 | return trans.UseTransaction(() => {
|
---|
603 | return dao.GetSlaveGroups(x => true).Where(x => x.OwnerUserId == null
|
---|
604 | || x.OwnerUserId == userManager.CurrentUserId
|
---|
605 | || userManager.VerifyUser(userManager.CurrentUserId, GetResourcePermissions(x.Id).Select(y => y.GrantedUserId).ToList())
|
---|
606 | || authen.IsInRole(HiveRoles.Administrator)).ToArray();
|
---|
607 | });
|
---|
608 | }
|
---|
609 |
|
---|
610 | public void UpdateSlave(Slave slave) {
|
---|
611 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
612 | trans.UseTransaction(() => {
|
---|
613 | dao.UpdateSlave(slave);
|
---|
614 | });
|
---|
615 | }
|
---|
616 |
|
---|
617 | public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
|
---|
618 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
619 | trans.UseTransaction(() => {
|
---|
620 | dao.UpdateSlaveGroup(slaveGroup);
|
---|
621 | });
|
---|
622 | }
|
---|
623 |
|
---|
624 | public void DeleteSlave(Guid slaveId) {
|
---|
625 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
626 | author.AuthorizeForResourceAdministration(slaveId);
|
---|
627 | trans.UseTransaction(() => {
|
---|
628 | dao.DeleteSlave(slaveId);
|
---|
629 | });
|
---|
630 | }
|
---|
631 |
|
---|
632 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
633 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
634 | author.AuthorizeForResourceAdministration(slaveGroupId);
|
---|
635 | trans.UseTransaction(() => {
|
---|
636 | dao.DeleteSlaveGroup(slaveGroupId);
|
---|
637 | });
|
---|
638 | }
|
---|
639 |
|
---|
640 | public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
641 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
642 | trans.UseTransaction(() => {
|
---|
643 | var resource = dao.GetResource(resourceId);
|
---|
644 | resource.ParentResourceId = slaveGroupId;
|
---|
645 | dao.UpdateResource(resource);
|
---|
646 | });
|
---|
647 | }
|
---|
648 |
|
---|
649 | public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
650 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
651 | trans.UseTransaction(() => {
|
---|
652 | var resource = dao.GetResource(resourceId);
|
---|
653 | resource.ParentResourceId = null;
|
---|
654 | dao.UpdateResource(resource);
|
---|
655 | });
|
---|
656 | }
|
---|
657 |
|
---|
658 | public Guid GetResourceId(string resourceName) {
|
---|
659 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
660 | return trans.UseTransaction(() => {
|
---|
661 | var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
|
---|
662 | if (resource != null) {
|
---|
663 | return resource.Id;
|
---|
664 | } else {
|
---|
665 | return Guid.Empty;
|
---|
666 | }
|
---|
667 | });
|
---|
668 | }
|
---|
669 |
|
---|
670 | public void TriggerEventManager(bool force) {
|
---|
671 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Slave);
|
---|
672 | // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
|
---|
673 | bool cleanup = false;
|
---|
674 | trans.UseTransaction(() => {
|
---|
675 | DateTime lastCleanup = dao.GetLastCleanup();
|
---|
676 | if (force || DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
|
---|
677 | dao.SetLastCleanup(DateTime.Now);
|
---|
678 | cleanup = true;
|
---|
679 | }
|
---|
680 | }, true);
|
---|
681 |
|
---|
682 | if (cleanup) {
|
---|
683 | eventManager.Cleanup();
|
---|
684 | }
|
---|
685 | }
|
---|
686 | #endregion
|
---|
687 |
|
---|
688 | #region Downtime Methods
|
---|
689 | public Guid AddDowntime(Downtime downtime) {
|
---|
690 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
691 | author.AuthorizeForResourceAdministration(downtime.ResourceId);
|
---|
692 | return trans.UseTransaction(() => dao.AddDowntime(downtime));
|
---|
693 | }
|
---|
694 |
|
---|
695 | public void DeleteDowntime(Guid downtimeId) {
|
---|
696 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
697 | // TODO: pass resource id
|
---|
698 | // author.AuthorizeForResource(resourceId);
|
---|
699 | trans.UseTransaction(() => {
|
---|
700 | dao.DeleteDowntime(downtimeId);
|
---|
701 | });
|
---|
702 | }
|
---|
703 |
|
---|
704 | public void UpdateDowntime(Downtime downtime) {
|
---|
705 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
706 | author.AuthorizeForResourceAdministration(downtime.ResourceId);
|
---|
707 | trans.UseTransaction(() => {
|
---|
708 | dao.UpdateDowntime(downtime);
|
---|
709 | });
|
---|
710 | }
|
---|
711 |
|
---|
712 | public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
|
---|
713 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
714 | return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
|
---|
715 | }
|
---|
716 | #endregion
|
---|
717 |
|
---|
718 | #region User Methods
|
---|
719 | public string GetUsernameByUserId(Guid userId) {
|
---|
720 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
721 | var user = ServiceLocator.Instance.UserManager.GetUserById(userId);
|
---|
722 | if (user != null)
|
---|
723 | return user.UserName;
|
---|
724 | else
|
---|
725 | return null;
|
---|
726 | }
|
---|
727 |
|
---|
728 | public Guid GetUserIdByUsername(string username) {
|
---|
729 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
730 | var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
|
---|
731 | return user != null ? (Guid)user.ProviderUserKey : Guid.Empty;
|
---|
732 | }
|
---|
733 | #endregion
|
---|
734 |
|
---|
735 | #region UserPriority Methods
|
---|
736 | public IEnumerable<UserPriority> GetUserPriorities() {
|
---|
737 | return trans.UseTransaction(() => dao.GetUserPriorities(x => true));
|
---|
738 | }
|
---|
739 | #endregion
|
---|
740 |
|
---|
741 | #region Helper Methods
|
---|
742 | private IEnumerable<Task> GetChildTasks(Guid? parentTaskId, bool recursive, bool includeParent) {
|
---|
743 | var tasks = new List<Task>(dao.GetTasks(x => parentTaskId == null ? !x.ParentTaskId.HasValue : x.ParentTaskId.Value == parentTaskId));
|
---|
744 |
|
---|
745 | if (recursive) {
|
---|
746 | var childs = new List<Task>();
|
---|
747 | foreach (var task in tasks) {
|
---|
748 | childs.AddRange(GetChildTasks(task.Id, recursive, false));
|
---|
749 | }
|
---|
750 | tasks.AddRange(childs);
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (includeParent) tasks.Add(GetTask(parentTaskId.Value));
|
---|
754 | return tasks;
|
---|
755 | }
|
---|
756 | #endregion
|
---|
757 |
|
---|
758 | #region Statistics Methods
|
---|
759 | public IEnumerable<Statistics> GetStatistics() {
|
---|
760 | return trans.UseTransaction(() => { return dao.GetStatistics(x => true); });
|
---|
761 | }
|
---|
762 | public IEnumerable<Statistics> GetStatisticsForTimePeriod(DateTime from, DateTime to) {
|
---|
763 | return trans.UseTransaction(() => { return dao.GetStatistics(x => x.Timestamp >= from && x.Timestamp <= to); });
|
---|
764 | }
|
---|
765 | #endregion
|
---|
766 | }
|
---|
767 | }
|
---|