[7339] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Windows.Forms;
|
---|
[7374] | 5 | using HeuristicLab.Clients.Hive.CloudManager.Azure;
|
---|
[7339] | 6 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 |
|
---|
| 9 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
| 10 | public partial class AddAzureServiceDialog : Form {
|
---|
| 11 | private BindingList<Subscription> subscriptions;
|
---|
| 12 | private BindingList<HostedService> hostedServices;
|
---|
| 13 | private BindingList<string> locations;
|
---|
| 14 | private BindingList<AffinityGroup> affinityGroups;
|
---|
| 15 |
|
---|
[7366] | 16 | private BackgroundWorker workerUpdate;
|
---|
| 17 | private BackgroundWorker workerCreate;
|
---|
[7339] | 18 |
|
---|
[7354] | 19 | private bool isInitializing;
|
---|
| 20 | private bool bwCompleted;
|
---|
| 21 | private bool closePending;
|
---|
| 22 |
|
---|
[7362] | 23 | private string certificateFile;
|
---|
| 24 | private string certificatePassword;
|
---|
| 25 |
|
---|
[7339] | 26 | public AddAzureServiceDialog(IItemList<Subscription> subs) {
|
---|
| 27 | InitializeComponent();
|
---|
[7354] | 28 | isInitializing = true;
|
---|
[7357] | 29 | bwCompleted = true;
|
---|
[7339] | 30 |
|
---|
| 31 | this.subscriptions = new BindingList<Subscription>(subs);
|
---|
[7354] | 32 | this.hostedServices = new BindingList<HostedService>();
|
---|
| 33 | this.locations = new BindingList<string>();
|
---|
| 34 | this.affinityGroups = new BindingList<AffinityGroup>();
|
---|
[7339] | 35 |
|
---|
[7354] | 36 | SetLookupBinding(cmbLocation, locations, "", "");
|
---|
| 37 | SetLookupBinding(cmbAffinityGroup, affinityGroups, "Label", "Name");
|
---|
| 38 | SetLookupBinding(cmbChooseHostedService, hostedServices, "ServiceName", "ServiceName");
|
---|
[7339] | 39 | SetLookupBinding(cmbChooseSubscription, subscriptions, "SubscriptionName", "SubscriptionID");
|
---|
| 40 |
|
---|
| 41 | if (!cbNewHostedService.Checked) {
|
---|
| 42 | SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[7374] | 45 | certificateFile = string.Empty;
|
---|
| 46 | certificatePassword = string.Empty;
|
---|
| 47 |
|
---|
[7366] | 48 | workerUpdate = new BackgroundWorker();
|
---|
| 49 | workerUpdate.DoWork += new DoWorkEventHandler(UpdateTask);
|
---|
| 50 | workerUpdate.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
|
---|
[7354] | 51 |
|
---|
[7366] | 52 | workerCreate = new BackgroundWorker();
|
---|
| 53 | workerCreate.DoWork += new DoWorkEventHandler(CreateDoploymentTask);
|
---|
| 54 | workerCreate.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CreateDeploymentCompleted);
|
---|
[7354] | 55 |
|
---|
| 56 | isInitializing = false;
|
---|
| 57 | cmbChooseSubscription_SelectedValueChanged(this, EventArgs.Empty);
|
---|
[7339] | 58 | }
|
---|
| 59 |
|
---|
[7354] | 60 | protected override void OnFormClosing(FormClosingEventArgs e) {
|
---|
| 61 | if (!bwCompleted) {
|
---|
[7357] | 62 | SetAllControlsEnabled(this, false);
|
---|
[7354] | 63 | e.Cancel = true;
|
---|
| 64 | closePending = true;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[7339] | 68 | private void SetLookupBinding(ListControl toBind, object dataSource, string displayMember, string valueMember) {
|
---|
| 69 | toBind.DisplayMember = displayMember;
|
---|
| 70 | toBind.ValueMember = valueMember;
|
---|
| 71 | toBind.DataSource = dataSource;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private void SetGroupBoxControlsEnabled(GroupBox gb, bool isEnabled) {
|
---|
| 75 | foreach (Control ctrl in gb.Controls) {
|
---|
| 76 | ctrl.Enabled = isEnabled;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | private void SetAllControlsEnabled(Form frm, bool isEnabled) {
|
---|
| 81 | foreach (Control ctrl in frm.Controls) {
|
---|
| 82 | if (!(ctrl is ProgressBar)) {
|
---|
[7357] | 83 | ctrl.Enabled = isEnabled;
|
---|
[7339] | 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private void SetBindingList<T>(ref BindingList<T> bindinglist, List<T> list) {
|
---|
| 89 | if (list != null) {
|
---|
| 90 | BindingList<T> newList = new BindingList<T>(list);
|
---|
| 91 | bindinglist = newList;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[7354] | 95 | private void UpdateBindingList<T>(BindingList<T> bindinglist, List<T> list) {
|
---|
| 96 | if (list != null) {
|
---|
| 97 | bindinglist.Clear();
|
---|
| 98 | foreach (T item in list)
|
---|
| 99 | bindinglist.Add(item);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[7366] | 103 | private void UpdateSubscriptionItem(BindingList<Subscription> bindinglist, Subscription item) {
|
---|
| 104 | if (item != null) {
|
---|
| 105 | foreach (Subscription blItem in bindinglist)
|
---|
| 106 | if (blItem.Equals(item)) {
|
---|
| 107 | blItem.Merge(item);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[7339] | 112 | private void cbNewHostedService_CheckedChanged(object sender, EventArgs e) {
|
---|
| 113 | if (cbNewHostedService.Checked) {
|
---|
| 114 | Subscription subscription = (Subscription)cmbChooseSubscription.SelectedItem;
|
---|
| 115 | if (subscription.CurrentHostedServices == subscription.MaxHostedServices) {
|
---|
| 116 | MessageBox.Show("You have already created the maximum number of hosted services", "Limit Reached", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 117 | cbNewHostedService.Checked = false;
|
---|
| 118 | } else {
|
---|
| 119 | SetGroupBoxControlsEnabled(gbNewHostedService, cbNewHostedService.Checked);
|
---|
| 120 | cmbChooseHostedService.Enabled = false;
|
---|
| 121 | }
|
---|
| 122 | } else {
|
---|
| 123 | // checked == false
|
---|
| 124 | SetGroupBoxControlsEnabled(gbNewHostedService, false);
|
---|
[7357] | 125 | if (hostedServices.Count == 0) {
|
---|
[7339] | 126 | cbNewHostedService.Checked = true;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | private void cmbChooseSubscription_SelectedValueChanged(object sender, EventArgs e) {
|
---|
[7354] | 132 | if (!isInitializing) {
|
---|
| 133 | Subscription item = (Subscription)cmbChooseSubscription.SelectedItem;
|
---|
| 134 | if (item != null) {
|
---|
[7357] | 135 | bwCompleted = false;
|
---|
[7354] | 136 | progressBar.Visible = true;
|
---|
[7357] | 137 | SetAllControlsEnabled(this, false);
|
---|
[7366] | 138 | workerUpdate.RunWorkerAsync(item);
|
---|
[7354] | 139 | }
|
---|
[7339] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | private void btnAddCertificate_Click(object sender, EventArgs e) {
|
---|
| 144 | using (var form = new AddCertificate()) {
|
---|
| 145 | form.ShowDialog();
|
---|
[7362] | 146 | if (!form.ErrorOccured) {
|
---|
| 147 | certificateFile = form.CertificateFile;
|
---|
| 148 | certificatePassword = form.CertificatePassword;
|
---|
| 149 | lblCertificateFile.Text = certificateFile;
|
---|
| 150 | }
|
---|
[7339] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | private void btnCancel_Click(object sender, EventArgs e) {
|
---|
| 155 | this.Close();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private void btnOk_Click(object sender, EventArgs e) {
|
---|
| 159 | if (cmbChooseSubscription.SelectedItem != null) {
|
---|
[7366] | 160 | Subscription sub = (Subscription)cmbChooseSubscription.SelectedItem;
|
---|
| 161 | bool newHostedServiceChecked = cbNewHostedService.Checked;
|
---|
| 162 | bool regionChecked = rbRegion.Checked;
|
---|
| 163 | string certFile = certificateFile;
|
---|
| 164 | string certPw = certificatePassword;
|
---|
| 165 | int instanceCount = Convert.ToInt32(tbInstanceCount.Text);
|
---|
| 166 | HostedService hostedService = new HostedService();
|
---|
| 167 | hostedService.ServiceName = tbServiceName.Text;
|
---|
| 168 | HostedServiceProperties properties = new HostedServiceProperties();
|
---|
| 169 | if (regionChecked) {
|
---|
| 170 | properties.AffinityGroup = string.Empty;
|
---|
| 171 | properties.Location = (string)cmbLocation.SelectedItem;
|
---|
| 172 | } else {
|
---|
| 173 | properties.AffinityGroup = ((AffinityGroup)cmbAffinityGroup.SelectedItem).Name;
|
---|
| 174 | properties.Location = string.Empty;
|
---|
[7354] | 175 | }
|
---|
[7366] | 176 | properties.AffinityGroup = ((AffinityGroup)cmbAffinityGroup.SelectedItem).Name;
|
---|
| 177 | properties.Label = tbLabel.Text;
|
---|
| 178 | properties.Location = (string)cmbLocation.SelectedItem;
|
---|
| 179 | hostedService.HostedServiceProperties = properties;
|
---|
| 180 |
|
---|
| 181 | var parameters = Tuple.Create<Subscription, bool, bool, string, string, HostedService, int>(sub, newHostedServiceChecked, regionChecked, certificateFile, certificatePassword, hostedService, instanceCount);
|
---|
| 182 |
|
---|
| 183 | bwCompleted = false;
|
---|
| 184 | progressBar.Visible = true;
|
---|
| 185 | SetAllControlsEnabled(this, false);
|
---|
| 186 | workerCreate.RunWorkerAsync(parameters);
|
---|
[7339] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | private void UpdateTask(object sender, DoWorkEventArgs e) {
|
---|
| 191 | try {
|
---|
| 192 | Subscription item = (Subscription)e.Argument;
|
---|
| 193 | item = CloudManagerClient.Instance.AzureProvider.GetSubscriptionInfo(item.SubscriptionID, item.CertificateThumbprint);
|
---|
| 194 | this.Invoke((MethodInvoker)delegate { lblCores.Text = item.CurrentCoreCount + " / " + item.MaxCoreCount; });
|
---|
| 195 |
|
---|
[7354] | 196 | List<HostedService> services = CloudManagerClient.Instance.AzureProvider.ListHostedServices(item);
|
---|
| 197 | List<string> locs = CloudManagerClient.Instance.AzureProvider.ListLocations(item);
|
---|
| 198 | List<AffinityGroup> groups = CloudManagerClient.Instance.AzureProvider.ListAffinityGroups(item);
|
---|
[7339] | 199 |
|
---|
[7366] | 200 | this.Invoke((MethodInvoker)delegate { UpdateSubscriptionItem(subscriptions, item); });
|
---|
[7354] | 201 | this.Invoke((MethodInvoker)delegate { UpdateBindingList<HostedService>(hostedServices, services); });
|
---|
| 202 | this.Invoke((MethodInvoker)delegate { UpdateBindingList<string>(locations, locs); });
|
---|
| 203 | this.Invoke((MethodInvoker)delegate { UpdateBindingList<AffinityGroup>(affinityGroups, groups); });
|
---|
[7339] | 204 | }
|
---|
| 205 | catch (Exception ex) {
|
---|
| 206 | MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
[7357] | 211 | SetAllControlsEnabled(this, true);
|
---|
| 212 | progressBar.Visible = false;
|
---|
[7354] | 213 | bwCompleted = true;
|
---|
| 214 | if (closePending) {
|
---|
| 215 | this.Close();
|
---|
| 216 | }
|
---|
[7339] | 217 | }
|
---|
[7366] | 218 |
|
---|
| 219 | private void CreateDoploymentTask(object sender, DoWorkEventArgs e) {
|
---|
| 220 | Tuple<Subscription, bool, bool, string, string, HostedService, int> parameters = (Tuple<Subscription, bool, bool, string, string, HostedService, int>)e.Argument;
|
---|
| 221 |
|
---|
| 222 | Subscription sub = parameters.Item1;
|
---|
| 223 | bool newHostedServiceChecked = parameters.Item2;
|
---|
| 224 | bool regionChecked = parameters.Item3;
|
---|
| 225 | string certFile = parameters.Item4;
|
---|
| 226 | string certPw = parameters.Item5;
|
---|
| 227 | HostedService hostedService = parameters.Item6;
|
---|
| 228 | int instanceCount = parameters.Item7;
|
---|
| 229 |
|
---|
[7374] | 230 | // STEP 1 - Create Hosted Service
|
---|
[7366] | 231 | if (newHostedServiceChecked) {
|
---|
| 232 | if (regionChecked) {
|
---|
| 233 | CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, hostedService.ServiceName, hostedService.HostedServiceProperties.Label, hostedService.HostedServiceProperties.Description, hostedService.HostedServiceProperties.Location);
|
---|
| 234 | } else {
|
---|
| 235 | CloudManagerClient.Instance.AzureProvider.CreateHostedService(sub, hostedService.ServiceName, hostedService.HostedServiceProperties.Label, hostedService.HostedServiceProperties.Description, new AffinityGroup() { Name = hostedService.HostedServiceProperties.AffinityGroup });
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[7374] | 239 | // STEP 2 - Add Certificate
|
---|
| 240 | if (certFile != string.Empty) {
|
---|
| 241 | CloudManagerClient.Instance.AzureProvider.AddCertificate(sub, hostedService, certFile, certPw);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | // STEP 3 - Create Deployment
|
---|
| 245 | CloudManagerClient.Instance.AzureProvider.CreateDeployment(sub, hostedService.ServiceName, Guid.NewGuid().ToString(), Constants.DeploymentSlotStaging, Constants.DeploymentPackageUrl, Constants.DeploymentConfigurationUrl, Constants.DeploymentLabel, instanceCount);
|
---|
| 246 |
|
---|
[7366] | 247 | }
|
---|
| 248 |
|
---|
| 249 | private void CreateDeploymentCompleted(object sender, RunWorkerCompletedEventArgs e) {
|
---|
| 250 | SetAllControlsEnabled(this, true);
|
---|
| 251 | progressBar.Visible = false;
|
---|
| 252 | bwCompleted = true;
|
---|
| 253 | if (closePending) {
|
---|
| 254 | this.Close();
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
[7339] | 257 | }
|
---|
| 258 | }
|
---|