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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Collections.Specialized;
|
---|
25 | using HeuristicLab.Clients.Hive.CloudManager.Azure;
|
---|
26 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
27 | using HeuristicLab.Clients.Hive.CloudManager.Properties;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Clients.Hive.CloudManager {
|
---|
32 | [Item("CloudManagerClient", "Hive Cloud Manager Client.")]
|
---|
33 | public sealed class CloudManagerClient : IContent {
|
---|
34 | private static CloudManagerClient instance;
|
---|
35 | public static CloudManagerClient Instance {
|
---|
36 | get {
|
---|
37 | if (instance == null) instance = new CloudManagerClient();
|
---|
38 | return instance;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private CloudManagerClient() {
|
---|
43 | subscriptions = new ItemList<Subscription>();
|
---|
44 | hostedServices = new ItemList<HostedService>();
|
---|
45 | azureProvider = new AzureProvider();
|
---|
46 | }
|
---|
47 |
|
---|
48 | #region Properties
|
---|
49 | private IItemList<Subscription> subscriptions;
|
---|
50 | public IItemList<Subscription> Subscriptions {
|
---|
51 | get { return subscriptions; }
|
---|
52 | set {
|
---|
53 | if (value != subscriptions) {
|
---|
54 | subscriptions = value;
|
---|
55 | //fire event OnSubscriptionsChagned
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private IItemList<HostedService> hostedServices;
|
---|
61 | public IItemList<HostedService> HostedServices {
|
---|
62 | get { return hostedServices; }
|
---|
63 | set {
|
---|
64 | if (value != hostedServices) {
|
---|
65 | hostedServices = value;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private IAzureProvider azureProvider;
|
---|
71 | public IAzureProvider AzureProvider {
|
---|
72 | get { return azureProvider; }
|
---|
73 | set { azureProvider = value; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 | #region Events
|
---|
79 |
|
---|
80 | public event EventHandler Refreshing;
|
---|
81 | private void OnRefreshing() {
|
---|
82 | EventHandler handler = Refreshing;
|
---|
83 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
84 | }
|
---|
85 | public event EventHandler Refreshed;
|
---|
86 | private void OnRefreshed() {
|
---|
87 | var handler = Refreshed;
|
---|
88 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
89 | }
|
---|
90 |
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | #region Refresh
|
---|
94 |
|
---|
95 | public void Refresh() {
|
---|
96 | OnRefreshing();
|
---|
97 |
|
---|
98 | try {
|
---|
99 | IItemList<Subscription> subs = new ItemList<Subscription>(Subscriptions);
|
---|
100 | foreach (Subscription subscription in subs) {
|
---|
101 | if (subscription.DiscoverServices) {
|
---|
102 | List<HostedService> servs = AzureProvider.DiscoverSlaveService(subscription);
|
---|
103 | foreach (HostedService s in servs) {
|
---|
104 | Add(s);
|
---|
105 | }
|
---|
106 |
|
---|
107 | List<HostedService> listToDelete = new List<HostedService>();
|
---|
108 | foreach (HostedService s in HostedServices) {
|
---|
109 | if (!servs.Contains(s)) {
|
---|
110 | //Remove(s);
|
---|
111 | listToDelete.Add(s);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | foreach (HostedService s in listToDelete) {
|
---|
115 | Remove(s);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | catch {
|
---|
121 | throw;
|
---|
122 | }
|
---|
123 | finally {
|
---|
124 | OnRefreshed();
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | #endregion
|
---|
129 |
|
---|
130 | public void Add(Subscription subscription) {
|
---|
131 | if (subscription == null) {
|
---|
132 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
133 | }
|
---|
134 | if (Subscriptions.Contains(subscription)) {
|
---|
135 | Subscriptions.Remove(subscription);
|
---|
136 | }
|
---|
137 | Subscriptions.Add(subscription);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void Remove(Subscription subscription) {
|
---|
141 | if (subscription == null) {
|
---|
142 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
143 | }
|
---|
144 | if (Subscriptions.Contains(subscription)) {
|
---|
145 | Subscriptions.Remove(subscription);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void Remove(HostedService hostedService) {
|
---|
150 | if (hostedService == null) {
|
---|
151 | throw new ArgumentNullException("hostedService", "HostedService must not be null.");
|
---|
152 | }
|
---|
153 | if (HostedServices.Contains(hostedService)) {
|
---|
154 | HostedServices.Remove(hostedService);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | public void Add(HostedService hostedService) {
|
---|
159 | if (hostedService == null) {
|
---|
160 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
161 | }
|
---|
162 | if (HostedServices.Contains(hostedService)) {
|
---|
163 | //HostedServices.Remove(hostedService);
|
---|
164 | HostedService hs = HostedServices[HostedServices.IndexOf(hostedService)];
|
---|
165 | hs.Merge(hostedService);
|
---|
166 |
|
---|
167 | } else {
|
---|
168 | HostedServices.Add(hostedService);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void ChangeIntances(Deployment deployment, HostedService hostedService) {
|
---|
173 | if (deployment == null) {
|
---|
174 | throw new ArgumentNullException("Deployment", "Deployment must not be null.");
|
---|
175 | }
|
---|
176 | if (hostedService == null) {
|
---|
177 | throw new ArgumentNullException("HostedService", "HostedService must not be null.");
|
---|
178 | }
|
---|
179 | if (deployment.Modified) {
|
---|
180 | AzureProvider.ChangeInstanceCount(deployment.Subscription,
|
---|
181 | hostedService.ServiceName,
|
---|
182 | deployment.DeploymentSlot,
|
---|
183 | HeuristicLab.Clients.Hive.CloudManager.Azure.Constants.DeploymentRoleName,
|
---|
184 | deployment.NewInstanceCount);
|
---|
185 | deployment.Modified = false;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void Delete(Deployment deployment, HostedService hostedService) {
|
---|
190 | if (deployment == null) {
|
---|
191 | throw new ArgumentNullException("Deployment", "Deployment must not be null.");
|
---|
192 | }
|
---|
193 | if (hostedService == null) {
|
---|
194 | throw new ArgumentNullException("HostedService", "HostedService must not be null.");
|
---|
195 | }
|
---|
196 | //AzureProvider.DeleteDeployment();
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void Delete(HostedService hostedService) {
|
---|
200 | if (hostedService == null) {
|
---|
201 | throw new ArgumentNullException("HostedService", "HostedService must not be null.");
|
---|
202 | }
|
---|
203 | AzureProvider.DeleteHostedService(hostedService.Subscription, hostedService.ServiceName);
|
---|
204 | }
|
---|
205 |
|
---|
206 | public void Delete(Subscription subscription) {
|
---|
207 | if (subscription == null) {
|
---|
208 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
209 | }
|
---|
210 | if (Subscriptions.Contains(subscription)) {
|
---|
211 | Subscriptions.Remove(subscription);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void PersistSubscriptionsToUserConfig() {
|
---|
216 | Settings.Default.Upgrade();
|
---|
217 | StringCollection strCol = Settings.Default.AzureSubscriptions;
|
---|
218 | strCol.Clear();
|
---|
219 | foreach (Subscription sub in Subscriptions) {
|
---|
220 | if (sub.SaveToConfig) {
|
---|
221 | string setting = sub.GetSettingString();
|
---|
222 | setting = CryptoService.EncryptString(CryptoService.ToSecureString(setting));
|
---|
223 | strCol.Add(setting);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | Settings.Default.Save();
|
---|
227 | }
|
---|
228 |
|
---|
229 | public List<Subscription> RestoreSubscriptionsFromUserConfig() {
|
---|
230 | List<Subscription> result = new List<Subscription>();
|
---|
231 | StringCollection strCol = Settings.Default.AzureSubscriptions;
|
---|
232 | foreach (string azureSub in strCol) {
|
---|
233 | string setting = CryptoService.ToInsecureString(CryptoService.DecryptString(azureSub));
|
---|
234 | Subscription tmp = Subscription.ParseSettingString(setting);
|
---|
235 | Subscription s = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(tmp.SubscriptionID, tmp.CertificateThumbprint);
|
---|
236 | s.SaveToConfig = true;
|
---|
237 | s.DiscoverServices = true;
|
---|
238 | result.Add(s);
|
---|
239 | }
|
---|
240 | return result;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|