Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs @ 6479

Last change on this file since 6479 was 6479, checked in by cneumuel, 13 years ago

#1233

  • finished experiment sharing
  • added role for executing privileged jobs
  • refreshing experiments in experimentManager does not delete already downloaded jobs
  • moved some properties from HiveExperiment into RefreshableHiveExperiment
File size: 25.0 KB
RevLine 
[6431]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
[5028]23using System.Collections.Generic;
24using System.Linq;
25using System.ServiceModel;
[5375]26using HeuristicLab.Services.Hive.Common;
27using HeuristicLab.Services.Hive.Common.DataTransfer;
[5028]28using HeuristicLab.Services.Hive.Common.ServiceContracts;
29
30namespace HeuristicLab.Services.Hive {
31
32  /// <summary>
33  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
[5599]34  /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
[5028]35  /// </summary>
[5599]36  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
[5028]37  public class HiveService : IHiveService {
38    private DataAccess.IHiveDao dao {
39      get { return ServiceLocator.Instance.HiveDao; }
40    }
[6362]41    private IAuthenticationManager authen {
42      get { return ServiceLocator.Instance.AuthenticationManager; }
43    }
44    private IAuthorizationManager author {
[5028]45      get { return ServiceLocator.Instance.AuthorizationManager; }
[6457]46    }
[6452]47    private ITransactionManager trans {
48      get { return ServiceLocator.Instance.TransactionManager; }
[5028]49    }
[5095]50    private ILifecycleManager lifecycleManager {
51      get { return ServiceLocator.Instance.LifecycleManager; }
[5028]52    }
[6463]53    private IUserManager userManager {
54      get { return ServiceLocator.Instance.UserManager; }
55    }
[5405]56    private HeartbeatManager heartbeatManager {
57      get { return ServiceLocator.Instance.HeartbeatManager; }
58    }
[5028]59
60    #region Job Methods
[5511]61    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> resourceIds) {
[6362]62      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6452]63      return trans.UseTransaction(() => {
[5155]64        job.Id = dao.AddJob(job);
65        jobData.JobId = job.Id;
[5106]66        jobData.LastUpdate = DateTime.Now;
[6367]67        foreach (Guid slaveGroupId in resourceIds) {
68          dao.AssignJobToResource(job.Id, slaveGroupId);
[5155]69        }
[5028]70        dao.AddJobData(jobData);
[6463]71        dao.UpdateJobState(job.Id, JobState.Waiting, null, userManager.CurrentUserId, null);
[5028]72        return jobData.JobId;
[6419]73      }, false, true);
[5028]74    }
75
76    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
[6362]77      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6452]78      return trans.UseTransaction(() => {
[5028]79        job.ParentJobId = parentJobId;
[5155]80        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
[6419]81      }, false, true);
[5028]82    }
83
84    public Job GetJob(Guid jobId) {
[6362]85      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]86      author.AuthorizeForJob(jobId, Permission.Read);
[5028]87      return dao.GetJob(jobId);
88    }
89
90    public IEnumerable<Job> GetJobs() {
[6362]91      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]92      var jobs = dao.GetJobs(x => true);
93      foreach (var job in jobs)
94        author.AuthorizeForJob(job.Id, Permission.Read);
95      return jobs;
[5028]96    }
97
98    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
[6362]99      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]100      var jobs = dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
101      foreach (var job in jobs)
102        author.AuthorizeForJob(job.Id, Permission.Read);
103      return jobs;
[5028]104    }
105
106    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
[6362]107      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]108      var jobs = GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
109      foreach (var job in jobs)
110        author.AuthorizeForJob(job.Id, Permission.Read);
111      return jobs;
[5028]112    }
113
[6006]114    public IEnumerable<LightweightJob> GetLightweightExperimentJobs(Guid experimentId) {
[6362]115      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]116      author.AuthorizeForExperiment(experimentId, Permission.Read);
[6006]117      return dao.GetJobs(x => x.HiveExperimentId == experimentId).Select(x => new LightweightJob(x)).ToArray();
118    }
119
[5028]120    public JobData GetJobData(Guid jobId) {
[6362]121      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]122      author.AuthorizeForJob(jobId, Permission.Read);
[5028]123      return dao.GetJobData(jobId);
124    }
125
[5511]126    public void UpdateJob(Job job) {
[6362]127      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]128      author.AuthorizeForJob(job.Id, Permission.Full);
[6452]129      trans.UseTransaction(() => {
[5511]130        dao.UpdateJob(job);
[5708]131      });
[5511]132    }
133
134    public void UpdateJobData(Job job, JobData jobData) {
[6362]135      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]136      author.AuthorizeForJob(job.Id, Permission.Full);
137      author.AuthorizeForJob(jobData.JobId, Permission.Full);
[6452]138      //trans.UseTransaction(() => { // cneumuel: try without transaction
[6426]139      jobData.LastUpdate = DateTime.Now;
140      dao.UpdateJob(job);
141      dao.UpdateJobData(jobData);
[6419]142      //}, false, true);
[5028]143    }
144
[5106]145    public void DeleteJob(Guid jobId) {
[6362]146      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]147      author.AuthorizeForJob(jobId, Permission.Full);
[6452]148      trans.UseTransaction(() => {
[5106]149        dao.DeleteJob(jobId);
[5708]150      });
[5028]151    }
152
[5106]153    public void DeleteChildJobs(Guid parentJobId) {
[6362]154      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]155      author.AuthorizeForJob(parentJobId, Permission.Full);
[6452]156      trans.UseTransaction(() => {
[5106]157        var jobs = GetChildJobs(parentJobId, true, false);
[5028]158        foreach (var job in jobs) {
159          dao.DeleteJob(job.Id);
160          dao.DeleteJobData(job.Id);
161        };
[5708]162      });
[5028]163    }
[5102]164
[5636]165    public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
[6362]166      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]167      author.AuthorizeForJob(jobId, Permission.Full);
[6452]168      return trans.UseTransaction(() => {
[5779]169        Job job = dao.UpdateJobState(jobId, jobState, slaveId, userId, exception);
[6267]170
[5779]171        if (job.Command.HasValue && job.Command.Value == Command.Pause && job.State == JobState.Paused) {
172          job.Command = null;
173        } else if (job.Command.HasValue && job.Command.Value == Command.Abort && job.State == JobState.Aborted) {
174          job.Command = null;
175        } else if (job.Command.HasValue && job.Command.Value == Command.Stop && job.State == JobState.Aborted) {
176          job.Command = null;
[6110]177        } else if (jobState == JobState.Paused && !job.Command.HasValue) {
[6367]178          // slave paused and uploaded the job (no user-command) -> set waiting.
[6110]179          job = dao.UpdateJobState(jobId, JobState.Waiting, slaveId, userId, exception);
[5779]180        }
[6110]181
[5779]182        dao.UpdateJob(job);
183        return job;
184      });
[5636]185    }
[6452]186
187    public IEnumerable<Job> GetJobsByResourceId(Guid resourceId) {
188      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6463]189      var jobs = trans.UseTransaction(() => dao.GetJobsByResourceId(resourceId));
190      foreach(var job in jobs)
191        author.AuthorizeForJob(job.Id, Permission.Read);
192      return jobs;
[6452]193    }
[5028]194    #endregion
195
196    #region Job Control Methods
[5053]197    public void StopJob(Guid jobId) {
[6362]198      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]199      author.AuthorizeForJob(jobId, Permission.Full);
[6452]200      trans.UseTransaction(() => {
[5779]201        var job = dao.GetJob(jobId);
[6168]202        if (job.State == JobState.Calculating || job.State == JobState.Transferring) {
203          job.Command = Command.Stop;
204          dao.UpdateJob(job);
205        } else {
206          if (job.State != JobState.Aborted && job.State != JobState.Finished && job.State != JobState.Failed) {
207            job = UpdateJobState(jobId, JobState.Aborted, null, null, string.Empty);
208          }
209        }
[5708]210      });
[5028]211    }
[5526]212
[5062]213    public void PauseJob(Guid jobId) {
[6362]214      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]215      author.AuthorizeForJob(jobId, Permission.Full);
[6452]216      trans.UseTransaction(() => {
[5779]217        var job = dao.GetJob(jobId);
[6168]218        if (job.State == JobState.Calculating || job.State == JobState.Transferring) {
219          job.Command = Command.Pause;
220          dao.UpdateJob(job);
221        } else {
222          job = UpdateJobState(jobId, JobState.Paused, null, null, string.Empty);
223        }
[5708]224      });
[5028]225    }
[5779]226
227    public void RestartJob(Guid jobId) {
[6362]228      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]229      author.AuthorizeForJob(jobId, Permission.Full);
[6452]230      trans.UseTransaction(() => {
[6463]231        Job job = dao.UpdateJobState(jobId, JobState.Waiting, null, userManager.CurrentUserId, string.Empty);
[5779]232        job.Command = null;
233        dao.UpdateJob(job);
234      });
235    }
[5028]236    #endregion
237
238    #region HiveExperiment Methods
239    public HiveExperiment GetHiveExperiment(Guid id) {
[6362]240      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]241      author.AuthorizeForExperiment(id, Permission.Read);
[6457]242      var hiveExperiment = dao.GetHiveExperiments(x =>
243            x.HiveExperimentId == id
[6463]244            && (x.OwnerUserId == userManager.CurrentUserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0)
[6457]245          ).FirstOrDefault();
[6465]246      if (hiveExperiment != null) {
247        hiveExperiment.Permission = dao.GetPermissionForExperiment(hiveExperiment.Id, userManager.CurrentUserId);
248        hiveExperiment.OwnerUsername = userManager.GetUserById(hiveExperiment.OwnerUserId).UserName;
249      }
[6457]250      return hiveExperiment;
[5028]251    }
252
253    public IEnumerable<HiveExperiment> GetHiveExperiments() {
[6362]254      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]255      var hiveExperiments = dao.GetHiveExperiments(x => x.OwnerUserId == userManager.CurrentUserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0);
256      foreach (var he in hiveExperiments) {
257        author.AuthorizeForExperiment(he.Id, Permission.Read);
258        he.Permission = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
[6465]259        he.OwnerUsername = userManager.GetUserById(he.OwnerUserId).UserName;
[6463]260      }
[6457]261      return hiveExperiments;
[5028]262    }
263
[5602]264    public IEnumerable<HiveExperiment> GetAllHiveExperiments() {
[6362]265      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6457]266      var hiveExperiments = dao.GetHiveExperiments(x => true);
[6465]267      foreach (var he in hiveExperiments) { // no authorization here, since this method is admin-only! (admin is allowed to read all jobs)
[6463]268        he.Permission = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
[6465]269        he.OwnerUsername = userManager.GetUserById(he.OwnerUserId).UserName;
270      }
[6457]271      return hiveExperiments;
[5602]272    }
273
[5028]274    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
[6362]275      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6452]276      return trans.UseTransaction(() => {
[6463]277        hiveExperimentDto.OwnerUserId = userManager.CurrentUserId;
[5106]278        hiveExperimentDto.DateCreated = DateTime.Now;
[5028]279        return dao.AddHiveExperiment(hiveExperimentDto);
[5708]280      });
[5028]281    }
282
283    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
[6362]284      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]285      author.AuthorizeForExperiment(hiveExperimentDto.Id, Permission.Full);
[6452]286      trans.UseTransaction(() => {
[5028]287        dao.UpdateHiveExperiment(hiveExperimentDto);
[5708]288      });
[5028]289    }
290
291    public void DeleteHiveExperiment(Guid hiveExperimentId) {
[6362]292      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6463]293      author.AuthorizeForExperiment(hiveExperimentId, Permission.Full);
[6452]294      trans.UseTransaction(() => {
[5106]295        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
[5708]296      });
[5028]297    }
298    #endregion
[5106]299
[6457]300    #region HiveExperimentPermission Methods
301    public void GrantPermission(Guid hiveExperimentId, Guid grantedUserId, Permission permission) {
302      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
303      trans.UseTransaction(() => {
304        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
305        if (he == null) throw new FaultException<FaultReason>(new FaultReason("Could not find hiveExperiment with id " + hiveExperimentId));
[6463]306        Permission perm = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
[6457]307        if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
[6463]308        dao.SetHiveExperimentPermission(hiveExperimentId, userManager.CurrentUserId, grantedUserId, permission);
[6457]309      });
310    }
311
312    public void RevokePermission(Guid hiveExperimentId, Guid grantedUserId) {
313      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
314      trans.UseTransaction(() => {
315        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
316        if (he == null) throw new FaultException<FaultReason>(new FaultReason("Could not find hiveExperiment with id " + hiveExperimentId));
[6463]317        Permission perm = dao.GetPermissionForExperiment(he.Id, userManager.CurrentUserId);
[6457]318        if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
[6463]319        dao.SetHiveExperimentPermission(hiveExperimentId, userManager.CurrentUserId, grantedUserId, Permission.NotAllowed);
[6457]320      });
321    }
[6463]322    public IEnumerable<HiveExperimentPermission> GetHiveExperimentPermissions(Guid hiveExperimentId) {
323      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
324      return trans.UseTransaction(() => {
325        Permission currentUserPermission = dao.GetPermissionForExperiment(hiveExperimentId, userManager.CurrentUserId);
326        if (currentUserPermission != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to list permissions for this experiment"));
327        return dao.GetHiveExperimentPermissions(x => x.HiveExperimentId == hiveExperimentId);
328      });
329    }
[6479]330
331    public bool IsAllowedPrivileged() {
332      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
333      return authen.IsInRole(HiveRoles.IsAllowedPrivileged);
334    }
[6457]335    #endregion
336
[5028]337    #region Login Methods
[5405]338    public void Hello(Slave slaveInfo) {
[6362]339      authen.AuthenticateForAnyRole(HiveRoles.Slave);
[6452]340      trans.UseTransaction(() => {
[5405]341        var slave = dao.GetSlave(slaveInfo.Id);
[5375]342
343        if (slave == null) {
[5405]344          dao.AddSlave(slaveInfo);
[5375]345        } else {
[6000]346          var dbSlave = dao.GetSlave(slaveInfo.Id);
347
348          dbSlave.Name = slaveInfo.Name;
349          dbSlave.Description = slaveInfo.Description;
350
351          dbSlave.Cores = slaveInfo.Cores;
352          dbSlave.CpuArchitecture = slaveInfo.CpuArchitecture;
353          dbSlave.CpuSpeed = slaveInfo.CpuSpeed;
354          dbSlave.FreeCores = slaveInfo.FreeCores;
355          dbSlave.FreeMemory = slaveInfo.FreeMemory;
356          dbSlave.Memory = slaveInfo.Memory;
357          dbSlave.OperatingSystem = slaveInfo.OperatingSystem;
[6267]358
359          dbSlave.LastHeartbeat = DateTime.Now;
[6000]360          dbSlave.SlaveState = SlaveState.Idle;
361
[6367]362          // don't update those properties: dbSlave.IsAllowedToCalculate, dbSlave.ParentResourceId
[6000]363
364          dao.UpdateSlave(dbSlave);
[5375]365        }
[5708]366      });
[5053]367    }
[5028]368
[5375]369    public void GoodBye(Guid slaveId) {
[6362]370      authen.AuthenticateForAnyRole(HiveRoles.Slave);
[6452]371      trans.UseTransaction(() => {
[5375]372        var slave = dao.GetSlave(slaveId);
373        if (slave != null) {
374          slave.SlaveState = SlaveState.Offline;
375          dao.UpdateSlave(slave);
376        }
[5708]377      });
[5053]378    }
[5028]379    #endregion
380
381    #region Heartbeat Methods
[5053]382    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
[6362]383      authen.AuthenticateForAnyRole(HiveRoles.Slave);
[5636]384      TriggerLifecycle(false);
[6452]385      return trans.UseTransaction(() => heartbeatManager.ProcessHeartbeat(heartbeat));
[5028]386    }
387    #endregion
388
389    #region Plugin Methods
390    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
[6362]391      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6452]392      return trans.UseTransaction(() => {
[6463]393        plugin.UserId = userManager.CurrentUserId;
[5402]394        plugin.DateCreated = DateTime.Now;
[6407]395
[6426]396        var existing = dao.GetPlugins(x => x.Hash != null).Where(x => x.Hash.SequenceEqual(plugin.Hash));
397        if (existing.Count() > 0) {
398          // a plugin already exists.
399          throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(existing.Single().Id));
[6000]400        }
[6426]401
[5028]402        Guid pluginId = dao.AddPlugin(plugin);
403        foreach (PluginData pluginData in pluginDatas) {
404          pluginData.PluginId = pluginId;
405          dao.AddPluginData(pluginData);
406        }
407        return pluginId;
[5708]408      });
[5028]409    }
[5526]410
[6000]411    public Plugin GetPlugin(Guid pluginId) {
[6362]412      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6000]413      return dao.GetPlugin(pluginId);
414    }
415
[6452]416    public Plugin GetPluginByHash(byte[] hash) {
417      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
418      return dao.GetPlugins(x => x.Hash == hash).FirstOrDefault();
419    }
420
[6463]421    // 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)
422    //       change so that only with GetPluginByHash it is possible to download plugins
[5053]423    public IEnumerable<Plugin> GetPlugins() {
[6362]424      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6426]425      return dao.GetPlugins(x => x.Hash != null);
[5028]426    }
[5375]427
[5053]428    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
[6362]429      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
[6463]430      var pluginDatas = new List<PluginData>();
[6452]431      return trans.UseTransaction(() => {
[5375]432        foreach (Guid guid in pluginIds) {
[6369]433          pluginDatas.AddRange(dao.GetPluginDatas(x => x.PluginId == guid).ToList());
[5375]434        }
435        return pluginDatas;
[5708]436      });
[5028]437    }
[5375]438
[6452]439    public void DeletePlugin(Guid pluginId) {
440      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
441      dao.DeletePlugin(pluginId);
442    }
[5028]443    #endregion
[5106]444
[5028]445    #region Slave Methods
446    public Guid AddSlave(Slave slave) {
[6463]447      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]448      return trans.UseTransaction(() => dao.AddSlave(slave));
[5028]449    }
450
451    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
[6463]452      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]453      return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
[5028]454    }
455
[5106]456    public Slave GetSlave(Guid slaveId) {
[6463]457      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[5106]458      return dao.GetSlave(slaveId);
459    }
460
461    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
[6463]462      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[5106]463      return dao.GetSlaveGroup(slaveGroupId);
464    }
465
[5028]466    public IEnumerable<Slave> GetSlaves() {
[6463]467      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[5028]468      return dao.GetSlaves(x => true);
469    }
470
471    public IEnumerable<SlaveGroup> GetSlaveGroups() {
[6463]472      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[5028]473      return dao.GetSlaveGroups(x => true);
474    }
475
[5106]476    public void UpdateSlave(Slave slave) {
[6463]477      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]478      trans.UseTransaction(() => {
[5106]479        dao.UpdateSlave(slave);
[5708]480      });
[5028]481    }
482
[5106]483    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
[6463]484      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]485      trans.UseTransaction(() => {
[5106]486        dao.UpdateSlaveGroup(slaveGroup);
[5708]487      });
[5028]488    }
489
[5106]490    public void DeleteSlave(Guid slaveId) {
[6463]491      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]492      trans.UseTransaction(() => {
[5106]493        dao.DeleteSlave(slaveId);
[5708]494      });
[5028]495    }
496
[5106]497    public void DeleteSlaveGroup(Guid slaveGroupId) {
[6463]498      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]499      trans.UseTransaction(() => {
[5106]500        dao.DeleteSlaveGroup(slaveGroupId);
[5708]501      });
[5028]502    }
[5106]503
504    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
[6463]505      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]506      trans.UseTransaction(() => {
[5106]507        var resource = dao.GetResource(resourceId);
508        resource.ParentResourceId = slaveGroupId;
509        dao.UpdateResource(resource);
[5708]510      });
[5106]511    }
512
513    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
[6463]514      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]515      trans.UseTransaction(() => {
[5106]516        var resource = dao.GetResource(resourceId);
517        resource.ParentResourceId = null;
518        dao.UpdateResource(resource);
[5708]519      });
[5106]520    }
521
[5458]522    public Guid GetResourceId(string resourceName) {
[6479]523      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
[6452]524      return trans.UseTransaction(() => {
[5458]525        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
526        if (resource != null) {
527          return resource.Id;
528        } else {
529          return Guid.Empty;
530        }
[5708]531      });
[5458]532    }
[5593]533
[5636]534    public void TriggerLifecycle(bool force) {
[6463]535      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Slave);
[6369]536      // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
[6452]537      trans.UseTransaction(() => {
[5593]538        DateTime lastCleanup = dao.GetLastCleanup();
[5636]539        if (force || DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
[5593]540          dao.SetLastCleanup(DateTime.Now);
541          lifecycleManager.Cleanup();
542        }
[6267]543      }, true);
[5593]544    }
[5028]545    #endregion
546
[6452]547    #region Downtime Methods
548    public Guid AddDowntime(Downtime downtime) {
[6362]549      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]550      return trans.UseTransaction(() => dao.AddDowntime(downtime));
[5633]551    }
552
[6452]553    public void DeleteDowntime(Guid downtimeId) {
[6362]554      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]555      trans.UseTransaction(() => {
556        dao.DeleteDowntime(downtimeId);
[5708]557      });
[5633]558    }
559
[6452]560    public void UpdateDowntime(Downtime downtime) {
[6362]561      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]562      trans.UseTransaction(() => {
563        dao.UpdateDowntime(downtime);
[5708]564      });
[5633]565    }
566
[6452]567    public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
[6362]568      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
[6452]569      return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
[5633]570    }
571    #endregion
[6457]572
[6463]573    #region User Methods
574    public string GetUsernameByUserId(Guid userId) {
575      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
576      var user = ServiceLocator.Instance.UserManager.GetUserById(userId);
577      if (user != null)
578        return user.UserName;
579      else
580        return null;
581    }
582
583    public Guid GetUserIdByUsername(string username) {
584      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
585      var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
586      return user != null ? (Guid)user.ProviderUserKey : Guid.Empty;
587    }
588    #endregion
589
[6457]590    #region Helper Methods
591    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
592      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
593
594      if (recursive) {
595        var childs = new List<Job>();
596        foreach (var job in jobs) {
597          childs.AddRange(GetChildJobs(job.Id, recursive, false));
598        }
599        jobs.AddRange(childs);
600      }
601
602      if (includeParent) jobs.Add(GetJob(parentJobId.Value));
603      return jobs;
604    }
605    #endregion
[5028]606  }
607}
Note: See TracBrowser for help on using the repository browser.