1
- /*****************************************************************`*******************************************
1
+ using Elastacloud . AzureManagement . Fluent . Clients . Helpers ;
2
+ using Elastacloud . AzureManagement . Fluent . Clients . Interfaces ;
3
+ using Elastacloud . AzureManagement . Fluent . Commands . Services ;
4
+ using Elastacloud . AzureManagement . Fluent . Commands . VirtualMachines ;
5
+ using Elastacloud . AzureManagement . Fluent . Commands . VirtualNetworks ;
6
+ using Elastacloud . AzureManagement . Fluent . Helpers ;
7
+ using Elastacloud . AzureManagement . Fluent . Types ;
8
+ using Elastacloud . AzureManagement . Fluent . Types . Exceptions ;
9
+ using Elastacloud . AzureManagement . Fluent . Types . VirtualMachines ;
10
+ using Elastacloud . AzureManagement . Fluent . Types . VirtualNetworks ;
11
+ using Elastacloud . AzureManagement . Fluent . VirtualMachines . Classes ;
12
+ /*****************************************************************`*******************************************
2
13
* This software is distributed under a GNU Lesser License by Elastacloud Limited and it is free to *
3
14
* modify and distribute providing the terms of the license are followed. From the root of the source the *
4
15
* license can be found in /Resources/license.txt *
9
20
using System ;
10
21
using System . Collections . Generic ;
11
22
using System . Diagnostics ;
12
- using System . IO ;
13
23
using System . Linq ;
14
- using System . Net ;
15
24
using System . Security . Cryptography . X509Certificates ;
16
25
using System . Threading ;
17
26
using System . Threading . Tasks ;
18
- using Elastacloud . AzureManagement . Fluent . Clients . Helpers ;
19
- using Elastacloud . AzureManagement . Fluent . Clients . Interfaces ;
20
- using Elastacloud . AzureManagement . Fluent . Commands . Blobs ;
21
- using Elastacloud . AzureManagement . Fluent . Commands . Services ;
22
- using Elastacloud . AzureManagement . Fluent . Commands . VirtualMachines ;
23
- using Elastacloud . AzureManagement . Fluent . Commands . VirtualNetworks ;
24
- using Elastacloud . AzureManagement . Fluent . Helpers ;
25
- using Elastacloud . AzureManagement . Fluent . Types ;
26
- using Elastacloud . AzureManagement . Fluent . Types . Exceptions ;
27
- using Elastacloud . AzureManagement . Fluent . Types . VirtualMachines ;
28
- using Elastacloud . AzureManagement . Fluent . Types . VirtualNetworks ;
29
- using Elastacloud . AzureManagement . Fluent . VirtualMachines . Classes ;
30
27
31
28
namespace Elastacloud . AzureManagement . Fluent . Clients
32
29
{
@@ -37,7 +34,7 @@ public class LinuxVirtualMachineClient : ILinuxVirtualMachineClient
37
34
38
35
public event EventHandler < LinuxVirtualMachineProperties > LinuxVirtualMachineCreationEvent ;
39
36
public event EventHandler < VirtualMachineStatus > LinuxVirtualMachineStatusEvent ;
40
-
37
+
41
38
/// <summary>
42
39
/// Constructs a LinuxVirtualMachineClient and will get the details of a virtual machine given a cloud service
43
40
/// </summary>
@@ -96,7 +93,7 @@ private void CheckVmDeploymentIsRunning(List<LinuxVirtualMachineProperties> prop
96
93
// 1. Get the number of vms in the role and create a binary list
97
94
var linuxProperties = new Dictionary < string , RoleInstanceStatus > ( ) ;
98
95
properties . ForEach ( property => linuxProperties . Add ( property . HostName , RoleInstanceStatus . RoleStateUnknown ) ) ;
99
- var vmProperties = new LinuxVirtualMachineProperties ( ) { CloudServiceName = properties [ 0 ] . CloudServiceName } ;
96
+ var vmProperties = new LinuxVirtualMachineProperties ( ) { CloudServiceName = properties [ 0 ] . CloudServiceName } ;
100
97
// 2. Set the value to if the vm is running or not
101
98
int index = 0 ;
102
99
// time this out just in case after an hour?
@@ -112,24 +109,25 @@ private void CheckVmDeploymentIsRunning(List<LinuxVirtualMachineProperties> prop
112
109
{
113
110
if ( linuxProperties [ vm . RoleName ] == vm . Status ) return ;
114
111
// raise the event with the old and new status
115
- LinuxVirtualMachineStatusEvent ( this , new VirtualMachineStatus ( )
116
- {
117
- NewStatus = vm . Status ,
118
- OldStatus = linuxProperties [ vm . RoleName ] ,
119
- VirtualMachineInstanceName = vm . RoleName ,
120
- CloudService = vmProperties . CloudServiceName
121
- } ) ;
112
+ if ( LinuxVirtualMachineStatusEvent != null )
113
+ LinuxVirtualMachineStatusEvent ( this , new VirtualMachineStatus ( )
114
+ {
115
+ NewStatus = vm . Status ,
116
+ OldStatus = linuxProperties [ vm . RoleName ] ,
117
+ VirtualMachineInstanceName = vm . RoleName ,
118
+ CloudService = vmProperties . CloudServiceName
119
+ } ) ;
122
120
// update the status in the dictionary
123
121
linuxProperties [ vm . RoleName ] = vm . Status ;
124
122
} ) ;
125
-
123
+
126
124
Task . Delay ( TimeSpan . FromSeconds ( 10 ) ) . RunSynchronously ( ) ;
127
125
index ++ ;
128
126
}
129
127
130
128
if ( index == 100 )
131
129
{
132
- throw new FluentManagementException ( "timed out listening for status changes - check vms are running correctly" , "LinuxVirtualMachineClient" ) ;
130
+ throw new FluentManagementException ( "timed out listening for status changes - check vms are running correctly" , "LinuxVirtualMachineClient" ) ;
133
131
}
134
132
}
135
133
@@ -143,9 +141,9 @@ private void CheckVmDeploymentIsRunning(List<LinuxVirtualMachineProperties> prop
143
141
/// <param name="affinityGroup">Affinity group that this service will live in</param>
144
142
public IVirtualMachineClient CreateNewVirtualMachineDeploymentFromTemplateGallery ( List < LinuxVirtualMachineProperties > properties , string cloudServiceName , ServiceCertificateModel serviceCertificate = null , string location = LocationConstants . NorthEurope , string affinityGroup = "" )
145
143
{
146
- if ( String . IsNullOrEmpty ( cloudServiceName ) )
144
+ if ( String . IsNullOrEmpty ( cloudServiceName ) )
147
145
throw new FluentManagementException ( "Cloud service name cannot be empty" , "LinuxVirtualMachineClient" ) ;
148
-
146
+
149
147
// if the cloud service doesn't exist we'll create it
150
148
// first check that the service is available
151
149
var checkCloudServiceAvailabilityCommand = new CheckCloudServiceNameAvailableCommand ( cloudServiceName )
@@ -173,10 +171,10 @@ public IVirtualMachineClient CreateNewVirtualMachineDeploymentFromTemplateGaller
173
171
Trace . WriteLine ( "A new service certificate has been added to the cloud service" ) ;
174
172
ServiceCertificate = serviceCertificate . ServiceCertificate . Export ( X509ContentType . Pfx ) ;
175
173
}
176
-
174
+
177
175
// This is really unfortunate and not documented anywhere - unable to add multiple roles to a rolelist!!!
178
176
// continue to the create the virtual machine in the cloud service
179
- var command = new CreateLinuxVirtualMachineDeploymentCommand ( new List < LinuxVirtualMachineProperties > ( new [ ] { properties [ 0 ] } ) , cloudServiceName )
177
+ var command = new CreateLinuxVirtualMachineDeploymentCommand ( new List < LinuxVirtualMachineProperties > ( new [ ] { properties [ 0 ] } ) , cloudServiceName )
180
178
{
181
179
SubscriptionId = SubscriptionId ,
182
180
Certificate = ManagementCertificate
@@ -298,7 +296,7 @@ private void AddServiceCertificateToRoles(ServiceCertificateModel serviceCertifi
298
296
FingerPrint = serviceCertificate . ServiceCertificate . GetCertHashString ( ) ,
299
297
Path = String . Format ( "/home/{0}/.ssh/id_rsa" , linuxVirtualMachineProperty . UserName )
300
298
} ) ;
301
-
299
+
302
300
linuxVirtualMachineProperty . DisableSSHPasswordAuthentication = true ;
303
301
}
304
302
}
@@ -332,7 +330,7 @@ public void AddRolesToExistingDeployment(List<LinuxVirtualMachineProperties> pro
332
330
/// </summary>
333
331
protected string SubscriptionId { get ; set ; }
334
332
335
-
333
+
336
334
#region Implementation of IVirtualMachineClient
337
335
338
336
/// <summary>
@@ -486,21 +484,21 @@ public void Restart()
486
484
restartCommand . Execute ( ) ;
487
485
Trace . WriteLine ( String . Format ( "VM restarted with hostname {0}" , linuxVirtualMachineProperty . HostName ) ) ;
488
486
}
489
-
487
+
490
488
}
491
489
492
490
/// <summary>
493
491
/// Stops the virtual machine instance
494
492
/// </summary>
495
493
public void Stop ( )
496
494
{
497
- Trace . WriteLine ( String . Format ( "Stopping {0} virtual machines in deployment {1} in cloud service {2}" ,
495
+ Trace . WriteLine ( String . Format ( "Stopping {0} virtual machines in deployment {1} in cloud service {2}" ,
498
496
Properties . Count , Properties . First ( ) . DeploymentName , Properties . First ( ) . CloudServiceName ) ) ;
499
497
foreach ( LinuxVirtualMachineProperties linuxVirtualMachineProperties in Properties )
500
498
{
501
499
var stopCommand = new StopVirtualMachineCommand ( linuxVirtualMachineProperties )
502
500
{
503
- SubscriptionId = SubscriptionId ,
501
+ SubscriptionId = SubscriptionId ,
504
502
Certificate = ManagementCertificate
505
503
} ;
506
504
stopCommand . Execute ( ) ;
0 commit comments