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 HeuristicLab.Clients.Hive.CloudManager.Azure;
|
---|
25 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 |
|
---|
29 | namespace 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 | }
|
---|
106 | }
|
---|
107 | catch {
|
---|
108 | throw;
|
---|
109 | }
|
---|
110 | finally {
|
---|
111 | OnRefreshed();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | public void Add(Subscription subscription) {
|
---|
118 | if (subscription == null) {
|
---|
119 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
120 | }
|
---|
121 | if (Subscriptions.Contains(subscription)) {
|
---|
122 | Subscriptions.Remove(subscription);
|
---|
123 | }
|
---|
124 | Subscriptions.Add(subscription);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void Remove(Subscription subscription) {
|
---|
128 | if (subscription == null) {
|
---|
129 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
130 | }
|
---|
131 | if (Subscriptions.Contains(subscription)) {
|
---|
132 | Subscriptions.Remove(subscription);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void Add(HostedService hostedService) {
|
---|
137 | if (hostedService == null) {
|
---|
138 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
139 | }
|
---|
140 | if (HostedServices.Contains(hostedService)) {
|
---|
141 | //HostedServices.Remove(hostedService);
|
---|
142 | HostedService hs = HostedServices[HostedServices.IndexOf(hostedService)];
|
---|
143 | hs.Merge(hostedService);
|
---|
144 |
|
---|
145 | } else {
|
---|
146 | HostedServices.Add(hostedService);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void ChangeIntances(Deployment deployment, HostedService hostedService) {
|
---|
151 | if (deployment == null) {
|
---|
152 | throw new ArgumentNullException("Deployment", "Deployment must not be null.");
|
---|
153 | }
|
---|
154 | if (hostedService == null) {
|
---|
155 | throw new ArgumentNullException("HostedService", "HostedService must not be null.");
|
---|
156 | }
|
---|
157 | if (deployment.Modified) {
|
---|
158 | AzureProvider.ChangeInstanceCount(deployment.Subscription,
|
---|
159 | hostedService.ServiceName,
|
---|
160 | deployment.DeploymentSlot,
|
---|
161 | HeuristicLab.Clients.Hive.CloudManager.Azure.Constants.DeploymentRoleName,
|
---|
162 | deployment.NewInstanceCount);
|
---|
163 | deployment.Modified = false;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void Delete(Deployment deployment, HostedService hostedService) {
|
---|
168 | if (deployment == null) {
|
---|
169 | throw new ArgumentNullException("Deployment", "Deployment must not be null.");
|
---|
170 | }
|
---|
171 | if (hostedService == null) {
|
---|
172 | throw new ArgumentNullException("HostedService", "HostedService must not be null.");
|
---|
173 | }
|
---|
174 | //AzureProvider.DeleteDeployment();
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void Delete(HostedService hostedService) {
|
---|
178 | if (hostedService == null) {
|
---|
179 | throw new ArgumentNullException("HostedService", "HostedService must not be null.");
|
---|
180 | }
|
---|
181 | AzureProvider.DeleteHostedService(hostedService.Subscription, hostedService.ServiceName);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void Delete(Subscription subscription) {
|
---|
185 | if (subscription == null) {
|
---|
186 | throw new ArgumentNullException("subscription", "Subscription must not be null.");
|
---|
187 | }
|
---|
188 | if (Subscriptions.Contains(subscription)) {
|
---|
189 | Subscriptions.Remove(subscription);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | }
|
---|
195 | }
|
---|