Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/CloudManagerClient.cs @ 7577

Last change on this file since 7577 was 7577, checked in by spimming, 12 years ago

#1680:

  • Check if role instance list is not null (while deploying a package)
  • Remove a hosted service when it's deleted
File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Collections.Generic;
24using HeuristicLab.Clients.Hive.CloudManager.Azure;
25using HeuristicLab.Clients.Hive.CloudManager.Model;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Clients.Hive.CloudManager {
30  [Item("CloudManagerClient", "Hive Cloud Manager Client.")]
31  public sealed class CloudManagerClient : IContent {
32    private static CloudManagerClient instance;
33    public static CloudManagerClient Instance {
34      get {
35        if (instance == null) instance = new CloudManagerClient();
36        return instance;
37      }
38    }
39
40    private CloudManagerClient() {
41      subscriptions = new ItemList<Subscription>();
42      hostedServices = new ItemList<HostedService>();
43      azureProvider = new AzureProvider();
44    }
45
46    #region Properties
47    private IItemList<Subscription> subscriptions;
48    public IItemList<Subscription> Subscriptions {
49      get { return subscriptions; }
50      set {
51        if (value != subscriptions) {
52          subscriptions = value;
53          //fire event OnSubscriptionsChagned
54        }
55      }
56    }
57
58    private IItemList<HostedService> hostedServices;
59    public IItemList<HostedService> HostedServices {
60      get { return hostedServices; }
61      set {
62        if (value != hostedServices) {
63          hostedServices = value;
64        }
65      }
66    }
67
68    private IAzureProvider azureProvider;
69    public IAzureProvider AzureProvider {
70      get { return azureProvider; }
71      set { azureProvider = value; }
72    }
73
74    #endregion
75
76    #region Events
77
78    public event EventHandler Refreshing;
79    private void OnRefreshing() {
80      EventHandler handler = Refreshing;
81      if (handler != null) handler(this, EventArgs.Empty);
82    }
83    public event EventHandler Refreshed;
84    private void OnRefreshed() {
85      var handler = Refreshed;
86      if (handler != null) handler(this, EventArgs.Empty);
87    }
88
89    #endregion
90
91    #region Refresh
92
93    public void Refresh() {
94      OnRefreshing();
95
96      try {
97        IItemList<Subscription> subs = new ItemList<Subscription>(Subscriptions);
98        foreach (Subscription subscription in subs) {
99          if (subscription.DiscoverServices) {
100            List<HostedService> servs = AzureProvider.DiscoverSlaveService(subscription);
101            foreach (HostedService s in servs) {
102              Add(s);
103            }
104
105            List<HostedService> listToDelete = new List<HostedService>();
106            foreach (HostedService s in HostedServices) {
107              if (!servs.Contains(s)) {
108                //Remove(s);
109                listToDelete.Add(s);
110              }
111            }
112            foreach (HostedService s in listToDelete) {
113              Remove(s);
114            }
115          }
116        }
117      }
118      catch {
119        throw;
120      }
121      finally {
122        OnRefreshed();
123      }
124    }
125
126    #endregion
127
128    public void Add(Subscription subscription) {
129      if (subscription == null) {
130        throw new ArgumentNullException("subscription", "Subscription must not be null.");
131      }
132      if (Subscriptions.Contains(subscription)) {
133        Subscriptions.Remove(subscription);
134      }
135      Subscriptions.Add(subscription);
136    }
137
138    public void Remove(Subscription subscription) {
139      if (subscription == null) {
140        throw new ArgumentNullException("subscription", "Subscription must not be null.");
141      }
142      if (Subscriptions.Contains(subscription)) {
143        Subscriptions.Remove(subscription);
144      }
145    }
146
147    public void Remove(HostedService hostedService) {
148      if (hostedService == null) {
149        throw new ArgumentNullException("hostedService", "HostedService must not be null.");
150      }
151      if (HostedServices.Contains(hostedService)) {
152        HostedServices.Remove(hostedService);
153      }
154    }
155
156    public void Add(HostedService hostedService) {
157      if (hostedService == null) {
158        throw new ArgumentNullException("subscription", "Subscription must not be null.");
159      }
160      if (HostedServices.Contains(hostedService)) {
161        //HostedServices.Remove(hostedService);
162        HostedService hs = HostedServices[HostedServices.IndexOf(hostedService)];
163        hs.Merge(hostedService);
164
165      } else {
166        HostedServices.Add(hostedService);
167      }
168    }
169
170    public void ChangeIntances(Deployment deployment, HostedService hostedService) {
171      if (deployment == null) {
172        throw new ArgumentNullException("Deployment", "Deployment must not be null.");
173      }
174      if (hostedService == null) {
175        throw new ArgumentNullException("HostedService", "HostedService must not be null.");
176      }
177      if (deployment.Modified) {
178        AzureProvider.ChangeInstanceCount(deployment.Subscription,
179                                          hostedService.ServiceName,
180                                          deployment.DeploymentSlot,
181                                          HeuristicLab.Clients.Hive.CloudManager.Azure.Constants.DeploymentRoleName,
182                                          deployment.NewInstanceCount);
183        deployment.Modified = false;
184      }
185    }
186
187    public void Delete(Deployment deployment, HostedService hostedService) {
188      if (deployment == null) {
189        throw new ArgumentNullException("Deployment", "Deployment must not be null.");
190      }
191      if (hostedService == null) {
192        throw new ArgumentNullException("HostedService", "HostedService must not be null.");
193      }
194      //AzureProvider.DeleteDeployment();
195    }
196
197    public void Delete(HostedService hostedService) {
198      if (hostedService == null) {
199        throw new ArgumentNullException("HostedService", "HostedService must not be null.");
200      }
201      AzureProvider.DeleteHostedService(hostedService.Subscription, hostedService.ServiceName);
202    }
203
204    public void Delete(Subscription subscription) {
205      if (subscription == null) {
206        throw new ArgumentNullException("subscription", "Subscription must not be null.");
207      }
208      if (Subscriptions.Contains(subscription)) {
209        Subscriptions.Remove(subscription);
210      }
211    }
212
213
214  }
215}
Note: See TracBrowser for help on using the repository browser.